Core Function JsonEncode
From Sputnik Wiki
(Difference between revisions)
(→options) |
(→options) |
||
Line 29: | Line 29: | ||
@JSON_BASE64_BINARY // Encodes Binary in Base64 | @JSON_BASE64_BINARY // Encodes Binary in Base64 | ||
@JSON_CONSTRUCTOR_CLASS // Encodes Classes in a Constructor so they will be decoded fully | @JSON_CONSTRUCTOR_CLASS // Encodes Classes in a Constructor so they will be decoded fully | ||
+ | // Note - This is ignored if you are using the JsonSerializable extend | ||
@JSON_PRETTY_PRINT // Use whitespace in returned data to format it so it is easy to read | @JSON_PRETTY_PRINT // Use whitespace in returned data to format it so it is easy to read | ||
@JSON_UNESCAPED_SLASHES // Don't escape / | @JSON_UNESCAPED_SLASHES // Don't escape / |
Revision as of 08:56, 30 July 2014
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 // Note - This is ignored if you are using the JsonSerializable extend @JSON_PRETTY_PRINT // Use whitespace in returned data to format it so it is easy to read @JSON_UNESCAPED_SLASHES // Don't escape / @JSON_UNESCAPED_UNICODE // Encode multibyte Unicode characters literally (default is to escape as \uXXXX)
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
Using @JSON_PRETTY_PRINT to make JSON easy to read
// Create something to encode my $Testy = Pack("iz0", 1337, "Hi"); my $Hehe = array("One", "Two", $Testy, array("Cat" => array("Four", "Five"), "Dog"), "FoX"); // JSON Encode it my $Encoded = JsonEncode($Hehe, @JSON_PRETTY_PRINT); // Print the Encoded Text say "Encoded:"; say $Encoded; // Encoded: // [ // "One", // "Two", // "9\u0005\u0000\u0000Hi", // { // "0": "Dog", // "Cat": [ // "Four", // "Five" // ] // }, // "FoX" // ]
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} // }