Skip to content

Commit

Permalink
removed ability to have null in collections (for now)...how often doe…
Browse files Browse the repository at this point in the history
…s that happen anyways?
  • Loading branch information
Karl Seguin committed May 23, 2010
1 parent 1797177 commit c44dcb4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
1 change: 0 additions & 1 deletion Metsys.Little.Tests/IntegrationTests.cs
Expand Up @@ -53,7 +53,6 @@ public void CollectionsAreLossless()
};
expected.Roles.Add("abc123");
expected.Roles.Add("lkasdk");
expected.Roles.Add(null);
var data = Serializer.Serialize(expected);
var actual = Deserializer.Deserialize<ComplexObject>(data);

Expand Down
2 changes: 1 addition & 1 deletion Metsys.Little.Tests/SerializerTests.cs
Expand Up @@ -136,7 +136,7 @@ public void NullArrayGetsSerialized()
Assert.Equal(0, data[0]);
Assert.Equal(1, data.Length);
}
[Fact]
[Fact(Skip="took out this feature")]
public void SerializesArrayWithMixOfNullsAndNotNulls()
{
var data = Serializer.Serialize(new[] {"ab", null, "cd", null, null});
Expand Down
5 changes: 2 additions & 3 deletions Metsys.Little/Deserializer.cs
Expand Up @@ -70,11 +70,10 @@ private object ReadList(Type listType, object existingContainer)
var count = 0;
var itemType = ListHelper.GetListItemType(listType);
var wrapper = BaseWrapper.Create(listType, itemType, existingContainer);
var nullable = MagicProperty.IsNullable(itemType);


while (count++ < totalItems)
{
var value = nullable && IsNull() ? null : DeserializeValue(itemType, null);
var value = DeserializeValue(itemType, null);
wrapper.Add(value);
}
return wrapper.Collection;
Expand Down
20 changes: 15 additions & 5 deletions Metsys.Little/Helpers/Lists/BaseWrapper.cs
Expand Up @@ -21,11 +21,20 @@ private static BaseWrapper CreateWrapperFromType(Type type, Type itemType)
return (BaseWrapper)Activator.CreateInstance(typeof(ArrayWrapper<>).MakeGenericType(itemType));
}

var isCollection = false;
var types = new List<Type>(type.GetInterfaces().Select(h => h.IsGenericType ? h.GetGenericTypeDefinition() : h));
types.Insert(0, type.IsGenericType ? type.GetGenericTypeDefinition() : type);
foreach(var @interface in types)
var isCollection = false;
var interfaces = type.GetInterfaces();
var count = interfaces.Length + 1;
var types = new List<Type>(count) {type.IsGenericType ? type.GetGenericTypeDefinition() : type};
types.AddRange(interfaces);

for(var i = 0; i < count; ++i)
{
var @interface = types[i];
if (@interface.IsGenericType)
{
@interface = @interface.GetGenericTypeDefinition();
types[i] = @interface;
}
if (typeof(IList<>).IsAssignableFrom(@interface) || typeof(IList).IsAssignableFrom(@interface))
{
return new ListWrapper();
Expand All @@ -41,8 +50,9 @@ private static BaseWrapper CreateWrapperFromType(Type type, Type itemType)
}

//a last-ditch pass
foreach (var @interface in types)
for(var i = 0; i < count; ++i)
{
var @interface = types[i];
if (typeof(IEnumerable<>).IsAssignableFrom(@interface) || typeof(IEnumerable).IsAssignableFrom(@interface))
{
return new ListWrapper();
Expand Down
15 changes: 6 additions & 9 deletions Metsys.Little/Serializer.cs
Expand Up @@ -64,10 +64,6 @@ private void WriteObject(object o)
var value = property.Getter(o);
if (value == null)
{
if (!property.Nullable)
{
throw new InvalidOperationException(string.Format("Got not value for non-nullable type: {0}", property.Type.Name));
}
WriteNull();
continue;
}
Expand All @@ -77,7 +73,7 @@ private void WriteObject(object o)

private void SerializeMember(object value, bool nullable)
{
Type type = value.GetType();
var type = value.GetType();
if (type.IsEnum)
{
type = Enum.GetUnderlyingType(type);
Expand Down Expand Up @@ -106,11 +102,11 @@ private void Write(IEnumerable enumerable)
var start = _stream.Position;
_writer.Write(0); //placeholder for # of elements
var count = 0;
bool? nullable = null;
//bool? nullable = null;
foreach (var item in enumerable)
{
++count;
if (item == null)
++count;
/* if (item == null)
{
WriteNull();
continue;
Expand All @@ -119,7 +115,8 @@ private void Write(IEnumerable enumerable)
{
nullable = MagicProperty.IsNullable(item.GetType());
}
SerializeMember(item, nullable.Value);
*/
SerializeMember(item, false);
}
_stream.Seek(start, SeekOrigin.Begin);
_writer.Write(count);
Expand Down

0 comments on commit c44dcb4

Please sign in to comment.