| 1 | /* |
|---|
| 2 | JavasSript Object Notation (JSON) - Copyright 2005 Six Apart |
|---|
| 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 | extend( Object, { |
|---|
| 40 | toJSON: function( o ) { |
|---|
| 41 | if( !defined( o ) || o == null ) |
|---|
| 42 | return "null"; |
|---|
| 43 | if( typeof o.toJSON == "function" && o.toJSON !== arguments.callee ) |
|---|
| 44 | return o.toJSON(); |
|---|
| 45 | var out = [ "{" ]; |
|---|
| 46 | for( var p in o ) { |
|---|
| 47 | if( o.hasOwnProperty && !o.hasOwnProperty( p ) ) |
|---|
| 48 | continue; |
|---|
| 49 | if( o[ p ] === undefined ) /* http://search.cpan.org/~makamaka/JSON-1.09/lib/JSON.pm#Mapping */ |
|---|
| 50 | continue; |
|---|
| 51 | if( out.length > 1 ) |
|---|
| 52 | out.push( "," ); |
|---|
| 53 | out.push( Object.toJSON( p ), ":", Object.toJSON( o[ p ] ) ); |
|---|
| 54 | } |
|---|
| 55 | out.push( "}" ); |
|---|
| 56 | return out.join( "" ); |
|---|
| 57 | }, |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | fromJSON: function( s ) { |
|---|
| 61 | try { |
|---|
| 62 | return eval( "(" + s.replace( /=/g, "\\u3D" ).replace( /\(/g, "\\u28" ) + ")" ); |
|---|
| 63 | } catch( e ) {} |
|---|
| 64 | return undefined; |
|---|
| 65 | } |
|---|
| 66 | } ); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | Array.prototype.toJSON = function() { |
|---|
| 70 | var out = [ "[" ]; |
|---|
| 71 | for( var i = 0; i < this.length; i++ ) { |
|---|
| 72 | if( out.length > 1 ) |
|---|
| 73 | out.push( "," ); |
|---|
| 74 | out.push( Object.toJSON( this[ i ] ) ); |
|---|
| 75 | } |
|---|
| 76 | out.push( "]" ); |
|---|
| 77 | return out.join( "" ); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | Boolean.prototype.toJSON = function() { |
|---|
| 82 | return this.toString(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | Number.prototype.toJSON = function() { |
|---|
| 87 | return isFinite( this ) ? this.toString() : "0"; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | Date.prototype.toJSON = function() { |
|---|
| 92 | return this.toUTCISOString |
|---|
| 93 | ? this.toUTCISOString().toJSON() |
|---|
| 94 | : this.toString().toJSON(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | String.prototype.toJSON = function() { |
|---|
| 99 | return '"' + this.escapeJS() + '"'; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | RegExp.prototype.toJSON = function() { |
|---|
| 104 | return this.toString().toJSON(); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | Function.prototype.toJSON = function() { |
|---|
| 109 | return this.toString().toJSON(); |
|---|
| 110 | } |
|---|