1. Introduction
The Uize.Json
module provides versatile methods for serializing and deserializing objects to and from the JSON (JavaScript Object Notation) format.
DEVELOPERS: Chris van Rensburg
The Uize.Json
module is a package under the Uize
namespace, providing versatile serializaton options to permit a wide array of results, from prettified JSON (with linebreaks, indentation, key sorting, etc.) to compact (efficient for AJAX communication).
2. Static Methods
2.1. Uize.Json.from
2.2. Uize.Json.to
Serializes the specified value to a string of JSON format. The value to serialize can be a string, boolean, number, object, array, null, undefined, or a regular expression.
SYNTAX
jsonSTR = Uize.Json.to (valueANYTYPE);
VARIATION 1
jsonSTR = Uize.Json.to (valueANYTYPE,serializationOptionsOBJ);
When the optional serializationOptionsOBJ
parameter is specified, then the way in which the value is serialized to JSON format can be configured to produce wide ranging results, from prettified to compact. The value of the serializationOptionsOBJ
parameter should be an object, with properties as follows...
SERIALIZATION OPTIONS
{ indentChars:indentCharsSTR, // optional, defaults to one tab linebreakChars:linebreakCharsSTR, // optional, defaults to one linebreak quoteChar:quoteCharSTR, // optional, defaults to a single quote keyDelimiter:keyDelimiterSTR, // optional, defaults to colon without spaces padKeys:padKeysBOOL, // optional, defaults to false keyAlign:keyAlignSTR, // 'left' (default) | 'center' | 'right' whenToQuoteKeys:whenToQuoteKeysSTR, // 'auto' (default) | 'auto all' | 'always' sortKeys:sortKeysBOOL // optional, defaults to false }
2.2.1. indentChars
A string, specifying the characters that should be used to indent key/value pairs of objects and elements of arrays. Defaults to one tab. Specify an empty string for compact serialization.
2.2.2. linebreakChars
A string, specifying the characters to be used to separate key/value pairs of objects and elements of arrays. Defaults to a single linebreak. Specify an empty string for compact serialization.
2.2.3. quoteChar
A string, specifying the character to be used when quoting string literals for string values or object property names (keys) that require quoting. Defaults to a single quote character.
2.2.4. keyDelimiter
A string, specifying the characters to be used when separating keys from values for serializing objects. Defaults to a colon without padding spaces around it. Add your own spaces to order.
2.2.5. padKeys
A boolean, specifying whether or not keys should be padded with spaces so that all keys occupy the same number of characters and so that the keyDelimiter
character lines up vertically. Defaults to false
. See the companion keyAlign
property.
2.2.6. keyAlign
A string, specifying whether the keys should be aligned left, center, or right when padding keys. Only applicable when the companion padKeys
property is set to true
.
2.2.7. whenToQuoteKeys
A string, specifying under what conditions object property names (keys) should be quoted. The default value of 'auto'
causes individual keys to only be quoted when necessary. The value of 'auto all'
causes all keys of the same object to be quoted if any of the keys needs to be quoted. The value of 'always'
causes all keys to always be quoted.
2.2.8. sortKeys
A boolean, specifying whether or not object property names (keys) should be sorted alphabetically when serializing objects. Defaults to false
.
EXAMPLES
For the following examples, let's assume that we have a JavaScript object defined as follows...
var myObject = { someNumber:123.456, aBoolean:true, 'value is a regular expression':/\d+/gim, 123.456:'it\'s a floating point key!', '~!@#$%^&*()_+':1, 'a key with a double quote "':1, 'a key with a single quote \'':1 };
EXAMPLE 1
The following call to Uize.Json.to
...
Uize.Json.to (myObject,{keyDelimiter:' : '});
...would produce the output...
{ someNumber : 123.456, aBoolean : true, 'value is a regular expression' : /\d+/gim, 123.456 : 'it\'s a floating point key!', '~!@#$%^&*()_+' : 1, 'a key with a double quote "' : 1, 'a key with a single quote \'' : 1 }
EXAMPLE 2
The following call to Uize.Json.to
...
Uize.Json.to (myObject,{keyDelimiter:' : ',padKeys:true,keyAlign:'left'});
...would produce the output...
{ someNumber : 123.456, aBoolean : true, 'value is a regular expression' : /\d+/gim, 123.456 : 'it\'s a floating point key!', '~!@#$%^&*()_+' : 1, 'a key with a double quote "' : 1, 'a key with a single quote \'' : 1 }
EXAMPLE 3
The following call to Uize.Json.to
...
Uize.Json.to (myObject,{keyDelimiter:' : ',padKeys:true,keyAlign:'right'});
...would produce the output...
{ someNumber : 123.456, aBoolean : true, 'value is a regular expression' : /\d+/gim, 123.456 : 'it\'s a floating point key!', '~!@#$%^&*()_+' : 1, 'a key with a double quote "' : 1, 'a key with a single quote \'' : 1 }
EXAMPLE 4
The following call to Uize.Json.to
...
Uize.Json.to (myObject,{keyDelimiter:' : ',whenToQuoteKeys:'auto all'});
...would produce the output...
{ 'someNumber' : 123.456, 'aBoolean' : true, 'value is a regular expression' : /\d+/gim, '123.456' : 'it\'s a floating point key!', '~!@#$%^&*()_+' : 1, 'a key with a double quote "' : 1, 'a key with a single quote \'' : 1 }
EXAMPLE 5
The following call to Uize.Json.to
...
Uize.Json.to (myObject,{keyDelimiter:' : ',quoteChar:'"'});
...would produce the output...
{ someNumber : 123.456, aBoolean : true, "value is a regular expression" : /\d+/gim, 123.456 : "it's a floating point key!", "~!@#$%^&*()_+" : 1, "a key with a double quote \"" : 1, "a key with a single quote '" : 1 }
EXAMPLE 6
The following call to Uize.Json.to
...
Uize.Json.to (myObject,{keyDelimiter:' : ',sortKeys:true});
...would produce the output...
{ 123.456 : 'it\'s a floating point key!', 'a key with a double quote "' : 1, 'a key with a single quote \'' : 1, aBoolean : true, someNumber : 123.456, 'value is a regular expression' : /\d+/gim, '~!@#$%^&*()_+' : 1 }
VARIATION 2
jsonSTR = Uize.Json.to (valueANYTYPE,'mini');
When the special string value 'mini'
is specified in place of the serializationOptionsOBJ
parameter, then serialization options are used that will produce a compact serialization of the specified value. This is equivalent to specifying the value {indentChars:'',linebreakChars:''}
for the serializationOptionsOBJ
parameter.
NOTES
see the companion Uize.Json.from static method |