Skip to content

Commit

Permalink
fixed bug relating to IModelSerializer and the use of the {{MODEL_NAM…
Browse files Browse the repository at this point in the history
…E}} key
  • Loading branch information
Nick Berardi committed Nov 27, 2010
1 parent 4442896 commit b910608
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/Serialization/Serializer.cs
Expand Up @@ -24,37 +24,6 @@ public class Serializer
private static readonly IDictionary<Type, PropertyInfo[]> _cache;
public const string ModelNameKey = "{{MODEL_NAME}}";

public static string GetSerializedObjectName(object obj)
{
string name = obj is ICollection ? "collection" : "object";

// make sure the object isn't an easily handled primity type with IEnumerable
if (Type.GetTypeCode(obj.GetType()) != TypeCode.Object)
name = "object";

// make sure this type of dictionary is treated as an object
if (obj is IDictionary<string, object>)
name = "object";

// check for special case name from dictionary object
if (obj is IDictionary<string, object> && ((IDictionary<string, object>)obj).ContainsKey(ModelNameKey))
name = Convert.ToString(((IDictionary<string, object>)obj)[ModelNameKey]);

// get what the object likes to be called
if (obj.GetType().IsDefined(typeof(SerializableObjectAttribute), true))
{
object[] attrs = obj.GetType().GetCustomAttributes(typeof(SerializableObjectAttribute), true);

if (attrs.Length > 0)
{
SerializableObjectAttribute attr = attrs[0] as SerializableObjectAttribute;
name = attr.Name;
}
}

return name;
}

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -116,14 +85,42 @@ public T Deserialize<T>(string input, IDeserializer deserializer)
public IDictionary<string, object> FromObject(object obj, ISerializerOptions options)
{
object value = SerializeValue(obj, 0 /* level */, options.MaxSerializableLevelsSupported ?? LevelsToSerialize);
string modelName = null;

// remove special case model name if found in output
if (value is IDictionary<string, object> && ((IDictionary<string, object>)value).ContainsKey(ModelNameKey))
{
modelName = Convert.ToString(((IDictionary<string, object>)value)[ModelNameKey]);
((IDictionary<string, object>)value).Remove(ModelNameKey);
}

if (options.CheckForObjectName || !(value is IDictionary<string, object>))
{
string name = GetSerializedObjectName(obj);
string name = obj is ICollection ? "collection" : "object";

// make sure the object isn't an easily handled primity type with IEnumerable
if (Type.GetTypeCode(obj.GetType()) != TypeCode.Object)
name = "object";

// make sure this type of dictionary is treated as an object
if (obj is IDictionary<string, object>)
name = "object";

// check for special case name from dictionary object
if (obj is IModelSerializer && !String.IsNullOrWhiteSpace(modelName))
name = modelName;

// get what the object likes to be called
if (obj.GetType().IsDefined(typeof(SerializableObjectAttribute), true))
{
object[] attrs = obj.GetType().GetCustomAttributes(typeof(SerializableObjectAttribute), true);

if (attrs.Length > 0)
{
SerializableObjectAttribute attr = attrs[0] as SerializableObjectAttribute;
name = attr.Name;
}
}

IDictionary<string, object> response = new Dictionary<string, object>(1);
response.Add(name, value);
Expand Down Expand Up @@ -317,7 +314,7 @@ private object SerializeValue(object obj, int level, int levelLimit)
return obj;

if (obj is IModelSerializer)
return ((IModelSerializer)obj).GetSerializedModel();
return new Dictionary<string, object>(((IModelSerializer)obj).GetSerializedModel());

if (obj is IDictionary<string, object>)
{
Expand Down
1 change: 1 addition & 0 deletions test/ManagedFusion.Tests/ManagedFusion.Tests.csproj
Expand Up @@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ModelSerializerTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerializerTest.cs" />
</ItemGroup>
Expand Down
47 changes: 47 additions & 0 deletions test/ManagedFusion.Tests/ModelSerializerTest.cs
@@ -0,0 +1,47 @@
using System;
using NUnit.Framework;
using System.Dynamic;
using ManagedFusion.Serialization;
using System.Collections.Generic;
using System.Linq;

namespace ManagedFusion.Tests
{
[TestFixture]
public class ModelSerializerTest
{
private class TestModel : IModelSerializer
{
#region IModelSerializer Members

IDictionary<string, object> IModelSerializer.GetSerializedModel()
{
dynamic model = new ExpandoObject();

((IDictionary<string, object>)model).Add(Serializer.ModelNameKey, "test");
model.name = "value";

return model;
}

#endregion
}

[Test]
public void Simple_With_Name()
{
// arrange
var expected = "test";
var obj = new TestModel();

var ser = new Serializer();
var options = new SerlizerOptions { CheckForObjectName = true };

// act
var result = ser.FromObject(obj, options);

// assert
Assert.AreEqual(expected, result.Keys.First());
}
}
}
8 changes: 8 additions & 0 deletions test/ManagedFusion.Tests/SerializerTest.cs
Expand Up @@ -13,21 +13,26 @@ public class SerializerTest
[Test]
public void Simple()
{
// arrange
var expected = "test";
var obj = new Dictionary<string, object>() {
{ "name", expected }
};

var ser = new Serializer();
var options = new SerlizerOptions();

// act
var result = ser.FromObject(obj, options);

// assert
Assert.AreEqual(expected, result["name"]);
}

[Test]
public void Simple_With_Name()
{
// arrange
var expected = "test";
var obj = new Dictionary<string, object>() {
{ Serializer.ModelNameKey, expected },
Expand All @@ -36,8 +41,11 @@ public void Simple_With_Name()

var ser = new Serializer();
var options = new SerlizerOptions { CheckForObjectName = true };

// act
var result = ser.FromObject(obj, options);

// assert
Assert.AreEqual(expected, result.Keys.First());
}
}
Expand Down

0 comments on commit b910608

Please sign in to comment.