/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/static/javascript/yui/build/json/json-parse-debug.js

  • Committer: Martin Albisetti
  • Date: 2008-10-20 21:18:06 UTC
  • mfrom: (229.1.3 add-yui-3)
  • Revision ID: martin.albisetti@canonical.com-20081020211806-rzs0ya40gz9wcpoz
Added yui library to the tree. Welcome to the future. (Paul Hummer)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.net/yui/license.txt
 
5
version: 3.0.0pr1
 
6
*/
 
7
YUI.add('json-parse', function(Y) {
 
8
 
 
9
/**
 
10
 * The JSON Utility provides methods to serialize JavaScript objects into
 
11
 * JSON strings and parse JavaScript objects from strings containing JSON data.
 
12
 * Three modules are available for inclusion:
 
13
 * <ol>
 
14
 * <li>1. <code>json-parse</code> for parsing JSON strings into native JavaScript data</li>
 
15
 * <li>2. <code>json-stringify</code> for stringification of JavaScript objects into JSON strings</li>
 
16
 * <li>3. <code>json</code> for both parsing and stringification</li>
 
17
 * </ol>
 
18
 * 
 
19
 * Both <code>json-parse</code> and <code>json-stringify</code> create functions in a static JSON class under your YUI instance (e.g. Y.JSON.parse(..)).
 
20
 * @module json
 
21
 * @class JSON
 
22
 * @static
 
23
 */
 
24
 
 
25
/**
 
26
 * Provides Y.JSON.parse method to take JSON strings and return native
 
27
 * JavaScript objects.
 
28
 * @module json
 
29
 * @submodule json-parse
 
30
 * @for JSON
 
31
 * @static
 
32
 */
 
33
Y.JSON = Y.JSON || {};
 
34
 
 
35
// All internals kept private for security reasons
 
36
 
 
37
/**
 
38
 * Replace certain Unicode characters that JavaScript may handle incorrectly
 
39
 * during eval--either by deleting them or treating them as line endings--with
 
40
 * escae sequences.
 
41
 * IMPORTANT NOTE: This regex will be used to modify the input if a match is
 
42
 * found.
 
43
 * @property _UNICODE_EXCEPTIONS
 
44
 * @type {RegExp}
 
45
 * @private
 
46
 */
 
47
var _UNICODE_EXCEPTIONS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
 
48
 
 
49
 
 
50
/**
 
51
 * First step in the validation.  Regex used to replace all escape
 
52
 * sequences (i.e. "\\", etc) with '@' characters (a non-JSON character).
 
53
 * @property _ESCAPES
 
54
 * @type {RegExp}
 
55
 * @private
 
56
 */
 
57
    _ESCAPES = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
 
58
 
 
59
/**
 
60
 * Second step in the validation.  Regex used to replace all simple
 
61
 * values with ']' characters.
 
62
 * @property _VALUES
 
63
 * @type {RegExp}
 
64
 * @private
 
65
 */
 
66
    _VALUES  = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
 
67
 
 
68
/**
 
69
 * Third step in the validation.  Regex used to remove all open square
 
70
 * brackets following a colon, comma, or at the beginning of the string.
 
71
 * @property _BRACKETS
 
72
 * @type {RegExp}
 
73
 * @private
 
74
 */
 
75
 
 
76
    _BRACKETS = /(?:^|:|,)(?:\s*\[)+/g,
 
77
 
 
78
/**
 
79
 * Final step in the validation.  Regex used to test the string left after
 
80
 * all previous replacements for invalid characters.
 
81
 * @property _INVALID
 
82
 * @type {RegExp}
 
83
 * @private
 
84
 */
 
85
    _INVALID  = /^[\],:{}\s]*$/,
 
86
 
 
87
    has = Object.prototype.hasOwnProperty,
 
88
/**
 
89
 * Traverses nested objects, applying a reviver function to each (key,value)
 
90
 * from the scope if the key:value's containing object.  The value returned
 
91
 * from the function will replace the original value in the key:value pair.
 
92
 * If the value returned is undefined, the key will be omitted from the
 
93
 * returned object.
 
94
 * @method _revive
 
95
 * @param data {MIXED} Any JavaScript data
 
96
 * @param reviver {Function} filter or mutation function
 
97
 * @return {MIXED} The results of the filtered data
 
98
 * @private
 
99
 */
 
100
    _revive = function (data, reviver) {
 
101
        var walk = function (o,key) {
 
102
            var k,v,value = o[key];
 
103
            if (value && typeof value === 'object') {
 
104
                for (k in value) {
 
105
                    if (has.call(value,k)) {
 
106
                        v = walk(value, k);
 
107
                        if (v === undefined) {
 
108
                            delete value[k];
 
109
                        } else {
 
110
                            value[k] = v;
 
111
                        }
 
112
                    }
 
113
                }
 
114
            }
 
115
            return reviver.call(o,key,value);
 
116
        };
 
117
 
 
118
        return typeof reviver === 'function' ? walk({'':data},'') : data;
 
119
    };
 
120
 
 
121
/**
 
122
 * Parse a JSON string, returning the native JavaScript representation.
 
123
 * @param s {string} JSON string data
 
124
 * @param reviver {function} (optional) function(k,v) passed each key value pair of object literals, allowing pruning or altering values
 
125
 * @return {MIXED} the native JavaScript representation of the JSON string
 
126
 * @throws SyntaxError
 
127
 * @method parse
 
128
 * @static
 
129
 * @public
 
130
 */
 
131
Y.JSON.parse = function (s,reviver) {
 
132
    // Ensure valid JSON
 
133
    if (typeof s === 'string') {
 
134
        // Replace certain Unicode characters that are otherwise handled
 
135
        // incorrectly by some browser implementations.
 
136
        // NOTE: This modifies the input if such characters are found!
 
137
        s = s.replace(_UNICODE_EXCEPTIONS, function (c) {
 
138
            return '\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4);
 
139
        });
 
140
        
 
141
        // Test for validity
 
142
        if (_INVALID.test(s.replace(_ESCAPES,'@').
 
143
                            replace(_VALUES,']').
 
144
                            replace(_BRACKETS,''))) {
 
145
 
 
146
            // Eval the text into a JavaScript data structure, apply any
 
147
            // reviver function, and return
 
148
            return _revive( eval('(' + s + ')'), reviver );
 
149
        }
 
150
    }
 
151
 
 
152
    // The text is not JSON parsable
 
153
    throw new SyntaxError('parseJSON');
 
154
};
 
155
 
 
156
 
 
157
}, '3.0.0pr1' );