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