JSON.js

JSON encoder / decoder

This object uses good practices to encode/decode quikly and a bit safer(*) every kind of JSON compatible variable.

(*) Please read more about JSON and Ajax JavaScript Hijacking problems, http://www.fortifysoftware.com/advisory.jsp

To download last version of this script use this link: http://www.devpro.it/code/149.html

Version

1.4fixed empty object encoding problems.

Compatibility

FireFoxVersion 1, 1.5, 2 and 3 (FireFox uses secure code evaluation)
Internet ExplorerVersion 5, 5.5, 6 and 7
Opera8 and 9 (probably 7 too)
SafariVersion 2 (probably 1 too)
KonquerorVersion 3 or greater

Dependencies

JSONError.js

Credits

Author

Andrea Giammarchi, http://www.3site.eu

License

Copyright (C) 2007 Andrea Giammarchi - www.3site.eu

Permission is hereby granted, free of charge,
to any person obtaining a copy of this software and associated
documentation files (the "Software"),
to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Summary
This object uses good practices to encode/decode quikly and a bit safer(*) every kind of JSON compatible variable.
Stand alone or prototyped encode, decode or toDate public methods.
decodes a valid JSON encoded string.
encode a generic JavaScript variable into a valid JSON string.
transforms a JSON encoded Date string into a native Date object.

JSON

JSON

Stand alone or prototyped encode, decode or toDate public methods.

Example

alert(JSON.encode([0,1,false,true,null,[2,3],{"some":"value"}]));
// [0,1,false,true,null,[2,3],{"some":"value"}]

alert(JSON.decode('[0,1,false,true,null,[2,3],{"some":"value"}]'))
// 0,1,false,true,,2,3,[object Object]

Methods - Public

Summary
decodes a valid JSON encoded string.
encode a generic JavaScript variable into a valid JSON string.
transforms a JSON encoded Date string into a native Date object.

decode

this.decode = function()

decodes a valid JSON encoded string.

Arguments

[String / Function]Optional JSON string to decode or a filter function if method is a String prototype.
[Function]Optional filter function if first argument is a JSON string and this method is not a String prototype.

Returns

ObjectGeneric JavaScript variable or undefined

Example [Basic]

var    arr = JSON.decode('[1,2,3]');
alert(arr); // 1,2,3

arr = JSON.decode('[1,2,3]', function(key, value){return key * value});
alert(arr); // 0,2,6

Example [Prototype]

String.prototype.parseJSON = JSON.decode;

alert('[1,2,3]'.parseJSON()); // 1,2,3

try {
alert('[1,2,3]'.parseJSON(function(key, value){return key * value}));
// 0,2,6
}
catch(e) {
alert(e.message);
}

Note

Internet Explorer 5 and other old browsers should use a different regular expression to check if a JSON string is valid or not.  This old browsers dedicated RegExp is not safe as native version is but it required for compatibility.

encode

this.encode = function()

encode a generic JavaScript variable into a valid JSON string.

Arguments

[Object]Optional generic JavaScript variable to encode if method is not an Object prototype.

Returns

StringValid JSON string or undefined

Example [Basic]

var    s = JSON.encode([1,2,3]);
alert(s); // [1,2,3]

Example [Prototype]

Object.prototype.toJSONString = JSON.encode;

alert([1,2,3].toJSONString()); // [1,2,3]

toDate

this.toDate = function()

transforms a JSON encoded Date string into a native Date object.

Arguments

[String/Number]Optional JSON Date string or server time if this method is not a String prototype.  Server time should be an integer, based on seconds since 1970/01/01 or milliseconds / 1000 since 1970/01/01.

Returns

DateDate object or undefined if string is not a valid Date

Example [Basic]

var    serverDate = JSON.toDate("2007-04-05T08:36:46");
alert(serverDate.getMonth()); // 3 (months start from 0)

Example [Prototype]

String.prototype.parseDate = JSON.toDate;

alert("2007-04-05T08:36:46".parseDate().getDate()); // 5

Example [Server Time]

var    phpServerDate = JSON.toDate(<?php echo time(); ?>);
var csServerDate = JSON.toDate(<%=(DateTime.Now.Ticks/10000-62135596800000)%>/1000);

Example [Server Time Prototype]

Number.prototype.parseDate = JSON.toDate;
var phpServerDate = (<?php echo time(); ?>).parseDate();
var csServerDate = (<%=(DateTime.Now.Ticks/10000-62135596800000)%>/1000).parseDate();

Note

This method accepts an integer or numeric string too to mantain compatibility with generic server side time() function.  You can convert quickly mtime, ctime, time and other time based values.  With languages that supports milliseconds you can send total milliseconds / 1000 (time is set as time * 1000)

Properties - Private

Private

List

Object’c’ - a dictionary with useful keys / values for fast encode convertion
Function’d’ - returns decimal string rappresentation of a number (“14”, “03”, etc)
Function’e’ - safe and native code evaulation
Function’i’ - returns integer from string (“01” => 1, “15” => 15, etc)
Array’p’ - a list with different “0” strings for fast special chars escape convertion
RegExp’rc’ - regular expression to check JSON strings (different for IE5 or old browsers and new one)
RegExp’rd’ - regular expression to check a JSON Date string
RegExp’rs’ - regular expression to check string chars to modify using c (char) values
RegExp’rt’ - regular expression to check integer numeric string (for toDate time version evaluation)
RegExp’ru’ - regular expression to check string chars to escape using “\u” prefix
Function’s’ - returns escaped string adding “\\” char as prefix (“\\” => “\\\\”, etc.)
Function’u’ - returns escaped string, modifyng special chars using “\uNNNN” notation
Function’v’ - returns boolean value to skip object methods or prototyped parameters (length, others), used for optional decode filter function
Function’$’ - returns object constructor if it was not cracked (someVar = {}; someVar.constructor = String <= ignore them)
Function’$$’ - returns boolean value to check native Array and Object constructors before convertion
JSON
Stand alone or prototyped encode, decode or toDate public methods.
this.decode = function()
decodes a valid JSON encoded string.
this.encode = function()
encode a generic JavaScript variable into a valid JSON string.
this.toDate = function()
transforms a JSON encoded Date string into a native Date object.