HaxeDoc2

(やまだぃちぅ) #1

1 typedef MyData ={
2 var name:String;
3 var tags:Array;
4 }
5
6 class Main {
7 static function main() {
8 var s = ’{
9 "name": "Haxe",
10 "tags": ["awesome"]
11 }’;
12 var o:MyData = haxe.Json.parse(s);
13 trace(o.name); // Haxe (a string)
14 // awesome (a string in an array)
15 trace(o.tags[0]);
16 }
17 }


10.8.2 Encoding JSON


Use thehaxe.Json.stringifystatic method to encode a Haxe value into aJSONstring:
1 class Main {
2 static function main() {
3 var o ={rating: 5};
4 var s = haxe.Json.stringify(o);
5 trace(s); // {"rating":5}
6 }
7 }

10.8.3 Implementation details


Thehaxe.JsonAPI automatically uses native implementation on targets where it is available,
i.e.JavaScript,FlashandPHPand provides its own implementation for other targets.
Usage of Haxe own implementation can be forced with-D haxeJSONcompiler argument.
This will also provide serialization of enums (2.4) by their index, maps (10.2.5) with string keys
and class instances.
Older browsers (Internet Explorer 7, for instance) may not have nativeJSONimplementation.
In case it’s required to support them, we can include one of the JSON implementations available
on the internet in the HTML page. Alternatively, a-D old_browsercompiler argument that
will makehaxe.Jsontry to use native JSON and fallback to its own implementation in case it’s
not available can be used.

10.9 Xml


10.10Input/Output


10.11Sys/sys............................................

Free download pdf