Skip to content

Commit

Permalink
Add property mappings to serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Sep 20, 2011
1 parent a4825bb commit 80297cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/mesh/model/serialization/Serializer.as
Expand Up @@ -18,6 +18,8 @@ package mesh.model.serialization
* <li><code>exclude:Array</code> - A list of leaf node properties to exclude from serializing.</li>
* <li><code>includes:Object</code> - A hash of association nodes to serialize. You can map each
* association to additional hashes of these options.</li>
* <li><code>mapping:Object</code> - A hash that will transform the name of a property to a
* different property name on the serialized object.
* </ul>
* </p>
*
Expand Down Expand Up @@ -53,13 +55,13 @@ package mesh.model.serialization
var reflection:Type = reflect(_object);
for each (var property:String in properties()) {
if (isImplicitLeaf(property) || isExplicitLeaf(reflection.property(property))) {
serialized[property] = leafValue(property);
serialized[mapped(property)] = leafValue(property);
}
}

// Serialize the associated includes.
for (var association:String in includes) {
serialized[association] = nodeValue(association, includes[association] is Boolean ? null : includes[association]);
serialized[mapped(association)] = nodeValue(association, includes[association] is Boolean ? null : includes[association]);
}

return serialized;
Expand Down Expand Up @@ -102,7 +104,7 @@ package mesh.model.serialization
{
var value:* = _object[property];

// Attempt to simply the value to an array if possible.
// Attempt to simplify the value to an array if possible.
if (value != null && value.hasOwnProperty("toArray")) {
value = value.toArray();
}
Expand Down Expand Up @@ -147,6 +149,18 @@ package mesh.model.serialization
return properties;
}

/**
* Returns the name of the property on the serialized object that the specified property
* is mapped to.
*
* @param property The property to get the mapping for.
* @return A mapped property name.
*/
protected function mapped(property:String):String
{
return _options.mapping == null || !_options.mapping.hasOwnProperty(property) ? property : _options.mapping[property];
}

private function isSerializable(property:Property):Boolean
{
return (only == null || only.indexOf(property.name) != -1) &&
Expand Down
15 changes: 15 additions & 0 deletions tests/mesh/model/serialization/SerializerTests.as
Expand Up @@ -18,6 +18,7 @@ package mesh.model.serialization
import org.hamcrest.object.hasProperties;
import org.hamcrest.object.hasProperty;
import org.hamcrest.object.hasPropertyWithValue;
import org.hamcrest.object.notNullValue;
import org.hamcrest.object.nullValue;

public class SerializerTests
Expand Down Expand Up @@ -81,6 +82,20 @@ package mesh.model.serialization
assertThat(serialized.age, nullValue());
}

[Test]
public function testSerializeWithMapping():void
{
var serialized:Object = _customer.serialize({
includes:{orders:true},
mapping:{age:"yearsOld", orders:"purchases"}
});

assertThat(serialized.age, nullValue());
assertThat(serialized.yearsOld, notNullValue());
assertThat(serialized.orders, nullValue());
assertThat(serialized.purchases, notNullValue());
}

[Test]
public function testSerializeNested():void
{
Expand Down

0 comments on commit 80297cc

Please sign in to comment.