Permalink
Browse files

Closes #50

  • Loading branch information...
dima committed Feb 27, 2011
1 parent 3328058 commit ce508541fb87a6cb7a8a863623f35e515d583b06
@@ -64,13 +64,13 @@ package org.restfulx.serializers {
}
try {
if (object is Array) {
- return unmarshallJSONArray(object as Array, disconnected);
+ return unmarshallJSONArray(object as Array, disconnected, defaultType);
} else {
var source:Object = JSON.decode(object.toString());
if (source is Array) {
- return unmarshallJSONArray(source as Array, disconnected);
+ return unmarshallJSONArray(source as Array, disconnected, defaultType);
} else {
- return unmarshallJSONObject(source, disconnected);
+ return unmarshallJSONObject(source, disconnected, defaultType);
}
}
} catch (e:Error) {
@@ -81,10 +81,13 @@ package org.restfulx.serializers {
}
// can digest both ActiveRecord-like JSON and CouchDB-like JSON
- private function unmarshallJSONArray(instances:Array, disconnected:Boolean = false):TypedArray {
+ private function unmarshallJSONArray(instances:Array, disconnected:Boolean = false, defaultType:String = null):TypedArray {
var result:TypedArray = new TypedArray;
- if (!instances || !instances.length) return result;
+ if (!instances || !instances.length) {
+ result.itemType = defaultType;
+ return result;
+ }
if (instances[0].hasOwnProperty("metadata")) {
result.metadata = new Object;
@@ -102,7 +105,7 @@ package org.restfulx.serializers {
return result;
}
- private function unmarshallJSONObject(source:Object, disconnected:Boolean = false):Object {
+ private function unmarshallJSONObject(source:Object, disconnected:Boolean = false, defaultType:String = null):Object {
if (!source.hasOwnProperty("id") && !source.hasOwnProperty("_id")) {
// ActiveRecord-like JSON array with element names as object keys
for (var prop:String in source) {
@@ -116,7 +119,7 @@ package org.restfulx.serializers {
convertProperties(source);
}
- return super.unmarshall(source, disconnected);
+ return super.unmarshall(source, disconnected, defaultType);
}
private function convertProperties(instance:Object):Object {
View
@@ -163,6 +163,7 @@
ts.addTest(new XMLSerializerTest("testRecursiveObjectMarshallingWithMixedRelationships"));
ts.addTest(new XMLSerializerTest("testNothingUnmarshalling"));
+ ts.addTest(new JSONSerializerTest("testEmptyObjectUnmarshalling"));
ts.addTest(new JSONSerializerTest("testObjectUnmarshalling"));
ts.addTest(new JSONSerializerTest("testErrorUnmarshalling"));
ts.addTest(new JSONSerializerTest("testObjectMarshalling"));
@@ -44,6 +44,13 @@ package restfulx.test.cases.serializers {
super(methodName);
json = new JSONSerializer;
}
+
+ public function testEmptyObjectUnmarshalling():void {
+ var marshalled:String = '[]';
+ var unmarshalled:Object = json.unmarshall(marshalled);
+ assertTrue(unmarshalled is TypedArray);
+ assertEquals(0, TypedArray(unmarshalled).source.length);
+ }
public function testObjectUnmarshalling():void {
var marshalled:String = '[{\"project\": {\"name\": \"Project4NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"id\": 490909803, \"tasks\": [{\"name\": \"Task4NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"project_id\": 490909803, \"id\": 540638538, \"created_at\": \"2008-12-09T00:02:40Z\"}], \"created_at\": \"2008-12-09T00:02:40Z\"}}, {\"project\": {\"name\": \"Project2NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"id\": 1043718716, \"tasks\": [{\"name\": \"Task2NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"project_id\": 1043718716, \"id\": 525404037, \"created_at\": \"2008-12-09T00:02:40Z\"}], \"created_at\": \"2008-12-09T00:02:40Z\"}}, {\"project\": {\"name\": \"Project1NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"id\": 1060557696, \"tasks\": [{\"name\": \"Task1NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"project_id\": 1060557696, \"id\": 568611273, \"created_at\": \"2008-12-09T00:02:40Z\"}], \"created_at\": \"2008-12-09T00:02:40Z\"}}, {\"project\": {\"name\": \"Project3NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"id\": 1063252898, \"tasks\": [{\"name\": \"Task3NameString\", \"updated_at\": \"2008-12-09T00:02:40Z\", \"project_id\": 1063252898, \"id\": 1069820074, \"created_at\": \"2008-12-09T00:02:40Z\"}], \"created_at\": \"2008-12-09T00:02:40Z\"}}]';

0 comments on commit ce50854

Please sign in to comment.