-Added IConvertible support to JValue
-Changed IDictionary<DateTime, object> to be serialized in ISO date format -Fixed DataContractAttribute to not be inherited
This commit is contained in:
@@ -4923,6 +4923,25 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
|
||||
[Test]
|
||||
public void ChildDataContractTest()
|
||||
{
|
||||
ChildDataContract cc = new ChildDataContract
|
||||
{
|
||||
VirtualMember = "VirtualMember!",
|
||||
NonVirtualMember = "NonVirtualMember!"
|
||||
};
|
||||
|
||||
string result = JsonConvert.SerializeObject(cc, Formatting.Indented);
|
||||
// Assert.AreEqual(@"{
|
||||
// ""VirtualMember"": ""VirtualMember!"",
|
||||
// ""NewMember"": null,
|
||||
// ""nonVirtualMember"": ""NonVirtualMember!""
|
||||
//}", result);
|
||||
|
||||
Console.WriteLine(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChildDataContractTestWithDataContractSerializer()
|
||||
{
|
||||
ChildDataContract cc = new ChildDataContract
|
||||
{
|
||||
@@ -4930,8 +4949,14 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
NonVirtualMember = "NonVirtualMember!"
|
||||
};
|
||||
|
||||
string result = JsonConvert.SerializeObject(cc);
|
||||
Assert.AreEqual(@"{""virtualMember"":""VirtualMember!"",""nonVirtualMember"":""NonVirtualMember!""}", result);
|
||||
DataContractSerializer serializer = new DataContractSerializer(typeof(ChildDataContract));
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
serializer.WriteObject(ms, cc);
|
||||
|
||||
string xml = Encoding.UTF8.GetString(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
|
||||
|
||||
Console.WriteLine(xml);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -6226,6 +6251,106 @@ Parameter name: value",
|
||||
Assert.AreEqual(@"{""Data"":["":::ONE:::"","":::TWO:::"","":::THREE:::""]}", json);
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
public void DateTimeDictionaryKey_DateTimeOffset_Iso()
|
||||
{
|
||||
IDictionary<DateTimeOffset, int> dic1 = new Dictionary<DateTimeOffset, int>
|
||||
{
|
||||
{new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero), 1},
|
||||
{new DateTimeOffset(2013, 12, 12, 12, 12, 12, TimeSpan.Zero), 2}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dic1, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""2000-12-12T12:12:12+00:00"": 1,
|
||||
""2013-12-12T12:12:12+00:00"": 2
|
||||
}", json);
|
||||
|
||||
IDictionary<DateTimeOffset, int> dic2 = JsonConvert.DeserializeObject<IDictionary<DateTimeOffset, int>>(json);
|
||||
|
||||
Assert.AreEqual(2, dic2.Count);
|
||||
Assert.AreEqual(1, dic2[new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero)]);
|
||||
Assert.AreEqual(2, dic2[new DateTimeOffset(2013, 12, 12, 12, 12, 12, TimeSpan.Zero)]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DateTimeDictionaryKey_DateTimeOffset_MS()
|
||||
{
|
||||
IDictionary<DateTimeOffset, int> dic1 = new Dictionary<DateTimeOffset, int>
|
||||
{
|
||||
{new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero), 1},
|
||||
{new DateTimeOffset(2013, 12, 12, 12, 12, 12, TimeSpan.Zero), 2}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dic1, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""\/Date(976623132000+0000)\/"": 1,
|
||||
""\/Date(1386850332000+0000)\/"": 2
|
||||
}", json);
|
||||
|
||||
IDictionary<DateTimeOffset, int> dic2 = JsonConvert.DeserializeObject<IDictionary<DateTimeOffset, int>>(json);
|
||||
|
||||
Assert.AreEqual(2, dic2.Count);
|
||||
Assert.AreEqual(1, dic2[new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero)]);
|
||||
Assert.AreEqual(2, dic2[new DateTimeOffset(2013, 12, 12, 12, 12, 12, TimeSpan.Zero)]);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void DateTimeDictionaryKey_DateTime_Iso()
|
||||
{
|
||||
IDictionary<DateTime, int> dic1 = new Dictionary<DateTime, int>
|
||||
{
|
||||
{new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc), 1},
|
||||
{new DateTime(2013, 12, 12, 12, 12, 12, DateTimeKind.Utc), 2}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dic1, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""2000-12-12T12:12:12Z"": 1,
|
||||
""2013-12-12T12:12:12Z"": 2
|
||||
}", json);
|
||||
|
||||
IDictionary<DateTime, int> dic2 = JsonConvert.DeserializeObject<IDictionary<DateTime, int>>(json);
|
||||
|
||||
Assert.AreEqual(2, dic2.Count);
|
||||
Assert.AreEqual(1, dic2[new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)]);
|
||||
Assert.AreEqual(2, dic2[new DateTime(2013, 12, 12, 12, 12, 12, DateTimeKind.Utc)]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DateTimeDictionaryKey_DateTime_MS()
|
||||
{
|
||||
IDictionary<DateTime, int> dic1 = new Dictionary<DateTime, int>
|
||||
{
|
||||
{new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc), 1},
|
||||
{new DateTime(2013, 12, 12, 12, 12, 12, DateTimeKind.Utc), 2}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dic1, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""\/Date(976623132000)\/"": 1,
|
||||
""\/Date(1386850332000)\/"": 2
|
||||
}", json);
|
||||
|
||||
IDictionary<DateTime, int> dic2 = JsonConvert.DeserializeObject<IDictionary<DateTime, int>>(json);
|
||||
|
||||
Assert.AreEqual(2, dic2.Count);
|
||||
Assert.AreEqual(1, dic2[new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)]);
|
||||
Assert.AreEqual(2, dic2[new DateTime(2013, 12, 12, 12, 12, 12, DateTimeKind.Utc)]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEmptyJsonString()
|
||||
{
|
||||
|
||||
@@ -24,10 +24,17 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if !NETFX_CORE
|
||||
@@ -346,5 +353,170 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Console.WriteLine("Error has been fixed");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DerivedSerializationEvents()
|
||||
{
|
||||
var c = JsonConvert.DeserializeObject<DerivedSerializationEventOrderTestObject>("{}");
|
||||
|
||||
JsonConvert.SerializeObject(c, Formatting.Indented);
|
||||
|
||||
IList<string> e = c.GetEvents();
|
||||
|
||||
Assert.AreEqual(@"OnDeserializing
|
||||
OnDeserializing_Derived
|
||||
OnDeserialized
|
||||
OnDeserialized_Derived
|
||||
OnSerializing
|
||||
OnSerializing_Derived
|
||||
OnSerialized
|
||||
OnSerialized_Derived", string.Join(Environment.NewLine, e.ToArray()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DerivedDerivedSerializationEvents()
|
||||
{
|
||||
var c = JsonConvert.DeserializeObject<DerivedDerivedSerializationEventOrderTestObject>("{}");
|
||||
|
||||
JsonConvert.SerializeObject(c, Formatting.Indented);
|
||||
|
||||
IList<string> e = c.GetEvents();
|
||||
|
||||
Assert.AreEqual(@"OnDeserializing
|
||||
OnDeserializing_Derived
|
||||
OnDeserializing_Derived_Derived
|
||||
OnDeserialized
|
||||
OnDeserialized_Derived
|
||||
OnDeserialized_Derived_Derived
|
||||
OnSerializing
|
||||
OnSerializing_Derived
|
||||
OnSerializing_Derived_Derived
|
||||
OnSerialized
|
||||
OnSerialized_Derived
|
||||
OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray()));
|
||||
}
|
||||
|
||||
#if !(NET20 || SILVERLIGHT)
|
||||
[Test]
|
||||
public void DerivedDerivedSerializationEvents_DataContractSerializer()
|
||||
{
|
||||
string xml = @"<DerivedDerivedSerializationEventOrderTestObject xmlns=""http://schemas.datacontract.org/2004/07/Newtonsoft.Json.Tests.Serialization"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>";
|
||||
|
||||
DataContractSerializer ss = new DataContractSerializer(typeof(DerivedDerivedSerializationEventOrderTestObject));
|
||||
|
||||
DerivedDerivedSerializationEventOrderTestObject c = (DerivedDerivedSerializationEventOrderTestObject)ss.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
ss.WriteObject(ms, c);
|
||||
|
||||
IList<string> e = c.GetEvents();
|
||||
|
||||
Assert.AreEqual(@"OnDeserializing
|
||||
OnDeserializing_Derived
|
||||
OnDeserializing_Derived_Derived
|
||||
OnDeserialized
|
||||
OnDeserialized_Derived
|
||||
OnDeserialized_Derived_Derived
|
||||
OnSerializing
|
||||
OnSerializing_Derived
|
||||
OnSerializing_Derived_Derived
|
||||
OnSerialized
|
||||
OnSerialized_Derived
|
||||
OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray()));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public class SerializationEventOrderTestObject
|
||||
{
|
||||
protected IList<string> Events { get; private set; }
|
||||
|
||||
public SerializationEventOrderTestObject()
|
||||
{
|
||||
Events = new List<string>();
|
||||
}
|
||||
|
||||
public IList<string> GetEvents()
|
||||
{
|
||||
return Events;
|
||||
}
|
||||
|
||||
[OnSerializing]
|
||||
internal void OnSerializingMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnSerializing");
|
||||
}
|
||||
|
||||
[OnSerialized]
|
||||
internal void OnSerializedMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnSerialized");
|
||||
}
|
||||
|
||||
[OnDeserializing]
|
||||
internal void OnDeserializingMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnDeserializing");
|
||||
}
|
||||
|
||||
[OnDeserialized]
|
||||
internal void OnDeserializedMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnDeserialized");
|
||||
}
|
||||
}
|
||||
|
||||
public class DerivedSerializationEventOrderTestObject : SerializationEventOrderTestObject
|
||||
{
|
||||
[OnSerializing]
|
||||
internal new void OnSerializingMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnSerializing_Derived");
|
||||
}
|
||||
|
||||
[OnSerialized]
|
||||
internal new void OnSerializedMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnSerialized_Derived");
|
||||
}
|
||||
|
||||
[OnDeserializing]
|
||||
internal new void OnDeserializingMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnDeserializing_Derived");
|
||||
}
|
||||
|
||||
[OnDeserialized]
|
||||
internal new void OnDeserializedMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnDeserialized_Derived");
|
||||
}
|
||||
}
|
||||
|
||||
public class DerivedDerivedSerializationEventOrderTestObject : DerivedSerializationEventOrderTestObject
|
||||
{
|
||||
[OnSerializing]
|
||||
internal new void OnSerializingMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnSerializing_Derived_Derived");
|
||||
}
|
||||
|
||||
[OnSerialized]
|
||||
internal new void OnSerializedMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnSerialized_Derived_Derived");
|
||||
}
|
||||
|
||||
[OnDeserializing]
|
||||
internal new void OnDeserializingMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnDeserializing_Derived_Derived");
|
||||
}
|
||||
|
||||
[OnDeserialized]
|
||||
internal new void OnDeserializedMethod(StreamingContext context)
|
||||
{
|
||||
Events.Add("OnDeserialized_Derived_Derived");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user