Tokiwa Battle Royale  GE777
A PHP Battle Royale inspired game
 All Data Structures Namespaces Files Functions Variables Pages
json.js
Go to the documentation of this file.
1 /*
2  json.js
3  2006-10-05
4 
5  This file adds these methods to JavaScript:
6 
7  object./()
8 
9  This method produces a JSON text from an object. The
10  object must not contain any cyclical references.
11 
12  array.toJSONString()
13 
14  This method produces a JSON text from an array. The
15  array must not contain any cyclical references.
16 
17  string.parseJSON()
18 
19  This method parses a JSON text to produce an object or
20  array. It will return false if there is an error.
21 
22  It is expected that these methods will formally become part of the
23  JavaScript Programming Language in the Fourth Edition of the
24  ECMAScript standard.
25 */
26 (function () {
27  var m = {
28  '\b': '\\b',
29  '\t': '\\t',
30  '\n': '\\n',
31  '\f': '\\f',
32  '\r': '\\r',
33  '"' : '\\"',
34  '\\': '\\\\'
35  },
36  s = {
37  array: function (x) {
38  var a = ['['], b, f, i, l = x.length, v;
39  for (i = 0; i < l; i += 1) {
40  v = x[i];
41  f = s[typeof v];
42  if (f) {
43  v = f(v);
44  if (typeof v == 'string') {
45  if (b) {
46  a[a.length] = ',';
47  }
48  a[a.length] = v;
49  b = true;
50  }
51  }
52  }
53  a[a.length] = ']';
54  return a.join('');
55  },
56  'boolean': function (x) {
57  return String(x);
58  },
59  'null': function (x) {
60  return "null";
61  },
62  number: function (x) {
63  return isFinite(x) ? String(x) : 'null';
64  },
65  object: function (x) {
66  if (x) {
67  if (x instanceof Array) {
68  return s.array(x);
69  }
70  var a = ['{'], b, f, i, v;
71  for (i in x) {
72  v = x[i];
73  f = s[typeof v];
74  if (f) {
75  v = f(v);
76  if (typeof v == 'string') {
77  if (b) {
78  a[a.length] = ',';
79  }
80  a.push(s.string(i), ':', v);
81  b = true;
82  }
83  }
84  }
85  a[a.length] = '}';
86  return a.join('');
87  }
88  return 'null';
89  },
90  string: function (x) {
91  if (/["\\\x00-\x1f]/.test(x)) {
92  x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
93  var c = m[b];
94  if (c) {
95  return c;
96  }
97  c = b.charCodeAt();
98  return '\\u00' +
99  Math.floor(c / 16).toString(16) +
100  (c % 16).toString(16);
101  });
102  }
103  return '"' + x + '"';
104  }
105  };
106 
107  Object.prototype.toJSONString = function () {
108  return s.object(this);
109  };
110 
111  Array.prototype.toJSONString = function () {
112  return s.array(this);
113  };
114 })();
115 
116 String.prototype.parseJSON = function () {
117  try {
118  return (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) &&
119  eval('(' + this + ')');
120  } catch (e) {
121  return false;
122  }
123 };