Skip to content

Commit da22bfe

Browse files
committed
- Added errors in JSONError for attempting getting a property that does not exist in the JSON object, or setting an object on a mapped AS3 class that does not exist during strong typed JSON decoding.
- Added a fix to the startup so the root object is an Array when it should be an array instead of the default Dictionary. - Added a registry of keys that exist in the JSON object. - Added a hasKey() method to check if a JSON object has a key before attempting to retrieve it. Signed-off-by: Omar Gonzalez <omar@almerblank.com>
1 parent ac934d3 commit da22bfe

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

bin/json_flash.swc

336 Bytes
Binary file not shown.

bin/json_flex.swc

337 Bytes
Binary file not shown.

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package ab.fl.utils.json
22
{
3-
import flash.utils.describeType;
4-
import flash.net.registerClassAlias;
53
import flash.net.getClassByAlias;
6-
import flash.utils.getQualifiedClassName;
4+
import flash.net.registerClassAlias;
75
import flash.utils.Dictionary;
8-
import json.JParser;
9-
import flash.utils.flash_proxy;
106
import flash.utils.Proxy;
7+
import flash.utils.describeType;
8+
import flash.utils.flash_proxy;
9+
import flash.utils.getQualifiedClassName;
10+
11+
import json.JParser;
1112

1213
/**
1314
* @author Omar Gonzalez :: omar@almerblank.com
@@ -146,6 +147,9 @@ package ab.fl.utils.json
146147
{
147148
_isRoot = true;
148149

150+
if (String(json).charAt(0) == "[")
151+
_objectReference = new Array();
152+
149153
_plainJson = JParser.decode(json);
150154

151155
_buildJSON();
@@ -207,6 +211,9 @@ package ab.fl.utils.json
207211
_setProperties(newJSON.objectReference, value, root);
208212
break;
209213
}
214+
215+
if (_keys.lastIndexOf(key) == -1)
216+
_keys.push(key);
210217
}
211218
}
212219

@@ -473,7 +480,15 @@ package ab.fl.utils.json
473480
}
474481
catch (e:Error)
475482
{
476-
target[key] = new Object();
483+
try
484+
{
485+
target[key] = new Object();
486+
}
487+
catch (e2:Error)
488+
{
489+
if (throwJSONErrors)
490+
throw new JSONError(JSONError.MAPPED_AS3_CLASS_IS_MISSING_PROPERTY + e2.getStackTrace());
491+
}
477492
}
478493
}
479494
else
@@ -638,6 +653,8 @@ package ab.fl.utils.json
638653
return null;
639654
}
640655

656+
private var _keys:Vector.<String> = new Vector.<String>();
657+
641658
/**
642659
* Method called when a property value is set on this
643660
* JSON object instance.
@@ -648,6 +665,11 @@ package ab.fl.utils.json
648665
{
649666
_objectReference[name] = value;
650667

668+
if (_keys.lastIndexOf(name) == -1)
669+
{
670+
_keys.push(name);
671+
}
672+
651673
if (!_isTree)
652674
{
653675
_nonTreeKey = name;
@@ -659,6 +681,12 @@ package ab.fl.utils.json
659681
throw new JSONError(JSONError.ERROR_COMMITING_PROPERTY.replace("{name}", name), 60000);
660682
}
661683
}
684+
685+
public function hasKey(keyName:String):Boolean
686+
{
687+
return (_keys.lastIndexOf(keyName) > -1);
688+
}
689+
662690
/**
663691
* Returns the object holding value references for this JSON
664692
* object proxy. This object is not meant to be used by classes

src/ab/fl/utils/json/JSONError.as

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ package ab.fl.utils.json
1616
*/
1717
static public const ERROR_COMMITING_PROPERTY:String = "JSON Error Code:#60000: Error comitting property '{name}', property does not exist.";
1818
/**
19-
* Error code: 60000. This error is thrown when an attempt to get
20-
* a property has been made on a property that does not exist.
19+
* Error code: 60001. This error is thrown when an attempt to get
20+
* a property has been made on a property that does not exist on the
21+
* JSON object that was decoded.
2122
*/
2223
static public const ERROR_GETTING_PROPERTY_DOESNT_EXIST:String = "JSON Error Code:#60001: Error getting property, JSON property '{name}' does not exist.";
24+
/**
25+
* Error code: 60002. This error is thrown when an attempt to set
26+
* a property has been made on a property that does not exist on the
27+
* AS3 object that is being mapped.
28+
*/
29+
static public const MAPPED_AS3_CLASS_IS_MISSING_PROPERTY:String = "JSON Error Code:#60002: Error setting property on mapped AS3 class, details: ";
2330

2431

2532
/**

0 commit comments

Comments
 (0)