Json Docs

See json.org for more information about the json format and benefits.

API

isSupported()

Determines if the browser supports the functions in Fork's JSON library. Returns true of so and false otherwise.

Examples

if (FORK.Json.isSupported()) {
  FORK.Json.load('{a:4}');
} else {
  alert('Your browser does not support the Fork JSON library.');
}

dump(entity)

The entity is returned as a Json string. This process is called serialization or marshaling in some languages. The dump function can dump Date objects to an ISO format from years to seconds but without milliseconds (eg 2006-12-26T12:04:05).

entity
A JavaScript object used as a hash or array.

Examples

FORK.Json.dump({a:4, b:new Date(), c:[d:{e:4}]});

load(json, hook)

If the json string is from a trusted source and requires no special manipulation during load you can parse it using var obj = eval('(' + json + ')');

This function parses a json string safely and returns a JavaScript object. The load function does not convert date strings into Date objects automatically. You can use the hook function for this type of behavior.

If the JSON string sent to load() does not match the JSON format then load() returns null.

json
A json string.
hook
A function that processes each object node in the JSON data structure. The hook function takes two arguments: The first is the property of the node and the second is the property value. The returned value is used in place of the object property value.

Examples

FORK.Json.load('{"a":2,"b":["asdf",55,{"f":9}],"day":"2007-01-10T21:10:40"}',
function(k, o) {
  if (typeof o=='string') {
    return o;
  }
  return o;
}
);

Design

Currently this library is intended to be a namespaced version of the library at http://json.org/json.js. See the official JSON proposal

Credits

Douglas Crockford is the JSON creator and wrote the original script which is only slightly modified and updated for inclusion with Fork. The new script on json.org now augments built-in prototypes more than the version that spawned the Fork version.