root/trunk/json.js

Revision 93, 3.6 kB (checked in by ydnar, 3 years ago)

fixed bsd headers

Line 
1/*
2JavaSript Object Notation (JSON)
3$Id$
4
5Copyright (c) 2005-2006, Six Apart, Ltd.
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are
10met:
11
12    * Redistributions of source code must retain the above copyright
13notice, this list of conditions and the following disclaimer.
14
15    * Redistributions in binary form must reproduce the above
16copyright notice, this list of conditions and the following disclaimer
17in the documentation and/or other materials provided with the
18distribution.
19
20    * Neither the name of "Six Apart" nor the names of its
21contributors may be used to endorse or promote products derived from
22this software without specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36*/
37
38
39JSON = {
40    parse: function( string ) {
41        string = string.replace( /=/g, "\\u3D" );
42        string = string.replace( /\(/g, "\\u28" );
43        try {
44            return eval( "(" + string + ")" );
45        } catch( e ) {}
46        return undefined;
47    }
48}
49
50
51Boolean.prototype.toJSON = function() {
52    return this.toString();
53}
54
55
56Number.prototype.toJSON = function() {
57    return isFinite( this ) ? this.toString() : "0";
58}
59
60
61Date.prototype.toJSON = function() {
62    return this.toUTCISOString
63        ? this.toUTCISOString().toJSON()
64        : this.toString().toJSON();
65}
66
67
68String.prototype.toJSON = function() {
69    return '"' + this.escapeJS() + '"';
70}
71
72
73RegExp.prototype.toJSON = function() {
74    return this.toString().toJSON();
75}
76
77
78Function.prototype.toJSON = function() {
79    return this.toString().toJSON();
80}
81
82
83Array.prototype.toJSON = function( root ) {
84    // crude recursion detection
85    if( !root )
86        root = this;
87    else if( root == this )
88        return "[]";
89   
90    var out = [ "[" ];
91    for( var i = 0; i < this.length; i++ ) {
92        if( out.length > 1 )
93            out.push( "," );
94        if( typeof this[ i ] == "undefined" || this[ i ] == null )
95            out.push( "null" );
96        else if( !this[ i ].toJSON )
97            out.push( "{}" );
98        else
99            out.push( this[ i ].toJSON( root ) );
100    }
101    out.push( "]" );
102    return out.join( "" );
103}
104
105
106Object.prototype.toJSON = function( root ) {
107    // crude recursion detection
108    if( !root )
109        root = this;
110    else if( root == this )
111        return "{}";
112   
113    var out = [ "{" ];
114    for( var i in this ) {
115        if( typeof this[ i ] == "undefined" ||
116            (this.hasOwnProperty && !this.hasOwnProperty( i )) )
117            continue;
118        if( out.length > 1 )
119            out.push( "," );
120        out.push( i.toJSON() );
121        if( this[ i ] == null )
122            out.push( ":null" );
123        else if( typeof this[ i ] == "function" )
124            continue;
125        else if( !this[ i ].toJSON )
126            out.push( ":{}" );
127        else
128            out.push( ":", this[ i ].toJSON( root ) );
129    }
130    out.push( "}" );
131    return out.join( "" );
132}
Note: See TracBrowser for help on using the browser.