92 lines
3.2 KiB
JavaScript
92 lines
3.2 KiB
JavaScript
|
define("dojo/_base/json", ["./kernel", "../json"], function(dojo, json){
|
||
|
|
||
|
// module:
|
||
|
// dojo/_base/json
|
||
|
|
||
|
/*=====
|
||
|
return {
|
||
|
// summary:
|
||
|
// This module defines the dojo JSON API.
|
||
|
};
|
||
|
=====*/
|
||
|
|
||
|
dojo.fromJson = function(/*String*/ js){
|
||
|
// summary:
|
||
|
// Parses a JavaScript expression and returns a JavaScript value.
|
||
|
// description:
|
||
|
// Throws for invalid JavaScript expressions. It does not use a strict JSON parser. It
|
||
|
// always delegates to eval(). The content passed to this method must therefore come
|
||
|
// from a trusted source.
|
||
|
// It is recommend that you use dojo/json's parse function for an
|
||
|
// implementation uses the (faster) native JSON parse when available.
|
||
|
// js:
|
||
|
// a string literal of a JavaScript expression, for instance:
|
||
|
// `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
|
||
|
|
||
|
return eval("(" + js + ")"); // Object
|
||
|
};
|
||
|
|
||
|
/*=====
|
||
|
dojo._escapeString = function(){
|
||
|
// summary:
|
||
|
// Adds escape sequences for non-visual characters, double quote and
|
||
|
// backslash and surrounds with double quotes to form a valid string
|
||
|
// literal.
|
||
|
};
|
||
|
=====*/
|
||
|
dojo._escapeString = json.stringify; // just delegate to json.stringify
|
||
|
|
||
|
dojo.toJsonIndentStr = "\t";
|
||
|
dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint){
|
||
|
// summary:
|
||
|
// Returns a [JSON](http://json.org) serialization of an object.
|
||
|
// description:
|
||
|
// Returns a [JSON](http://json.org) serialization of an object.
|
||
|
// Note that this doesn't check for infinite recursion, so don't do that!
|
||
|
// It is recommend that you use dojo/json's stringify function for an lighter
|
||
|
// and faster implementation that matches the native JSON API and uses the
|
||
|
// native JSON serializer when available.
|
||
|
// it:
|
||
|
// an object to be serialized. Objects may define their own
|
||
|
// serialization via a special "__json__" or "json" function
|
||
|
// property. If a specialized serializer has been defined, it will
|
||
|
// be used as a fallback.
|
||
|
// Note that in 1.6, toJson would serialize undefined, but this no longer supported
|
||
|
// since it is not supported by native JSON serializer.
|
||
|
// prettyPrint:
|
||
|
// if true, we indent objects and arrays to make the output prettier.
|
||
|
// The variable `dojo.toJsonIndentStr` is used as the indent string --
|
||
|
// to use something other than the default (tab), change that variable
|
||
|
// before calling dojo.toJson().
|
||
|
// Note that if native JSON support is available, it will be used for serialization,
|
||
|
// and native implementations vary on the exact spacing used in pretty printing.
|
||
|
// returns:
|
||
|
// A JSON string serialization of the passed-in object.
|
||
|
// example:
|
||
|
// simple serialization of a trivial object
|
||
|
// | var jsonStr = dojo.toJson({ howdy: "stranger!", isStrange: true });
|
||
|
// | doh.is('{"howdy":"stranger!","isStrange":true}', jsonStr);
|
||
|
// example:
|
||
|
// a custom serializer for an objects of a particular class:
|
||
|
// | dojo.declare("Furby", null, {
|
||
|
// | furbies: "are strange",
|
||
|
// | furbyCount: 10,
|
||
|
// | __json__: function(){
|
||
|
// | },
|
||
|
// | });
|
||
|
|
||
|
// use dojo/json
|
||
|
return json.stringify(it, function(key, value){
|
||
|
if(value){
|
||
|
var tf = value.__json__||value.json;
|
||
|
if(typeof tf == "function"){
|
||
|
return tf.call(value);
|
||
|
}
|
||
|
}
|
||
|
return value;
|
||
|
}, prettyPrint && dojo.toJsonIndentStr); // String
|
||
|
};
|
||
|
|
||
|
return dojo;
|
||
|
});
|