-Fixed generating generic simple type names

-Changed JsonObjectAttribute to be compatible with structs
This commit is contained in:
JamesNK
2010-07-06 10:20:10 +00:00
parent c2800930ab
commit 08b8477f5e
9 changed files with 117 additions and 4 deletions
@@ -203,6 +203,7 @@
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
<Compile Include="TestObjects\WagePerson.cs" />
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
<Compile Include="Utilities\ReflectionUtilsTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.Compact.csproj">
@@ -209,6 +209,7 @@
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
<Compile Include="TestObjects\WagePerson.cs" />
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
<Compile Include="Utilities\ReflectionUtilsTests.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{3259AA49-8AA1-44D3-9025-A0B520596A8C}" />
@@ -214,6 +214,7 @@
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
<Compile Include="TestObjects\WagePerson.cs" />
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
<Compile Include="Utilities\ReflectionUtilsTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.Silverlight.csproj">
@@ -249,6 +249,7 @@
<Compile Include="TestObjects\TypedSubHashtable.cs" />
<Compile Include="TestObjects\UserNullable.cs" />
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
<Compile Include="Utilities\ReflectionUtilsTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.csproj">
@@ -3506,5 +3506,32 @@ keyword such as type of business.""
JsonConvert.DeserializeObject<StringDictionaryTestClass>(json);
}
#endif
[JsonObject(MemberSerialization.OptIn)]
public struct StructWithAttribute
{
public string MyString { get; set; }
[JsonProperty]
public int MyInt { get; set; }
}
[Test]
public void SerializeStructWithJsonObjectAttribute()
{
StructWithAttribute testStruct = new StructWithAttribute
{
MyInt = int.MaxValue
};
string json = JsonConvert.SerializeObject(testStruct, Formatting.Indented);
Assert.AreEqual(@"{
""MyInt"": 2147483647
}", json);
StructWithAttribute newStruct = JsonConvert.DeserializeObject<StructWithAttribute>(json);
Assert.AreEqual(int.MaxValue, newStruct.MyInt);
}
}
}
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Tests.Utilities
{
public class ReflectionUtilsTests : TestFixtureBase
{
[Test]
public void GetTypeNameSimpleForGenericTypes()
{
string typeName;
typeName = ReflectionUtils.GetTypeName(typeof(IList<Type>), FormatterAssemblyStyle.Simple);
Assert.AreEqual("System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<IList<Type>, IList<Type>>), FormatterAssemblyStyle.Simple);
Assert.AreEqual("System.Collections.Generic.IDictionary`2[[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib],[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib]], mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IList<>), FormatterAssemblyStyle.Simple);
Assert.AreEqual("System.Collections.Generic.IList`1, mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<,>), FormatterAssemblyStyle.Simple);
Assert.AreEqual("System.Collections.Generic.IDictionary`2, mscorlib", typeName);
}
}
}