]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/json.js.uncompressed.js
1 define("dojo/_base/json", ["./kernel", "../json"], function(dojo
, json
){
5 // This module defines the dojo JSON API.
7 dojo
.fromJson = function(/*String*/ js
){
9 // Parses a JavaScript expression and returns a JavaScript value.
11 // Throws for invalid JavaScript expressions. It does not use a strict JSON parser. It
12 // always delegates to eval(). The content passed to this method must therefore come
13 // from a trusted source.
14 // It is recommend that you use dojo/json's parse function for an
15 // implementation uses the (faster) native JSON parse when available.
17 // a string literal of a JavaScript expression, for instance:
18 // `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
20 return eval("(" + js
+ ")"); // Object
24 dojo._escapeString = function(){
26 // Adds escape sequences for non-visual characters, double quote and
27 // backslash and surrounds with double quotes to form a valid string
31 dojo
._escapeString
= json
.stringify
; // just delegate to json.stringify
33 dojo
.toJsonIndentStr
= "\t";
34 dojo
.toJson = function(/*Object*/ it
, /*Boolean?*/ prettyPrint
){
36 // Returns a [JSON](http://json.org) serialization of an object.
38 // Returns a [JSON](http://json.org) serialization of an object.
39 // Note that this doesn't check for infinite recursion, so don't do that!
40 // It is recommend that you use dojo/json's stringify function for an lighter
41 // and faster implementation that matches the native JSON API and uses the
42 // native JSON serializer when available.
44 // an object to be serialized. Objects may define their own
45 // serialization via a special "__json__" or "json" function
46 // property. If a specialized serializer has been defined, it will
47 // be used as a fallback.
48 // Note that in 1.6, toJson would serialize undefined, but this no longer supported
49 // since it is not supported by native JSON serializer.
51 // if true, we indent objects and arrays to make the output prettier.
52 // The variable `dojo.toJsonIndentStr` is used as the indent string --
53 // to use something other than the default (tab), change that variable
54 // before calling dojo.toJson().
55 // Note that if native JSON support is available, it will be used for serialization,
56 // and native implementations vary on the exact spacing used in pretty printing.
58 // A JSON string serialization of the passed-in object.
60 // simple serialization of a trivial object
61 // | var jsonStr = dojo.toJson({ howdy: "stranger!", isStrange: true });
62 // | doh.is('{"howdy":"stranger!","isStrange":true}', jsonStr);
64 // a custom serializer for an objects of a particular class:
65 // | dojo.declare("Furby", null, {
66 // | furbies: "are strange",
68 // | __json__: function(){
73 return json
.stringify(it
, function(key
, value
){
75 var tf
= value
.__json__
||value
.json
;
76 if(typeof tf
== "function"){
77 return tf
.call(value
);
81 }, prettyPrint
&& dojo
.toJsonIndentStr
); // String