Core Function JsonEncode

From Sputnik Wiki
Revision as of 07:43, 30 July 2014 by UberFoX (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
JsonEncode( <value>, <options> )

Contents

Description

Returns the JSON representation of a value.

Parameters

value

The input string.

options

Optional; Bitmask consisting of any/all

@JSON_HEX_TAG            // All < and > are converted to \u003C and \u003E
@JSON_HEX_AMP            // All &s are converted to \u0026
@JSON_HEX_APOS           // All ' are converted to \u0027
@JSON_HEX_QUOT           // All " are converted to \u0022
@JSON_FORCE_OBJECT       // Outputs an object rather than an array
                         // when a non-associative array is used.
                         // Especially useful when the recipient of the
                         // output is expecting an object and the array is empty
@JSON_NUMERIC_CHECK      // Encodes numeric strings as numbers
@JSON_BASE64_BINARY      // Encodes Binary in Base64
@JSON_CONSTRUCTOR_CLASS  // Encodes Classes in a Constructor so they will be decoded fully
@JSON_PRETTY_PRINT       // Use whitespace in returned data to format it so it is easy to read

Default: 0 (All options False)

Return Value

Success: Returns a JSON encoded string on success or FALSE on failure.

Failure: Returns empty string.

Remarks

None.

Example

Encode and Decode binary (Default method)

// Create a binary to encode
my $Testy = Pack("iz0", 1337, "Hi");
// JSON Encode it
my $Encoded = JsonEncode($Testy);
// Print the Encoded Text
say "Encoded:";
say $Encoded;
say;
// Decode the JSON
my $Decoded = JsonDecode($Encoded);
// Decode the output into binary by casting it
my $Bin = (binary)$Decoded;
// Print the Decoded binary
say "Decoded:";
printr (binary)$Bin;
// Prints:
// Encoded:
// "9\u0005\u0000\u0000Hi"
 
// Decoded:
// {BINARY:6}
// {
//         [0] => 57
//         [1] => 5
//         [2] => 0
//         [3] => 0
//         [4] => 72
//         [5] => 105
// }

Encode and Decode binary (Base64 method: @JSON_BASE64_BINARY)

// Create a binary to encode
my $Testy = Pack("iz0", 1337, "Hi");
// JSON Encode it
my $Encoded = JsonEncode($Testy, @JSON_BASE64_BINARY);
// Print the Encoded Text
say "Encoded:";
say $Encoded;
say;
// Decode the JSON
my $Decoded = JsonDecode($Encoded);
// Decode the output into binary by casting it
my $Bin = (binary)$Decoded;
// Print the Decoded binary
say "Decoded:";
printr (binary)Decode64($Bin);
// Prints:
// Encoded:
// "OQUAAEhp"
 
// Decoded:
// {BINARY:6}
// {
//         [0] => 57
//         [1] => 5
//         [2] => 0
//         [3] => 0
//         [4] => 72
//         [5] => 105
// }

Encode and Decode an array

// Create an array to Json encode
my $Testy = array("Cat", "Dog", "Fox", 100, 200, 300);
// JSON Encode it
my $Encoded = JsonEncode($Testy);
// Print the Encoded Text
say "Encoded:";
say $Encoded;
say;
// Decode the JSON
my $Decoded = JsonDecode($Encoded);
// Print the Decoded object
say "Decoded:";
printr $Decoded;
// Prints:
// Encoded:
// ["Cat","Dog","Fox",100,200,300]
 
// Decoded:
// ARRAY
// {
//         [0] => Cat
//         [1] => Dog
//         [2] => Fox
//         [3] => 100
//         [4] => 200
//         [5] => 300
// }

Encode and Decode an associative array

// Create an array to Json encode
my $Testy = array("Cat" => "Meow", "Dog" => "Woof");
// JSON Encode it
my $Encoded = JsonEncode($Testy);
// Print the Encoded Text
say "Encoded:";
say $Encoded;
say;
// Decode the JSON
my $Decoded = JsonDecode($Encoded);
// Print the Decoded object
say "Decoded:";
printr $Decoded;
// Prints:
// Encoded:
// {"Cat":"Meow","Dog":"Woof"}
 
// Decoded:
// ARRAY
// {
//        [Cat] => Meow
//        [Dog] => Woof
// }

Encode and Decode a simple string

// Create the string to Json encode
my $Testy = array("Cat" => "Meow", "Dog" => "Woof");
// JSON Encode it
my $Encoded = JsonEncode("Hello");
// Print the Encoded Text
say "Encoded:";
say $Encoded;
say;
// Decode the JSON
my $Decoded = JsonDecode($Encoded);
// Print the Decoded object
say "Decoded:";
printr $Decoded;
// Prints:
// Encoded:
// "Hello"
 
// Decoded:
// Hello

Encode and Decode a class (using @JSON_CONSTRUCTOR_CLASS)

// The @JSON_CONSTRUCTOR_CLASS helps to recreate
// classes properly
 
// Create a base class
Class Testy
{
};
// Create an array to Json encode
my $Testy = new Testy();
// Add some variables to the class
$Testy->$Cat = "Meow";
$Testy->$Dog = "Woof";
$Testy->$Position = 1337;
// JSON Encode it
my $Encoded = JsonEncode($Testy, @JSON_CONSTRUCTOR_CLASS);
// Print the Encoded Text
say "Encoded:";
say $Encoded;
say;
// Decode the JSON
my $Decoded = JsonDecode($Encoded);
// Print the Decoded object
say "Decoded:";
printr $Decoded;
// Prints:
// Encoded:
// new testy({"cat":"Meow","dog":"Woof","position":1337})
 
// Decoded:
// {CLASS:testy;ID:5}
// {
//         [cat] => Meow
//         [dog] => Woof
//         [position] => 1337
// }

Encode and Decode a class (without using @JSON_CONSTRUCTOR_CLASS)

// Create a base class
Class Testy
{
};
// Create an array to Json encode
my $Testy = new Testy();
// Add some variables to the class
$Testy->$Cat = "Meow";
$Testy->$Dog = "Woof";
$Testy->$Position = 1337;
// JSON Encode it
my $Encoded = JsonEncode($Testy);
// Print the Encoded Text
say "Encoded:";
say $Encoded;
say;
// Decode the JSON
my $Decoded = JsonDecode($Encoded);
// Print the Decoded object
say "Decoded:";
// JSON returned an array so lets convert it
// into a class now
my $cls = NewClassFromArray("Testy", $Decoded);
printr $cls;
// Prints:
// Encoded:
// {"cat":"Meow","dog":"Woof","position":1337}
 
// Decoded:
// {CLASS:testy;ID:5}
// {
//         [cat] => Meow
//         [dog] => Woof
//         [position] => 1337
//         [this] =>       {CLASS:testy;ID:5}
// }
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox