json.js 3.2 KB

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