Requires

Provides

JSON encoder and decoder.

License:
MIT-style license.
  1. 20
if (typeof JSON == 'undefined') this.JSON = {};

<1.2compat>

  1. 24
  2. 25
  3. 26
  4. 27
JSON = new Hash({ stringify: JSON.stringify, parse: JSON.parse });

</1.2compat>

  1. 31
  2. 32
  3. 33
  4. 34
  5. 35
  6. 36
  7. 37
  8. 38
  9. 39
  10. 40
  11. 41
  12. 42
  13. 43
  14. 44
  15. 45
  16. 46
  17. 47
  18. 48
  19. 49
  20. 50
  21. 51
  22. 52
  23. 53
  24. 54
  25. 55
  26. 56
  27. 57
  28. 58
  29. 59
  30. 60
  31. 61
  32. 62
  33. 63
  34. 64
Object.append(JSON, { $specialChars: {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'}, $replaceChars: function(chr){ return JSON.$specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16); }, encode: function(obj){ switch (typeOf(obj)){ case 'string': return '"' + obj.replace(/[\x00-\x1f\\"]/g, JSON.$replaceChars) + '"'; case 'array': return '[' + String(obj.map(JSON.encode).clean()) + ']'; case 'object': case 'hash': var string = []; Object.each(obj, function(value, key){ var json = JSON.encode(value); if (json) string.push(JSON.encode(key) + ':' + json); }); return '{' + string + '}'; case 'number': case 'boolean': return String(obj); case 'null': return 'null'; } return null; }, decode: function(string, secure){ if (typeOf(string) != 'string' || !string.length) return null; if (secure && !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) return null; return eval('(' + string + ')'); } });