3
Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
The contents of this file are subject to the Mozilla Public License Version
6
1.1 (the "License"); you may not use this file except in compliance with
7
the License. You may obtain a copy of the License at
9
http://www.mozilla.org/MPL/
11
Software distributed under the License is distributed on an "AS IS" basis,
12
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
for the specific language governing rights and limitations under the License.
15
The Original Code is VEGAS Framework.
17
The Initial Developer of the Original Code is
18
ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
19
Portions created by the Initial Developer are Copyright (C) 2004-2011
20
the Initial Developer. All Rights Reserved.
24
Alternatively, the contents of this file may be used under the terms of
25
either the GNU General Public License Version 2 or later (the "GPL"), or
26
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
in which case the provisions of the GPL or the LGPL are applicable instead
28
of those above. If you wish to allow use of your version of this file only
29
under the terms of either the GPL or the LGPL, and not to allow others to
30
use your version of this file under the terms of the MPL, indicate your
31
decision by deleting the provisions above and replace them with the notice
32
and other provisions required by the LGPL or the GPL. If you do not delete
33
the provisions above, a recipient may use your version of this file under
34
the terms of any one of the MPL, the GPL or the LGPL.
38
package vegas.strings.json
40
import system.Serializer;
43
* This class is the concrete class of the JSON singleton.
44
* <code class="prettyprint">JSON</code> (JavaScript object Notation) is a lightweight data-interchange format.
45
* <p>More information in the official site : <a href="http://www.JSON.org/">http://www.JSON.org</a></p>
46
* <p>Add Hexa Digits tool in deserialize method - <a href="http://code.google.com/p/edenrr/">eden inspiration</a></p>
47
* <p><b>Example :</b></p>
48
* <pre class="prettyprint">
49
* import core.getClassName ;
51
* import vegas.strings.JSON;
52
* import vegas.strings.JSONError;
56
* var a:Array = [2, true, "hello"] ;
57
* var o:Object = { prop1 : 1 , prop2 : 2 } ;
58
* var s:String = "hello world" ;
60
* var b:Boolean = true ;
62
* trace("Serialize") ;
64
* trace("- a : " + JSON.serialize( a ) ) ;
65
* trace("- o : " + JSON.serialize( o ) ) ;
66
* trace("- s : " + JSON.serialize( s ) ) ;
67
* trace("- n : " + JSON.serialize( n ) ) ;
68
* trace("- b : " + JSON.serialize( b ) ) ;
70
* trace ("Deserialize") ;
72
* var source:String = '[ { "prop1" : 0xFF0000 , prop2:2, prop3:"hello", prop4:true} , 2, true, 3, [3, 2] ]' ;
74
* o = JSON.deserialize(source) ;
76
* var l:uint = o.length ;
77
* for (var i:uint = 0 ; i < l ; i++)
79
* trace("- " + i + " : " + o[i] + " , typeof :: " + typeof(o[i])) ;
80
* if (typeof(o[i]) == "object")
82
* for (var each:String in o[i])
84
* trace(" + " + each + " : " + o[i][each] + " :: " + getClassName(o[i][each]) ) ;
89
* trace ("JSONError") ;
91
* source = "[3, 2," ; // test1
93
* // var source:String = '{"prop1":coucou"}' ; // test2
97
* var errorObj:Object = JSON.deserialize(source) ;
99
* catch( e:JSONError )
101
* trace( e.toString() ) ;
105
public class JSONSerializer implements Serializer
108
* Creates a new JSONSerializer instance.
110
public function JSONSerializer()
116
* The source to evaluate.
118
public var source:String ;
121
* Indicates the indentor string representation.
123
public function get indentor():String
131
public function set indentor(value:String):void
137
* Indicates the pretty indent value.
139
public function get prettyIndent():int
141
return _prettyIndent;
147
public function set prettyIndent(value:int):void
149
_prettyIndent = value ;
153
* Indicates the pretty printing flag value.
155
public function get prettyPrinting():Boolean
157
return _prettyPrinting ;
163
public function set prettyPrinting(value:Boolean):void
165
_prettyPrinting = value;
169
* Parse a string and interpret the source code to the correct object construct.
170
* <p><b>Example :</b></p>
171
* <pre class="prettyprint">
172
* "hello world" --> "hello world"
174
* "{a:1,"b":2}" --> {a:1,b:2}
176
* @return a string representing the data.
178
public function deserialize( source:String ):*
180
this.source = source ;
187
* Serialize the specified value object passed-in argument.
189
public function serialize( value:* ):String
191
var c:String ; // char
196
var tof:String = typeof(value) ;
205
l = (value as Array).length ;
206
for (i = 0 ; i < l ; ++i)
208
v = serialize(value[i]);
212
return '[' + s + ']';
214
else if ( typeof( value.toString ) != 'undefined')
216
for (var prop:String in value)
219
if ( (typeof(v) != 'undefined') && (typeof(v) != 'function') )
226
s += serialize(prop) + ':' + v ;
229
return "{" + s + "}";
236
return isFinite(value) ? String(value) : 'null' ;
240
l = (value as String).length ;
242
for (i = 0 ; i < l ; i += 1)
244
c = (value as String).charAt(i) ;
247
if (c == '\\' || c == '"')
284
var code:Number = c.charCodeAt() ;
285
s += '\\u00' + String(Math.floor(code / 16).toString(16)) + ((code % 16).toString(16)) ;
294
return String(value);
304
* The current position of the iterator in the source.
306
protected var at:Number = 0 ;
309
* The current character of the iterator in the source.
311
protected var ch:String = ' ' ;
314
* Check the Array objects in the source expression.
316
protected function array():Array
345
error( JSONStrings.badArray );
350
* Throws a JSONError with the passed-in message.
352
protected function error( m:String ):void
354
throw new JSONError( m, at - 1 , source) ;
358
* Indicates if the passed-in character is a digit.
360
protected function isDigit( c:String ):Boolean
362
return( ("0" <= c) && (c <= "9") );
366
* Indicates if the passed-in character is a hexadecimal digit.
368
protected function isHexDigit( c:String ):Boolean
370
return( isDigit( c ) || (("A" <= c) && (c <= "F")) || (("a" <= c) && (c <= "f")) );
374
* Indicates if the current character is a key.
376
protected function key():*
379
var semiColon:int = source.indexOf( ':' , at ) ;
380
var quoteIndex:int = source.indexOf( '"' , at ) ;
381
var squoteIndex:int = source.indexOf( "'" , at ) ;
382
if( (quoteIndex <= semiColon && quoteIndex > -1) || (squoteIndex <= semiColon && squoteIndex > -1))
392
error(JSONStrings.badKey);
395
while ( next() ) // Use key handling
410
error( JSONStrings.badKey ) ;
414
* Returns the next character in the source String representation.
415
* @return the next character in the source String representation.
417
protected function next():String
419
ch = source.charAt(at);
425
* Check the Number values in the source expression.
427
protected function number():*
432
var hex:String = '' ;
433
var sign:String = '' ;
443
if( ( ch == "x") || ( ch == "X") )
446
while( isHexDigit( ch ) )
453
error(JSONStrings.malFormedHexadecimal) ;
457
return Number( sign + "0x" + hex ) ;
465
while ( isDigit(ch) )
473
while (next() && ch >= '0' && ch <= '9')
481
error( JSONStrings.badNumber );
491
* Check the Object values in the source expression.
493
protected function object():*
530
error( JSONStrings.badObject ) ;
534
* Check the string objects in the source expression.
536
protected function string():*
543
if (ch == '"' || ch == "'" )
545
var outerChar:String = ch ;
585
for (i = 0; i < 4; i += 1)
587
t = parseInt( next() , 16 ) ;
600
s += String.fromCharCode(u);
615
error( JSONStrings.badString );
620
* Evaluates the values in the source expression.
622
protected function value():*
629
else if ( ch == '[' )
633
else if ( ch == '"' || ch == "'" )
637
else if ( ch == '-' )
643
return ( ch >= '0' && ch <= '9' ) ? number() : word() ;
648
* Check all white spaces.
650
protected function white():void
664
while ( next() && ch != '\n' && ch != '\r')
691
error( JSONStrings.unterminatedComment );
698
error( JSONStrings.syntaxError );
710
* Check all special words in the source to evaluate.
712
protected function word():*
716
if (next() == 'r' && next() == 'u' && next() == 'e')
722
else if ( ch == 'f' )
724
if (next() == 'a' && next() == 'l' && next() == 's' && next() == 'e')
730
else if ( ch == 'n' )
732
if (next() == 'u' && next() == 'l' && next() == 'l')
738
error( JSONStrings.syntaxError );
745
private var _prettyIndent:int = 0 ;
750
private var _prettyPrinting:Boolean ;
755
private var _indentor:String = " " ;