diff --git a/.gitignore b/.gitignore index 0806852..b7e7801 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,35 @@ -.project +#OS X .DS_Store -.settings +Icon? + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + +#Flex/Flash + +# Build Dir +bin/ +bin-release/ +bin-debug/ +out/ + +# Project property files +.actionScriptProperties +.flexProperties +.settings/ +.project + +#IntelliJ IDEA +*.iml +*.ipr +*.iws +.idea/ +#TextMate +*.tmproj +*.tmproject +tmtags \ No newline at end of file diff --git a/bin/json_flash.swc b/bin/json_flash.swc index 8dbbda8..5fa6d28 100644 Binary files a/bin/json_flash.swc and b/bin/json_flash.swc differ diff --git a/bin/json_flex.swc b/bin/json_flex.swc index d1505d1..9ba2448 100644 Binary files a/bin/json_flex.swc and b/bin/json_flex.swc differ diff --git a/builds/json_flash_v.0.1.swc b/builds/json_flash_v.0.1.swc new file mode 100644 index 0000000..8dbbda8 Binary files /dev/null and b/builds/json_flash_v.0.1.swc differ diff --git a/builds/json_flash_v.0.2.swc b/builds/json_flash_v.0.2.swc new file mode 100644 index 0000000..5fa6d28 Binary files /dev/null and b/builds/json_flash_v.0.2.swc differ diff --git a/builds/json_flash_v.0.3.swc b/builds/json_flash_v.0.3.swc new file mode 100644 index 0000000..06db335 Binary files /dev/null and b/builds/json_flash_v.0.3.swc differ diff --git a/builds/json_flex_v.0.1.swc b/builds/json_flex_v.0.1.swc new file mode 100644 index 0000000..d1505d1 Binary files /dev/null and b/builds/json_flex_v.0.1.swc differ diff --git a/builds/json_flex_v.0.2.swc b/builds/json_flex_v.0.2.swc new file mode 100644 index 0000000..9ba2448 Binary files /dev/null and b/builds/json_flex_v.0.2.swc differ diff --git a/builds/json_flex_v.0.3.swc b/builds/json_flex_v.0.3.swc new file mode 100644 index 0000000..b38e9bc Binary files /dev/null and b/builds/json_flex_v.0.3.swc differ diff --git a/src/ab/fl/utils/json/JSON.as b/src/ab/fl/utils/json/JSON.as index f332001..2267ebd 100644 --- a/src/ab/fl/utils/json/JSON.as +++ b/src/ab/fl/utils/json/JSON.as @@ -118,6 +118,9 @@ package ab.fl.utils.json */ static public var throwJSONErrors:Boolean = true; + static public var encoder:Function = JParser.encode; + static public var decoder:Function = JParser.decode; + /** * @Constructor * @@ -150,7 +153,7 @@ package ab.fl.utils.json if (String(json).charAt(0) == "[") _objectReference = new Array(); - _plainJson = JParser.decode(json); + _plainJson = decoder(json); _buildJSON(); } @@ -212,8 +215,8 @@ package ab.fl.utils.json break; } - if (_keys.lastIndexOf(key) == -1) - _keys.push(key); +// if (target.keys && target.keys.lastIndexOf(key) == -1) +// target.keys.push(key); } } @@ -299,7 +302,7 @@ package ab.fl.utils.json */ static public function decodeToTyped(json:String):* { - var plainJSON:Object = JParser.decode(json); + var plainJSON:Object = decoder(json); var rootType:String = (plainJSON._explicitType) ? plainJSON._explicitType : "Object"; var rootClass:Class; @@ -333,7 +336,7 @@ package ab.fl.utils.json _encodeStrongProperties(root, data); - return JParser.encode(root); + return encoder(root); } /** * Recursive method used to encode an object tree @@ -379,7 +382,7 @@ package ab.fl.utils.json for each (key in sourceKeys) { value = source[key]; - if (!value) + if (value==null) { target[key] = null; continue; @@ -390,13 +393,15 @@ package ab.fl.utils.json switch (true) { case (value is String): - target[key] = value; + target[key] = (value.length) ? value : ""; break; case (value is Boolean): + target[key] = (value !== null) ? value : false; + break; case (value is Number): case (value is int): case (value is uint): - target[key] = (value) ? value : null; + target[key] = (value !== null) ? value : null; break; case (className == "Object"): @@ -420,6 +425,9 @@ package ab.fl.utils.json _encodeStrongProperties(target[key], value); break; } + +// if (target.keys && target.keys.lastIndexOf(key) == -1) +// target.keys.push(key); } } /** @@ -504,6 +512,9 @@ package ab.fl.utils.json _setStrongProperties(target[key], value); break; } + +// if (target.keys && target.keys.lastIndexOf(key) == -1) +// target.keys.push(key); } } @@ -665,9 +676,9 @@ package ab.fl.utils.json { _objectReference[name] = value; - if (_keys.lastIndexOf(name) == -1) + if (keys && keys.lastIndexOf(name) == -1) { - _keys.push(name); + keys.push(name); } if (!_isTree) @@ -753,5 +764,26 @@ package ab.fl.utils.json { _treeKey = treeKey; } + + public function get keys():Vector. + { + _keys = new Vector.(); + + var key:String; + for (key in objectReference) + { + if (_keys.lastIndexOf(key) == -1) + { + _keys.push(key); + } + } + + return _keys; + } + + public function set keys(keys:Vector.):void + { + _keys = keys; + } } } diff --git a/src_test/EncodeToTypedTest.as b/src_test/EncodeToTypedTest.as new file mode 100644 index 0000000..1b82176 --- /dev/null +++ b/src_test/EncodeToTypedTest.as @@ -0,0 +1,109 @@ +package { + +import ab.fl.utils.json.JSON; + +import helpers.vo.TypedVO; + +import org.flexunit.Assert; + +public class EncodeToTypedTest { + + private var strongTypedVO:TypedVO; + + [Before] + public function setup():void { + strongTypedVO = typedVO; + ab.fl.utils.json.JSON.registerClass("TypedVO", TypedVO); + } + + [After] + public function tearDown():void { + strongTypedVO = null; + } + + [Test] + public function int_value_of_1_should_encode_to_1():void { + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aInt/i) + 6; + Assert.assertEquals(1, json.substr(index, 1)); + } + + [Test] + public function int_value_of_0_should_encode_to_0():void { + strongTypedVO.aInt = 0; + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aInt/i) + 6; + Assert.assertEquals(0, json.substr(index, 1)); + } + + [Test] + public function uint_value_of_1_should_encode_to_1():void { + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aUint/i) + 7; + Assert.assertEquals(1, json.substr(index, 1)); + } + + [Test] + public function uint_value_of_0_should_encode_to_0():void { + strongTypedVO.aUint = 0; + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aUint/i) + 7; + Assert.assertEquals(0, json.substr(index, 1)); + } + + [Test] + public function number_value_of_1_should_encode_to_1():void { + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aNumber/i) + 9; + Assert.assertEquals(1, json.substr(index, 1)); + } + + [Test] + public function number_value_of_0_should_encode_to_0():void { + strongTypedVO.aNumber = 0; + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aNumber/i) + 9; + Assert.assertEquals(0, json.substr(index, 1)); + } + + [Test] + public function string_value_of_a_should_encode_to_a():void { + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aString/i) + 10; + Assert.assertEquals("a", json.substr(index, 1)); + } + + [Test] + public function empty_string_value_should_encode_to_empty_string():void { + strongTypedVO.aString = ""; + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aString/i) + 10; + Assert.assertEquals('"', json.substr(index, 1)); + } + + [Test] + public function boolean_value_of_true_should_encode_to_true():void { + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aBoolean/i) + 10; + Assert.assertEquals("true", json.substr(index, 4)); + } + + [Test] + public function boolean_value_of_false_should_encode_to_false():void { + strongTypedVO.aBoolean = false; + var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO); + var index:int = json.search(/aBoolean/i) + 10; + Assert.assertEquals("false", json.substr(index, 5)); + } + + private function get typedVO():TypedVO { + var _typedVO:TypedVO = new TypedVO(); + _typedVO.aInt = 1; + _typedVO.aUint = 1; + _typedVO.aNumber = 1; + _typedVO.aString = "a"; + _typedVO.aBoolean = true; + return _typedVO; + } +} +} diff --git a/src_test/FlexTestSuite.as b/src_test/FlexTestSuite.as new file mode 100755 index 0000000..3b49876 --- /dev/null +++ b/src_test/FlexTestSuite.as @@ -0,0 +1,10 @@ +package +{ + [Suite] + [RunWith("org.flexunit.runners.Suite")] + public class FlexTestSuite + { + public var simpleTest:SimpleTest; + public var encodeToTypedIntTest:EncodeToTypedTest; + } +} \ No newline at end of file diff --git a/src_test/FlexUnitRunner.as b/src_test/FlexUnitRunner.as index 94c3c86..ae74c1e 100644 --- a/src_test/FlexUnitRunner.as +++ b/src_test/FlexUnitRunner.as @@ -28,7 +28,7 @@ package { //You can also pass a Request Object, which allows you to sort, filter and subselect. //var request:Request = Request.methods( someClass, ["method1", "method2", "method3"] ).sortWith( someSorter ).filterWith( someFilter ); //core.run( request ); - core.run(SimpleTest); + core.run(FlexTestSuite); } } } diff --git a/src_test/SimpleTest.as b/src_test/SimpleTest.as index 1782292..1ad6456 100644 --- a/src_test/SimpleTest.as +++ b/src_test/SimpleTest.as @@ -5,7 +5,7 @@ package { [Test] public function testMe() : void { - Assert.assertTrue("Was not true", false); + Assert.assertTrue("Was not true", true); } } } diff --git a/src_test/helpers/vo/TypedVO.as b/src_test/helpers/vo/TypedVO.as new file mode 100644 index 0000000..22b3315 --- /dev/null +++ b/src_test/helpers/vo/TypedVO.as @@ -0,0 +1,13 @@ +package helpers.vo +{ + [RemoteClass(alias="TypedVO")] + public class TypedVO + { + public var aInt:int; + public var aUint:uint; + public var aNumber:Number; + public var aString:String; + public var aBoolean:Boolean; + public var _explicitType:String; + } +}