Skip to content

Commit

Permalink
Serialize inherited list from root JSON array
Browse files Browse the repository at this point in the history
This test actually comes from a scenario in RestSharp. Given a root JSON
array that matches a generic list, we should be able to deserialize that
to a type that derives from that generic list.
  • Loading branch information
haacked committed Sep 9, 2013
1 parent fd63918 commit 25f63e0
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 4 deletions.
100 changes: 100 additions & 0 deletions src/SimpleJson.Tests/DeserializeGenericListTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//-----------------------------------------------------------------------
// <copyright file="<file>.cs" company="The Outercurve Foundation">
// Copyright (c) 2011, The Outercurve Foundation.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.opensource.org/licenses/mit-license.php
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Nathan Totten (ntotten.com), Jim Zimmerman (jimzimmerman.com) and Prabir Shrestha (prabir.me)</author>
// <website>https://github.com/facebook-csharp-sdk/simple-json</website>
//-----------------------------------------------------------------------

namespace SimpleJsonTests
{
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

#if NUNIT
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using NUnit.Framework;
#else
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
#endif

using SimpleJson;

[TestClass]
public class DeserializeGenericListTests
{
private class NumberList : List<int>
{
}

private class ItemList : List<Item>
{
}

private class Item
{
public string SomeProperty { get; set; }
}

[TestMethod]
public void Can_Deserialize_Root_Json_Array_Of_Primitives_To_Inherited_List()
{
var json = "[0,1,2]";
var result = SimpleJson.DeserializeObject<NumberList>(json);

Assert.IsNotNull(result);
Assert.AreEqual(3, result.Count);
for (int i = 0; i < 3; i++)
{
Assert.AreEqual(i, result[i]);
}
}

[TestMethod]
public void Can_Deserialize_Root_Json_Array_Of_Primitives_To_Generic_List()
{
var json = "[0,1,2]";
var result = SimpleJson.DeserializeObject<List<int>>(json);

Assert.IsNotNull(result);
Assert.AreEqual(3, result.Count);
for (int i = 0; i < 3; i++)
{
Assert.AreEqual(i, result[i]);
}
}

[TestMethod]
public void Can_Deserialize_Root_Json_Array_To_Inherited_List()
{
var json = @"[{""SomeProperty"":""bar0""},{""SomeProperty"":""bar1""}]";
var result = SimpleJson.DeserializeObject<ItemList>(json);

Assert.IsNotNull(result);
Assert.AreEqual(2, result.Count);
Assert.AreEqual("bar0", result[0].SomeProperty);
Assert.AreEqual("bar1", result[1].SomeProperty);
}
}
}
1 change: 1 addition & 0 deletions src/SimpleJson.Tests/SimpleJson.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DeserializeGenericListTests.cs" />
<Compile Include="DataContractTests\DataContractSampleClassess.cs" />
<Compile Include="DataContractTests\PrivateFieldsSerializeTests.cs" />
<Compile Include="DataContractTests\PrivateGetterSettersSerializeTests.cs" />
Expand Down
62 changes: 62 additions & 0 deletions src/SimpleJson/EscapeToJavascriptStringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//-----------------------------------------------------------------------
// <copyright file="<file>.cs" company="The Outercurve Foundation">
// Copyright (c) 2011, The Outercurve Foundation.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.opensource.org/licenses/mit-license.php
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Nathan Totten (ntotten.com), Jim Zimmerman (jimzimmerman.com) and Prabir Shrestha (prabir.me)</author>
// <website>https://github.com/facebook-csharp-sdk/simple-json</website>
//-----------------------------------------------------------------------

namespace SimpleJsonTests
{

#if NUNIT
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using NUnit.Framework;
#else
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
#endif

using SimpleJson;

[TestClass]
public class EscapeToJavascriptStringTests
{
[TestMethod]
public void BackSlash()
{
var serialized = SimpleJson.SerializeObject("c:\\haha.pl");
var result = SimpleJson.EscapeToJavascriptString(serialized);

Assert.AreEqual("\"c:\\haha.pl\"", result);
}

[TestMethod]
public void BackSlashWithT()
{
var serialized = SimpleJson.SerializeObject("c:\\taha.pl");
var result = SimpleJson.EscapeToJavascriptString(serialized);

Assert.AreEqual("\"c:\\taha.pl\"", result);
}
}
}
1 change: 1 addition & 0 deletions src/SimpleJson/SimpleJson-Net20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="EscapeToJavascriptStringTests.cs" />

This comment has been minimized.

Copy link
@NathanBaulch

NathanBaulch Jun 3, 2014

Why is a unit test class being added to the Net20 project?

This comment has been minimized.

Copy link
@prabirshrestha

prabirshrestha Jun 5, 2014

Member

this shouldn't had been here.

<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleJson.cs" />
</ItemGroup>
Expand Down
31 changes: 27 additions & 4 deletions src/SimpleJson/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,9 +1395,8 @@ public virtual object DeserializeObject(object value, Type type)
}
else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || ReflectionUtils.IsAssignableFrom(typeof(IList), type))
{
Type innerType = ReflectionUtils.GetGenericTypeArguments(type)[0];
Type genericType = typeof(List<>).MakeGenericType(innerType);
list = (IList)ConstructorCache[genericType](jsonObject.Count);
Type innerType = ReflectionUtils.GetGenericListElementType(type);
list = (IList)(ConstructorCache[type] ?? ConstructorCache[typeof(List<>).MakeGenericType(innerType)])(jsonObject.Count);
foreach (object o in jsonObject)
list.Add(DeserializeObject(o, innerType));
}
Expand Down Expand Up @@ -1585,6 +1584,25 @@ public static Attribute GetAttribute(MemberInfo info, Type type)
#endif
}

public static Type GetGenericListElementType(Type type)
{
IEnumerable<Type> interfaces;
#if SIMPLE_JSON_TYPEINFO
interfaces = type.GetTypeInfo().ImplementedInterfaces;
#else
interfaces = type.GetInterfaces();
#endif
foreach (Type implementedInterface in interfaces)
{
if (IsTypeGeneric(implementedInterface) &&
implementedInterface.GetGenericTypeDefinition() == typeof (IList<>))
{
return GetGenericTypeArguments(implementedInterface)[0];
}
}
return GetGenericTypeArguments(type)[0];
}

public static Attribute GetAttribute(Type objectType, Type attributeType)
{

Expand All @@ -1608,9 +1626,14 @@ public static Type[] GetGenericTypeArguments(Type type)
#endif
}

public static bool IsTypeGeneric(Type type)
{
return GetTypeInfo(type).IsGenericType;
}

public static bool IsTypeGenericeCollectionInterface(Type type)
{
if (!GetTypeInfo(type).IsGenericType)
if (!IsTypeGeneric(type))
return false;

Type genericDefinition = type.GetGenericTypeDefinition();
Expand Down

0 comments on commit 25f63e0

Please sign in to comment.