JSON (library)

Categories:Data

Overview

Summary

Douglas Crockford's standard JSON library. Note that this library, or perhaps SpiderMonkey, does not seem to serialize and deserialize Dates correctly.

Contents

The JSON library is currently disabled in the core bootstrap, as it extends native objects and breaks for..in loops. I'll look into rewriting the JSON lib once the framework is officially in beta.

API

stringify(value, replacer, space)

Converts a JavaScript value to a valid JSON String

Parameters:

  • value Value A JavaScript value
  • replacer Function An optional Function that can replace values
  • replacer Array An optional Array of strings that will select the keys
  • space String An optional String to use for spaces to make the JSON more readable

Returns:

  •   String A valid JSON String

Serialize a JavaScript value to a JSON String:

var json = JSON.stringify([1, 2, 3, {a:1, b:'Hello'}]);
trace(json);
[1,2,3,{"a":1,"b":"Hello"}]

parse(text, reviver)

Converts a valid JSON String to a JavaScript value

Parameters:

  • text String A vaild JSON String
  • reviver Function An optional function to process each key

Returns:

  •   Value A JavaScript value

Deserialize a JSON string to a javaScript object:

var value = JSON.parse('[1,2,3,{"a":1,"b":"Hello"}]')
Output.inspect(value);
Inspect: Array (depth:4, objects:1, values:5, time:0.0 seconds)
--------------------------------------------------------------------------------
array => Array
	 0: 1
	 1: 2
	 2: 3
	[3] => Object
		 a: 1
		 b: "Hello"

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>