-Added JsonDictionaryAttribute
-Added JsonContainerContract -Added support for customizing the JsonConverter, type name handling, reference handling of collection items
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
|
||||
using System;
|
||||
#if !NETFX_CORE
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
#else
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
@@ -32,6 +33,7 @@ using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttrib
|
||||
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
|
||||
#endif
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Converters
|
||||
@@ -129,5 +131,118 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
DateTime result = JsonConvert.DeserializeObject<DateTime>("new Date(976918263055)", converter);
|
||||
Assert.AreEqual(new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConverterList()
|
||||
{
|
||||
ConverterList<object> l1 = new ConverterList<object>();
|
||||
l1.Add(new DateTime(2000, 12, 12, 20, 10, 0, DateTimeKind.Utc));
|
||||
l1.Add(new DateTime(1983, 10, 9, 23, 10, 0, DateTimeKind.Utc));
|
||||
|
||||
string json = JsonConvert.SerializeObject(l1, Formatting.Indented);
|
||||
Assert.AreEqual(@"[
|
||||
new Date(
|
||||
976651800000
|
||||
),
|
||||
new Date(
|
||||
434589000000
|
||||
)
|
||||
]", json);
|
||||
|
||||
ConverterList<object> l2 = JsonConvert.DeserializeObject<ConverterList<object>>(json);
|
||||
Assert.IsNotNull(l2);
|
||||
|
||||
Assert.AreEqual(new DateTime(2000, 12, 12, 20, 10, 0, DateTimeKind.Utc), l2[0]);
|
||||
Assert.AreEqual(new DateTime(1983, 10, 9, 23, 10, 0, DateTimeKind.Utc), l2[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConverterDictionary()
|
||||
{
|
||||
ConverterDictionary<object> l1 = new ConverterDictionary<object>();
|
||||
l1.Add("First", new DateTime(2000, 12, 12, 20, 10, 0, DateTimeKind.Utc));
|
||||
l1.Add("Second", new DateTime(1983, 10, 9, 23, 10, 0, DateTimeKind.Utc));
|
||||
|
||||
string json = JsonConvert.SerializeObject(l1, Formatting.Indented);
|
||||
Assert.AreEqual(@"{
|
||||
""First"": new Date(
|
||||
976651800000
|
||||
),
|
||||
""Second"": new Date(
|
||||
434589000000
|
||||
)
|
||||
}", json);
|
||||
|
||||
ConverterDictionary<object> l2 = JsonConvert.DeserializeObject<ConverterDictionary<object>>(json);
|
||||
Assert.IsNotNull(l2);
|
||||
|
||||
Assert.AreEqual(new DateTime(2000, 12, 12, 20, 10, 0, DateTimeKind.Utc), l2["First"]);
|
||||
Assert.AreEqual(new DateTime(1983, 10, 9, 23, 10, 0, DateTimeKind.Utc), l2["Second"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConverterObject()
|
||||
{
|
||||
ConverterObject l1 = new ConverterObject();
|
||||
l1.Object1 = new DateTime(2000, 12, 12, 20, 10, 0, DateTimeKind.Utc);
|
||||
l1.Object2 = null;
|
||||
l1.ObjectNotHandled = new DateTime(2000, 12, 12, 20, 10, 0, DateTimeKind.Utc);
|
||||
|
||||
string json = JsonConvert.SerializeObject(l1, Formatting.Indented);
|
||||
Assert.AreEqual(@"{
|
||||
""Object1"": new Date(
|
||||
976651800000
|
||||
),
|
||||
""Object2"": null,
|
||||
""ObjectNotHandled"": 631122486000000000
|
||||
}", json);
|
||||
|
||||
ConverterObject l2 = JsonConvert.DeserializeObject<ConverterObject>(json);
|
||||
Assert.IsNotNull(l2);
|
||||
|
||||
//Assert.AreEqual(new DateTime(2000, 12, 12, 20, 10, 0, DateTimeKind.Utc), l2["First"]);
|
||||
//Assert.AreEqual(new DateTime(1983, 10, 9, 23, 10, 0, DateTimeKind.Utc), l2["Second"]);
|
||||
}
|
||||
}
|
||||
|
||||
[JsonArray(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
|
||||
public class ConverterList<T> : List<T>
|
||||
{
|
||||
}
|
||||
|
||||
[JsonDictionary(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
|
||||
public class ConverterDictionary<T> : Dictionary<string, T>
|
||||
{
|
||||
}
|
||||
|
||||
[JsonObject(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
|
||||
public class ConverterObject
|
||||
{
|
||||
public object Object1 { get; set; }
|
||||
public object Object2 { get; set; }
|
||||
[JsonConverter(typeof(DateIntConverter))]
|
||||
public object ObjectNotHandled { get; set; }
|
||||
}
|
||||
|
||||
public class DateIntConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
DateTime? d = (DateTime?) value;
|
||||
if (d == null)
|
||||
writer.WriteNull();
|
||||
else
|
||||
writer.WriteValue(d.Value.Ticks);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
return new DateTime(Convert.ToInt64(reader.Value), DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof (DateTime) || objectType == typeof (DateTime?);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,17 +75,5 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.AreEqual(DefaultValueHandling.Ignore, attribute._defaultValueHandling);
|
||||
Assert.AreEqual(DefaultValueHandling.Ignore, attribute.DefaultValueHandling);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,70 +50,6 @@ namespace Newtonsoft.Json.Tests
|
||||
[TestFixture]
|
||||
public class JsonConvertTest : TestFixtureBase
|
||||
{
|
||||
#if Entities
|
||||
[Test]
|
||||
public void EntitiesTest()
|
||||
{
|
||||
Purchase purchase = new Purchase() { Id = 1 };
|
||||
purchase.PurchaseLine.Add(new PurchaseLine() { Id = 1, Purchase = purchase });
|
||||
purchase.PurchaseLine.Add(new PurchaseLine() { Id = 2, Purchase = purchase });
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||
|
||||
using (JsonWriter jw = new JsonTextWriter(sw))
|
||||
{
|
||||
jw.Formatting = Formatting.Indented;
|
||||
|
||||
serializer.Serialize(jw, purchase);
|
||||
}
|
||||
|
||||
string json = sw.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Id"": 1,
|
||||
""PurchaseLine"": [
|
||||
{
|
||||
""Id"": 1,
|
||||
""PurchaseReference"": {
|
||||
""EntityKey"": null,
|
||||
""RelationshipName"": ""EntityDataModel.PurchasePurchaseLine"",
|
||||
""SourceRoleName"": ""PurchaseLine"",
|
||||
""TargetRoleName"": ""Purchase"",
|
||||
""RelationshipSet"": null,
|
||||
""IsLoaded"": false
|
||||
},
|
||||
""EntityState"": 1,
|
||||
""EntityKey"": null
|
||||
},
|
||||
{
|
||||
""Id"": 2,
|
||||
""PurchaseReference"": {
|
||||
""EntityKey"": null,
|
||||
""RelationshipName"": ""EntityDataModel.PurchasePurchaseLine"",
|
||||
""SourceRoleName"": ""PurchaseLine"",
|
||||
""TargetRoleName"": ""Purchase"",
|
||||
""RelationshipSet"": null,
|
||||
""IsLoaded"": false
|
||||
},
|
||||
""EntityState"": 1,
|
||||
""EntityKey"": null
|
||||
}
|
||||
],
|
||||
""EntityState"": 1,
|
||||
""EntityKey"": null
|
||||
}", json);
|
||||
|
||||
Purchase newPurchase = JsonConvert.DeserializeObject<Purchase>(json);
|
||||
Assert.AreEqual(1, newPurchase.Id);
|
||||
|
||||
Assert.AreEqual(2, newPurchase.PurchaseLine.Count);
|
||||
Assert.AreEqual(1, newPurchase.PurchaseLine.ElementAt(0).Id);
|
||||
Assert.AreEqual(2, newPurchase.PurchaseLine.ElementAt(1).Id);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void DeserializeObject_EmptyString()
|
||||
{
|
||||
|
||||
@@ -93,7 +93,12 @@
|
||||
<Compile Include="Linq\JPathTests.cs" />
|
||||
<Compile Include="Linq\JRawTests.cs" />
|
||||
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
|
||||
<Compile Include="Serialization\ContractResolverTests.cs" />
|
||||
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
|
||||
<Compile Include="Serialization\DynamicTests.cs" />
|
||||
<Compile Include="Serialization\JsonPropertyCollectionTests.cs" />
|
||||
<Compile Include="Serialization\NullValueHandlingTests.cs" />
|
||||
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
|
||||
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
|
||||
<Compile Include="TestObjects\Bar.cs" />
|
||||
<Compile Include="TestObjects\Car.cs" />
|
||||
|
||||
@@ -138,7 +138,9 @@
|
||||
<Compile Include="Serialization\ContractResolverTests.cs" />
|
||||
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
|
||||
<Compile Include="Serialization\DynamicTests.cs" />
|
||||
<Compile Include="Serialization\JsonPropertyCollectionTests.cs" />
|
||||
<Compile Include="Serialization\NullValueHandlingTests.cs" />
|
||||
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
|
||||
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
|
||||
<Compile Include="TestObjects\Bar.cs" />
|
||||
<Compile Include="TestObjects\Car.cs" />
|
||||
@@ -149,8 +151,13 @@
|
||||
<Compile Include="TestObjects\ContentSubClass.cs" />
|
||||
<Compile Include="TestObjects\Foo.cs" />
|
||||
<Compile Include="TestObjects\HolderClass.cs" />
|
||||
<Compile Include="TestObjects\IPrivateImplementationA.cs" />
|
||||
<Compile Include="TestObjects\IPrivateImplementationB.cs" />
|
||||
<Compile Include="TestObjects\IPrivateOverriddenImplementation.cs" />
|
||||
<Compile Include="TestObjects\ListOfIds.cs" />
|
||||
<Compile Include="TestObjects\Movie.cs" />
|
||||
<Compile Include="TestObjects\PrivateImplementationAClass.cs" />
|
||||
<Compile Include="TestObjects\PrivateImplementationBClass.cs" />
|
||||
<Compile Include="TestObjects\PublicParametizedConstructorRequiringConverterTestClass.cs" />
|
||||
<Compile Include="TestObjects\PublicParametizedConstructorTestClass.cs" />
|
||||
<Compile Include="TestObjects\PersonError.cs" />
|
||||
|
||||
@@ -136,6 +136,7 @@
|
||||
<Compile Include="Serialization\NullValueHandlingTests.cs" />
|
||||
<Compile Include="Serialization\PopulateTests.cs" />
|
||||
<Compile Include="Serialization\PreserveReferencesHandlingTests.cs" />
|
||||
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
|
||||
<Compile Include="Serialization\SerializationErrorHandlingTests.cs" />
|
||||
<Compile Include="Serialization\SerializationEventAttributeTests.cs" />
|
||||
<Compile Include="Serialization\TypeNameHandlingTests.cs" />
|
||||
|
||||
@@ -99,6 +99,7 @@
|
||||
<Compile Include="Serialization\NullValueHandlingTests.cs" />
|
||||
<Compile Include="Serialization\PopulateTests.cs" />
|
||||
<Compile Include="Serialization\PreserveReferencesHandlingTests.cs" />
|
||||
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
|
||||
<Compile Include="Serialization\SerializationErrorHandlingTests.cs" />
|
||||
<Compile Include="Serialization\SerializationEventAttributeTests.cs" />
|
||||
<Compile Include="Serialization\TypeNameHandlingTests.cs" />
|
||||
|
||||
@@ -144,6 +144,7 @@
|
||||
<Compile Include="Serialization\ContractResolverTests.cs" />
|
||||
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
|
||||
<Compile Include="Serialization\DynamicTests.cs" />
|
||||
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
|
||||
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
|
||||
<Compile Include="Serialization\JsonPropertyCollectionTests.cs" />
|
||||
<Compile Include="Serialization\NullValueHandlingTests.cs" />
|
||||
|
||||
@@ -72,5 +72,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.3.14821")]
|
||||
[assembly: AssemblyFileVersion("4.5.3.14822")]
|
||||
#endif
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user