~ubuntu-branches/ubuntu/karmic/gears/karmic

« back to all changes in this revision

Viewing changes to third_party/jsonjs/json_noeval.js

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2009-04-30 19:15:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090430191525-0790sb5wzg8ou0xb
Tags: upstream-0.5.21.0~svn3334+dfsg
ImportĀ upstreamĀ versionĀ 0.5.21.0~svn3334+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2005 JSON.org
 
3
 
 
4
Permission is hereby granted, free of charge, to any person obtaining a copy
 
5
of this software and associated documentation files (the "Software"), to deal
 
6
in the Software without restriction, including without limitation the rights
 
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
8
copies of the Software, and to permit persons to whom the Software is
 
9
furnished to do so, subject to the following conditions:
 
10
 
 
11
The Software shall be used for Good, not Evil.
 
12
 
 
13
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
14
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
15
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
16
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
17
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
18
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
19
SOFTWARE.
 
20
*/
 
21
 
 
22
//Array.prototype.______array = '______array';
 
23
 
 
24
var JSON = {
 
25
    org: 'http://www.JSON.org',
 
26
    copyright: '(c)2005 JSON.org',
 
27
    license: 'http://www.crockford.com/JSON/license.html',
 
28
 
 
29
    stringify: function (arg) {
 
30
        var c, i, l, s = '', v;
 
31
 
 
32
        switch (typeof arg) {
 
33
        case 'object':
 
34
            if (arg) {
 
35
                if (arg.constructor == Array.prototype.constructor) {
 
36
                    for (i = 0; i < arg.length; ++i) {
 
37
                        v = this.stringify(arg[i]);
 
38
                        if (s) {
 
39
                            s += ',';
 
40
                        }
 
41
                        s += v;
 
42
                    }
 
43
                    return '[' + s + ']';
 
44
                } else if (typeof arg.toString != 'undefined') {
 
45
                    for (i in arg) {
 
46
                        v = arg[i];
 
47
                        if (typeof v != 'undefined' && typeof v != 'function') {
 
48
                            v = this.stringify(v);
 
49
                            if (s) {
 
50
                                s += ',';
 
51
                            }
 
52
                            s += this.stringify(i) + ':' + v;
 
53
                        }
 
54
                    }
 
55
                    return '{' + s + '}';
 
56
                }
 
57
            }
 
58
            return 'null';
 
59
        case 'number':
 
60
            return isFinite(arg) ? String(arg) : 'null';
 
61
        case 'string':
 
62
            l = arg.length;
 
63
            s = '"';
 
64
            for (i = 0; i < l; i += 1) {
 
65
                c = arg.charAt(i);
 
66
                if (c >= ' ') {
 
67
                    if (c == '\\' || c == '"') {
 
68
                        s += '\\';
 
69
                    }
 
70
                    s += c;
 
71
                } else {
 
72
                    switch (c) {
 
73
                        case '\b':
 
74
                            s += '\\b';
 
75
                            break;
 
76
                        case '\f':
 
77
                            s += '\\f';
 
78
                            break;
 
79
                        case '\n':
 
80
                            s += '\\n';
 
81
                            break;
 
82
                        case '\r':
 
83
                            s += '\\r';
 
84
                            break;
 
85
                        case '\t':
 
86
                            s += '\\t';
 
87
                            break;
 
88
                        default:
 
89
                            c = c.charCodeAt();
 
90
                            s += '\\u00' + Math.floor(c / 16).toString(16) +
 
91
                                (c % 16).toString(16);
 
92
                    }
 
93
                }
 
94
            }
 
95
            return s + '"';
 
96
        case 'boolean':
 
97
            return String(arg);
 
98
        default:
 
99
            return 'null';
 
100
        }
 
101
    },
 
102
    parse: function (text) {
 
103
        var at = 0;
 
104
        var ch = ' ';
 
105
 
 
106
        function error(m) {
 
107
            throw {
 
108
                name: 'JSONError',
 
109
                message: m,
 
110
                at: at - 1,
 
111
                text: text
 
112
            };
 
113
        }
 
114
 
 
115
        function next() {
 
116
            ch = text.charAt(at);
 
117
            at += 1;
 
118
            return ch;
 
119
        }
 
120
 
 
121
        function white() {
 
122
            while (ch) {
 
123
                if (ch <= ' ') {
 
124
                    next();
 
125
                } else if (ch == '/') {
 
126
                    switch (next()) {
 
127
                        case '/':
 
128
                            while (next() && ch != '\n' && ch != '\r') {}
 
129
                            break;
 
130
                        case '*':
 
131
                            next();
 
132
                            for (;;) {
 
133
                                if (ch) {
 
134
                                    if (ch == '*') {
 
135
                                        if (next() == '/') {
 
136
                                            next();
 
137
                                            break;
 
138
                                        }
 
139
                                    } else {
 
140
                                        next();
 
141
                                    }
 
142
                                } else {
 
143
                                    error("Unterminated comment");
 
144
                                }
 
145
                            }
 
146
                            break;
 
147
                        default:
 
148
                            error("Syntax error");
 
149
                    }
 
150
                } else {
 
151
                    break;
 
152
                }
 
153
            }
 
154
        }
 
155
 
 
156
        function string() {
 
157
            var i, s = '', t, u;
 
158
 
 
159
            if (ch == '"') {
 
160
outer:          while (next()) {
 
161
                    if (ch == '"') {
 
162
                        next();
 
163
                        return s;
 
164
                    } else if (ch == '\\') {
 
165
                        switch (next()) {
 
166
                        case 'b':
 
167
                            s += '\b';
 
168
                            break;
 
169
                        case 'f':
 
170
                            s += '\f';
 
171
                            break;
 
172
                        case 'n':
 
173
                            s += '\n';
 
174
                            break;
 
175
                        case 'r':
 
176
                            s += '\r';
 
177
                            break;
 
178
                        case 't':
 
179
                            s += '\t';
 
180
                            break;
 
181
                        case 'u':
 
182
                            u = 0;
 
183
                            for (i = 0; i < 4; i += 1) {
 
184
                                t = parseInt(next(), 16);
 
185
                                if (!isFinite(t)) {
 
186
                                    break outer;
 
187
                                }
 
188
                                u = u * 16 + t;
 
189
                            }
 
190
                            s += String.fromCharCode(u);
 
191
                            break;
 
192
                        default:
 
193
                            s += ch;
 
194
                        }
 
195
                    } else {
 
196
                        s += ch;
 
197
                    }
 
198
                }
 
199
            }
 
200
            error("Bad string");
 
201
        }
 
202
 
 
203
        function array() {
 
204
            var a = [];
 
205
 
 
206
            if (ch == '[') {
 
207
                next();
 
208
                white();
 
209
                if (ch == ']') {
 
210
                    next();
 
211
                    return a;
 
212
                }
 
213
                while (ch) {
 
214
                    a.push(value());
 
215
                    white();
 
216
                    if (ch == ']') {
 
217
                        next();
 
218
                        return a;
 
219
                    } else if (ch != ',') {
 
220
                        break;
 
221
                    }
 
222
                    next();
 
223
                    white();
 
224
                }
 
225
            }
 
226
            error("Bad array");
 
227
        }
 
228
 
 
229
        function object() {
 
230
            var k, o = {};
 
231
 
 
232
            if (ch == '{') {
 
233
                next();
 
234
                white();
 
235
                if (ch == '}') {
 
236
                    next();
 
237
                    return o;
 
238
                }
 
239
                while (ch) {
 
240
                    k = string();
 
241
                    white();
 
242
                    if (ch != ':') {
 
243
                        break;
 
244
                    }
 
245
                    next();
 
246
                    o[k] = value();
 
247
                    white();
 
248
                    if (ch == '}') {
 
249
                        next();
 
250
                        return o;
 
251
                    } else if (ch != ',') {
 
252
                        break;
 
253
                    }
 
254
                    next();
 
255
                    white();
 
256
                }
 
257
            }
 
258
            error("Bad object");
 
259
        }
 
260
 
 
261
        function number() {
 
262
            var n = '', v;
 
263
            if (ch == '-') {
 
264
                n = '-';
 
265
                next();
 
266
            }
 
267
            while (ch >= '0' && ch <= '9') {
 
268
                n += ch;
 
269
                next();
 
270
            }
 
271
            if (ch == '.') {
 
272
                n += '.';
 
273
                while (next() && ch >= '0' && ch <= '9') {
 
274
                    n += ch;
 
275
                }
 
276
            }
 
277
            if (ch == 'e' || ch == 'E') {
 
278
                n += 'e';
 
279
                next();
 
280
                if (ch == '-' || ch == '+') {
 
281
                    n += ch;
 
282
                    next();
 
283
                }
 
284
                while (ch >= '0' && ch <= '9') {
 
285
                    n += ch;
 
286
                    next();
 
287
                }
 
288
            }
 
289
            v = +n;
 
290
            if (!isFinite(v)) {
 
291
                ////error("Bad number");
 
292
            } else {
 
293
                return v;
 
294
            }
 
295
        }
 
296
 
 
297
        function word() {
 
298
            switch (ch) {
 
299
                case 't':
 
300
                    if (next() == 'r' && next() == 'u' && next() == 'e') {
 
301
                        next();
 
302
                        return true;
 
303
                    }
 
304
                    break;
 
305
                case 'f':
 
306
                    if (next() == 'a' && next() == 'l' && next() == 's' &&
 
307
                            next() == 'e') {
 
308
                        next();
 
309
                        return false;
 
310
                    }
 
311
                    break;
 
312
                case 'n':
 
313
                    if (next() == 'u' && next() == 'l' && next() == 'l') {
 
314
                        next();
 
315
                        return null;
 
316
                    }
 
317
                    break;
 
318
            }
 
319
            error("Syntax error");
 
320
        }
 
321
 
 
322
        function value() {
 
323
            white();
 
324
            switch (ch) {
 
325
                case '{':
 
326
                    return object();
 
327
                case '[':
 
328
                    return array();
 
329
                case '"':
 
330
                    return string();
 
331
                case '-':
 
332
                    return number();
 
333
                default:
 
334
                    return ch >= '0' && ch <= '9' ? number() : word();
 
335
            }
 
336
        }
 
337
 
 
338
        return value();
 
339
    }
 
340
};