| 1 | /* |
|---|
| 2 | JavaSript Object Notation (JSON) |
|---|
| 3 | $Id$ |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 2005-2006, Six Apart, Ltd. |
|---|
| 6 | All rights reserved. |
|---|
| 7 | |
|---|
| 8 | Redistribution and use in source and binary forms, with or without |
|---|
| 9 | modification, are permitted provided that the following conditions are |
|---|
| 10 | met: |
|---|
| 11 | |
|---|
| 12 | * Redistributions of source code must retain the above copyright |
|---|
| 13 | notice, this list of conditions and the following disclaimer. |
|---|
| 14 | |
|---|
| 15 | * Redistributions in binary form must reproduce the above |
|---|
| 16 | copyright notice, this list of conditions and the following disclaimer |
|---|
| 17 | in the documentation and/or other materials provided with the |
|---|
| 18 | distribution. |
|---|
| 19 | |
|---|
| 20 | * Neither the name of "Six Apart" nor the names of its |
|---|
| 21 | contributors may be used to endorse or promote products derived from |
|---|
| 22 | this software without specific prior written permission. |
|---|
| 23 | |
|---|
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 28 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 29 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 30 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 31 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 32 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 34 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 35 | |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | JSON = { |
|---|
| 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 | |
|---|
| 51 | Boolean.prototype.toJSON = function() { |
|---|
| 52 | return this.toString(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | Number.prototype.toJSON = function() { |
|---|
| 57 | return isFinite( this ) ? this.toString() : "0"; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | Date.prototype.toJSON = function() { |
|---|
| 62 | return this.toUTCISOString |
|---|
| 63 | ? this.toUTCISOString().toJSON() |
|---|
| 64 | : this.toString().toJSON(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | String.prototype.toJSON = function() { |
|---|
| 69 | return '"' + this.escapeJS() + '"'; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | RegExp.prototype.toJSON = function() { |
|---|
| 74 | return this.toString().toJSON(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | Function.prototype.toJSON = function() { |
|---|
| 79 | return this.toString().toJSON(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | Array.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 | |
|---|
| 106 | Object.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 | } |
|---|