Skip to content

Commit

Permalink
wrote failing test cases for deserializing from a ReadOnlyCollection<…
Browse files Browse the repository at this point in the history
…T> (specifically int, string, double like those for HashSet)

added ReadOnlyCollection<> check to DeSerializeListWithElement.ParseGenericList, since ReadOnlyCollection<> implements IList<>, but throws on .Add :( -- workaround for a poor design decision
  • Loading branch information
Iristyle committed Aug 10, 2011
1 parent 6d4f9de commit e4d60b8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/ServiceStack.Text/Common/DeserializeListWithElements.cs
Expand Up @@ -12,6 +12,7 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;

namespace ServiceStack.Text.Common
Expand Down Expand Up @@ -103,7 +104,10 @@ public static IList<T> ParseGenericList(string value, Type createListType, Parse
{
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)) == null) return null;

var to = (createListType == null)
bool isReadOnly = null == createListType ? false
: createListType.IsGenericType && createListType.GetGenericTypeDefinition() == typeof(ReadOnlyCollection<>);

var to = (createListType == null || isReadOnly)
? new List<T>()
: (IList<T>)ReflectionExtensions.CreateInstance(createListType);

Expand Down Expand Up @@ -135,7 +139,10 @@ public static IList<T> ParseGenericList(string value, Type createListType, Parse

}
}
return to;

//TODO: 8-10-2011 -- this CreateInstance call should probably be moved over to ReflectionExtensions,
//but not sure how you'd like to go about caching constructors with parameters -- I would probably build a NewExpression, .Compile to a LambdaExpression and cache
return isReadOnly ? (IList<T>)Activator.CreateInstance(createListType, to) : to;
}
}

Expand Down
45 changes: 36 additions & 9 deletions tests/ServiceStack.Text.Tests/BasicStringSerializerTests.cs
@@ -1,12 +1,12 @@
using System;
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Northwind.Common.ComplexModel;
using NUnit.Framework;
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Tests.Models;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Northwind.Common.ComplexModel;
using NUnit.Framework;
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Tests.Models;
using ServiceStack.Text.Common;

namespace ServiceStack.Text.Tests
Expand Down Expand Up @@ -386,6 +386,33 @@ public void Can_convert_string_to_double_HashSet()
var toHashSet = Serialize(fromHashSet);

Assert.That(toHashSet.EquivalentTo(fromHashSet), Is.True);
}

[Test]
public void Can_convert_string_to_string_ReadOnlyCollection()
{
var fromCollection = new ReadOnlyCollection<string>(stringValues);
var toCollection = Serialize(fromCollection);

Assert.That(toCollection.EquivalentTo(fromCollection), Is.True);
}

[Test]
public void Can_convert_string_to_int_ReadOnlyCollection()
{
var fromCollection = new ReadOnlyCollection<int>(intValues);
var toCollection = Serialize(fromCollection);

Assert.That(toCollection.EquivalentTo(fromCollection), Is.True);
}

[Test]
public void Can_convert_string_to_double_ReadOnlyCollection()
{
var fromCollection = new ReadOnlyCollection<double>(doubleValues);
var toCollection = Serialize(fromCollection);

Assert.That(toCollection.EquivalentTo(fromCollection), Is.True);
}

public T Serialize<T>(T model)
Expand Down

0 comments on commit e4d60b8

Please sign in to comment.