-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:
@@ -397,5 +397,145 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
CustomAssert.IsInstanceOfType(typeof(DateTimeOffset), startDateTime);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void ConvertsToBoolean()
|
||||
{
|
||||
Assert.AreEqual(true, Convert.ToBoolean(new JValue(true)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToBoolean_String()
|
||||
{
|
||||
Assert.AreEqual(true, Convert.ToBoolean(new JValue("true")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToInt32()
|
||||
{
|
||||
Assert.AreEqual(Int32.MaxValue, Convert.ToInt32(new JValue(Int32.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToChar()
|
||||
{
|
||||
Assert.AreEqual('c', Convert.ToChar(new JValue('c')));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToSByte()
|
||||
{
|
||||
Assert.AreEqual(SByte.MaxValue, Convert.ToSByte(new JValue(SByte.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToByte()
|
||||
{
|
||||
Assert.AreEqual(Byte.MaxValue, Convert.ToByte(new JValue(Byte.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToInt16()
|
||||
{
|
||||
Assert.AreEqual(Int16.MaxValue, Convert.ToInt16(new JValue(Int16.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToUInt16()
|
||||
{
|
||||
Assert.AreEqual(UInt16.MaxValue, Convert.ToUInt16(new JValue(UInt16.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToUInt32()
|
||||
{
|
||||
Assert.AreEqual(UInt32.MaxValue, Convert.ToUInt32(new JValue(UInt32.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToInt64()
|
||||
{
|
||||
Assert.AreEqual(Int64.MaxValue, Convert.ToInt64(new JValue(Int64.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToUInt64()
|
||||
{
|
||||
Assert.AreEqual(UInt64.MaxValue, Convert.ToUInt64(new JValue(UInt64.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToSingle()
|
||||
{
|
||||
Assert.AreEqual(Single.MaxValue, Convert.ToSingle(new JValue(Single.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToDouble()
|
||||
{
|
||||
Assert.AreEqual(Double.MaxValue, Convert.ToDouble(new JValue(Double.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToDecimal()
|
||||
{
|
||||
Assert.AreEqual(Decimal.MaxValue, Convert.ToDecimal(new JValue(Decimal.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToString_Decimal()
|
||||
{
|
||||
Assert.AreEqual("79228162514264337593543950335", Convert.ToString(new JValue(Decimal.MaxValue)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToString_Uri()
|
||||
{
|
||||
Assert.AreEqual("http://www.google.com/", Convert.ToString(new JValue(new Uri("http://www.google.com"))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToString_Null()
|
||||
{
|
||||
Assert.AreEqual(string.Empty, Convert.ToString(new JValue((object)null)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToString_Guid()
|
||||
{
|
||||
Guid g = new Guid("0B5D4F85-E94C-4143-94C8-35F2AAEBB100");
|
||||
|
||||
Assert.AreEqual("0b5d4f85-e94c-4143-94c8-35f2aaebb100", Convert.ToString(new JValue(g)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToType()
|
||||
{
|
||||
Assert.AreEqual(Int32.MaxValue, Convert.ChangeType(new JValue(Int32.MaxValue), typeof(Int32), CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsToDateTime()
|
||||
{
|
||||
Assert.AreEqual(new DateTime(2013, 02, 01, 01, 02, 03, 04), Convert.ToDateTime(new JValue(new DateTime(2013, 02, 01, 01, 02, 03, 04))));
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
public void ConvertsToDateTime_DateTimeOffset()
|
||||
{
|
||||
var offset = new DateTimeOffset(2013, 02, 01, 01, 02, 03, 04, TimeSpan.Zero);
|
||||
|
||||
Assert.AreEqual(new DateTime(2013, 02, 01, 01, 02, 03, 04), Convert.ToDateTime(new JValue(offset)));
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void GetTypeCode()
|
||||
{
|
||||
IConvertible v = new JValue(new Guid("0B5D4F85-E94C-4143-94C8-35F2AAEBB100"));
|
||||
|
||||
Assert.AreEqual(TypeCode.Object, v.GetTypeCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\Net20\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<DefineConstants>TRACE;NET20</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
|
||||
@@ -73,4 +73,4 @@ using System.Security;
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
[assembly: AssemblyFileVersion("4.5.11.15906")]
|
||||
[assembly: AssemblyFileVersion("4.5.11.15911")]
|
||||
|
||||
@@ -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