Sitelet https://github.com/s9tpepper/JSONTools/commit/3064be9d654090b87231ef712a1c27cd4d842431
Skip to content

Commit 3064be9

Browse files
committed
Added JSON object to AS3 class mapping. Add "_explicitType":"GroupVO" to a JSON object and JSONTools will parse the JSON into a strong typed object if it finds a class alias registered for the value of _explicitType. This feature works identical to AMFPHP and other AMF libraries.
1 parent 6c02294 commit 3064be9

5 files changed

Lines changed: 74 additions & 15 deletions

File tree

‎bin/JsonTest.swf‎

799 Bytes
Binary file not shown.

‎bin/tempViewerHTML.html‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<html><head><title>file:////Users/omargonzalez/Documents/work/og/fdt/e4j/bin/JsonTest.swf</title><style><!--body {margin: 0;padding: 0;overflow: hidden;}#container {width: 100%;height: 100%;} //-->
2+
</style></head><body marginwidth="0" marginheight="0" topmargin="0" leftmargin="0"><object id="container" classid="CLSID:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0"><param name="movie" value="file:////Users/omargonzalez/Documents/work/og/fdt/e4j/bin/JsonTest.swf"/><param name="allowFullScreen" value="true"/><embed src="file:////Users/omargonzalez/Documents/work/og/fdt/e4j/bin/JsonTest.swf" swLiveConnect="false"allowFullScreen="true"type="application/x-shockwave-flash"pluginspage="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object></body></html>

‎src/JsonTest.as‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package
22
{
3+
import com.project.example.vo.GroupVO;
34
import com.rational.serialization.json.JSON;
4-
import com.brokenfunction.json.decodeJson;
55
import com.adobe.serialization.json.JSON;
66
import flash.net.URLRequest;
77
import flash.events.Event;
88
import flash.net.URLLoader;
99
import flash.utils.getTimer;
10-
import mx.utils.ObjectUtil;
1110
import ab.fl.utils.json.JSON;
1211
import flash.display.Sprite;
1312

@@ -24,56 +23,62 @@ package
2423

2524
private function _init():void
2625
{
26+
// Register mapped classes
27+
ab.fl.utils.json.JSON.registerClass("GroupVO", GroupVO);
28+
2729
var url:URLLoader = new URLLoader();
2830
url.addEventListener(Event.COMPLETE, _handleJSONLoaded);
2931
url.load(new URLRequest("http://plugrman.com/example/json/testJson.txt"));
3032
}
3133

3234
private function _handleJSONLoaded(event:Event):void
3335
{
34-
// JSON
36+
// JSONTools
37+
trace("\n*******************************\nStarting to parse using JSONTools");
3538
var startTime:Number = getTimer();
3639
_json = new ab.fl.utils.json.JSON(URLLoader(event.target).data);
37-
var search:Array = _json.DATA["(ID == 355)"]; // Searching for objects in DATA with ID of 355
38-
trace("search[0].NAME = " + search[0].NAME); // Output: search[0].NAME = AdobeTURK - Ankara - TURKEY
40+
var search:Array = _json.DATA["(ID == 831)"]; // Searching for objects in DATA with ID of 355
3941
var endTime:Number = getTimer();
4042
trace("Total time elapsed: " + (endTime - startTime).toString() + "ms");
43+
trace("search[0] = " + search[0]); // Output: search[0].NAME = AdobeTURK - Ankara - TURKEY
44+
trace("search[0].NAME = " + search[0].NAME); // Output: search[0].NAME = AdobeTURK - Ankara - TURKEY
4145

4246

4347
// AS3CoreLib
44-
trace("Starting to parse using as3corelib");
48+
trace("\n*******************************\nStarting to parse using as3corelib");
4549
startTime = getTimer();
4650
var searchResults:Array = new Array();
47-
var jsonObj:Object = com.adobe.serialization.json.JSON.decode(URLLoader(event.target).data);
51+
jsonObj = com.adobe.serialization.json.JSON.decode(URLLoader(event.target).data);
4852
var jsonItem:Object;
4953
for each (jsonItem in jsonObj.DATA)
5054
{
51-
if (jsonItem.ID == 355)
55+
if (jsonItem.ID == 831)
5256
{
5357
searchResults.push(jsonItem);
5458
}
5559
}
56-
trace("searchResults[0].NAME = " + searchResults[0].NAME);
5760
endTime = getTimer();
5861
trace("Total time elapsed: " + (endTime - startTime).toString() + "ms");
62+
trace("searchResults[0] = " + searchResults[0]); // Output:
63+
trace("searchResults[0].NAME = " + searchResults[0].NAME);
5964

6065

6166
// actionjson
62-
trace("Starting to parse using ason");
67+
trace("\n*******************************\nStarting to parse using ason");
6368
startTime = getTimer();
64-
var searchResults:Array = new Array();
69+
searchResults = new Array();
6570
var jsonObj:Object = com.rational.serialization.json.JSON.decode(URLLoader(event.target).data);
66-
var jsonItem:Object;
6771
for each (jsonItem in jsonObj.DATA)
6872
{
69-
if (jsonItem.ID == 355)
73+
if (jsonItem.ID == 831)
7074
{
7175
searchResults.push(jsonItem);
7276
}
7377
}
74-
trace("searchResults[0].NAME = " + searchResults[0].NAME);
7578
endTime = getTimer();
7679
trace("Total time elapsed: " + (endTime - startTime).toString() + "ms");
80+
trace("searchResults[0] = " + searchResults[0]); // Output:
81+
trace("searchResults[0].NAME = " + searchResults[0].NAME);
7782
}
7883
}
7984
}

‎src/ab/fl/utils/json/JSON.as‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ab.fl.utils.json
22
{
3+
import flash.net.registerClassAlias;
4+
import flash.net.getClassByAlias;
35
import flash.utils.getQualifiedClassName;
46
import flash.utils.Dictionary;
57
import json.JParser;
@@ -128,7 +130,24 @@ package ab.fl.utils.json
128130

129131
if (className == "Object")
130132
{
131-
newJSON.objectReference = new Object();
133+
// Look for Class mapping
134+
if (Object(value).hasOwnProperty("_explicitType"))
135+
{
136+
var alias:String = Object(value)._explicitType;
137+
try
138+
{
139+
var mappedClass:Class = getClassByAlias(alias);
140+
newJSON.objectReference = new mappedClass();
141+
}
142+
catch (e:Error)
143+
{
144+
newJSON.objectReference = new Object();
145+
}
146+
}
147+
else
148+
{
149+
newJSON.objectReference = new Object();
150+
}
132151
}
133152
else
134153
{
@@ -141,6 +160,11 @@ package ab.fl.utils.json
141160
}
142161
}
143162

163+
static public function registerClass(alias:String, aliasedClass:Class):void
164+
{
165+
registerClassAlias(alias, aliasedClass);
166+
}
167+
144168
flash_proxy override function callProperty(name:*, ...rest):*
145169
{
146170
trace("name = " + name);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.project.example.vo
2+
{
3+
[RemoteClass(alias="GroupVO")]
4+
/**
5+
* Class represents a group object from the JSON.
6+
* Properties are uppercase to match the JSON output.
7+
*
8+
* @author Omar Gonzalez
9+
*/
10+
public class GroupVO
11+
{
12+
public var TAGLINE:String;
13+
public var AVATAR:String;
14+
public var NAME:String;
15+
public var MEMBERCOUNT:Number;
16+
public var ADDRESS:String;
17+
public var ID:Number;
18+
public var LONGITUDE:Number;
19+
public var URL:String;
20+
public var TYPE:Number;
21+
public var LATITUDE:Number;
22+
public var _explicitType:String;
23+
24+
public function GroupVO()
25+
{
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)