json.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. define(["./kernel", "../json"], function(dojo, json){
  2. // module:
  3. // dojo/_base/json
  4. /*=====
  5. return {
  6. // summary:
  7. // This module defines the dojo JSON API.
  8. };
  9. =====*/
  10. dojo.fromJson = function(/*String*/ js){
  11. // summary:
  12. // Parses a JavaScript expression and returns a JavaScript value.
  13. // description:
  14. // Throws for invalid JavaScript expressions. It does not use a strict JSON parser. It
  15. // always delegates to eval(). The content passed to this method must therefore come
  16. // from a trusted source.
  17. // It is recommend that you use dojo/json's parse function for an
  18. // implementation uses the (faster) native JSON parse when available.
  19. // js:
  20. // a string literal of a JavaScript expression, for instance:
  21. // `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
  22. return eval("(" + js + ")"); // Object
  23. };
  24. /*=====
  25. dojo._escapeString = function(){
  26. // summary:
  27. // Adds escape sequences for non-visual characters, double quote and
  28. // backslash and surrounds with double quotes to form a valid string
  29. // literal.
  30. };
  31. =====*/
  32. dojo._escapeString = json.stringify; // just delegate to json.stringify
  33. dojo.toJsonIndentStr = "\t";
  34. dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint){
  35. // summary:
  36. // Returns a [JSON](http://json.org) serialization of an object.
  37. // description:
  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.
  43. // it:
  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.
  50. // prettyPrint:
  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.
  57. // returns:
  58. // A JSON string serialization of the passed-in object.
  59. // example:
  60. // simple serialization of a trivial object
  61. // | var jsonStr = dojo.toJson({ howdy: "stranger!", isStrange: true });
  62. // | doh.is('{"howdy":"stranger!","isStrange":true}', jsonStr);
  63. // example:
  64. // a custom serializer for an objects of a particular class:
  65. // | dojo.declare("Furby", null, {
  66. // | furbies: "are strange",
  67. // | furbyCount: 10,
  68. // | __json__: function(){
  69. // | },
  70. // | });
  71. // use dojo/json
  72. return json.stringify(it, function(key, value){
  73. if(value){
  74. var tf = value.__json__||value.json;
  75. if(typeof tf == "function"){
  76. return tf.call(value);
  77. }
  78. }
  79. return value;
  80. }, prettyPrint && dojo.toJsonIndentStr); // String
  81. };
  82. return dojo;
  83. });