~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to lib/jquery.json-2.3/jquery.json-2.3.js

  • Committer: V. Keith Hughitt
  • Date: 2009-04-01 21:08:05 UTC
  • Revision ID: hughitt1@kore-20090401210805-372f7dgih07vxk42
nightly build 04-01-2009

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * jQuery JSON Plugin
3
 
 * version: 2.3 (2011-09-17)
4
 
 *
5
 
 * This document is licensed as free software under the terms of the
6
 
 * MIT License: http://www.opensource.org/licenses/mit-license.php
7
 
 *
8
 
 * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
9
 
 * website's http://www.json.org/json2.js, which proclaims:
10
 
 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
11
 
 * I uphold.
12
 
 *
13
 
 * It is also influenced heavily by MochiKit's serializeJSON, which is
14
 
 * copyrighted 2005 by Bob Ippolito.
15
 
 */
16
 
 
17
 
(function( $ ) {
18
 
 
19
 
        var     escapeable = /["\\\x00-\x1f\x7f-\x9f]/g,
20
 
                meta = {
21
 
                        '\b': '\\b',
22
 
                        '\t': '\\t',
23
 
                        '\n': '\\n',
24
 
                        '\f': '\\f',
25
 
                        '\r': '\\r',
26
 
                        '"' : '\\"',
27
 
                        '\\': '\\\\'
28
 
                };
29
 
 
30
 
        /**
31
 
         * jQuery.toJSON
32
 
         * Converts the given argument into a JSON respresentation.
33
 
         *
34
 
         * @param o {Mixed} The json-serializble *thing* to be converted
35
 
         *
36
 
         * If an object has a toJSON prototype, that will be used to get the representation.
37
 
         * Non-integer/string keys are skipped in the object, as are keys that point to a
38
 
         * function.
39
 
         *
40
 
         */
41
 
        $.toJSON = typeof JSON === 'object' && JSON.stringify
42
 
                ? JSON.stringify
43
 
                : function( o ) {
44
 
 
45
 
                if ( o === null ) {
46
 
                        return 'null';
47
 
                }
48
 
 
49
 
                var type = typeof o;
50
 
 
51
 
                if ( type === 'undefined' ) {
52
 
                        return undefined;
53
 
                }
54
 
                if ( type === 'number' || type === 'boolean' ) {
55
 
                        return '' + o;
56
 
                }
57
 
                if ( type === 'string') {
58
 
                        return $.quoteString( o );
59
 
                }
60
 
                if ( type === 'object' ) {
61
 
                        if ( typeof o.toJSON === 'function' ) {
62
 
                                return $.toJSON( o.toJSON() );
63
 
                        }
64
 
                        if ( o.constructor === Date ) {
65
 
                                var     month = o.getUTCMonth() + 1,
66
 
                                        day = o.getUTCDate(),
67
 
                                        year = o.getUTCFullYear(),
68
 
                                        hours = o.getUTCHours(),
69
 
                                        minutes = o.getUTCMinutes(),
70
 
                                        seconds = o.getUTCSeconds(),
71
 
                                        milli = o.getUTCMilliseconds();
72
 
 
73
 
                                if ( month < 10 ) {
74
 
                                        month = '0' + month;
75
 
                                }
76
 
                                if ( day < 10 ) {
77
 
                                        day = '0' + day;
78
 
                                }
79
 
                                if ( hours < 10 ) {
80
 
                                        hours = '0' + hours;
81
 
                                }
82
 
                                if ( minutes < 10 ) {
83
 
                                        minutes = '0' + minutes;
84
 
                                }
85
 
                                if ( seconds < 10 ) {
86
 
                                        seconds = '0' + seconds;
87
 
                                }
88
 
                                if ( milli < 100 ) {
89
 
                                        milli = '0' + milli;
90
 
                                }
91
 
                                if ( milli < 10 ) {
92
 
                                        milli = '0' + milli;
93
 
                                }
94
 
                                return '"' + year + '-' + month + '-' + day + 'T' +
95
 
                                        hours + ':' + minutes + ':' + seconds +
96
 
                                        '.' + milli + 'Z"';
97
 
                        }
98
 
                        if ( o.constructor === Array ) {
99
 
                                var ret = [];
100
 
                                for ( var i = 0; i < o.length; i++ ) {
101
 
                                        ret.push( $.toJSON( o[i] ) || 'null' );
102
 
                                }
103
 
                                return '[' + ret.join(',') + ']';
104
 
                        }
105
 
                        var     name,
106
 
                                val,
107
 
                                pairs = [];
108
 
                        for ( var k in o ) {
109
 
                                type = typeof k;
110
 
                                if ( type === 'number' ) {
111
 
                                        name = '"' + k + '"';
112
 
                                } else if (type === 'string') {
113
 
                                        name = $.quoteString(k);
114
 
                                } else {
115
 
                                        // Keys must be numerical or string. Skip others
116
 
                                        continue;
117
 
                                }
118
 
                                type = typeof o[k];
119
 
 
120
 
                                if ( type === 'function' || type === 'undefined' ) {
121
 
                                        // Invalid values like these return undefined
122
 
                                        // from toJSON, however those object members
123
 
                                        // shouldn't be included in the JSON string at all.
124
 
                                        continue;
125
 
                                }
126
 
                                val = $.toJSON( o[k] );
127
 
                                pairs.push( name + ':' + val );
128
 
                        }
129
 
                        return '{' + pairs.join( ',' ) + '}';
130
 
                }
131
 
        };
132
 
 
133
 
        /**
134
 
         * jQuery.evalJSON
135
 
         * Evaluates a given piece of json source.
136
 
         *
137
 
         * @param src {String}
138
 
         */
139
 
        $.evalJSON = typeof JSON === 'object' && JSON.parse
140
 
                ? JSON.parse
141
 
                : function( src ) {
142
 
                return eval('(' + src + ')');
143
 
        };
144
 
 
145
 
        /**
146
 
         * jQuery.secureEvalJSON
147
 
         * Evals JSON in a way that is *more* secure.
148
 
         *
149
 
         * @param src {String}
150
 
         */
151
 
        $.secureEvalJSON = typeof JSON === 'object' && JSON.parse
152
 
                ? JSON.parse
153
 
                : function( src ) {
154
 
 
155
 
                var filtered = 
156
 
                        src
157
 
                        .replace( /\\["\\\/bfnrtu]/g, '@' )
158
 
                        .replace( /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
159
 
                        .replace( /(?:^|:|,)(?:\s*\[)+/g, '');
160
 
 
161
 
                if ( /^[\],:{}\s]*$/.test( filtered ) ) {
162
 
                        return eval( '(' + src + ')' );
163
 
                } else {
164
 
                        throw new SyntaxError( 'Error parsing JSON, source is not valid.' );
165
 
                }
166
 
        };
167
 
 
168
 
        /**
169
 
         * jQuery.quoteString
170
 
         * Returns a string-repr of a string, escaping quotes intelligently.
171
 
         * Mostly a support function for toJSON.
172
 
         * Examples:
173
 
         * >>> jQuery.quoteString('apple')
174
 
         * "apple"
175
 
         *
176
 
         * >>> jQuery.quoteString('"Where are we going?", she asked.')
177
 
         * "\"Where are we going?\", she asked."
178
 
         */
179
 
        $.quoteString = function( string ) {
180
 
                if ( string.match( escapeable ) ) {
181
 
                        return '"' + string.replace( escapeable, function( a ) {
182
 
                                var c = meta[a];
183
 
                                if ( typeof c === 'string' ) {
184
 
                                        return c;
185
 
                                }
186
 
                                c = a.charCodeAt();
187
 
                                return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
188
 
                        }) + '"';
189
 
                }
190
 
                return '"' + string + '"';
191
 
        };
192
 
 
193
 
})( jQuery );