Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2010-07-06 14:20:10 +0400
committerJamesNK <james@newtonking.com>2010-07-06 14:20:10 +0400
commit08b8477f5e1458fc98d95e5d33ede6c8fa115478 (patch)
tree0109fe8eed42bcbe5cea74d18befe4492ee31067
parentc2800930ab4a66710c56d5aa6c3fb32269af0446 (diff)
-Fixed generating generic simple type names
-Changed JsonObjectAttribute to be compatible with structs
-rw-r--r--Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj1
-rw-r--r--Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj1
-rw-r--r--Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj1
-rw-r--r--Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj1
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs27
-rw-r--r--Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs31
-rw-r--r--Src/Newtonsoft.Json/JsonObjectAttribute.cs2
-rw-r--r--Src/Newtonsoft.Json/Linq/JProperty.cs4
-rw-r--r--Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs53
9 files changed, 117 insertions, 4 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj
index 1d4cc07..cd48bfe 100644
--- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj
+++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj
@@ -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">
diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj
index 7e43745..a1684bf 100644
--- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj
+++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.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}" />
diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj
index 4de0466..4bc8d11 100644
--- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj
+++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj
@@ -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">
diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj
index 17695f7..872f5b3 100644
--- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj
+++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.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">
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
index 5c5a84f..fa0e14f 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -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);
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs
new file mode 100644
index 0000000..9f17faf
--- /dev/null
+++ b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs
@@ -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);
+ }
+ }
+} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json/JsonObjectAttribute.cs b/Src/Newtonsoft.Json/JsonObjectAttribute.cs
index 4241032..6035009 100644
--- a/Src/Newtonsoft.Json/JsonObjectAttribute.cs
+++ b/Src/Newtonsoft.Json/JsonObjectAttribute.cs
@@ -30,7 +30,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Instructs the <see cref="JsonSerializer"/> how to serialize the object.
/// </summary>
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false)]
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = false)]
public sealed class JsonObjectAttribute : JsonContainerAttribute
{
private MemberSerialization _memberSerialization = MemberSerialization.OptOut;
diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs
index 7996ed7..d100388 100644
--- a/Src/Newtonsoft.Json/Linq/JProperty.cs
+++ b/Src/Newtonsoft.Json/Linq/JProperty.cs
@@ -161,10 +161,10 @@ namespace Newtonsoft.Json.Linq
/// </returns>
public override JEnumerable<JToken> Children()
{
- return new JEnumerable<JToken>(ChildrenInternal());
+ return new JEnumerable<JToken>(GetValueEnumerable());
}
- private IEnumerable<JToken> ChildrenInternal()
+ private IEnumerable<JToken> GetValueEnumerable()
{
yield return Value;
}
diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs
index 3d4a0ce..682919d 100644
--- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs
+++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs
@@ -30,6 +30,8 @@ using System.Collections;
using System.Linq;
using System.Globalization;
using System.Runtime.Serialization.Formatters;
+using System.Text;
+using System.Text.RegularExpressions;
namespace Newtonsoft.Json.Utilities
{
@@ -45,7 +47,7 @@ namespace Newtonsoft.Json.Utilities
switch (assemblyFormat)
{
case FormatterAssemblyStyle.Simple:
- return t.FullName + ", " + t.Assembly.GetName().Name;
+ return GetSimpleTypeName(t);
case FormatterAssemblyStyle.Full:
return t.AssemblyQualifiedName;
default:
@@ -53,6 +55,55 @@ namespace Newtonsoft.Json.Utilities
}
}
+ private static string GetSimpleTypeName(Type type)
+ {
+ string fullyQualifiedTypeName = type.FullName + ", " + type.Assembly.GetName().Name;
+
+ // for type names with no nested type names then return
+ if (!type.IsGenericType || type.IsGenericTypeDefinition)
+ return fullyQualifiedTypeName;
+
+ StringBuilder builder = new StringBuilder();
+
+ // loop through the type name and filter out qualified assembly details from nested type names
+ bool writingAssemblyName = false;
+ bool skippingAssemblyDetails = false;
+ for (int i = 0; i < fullyQualifiedTypeName.Length; i++)
+ {
+ char current = fullyQualifiedTypeName[i];
+ switch (current)
+ {
+ case '[':
+ writingAssemblyName = false;
+ skippingAssemblyDetails = false;
+ builder.Append(current);
+ break;
+ case ']':
+ writingAssemblyName = false;
+ skippingAssemblyDetails = false;
+ builder.Append(current);
+ break;
+ case ',':
+ if (!writingAssemblyName)
+ {
+ writingAssemblyName = true;
+ builder.Append(current);
+ }
+ else
+ {
+ skippingAssemblyDetails = true;
+ }
+ break;
+ default:
+ if (!skippingAssemblyDetails)
+ builder.Append(current);
+ break;
+ }
+ }
+
+ return builder.ToString();
+ }
+
public static bool IsInstantiatableType(Type t)
{
ValidationUtils.ArgumentNotNull(t, "t");