-Added JsonDictionaryAttribute

-Added JsonContainerContract
-Added support for customizing the JsonConverter, type name handling, reference handling of collection items
This commit is contained in:
James Newton-King
2012-04-22 23:17:58 +12:00
parent 450996eb25
commit 7b776e9298
34 changed files with 894 additions and 252 deletions
@@ -1,15 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
#if !NETFX_CORE
using System.Runtime.Serialization;
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
using System.Reflection;
@@ -59,16 +62,19 @@ namespace Newtonsoft.Json.Tests.Serialization
}
}
#if !NET20
public class AddressWithDataMember
{
[DataMember(Name = "CustomerAddress1")]
public string AddressLine1 { get; set; }
}
#endif
[TestFixture]
public class ContractResolverTests : TestFixtureBase
{
#if !NET20
[Test]
public void DeserializeDataMemberClassWithNoDataContract()
{
@@ -77,6 +83,7 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("AddressLine1", contract.Properties[0].PropertyName);
}
#endif
[Test]
public void ResolveProperties_IgnoreStatic()
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Tests.TestObjects;
#if !NETFX_CORE
using NUnit.Framework;
@@ -1016,7 +1016,7 @@ keyword such as type of business.""
public void IncompatibleJsonAttributeShouldThrow()
{
ExceptionAssert.Throws<JsonSerializationException>(
"JsonConverter IsoDateTimeConverter on Newtonsoft.Json.Tests.TestObjects.IncompatibleJsonAttributeClass is not compatible with member type IncompatibleJsonAttributeClass.",
"Unexpected value when converting date. Expected DateTime or DateTimeOffset, got Newtonsoft.Json.Tests.TestObjects.IncompatibleJsonAttributeClass.",
() =>
{
IncompatibleJsonAttributeClass c = new IncompatibleJsonAttributeClass();
@@ -6193,4 +6193,15 @@ Parameter name: value",
}
}
#endif
[JsonObject(MemberSerialization.OptIn)]
public class TestComponentSimple
{
[JsonProperty]
public int MyProperty { get; set; }
public TestComponentSimple()
{
}
}
}
@@ -24,10 +24,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Tests.TestObjects;
#if !NETFX_CORE
using NUnit.Framework;
@@ -42,6 +39,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[TestFixture]
public class NullValueHandlingTests : TestFixtureBase
{
#if !NET20
[Test]
public void DeserializeNullIntoDateTime()
{
@@ -55,6 +53,7 @@ namespace Newtonsoft.Json.Tests.Serialization
DateTimeTestClass c = JsonConvert.DeserializeObject<DateTimeTestClass>(@"{DateTimeField:""""}", new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Assert.AreEqual(c.DateTimeField, default(DateTime));
}
#endif
[Test]
public void NullValueHandlingSerialization()
@@ -884,5 +884,148 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreNotEqual(myClasses1[0], myClasses2[0]);
}
[Test]
public void ReferencedIntList()
{
ReferencedList<int> l = new ReferencedList<int>();
l.Add(1);
l.Add(2);
l.Add(3);
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"[
1,
2,
3
]", json);
}
[Test]
public void ReferencedComponentList()
{
var c1 = new TestComponentSimple();
ReferencedList<TestComponentSimple> l = new ReferencedList<TestComponentSimple>();
l.Add(c1);
l.Add(new TestComponentSimple());
l.Add(c1);
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"[
{
""$id"": ""1"",
""MyProperty"": 0
},
{
""$id"": ""2"",
""MyProperty"": 0
},
{
""$ref"": ""1""
}
]", json);
}
[Test]
public void ReferencedIntDictionary()
{
ReferencedDictionary<int> l = new ReferencedDictionary<int>();
l.Add("First", 1);
l.Add("Second", 2);
l.Add("Third", 3);
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"{
""First"": 1,
""Second"": 2,
""Third"": 3
}", json);
}
[Test]
public void ReferencedComponentDictionary()
{
var c1 = new TestComponentSimple();
ReferencedDictionary<TestComponentSimple> l = new ReferencedDictionary<TestComponentSimple>();
l.Add("First", c1);
l.Add("Second", new TestComponentSimple());
l.Add("Third", c1);
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"{
""First"": {
""$id"": ""1"",
""MyProperty"": 0
},
""Second"": {
""$id"": ""2"",
""MyProperty"": 0
},
""Third"": {
""$ref"": ""1""
}
}", json);
ReferencedDictionary<TestComponentSimple> d = JsonConvert.DeserializeObject<ReferencedDictionary<TestComponentSimple>>(json);
Assert.AreEqual(3, d.Count);
Assert.IsTrue(ReferenceEquals(d["First"], d["Third"]));
}
[Test]
public void ReferencedObjectItems()
{
ReferenceObject o1 = new ReferenceObject();
o1.Component1 = new TestComponentSimple { MyProperty = 1 };
o1.Component2 = o1.Component1;
o1.ComponentNotReference = new TestComponentSimple();
o1.String = "String!";
o1.Integer = int.MaxValue;
string json = JsonConvert.SerializeObject(o1, Formatting.Indented);
string expected = @"{
""Component1"": {
""$id"": ""1"",
""MyProperty"": 1
},
""Component2"": {
""$ref"": ""1""
},
""ComponentNotReference"": {
""MyProperty"": 0
},
""String"": ""String!"",
""Integer"": 2147483647
}";
Assert.AreEqual(expected, json);
ReferenceObject referenceObject = JsonConvert.DeserializeObject<ReferenceObject>(json);
Assert.IsNotNull(referenceObject);
Assert.IsTrue(ReferenceEquals(referenceObject.Component1, referenceObject.Component2));
}
}
[JsonArray(ItemIsReference = true)]
public class ReferencedList<T> : List<T>
{
}
[JsonDictionary(ItemIsReference = true)]
public class ReferencedDictionary<T> : Dictionary<string, T>
{
}
[JsonObject(ItemIsReference = true)]
public class ReferenceObject
{
public TestComponentSimple Component1 { get; set; }
public TestComponentSimple Component2 { get; set; }
[JsonProperty(IsReference = false)]
public TestComponentSimple ComponentNotReference { get; set; }
public string String { get; set; }
public int Integer { get; set; }
}
}
@@ -0,0 +1,123 @@
using System.Collections.Generic;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests.Serialization
{
[TestFixture]
public class ReferenceLoopHandlingTests : TestFixtureBase
{
[Test]
public void ReferenceLoopHandlingTest()
{
JsonPropertyAttribute attribute = new JsonPropertyAttribute();
Assert.AreEqual(null, attribute._defaultValueHandling);
Assert.AreEqual(ReferenceLoopHandling.Error, attribute.ReferenceLoopHandling);
attribute.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
Assert.AreEqual(ReferenceLoopHandling.Ignore, attribute._referenceLoopHandling);
Assert.AreEqual(ReferenceLoopHandling.Ignore, attribute.ReferenceLoopHandling);
}
[Test]
public void IgnoreObjectReferenceLoop()
{
ReferenceLoopHandlingObjectContainerAttribute o = new ReferenceLoopHandlingObjectContainerAttribute();
o.Value = o;
string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
});
Assert.AreEqual("{}", json);
}
[Test]
public void IgnoreObjectReferenceLoopWithPropertyOverride()
{
ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride o = new ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride();
o.Value = o;
string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
});
Assert.AreEqual(@"{
""Value"": {
""Value"": {
""Value"": {
""Value"": {
""Value"": {
""Value"": null
}
}
}
}
}
}", json);
}
[Test]
public void IgnoreArrayReferenceLoop()
{
ReferenceLoopHandlingList a = new ReferenceLoopHandlingList();
a.Add(a);
string json = JsonConvert.SerializeObject(a, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
});
Assert.AreEqual("[]", json);
}
[Test]
public void IgnoreDictionaryReferenceLoop()
{
ReferenceLoopHandlingDictionary d = new ReferenceLoopHandlingDictionary();
d.Add("First", d);
string json = JsonConvert.SerializeObject(d, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
});
Assert.AreEqual("{}", json);
}
}
[JsonArray(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
public class ReferenceLoopHandlingList : List<ReferenceLoopHandlingList>
{
}
[JsonDictionary(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
public class ReferenceLoopHandlingDictionary : Dictionary<string, ReferenceLoopHandlingDictionary>
{
}
[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
public class ReferenceLoopHandlingObjectContainerAttribute
{
public ReferenceLoopHandlingObjectContainerAttribute Value { get; set; }
}
[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
public class ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride
{
private ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride _value;
private int _getCount;
[JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
public ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride Value
{
get
{
if (_getCount < 5)
{
_getCount++;
return _value;
}
return null;
}
set { _value = value; }
}
}
}
@@ -628,7 +628,6 @@ namespace Newtonsoft.Json.Tests.Serialization
testObject.AnotherTestMember[1].Add(new ContentSubClass("Second One"));
testObject.AThirdTestMember = new ContentSubClass("Third One");
JsonSerializer serializingTester = new JsonSerializer();
serializingTester.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
@@ -646,29 +645,27 @@ namespace Newtonsoft.Json.Tests.Serialization
string dictionaryRef = ReflectionUtils.GetTypeName(typeof(Dictionary<int, IList<ContentBaseClass>>), FormatterAssemblyStyle.Simple);
string listRef = ReflectionUtils.GetTypeName(typeof(List<ContentBaseClass>), FormatterAssemblyStyle.Simple);
Assert.AreEqual(@"{
string expected = @"{
""TestMember"": {
""$type"": """ + contentSubClassRef + @""",
""SomeString"": ""First One""
},
""AnotherTestMember"": {
""$type"": """ + dictionaryRef + @""",
""1"": {
""$type"": """ + listRef + @""",
""$values"": [
{
""$type"": """ + contentSubClassRef + @""",
""SomeString"": ""Second One""
}
]
}
""1"": [
{
""$type"": """ + contentSubClassRef + @""",
""SomeString"": ""Second One""
}
]
},
""AThirdTestMember"": {
""$type"": """ + contentSubClassRef + @""",
""SomeString"": ""Third One""
}
}", json);
}";
Assert.AreEqual(expected, json);
Console.WriteLine(json);
StringReader sr = new StringReader(json);
@@ -1013,6 +1010,129 @@ namespace Newtonsoft.Json.Tests.Serialization
}
}
#endif
[Test]
public void TypeNameIntList()
{
TypeNameList<int> l = new TypeNameList<int>();
l.Add(1);
l.Add(2);
l.Add(3);
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"[
1,
2,
3
]", json);
}
[Test]
public void TypeNameComponentList()
{
var c1 = new TestComponentSimple();
TypeNameList<object> l = new TypeNameList<object>();
l.Add(c1);
l.Add(new Employee
{
BirthDate = new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc),
Department = "Department!"
});
l.Add("String!");
l.Add(long.MaxValue);
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"[
{
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 0
},
{
""$type"": ""Newtonsoft.Json.Tests.TestObjects.Employee, Newtonsoft.Json.Tests"",
""FirstName"": null,
""LastName"": null,
""BirthDate"": ""2000-12-12T12:12:12Z"",
""Department"": ""Department!"",
""JobTitle"": null
},
""String!"",
9223372036854775807
]", json);
TypeNameList<object> l2 = JsonConvert.DeserializeObject<TypeNameList<object>>(json);
Assert.AreEqual(4, l2.Count);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), l2[0]);
CustomAssert.IsInstanceOfType(typeof(Employee), l2[1]);
CustomAssert.IsInstanceOfType(typeof(string), l2[2]);
CustomAssert.IsInstanceOfType(typeof(long), l2[3]);
}
[Test]
public void TypeNameDictionary()
{
TypeNameDictionary<object> l = new TypeNameDictionary<object>();
l.Add("First", new TestComponentSimple { MyProperty = 1 });
l.Add("Second", "String!");
l.Add("Third", long.MaxValue);
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"{
""First"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
""Second"": ""String!"",
""Third"": 9223372036854775807
}", json);
TypeNameDictionary<object> l2 = JsonConvert.DeserializeObject<TypeNameDictionary<object>>(json);
Assert.AreEqual(3, l2.Count);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), l2["First"]);
Assert.AreEqual(1, ((TestComponentSimple)l2["First"]).MyProperty);
CustomAssert.IsInstanceOfType(typeof(string), l2["Second"]);
CustomAssert.IsInstanceOfType(typeof(long), l2["Third"]);
}
[Test]
public void TypeNameObjectItems()
{
TypeNameObject o1 = new TypeNameObject();
o1.Object1 = new TestComponentSimple { MyProperty = 1 };
o1.Object2 = 123;
o1.ObjectNotHandled = new TestComponentSimple { MyProperty = int.MaxValue };
o1.String = "String!";
o1.Integer = int.MaxValue;
string json = JsonConvert.SerializeObject(o1, Formatting.Indented);
string expected = @"{
""Object1"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
""Object2"": 123,
""ObjectNotHandled"": {
""MyProperty"": 2147483647
},
""String"": ""String!"",
""Integer"": 2147483647
}";
Assert.AreEqual(expected, json);
TypeNameObject o2 = JsonConvert.DeserializeObject<TypeNameObject>(json);
Assert.IsNotNull(o2);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), o2.Object1);
Assert.AreEqual(1, ((TestComponentSimple)o2.Object1).MyProperty);
CustomAssert.IsInstanceOfType(typeof(long), o2.Object2);
CustomAssert.IsInstanceOfType(typeof(JObject), o2.ObjectNotHandled);
Assert.AreEqual(@"{
""MyProperty"": 2147483647
}", o2.ObjectNotHandled.ToString());
}
}
public class Message
@@ -1102,4 +1222,25 @@ namespace Newtonsoft.Json.Tests.Serialization
}
}
#endif
[JsonArray(ItemTypeNameHandling = TypeNameHandling.All)]
public class TypeNameList<T> : List<T>
{
}
[JsonDictionary(ItemTypeNameHandling = TypeNameHandling.All)]
public class TypeNameDictionary<T> : Dictionary<string, T>
{
}
[JsonObject(ItemTypeNameHandling = TypeNameHandling.All)]
public class TypeNameObject
{
public object Object1 { get; set; }
public object Object2 { get; set; }
[JsonProperty(TypeNameHandling = TypeNameHandling.None)]
public object ObjectNotHandled { get; set; }
public string String { get; set; }
public int Integer { get; set; }
}
}