Skip to content

Commit

Permalink
Fixes to GAE unserialization to support nulls better.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Berastau committed Jan 2, 2009
1 parent ea93e05 commit a378e30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
20 changes: 15 additions & 5 deletions framework/src/org/ruboss/serializers/GAEXMLSerializer.as
Expand Up @@ -49,6 +49,8 @@ package org.ruboss.serializers {

var objectName:String = xmlFragment.@kind;

if (RubossUtils.isEmpty(objectName) && xmlFragment.localName().toString() == "entities") return new Array;

var results:TypedArray = new TypedArray;
if (xmlFragment.@type == "array") {
// we are only going to specifically unmarshall known relationships
Expand All @@ -74,6 +76,8 @@ package org.ruboss.serializers {
protected override function unmarshallObject(source:Object, type:String = null):Object {
var node:XML = XML(source);
var localName:String = RubossUtils.lowerCaseFirst(node.@kind);
if (RubossUtils.isEmpty(localName)) return null;

var fqn:String = (!type) ? state.fqns[localName] : type;
var nodeId:String = node.@key;
var updatingExistingReference:Boolean = false;
Expand All @@ -93,17 +97,18 @@ package org.ruboss.serializers {
object["rev"] = node.key;

for each (var element:XML in node.elements()) {
if (node.localName().toString() == "key") continue;
if (element.localName().toString() == "key") continue;

var targetName:String = element.@name;
var targetType:String = element.@type;
var defaultValue:* = null;

if (targetType != "key") {
if (targetType != "key" && targetType != "null") {
defaultValue = RubossUtils.cast(types[targetType], element.toString());
} else {
} else if (targetType == "key") {
targetName = targetName + "_id";
}

unmarshallAttribute(node, object, element, fqn, targetName, defaultValue,
updatingExistingReference);
}
Expand All @@ -113,8 +118,13 @@ package org.ruboss.serializers {
return object;
}

protected override function getRefId(id:String):String {
return id.replace(/.*\[(\w+)\]/, "$1");
protected override function getRefId(id:Object):String {
try {
return XML(id).toString().replace(/.*\[(\w+)\]/, "$1");
} catch (e:Error) {
return "";
}
return "";
}

protected override function getPolymorphicRef(source:Object, name:String):String {
Expand Down
10 changes: 6 additions & 4 deletions framework/src/org/ruboss/serializers/GenericSerializer.as
Expand Up @@ -111,13 +111,15 @@ package org.ruboss.serializers {
if (attribute is Array || (attribute is XML && XML(attribute).@type == "array")) {
isNestedArray = true;
} else {
isNestedObject = true;
if (RubossUtils.isEmpty(targetType)) {
// we potentially have a nested polymorphic relationship here
var nestedPolymorphicRef:String = source[RubossUtils.toSnakeCase(targetName) + "_type"];
if (!RubossUtils.isEmpty(nestedPolymorphicRef)) {
targetType = state.fqns[nestedPolymorphicRef];
isNestedObject = true;
}
} else {
isNestedObject = true;
}
}
} catch (e:Error) {
Expand All @@ -129,7 +131,7 @@ package org.ruboss.serializers {
// if this property is a reference, try to resolve the
// reference and set up biderctional links between models
if (isRef) {
var refId:String = (attribute) ? getRefId(attribute.toString()) : "";
var refId:String = (attribute) ? getRefId(attribute) : "";
if (RubossUtils.isEmpty(refId)) {
Ruboss.log.warn("reference id :" + fqn + "." + targetName + " is empty, setting it to null.");
if (updatingExistingReference) {
Expand Down Expand Up @@ -217,8 +219,8 @@ package org.ruboss.serializers {
return new ModelsCollection;
}

protected function getRefId(id:String):String {
return id;
protected function getRefId(id:Object):String {
return id.toString()
}

protected function getPolymorphicRef(source:Object, name:String):String {
Expand Down

0 comments on commit a378e30

Please sign in to comment.