JS object serialized as JSON object

Source: Internet
Author: User
Tags object eval join json return string tostring hasownproperty

The JS object is serialized as a JSON object with the following code:

 
 
  1. /**
  2. * JSON Parsing class
  3. Method
  4. * Decodes a JSON string into an object that is recognized by the page
  5. * @param {string} JSON the JSON String
  6. * @return {Object} The resulting object
  7. * Object o = jsonutil.decode (JSON);
  8. *
  9. * Serializing a JS object as a JSON string
  10. * @param {Mixed} o the variable to decode
  11. * @return {string} The JSON string
  12. * String json = Jsonutil.encode (o);
  13. */
  14. var jsonutil;
  15. if (! Jsonutil) {
  16. Jsonutil = {};
  17. }
  18. Jsonutil.decode = function (JSON) {
  19. try {
  20. Return eval ("\u0028" + json + ' \u0029 ');
  21. } catch (Exception) {
  22. Return eval ("\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064");
  23. }
  24. };
  25. Jsonutil.encode = (function () {
  26. var $ =!! {}.hasownproperty, _ = function ($) {
  27. Return $ < 10? "0" + $: $
  28. }, A = {
  29. "\b": "\\b",
  30. "\ T": "\\t",
  31. "\ n": "\\n",
  32. "\f": "\\f",
  33. ' \ r ': ' \ R ',
  34. "\"" : "\\\"",
  35. "\\" : "\\\\"
  36. };
  37. Return (function (C) {
  38. if (typeof c = = "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064" c = = null)
  39. return "NULL";
  40. else if (Object.prototype.toString.call (C) = = "\u005b\u006f\u0062\u006a\u0065\u0063\u0074\u0020\u0041\u0072\u0072 \u0061\u0079\u005d ") {
  41. var B = ["\u005b"], G, E, D = C.length, F;
  42. for (E = 0; E < D; E = 1) {
  43. F = C[e];
  44. Switch (typeof F) {
  45. Case "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064":
  46. Case "\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e":
  47. Case "\u0075\u006e\u006b\u006e\u006f\u0077\u006e":
  48. Break
  49. Default:
  50. if (G)
  51. B.push ("\u002c");
  52. B.push (F = = null?) "Null": This.encode (F));
  53. G = True
  54. }
  55. }
  56. B.push ("\u005d");
  57. Return B.join ("")
  58. else if ((Object.prototype.toString.call (C) = = "\u005b\u006f\u0062\u006a\u0065\u0063\u0074\u0020\u0044\u0061\ u0074\u0065\u005d "))
  59. Return "" "+ c.getfullyear () +"-"+ _ (C.getmonth () + 1) +"-"+ _ (C.getdate ()) +" T "+ _ (c.gethours ()) +": "+ _ (C.GETMI Nutes ()) + ":" + _ (C.getseconds ()) + "" ";
  60. else if (typeof C = = "\u0073\u0074\u0072\u0069\u006e\u0067") {
  61. if (/["\\\x00-\x1f]/.test (C))
  62. Return "\" + c.replace (/([\x00-\x1f\\ "])/g, function (B, _) {
  63. var $ = a[_];
  64. if ($)
  65. return $;
  66. $ = _.charcodeat ();
  67. Return "\\u00" + math.floor ($/). ToString ($%). ToString (16)
  68. }) + "\"";
  69. return "\" "+ C +" \ ""
  70. else if (typeof C = "\u006e\u0075\u006d\u0062\u0065\u0072")
  71. return Isfinite (C)? String (C): "NULL";
  72. else if (typeof C = "\u0062\u006f\u006f\u006c\u0065\u0061\u006e")
  73. Return String (C);
  74. else {
  75. B = ["\u007b"], G, E, F;
  76. For (E in C)
  77. if (!$ c.hasownproperty (E)) {
  78. F = C[e];
  79. if (F = = null)
  80. Continue
  81. Switch (typeof F) {
  82. Case "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064":
  83. Case "\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e":
  84. Case "\u0075\u006e\u006b\u006e\u006f\u0077\u006e":
  85. Break
  86. Default:
  87. if (G)
  88. B.push ("\u002c");
  89. B.push (This.encode (E), "\u003a", This.encode (F));
  90. G = True
  91. }
  92. }
  93. B.push ("\u007d");
  94. Return B.join ("")
  95. }
  96. })
  97. })();
  98. Window. Jsonutil = Jsonutil;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.