Merged conflict

This commit is contained in:
James Newton-King
2013-04-12 12:23:13 +12:00
127 changed files with 2054 additions and 1175 deletions
@@ -158,6 +158,249 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(@"{""selectList"":[{""Value"":""Hello""}]}", json);
}
#if !NET20
[DataContract]
public class BaseDataContractWithHidden
{
[DataMember(Name = "virtualMember")]
public virtual string VirtualMember { get; set; }
[DataMember(Name = "nonVirtualMember")]
public string NonVirtualMember { get; set; }
public virtual object NewMember { get; set; }
}
public class ChildDataContractWithHidden : BaseDataContractWithHidden
{
[DataMember(Name = "NewMember")]
public virtual new string NewMember { get; set; }
public override string VirtualMember { get; set; }
public string AddedMember { get; set; }
}
[Test]
public void ChildDataContractTestWithHidden()
{
var cc = new ChildDataContractWithHidden
{
VirtualMember = "VirtualMember!",
NonVirtualMember = "NonVirtualMember!",
NewMember = "NewMember!"
};
string result = JsonConvert.SerializeObject(cc);
Assert.AreEqual(@"{""NewMember"":""NewMember!"",""virtualMember"":""VirtualMember!"",""nonVirtualMember"":""NonVirtualMember!""}", result);
}
// ignore hiding members compiler warning
#pragma warning disable 108,114
[DataContract]
public class BaseWithContract
{
[DataMember(Name = "VirtualWithDataMemberBase")]
public virtual string VirtualWithDataMember { get; set; }
[DataMember]
public virtual string Virtual { get; set; }
[DataMember(Name = "WithDataMemberBase")]
public string WithDataMember { get; set; }
[DataMember]
public string JustAProperty { get; set; }
}
[DataContract]
public class BaseWithoutContract
{
[DataMember(Name = "VirtualWithDataMemberBase")]
public virtual string VirtualWithDataMember { get; set; }
[DataMember]
public virtual string Virtual { get; set; }
[DataMember(Name = "WithDataMemberBase")]
public string WithDataMember { get; set; }
[DataMember]
public string JustAProperty { get; set; }
}
[DataContract]
public class SubWithoutContractNewProperties : BaseWithContract
{
[DataMember(Name = "VirtualWithDataMemberSub")]
public string VirtualWithDataMember { get; set; }
public string Virtual { get; set; }
[DataMember(Name = "WithDataMemberSub")]
public string WithDataMember { get; set; }
public string JustAProperty { get; set; }
}
[DataContract]
public class SubWithoutContractVirtualProperties : BaseWithContract
{
public override string VirtualWithDataMember { get; set; }
[DataMember(Name = "VirtualSub")]
public override string Virtual { get; set; }
}
[DataContract]
public class SubWithContractNewProperties : BaseWithContract
{
[DataMember(Name = "VirtualWithDataMemberSub")]
public string VirtualWithDataMember { get; set; }
[DataMember(Name = "Virtual2")]
public string Virtual { get; set; }
[DataMember(Name = "WithDataMemberSub")]
public string WithDataMember { get; set; }
[DataMember(Name = "JustAProperty2")]
public string JustAProperty { get; set; }
}
[DataContract]
public class SubWithContractVirtualProperties : BaseWithContract
{
[DataMember(Name = "VirtualWithDataMemberSub")]
public virtual string VirtualWithDataMember { get; set; }
}
#pragma warning restore 108,114
[Test]
public void SubWithoutContractNewPropertiesTest()
{
BaseWithContract baseWith = new SubWithoutContractNewProperties
{
JustAProperty = "JustAProperty!",
Virtual = "Virtual!",
VirtualWithDataMember = "VirtualWithDataMember!",
WithDataMember = "WithDataMember!"
};
baseWith.JustAProperty = "JustAProperty2!";
baseWith.Virtual = "Virtual2!";
baseWith.VirtualWithDataMember = "VirtualWithDataMember2!";
baseWith.WithDataMember = "WithDataMember2!";
string json = AssertSerializeDeserializeEqual(baseWith);
Assert.AreEqual(@"{
""JustAProperty"": ""JustAProperty2!"",
""Virtual"": ""Virtual2!"",
""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"",
""VirtualWithDataMemberSub"": ""VirtualWithDataMember!"",
""WithDataMemberBase"": ""WithDataMember2!"",
""WithDataMemberSub"": ""WithDataMember!""
}", json);
}
[Test]
public void SubWithoutContractVirtualPropertiesTest()
{
BaseWithContract baseWith = new SubWithoutContractVirtualProperties
{
JustAProperty = "JustAProperty!",
Virtual = "Virtual!",
VirtualWithDataMember = "VirtualWithDataMember!",
WithDataMember = "WithDataMember!"
};
baseWith.JustAProperty = "JustAProperty2!";
baseWith.Virtual = "Virtual2!";
baseWith.VirtualWithDataMember = "VirtualWithDataMember2!";
baseWith.WithDataMember = "WithDataMember2!";
string json = JsonConvert.SerializeObject(baseWith, Formatting.Indented);
Console.WriteLine(json);
Assert.AreEqual(@"{
""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"",
""VirtualSub"": ""Virtual2!"",
""WithDataMemberBase"": ""WithDataMember2!"",
""JustAProperty"": ""JustAProperty2!""
}", json);
}
[Test]
public void SubWithContractNewPropertiesTest()
{
BaseWithContract baseWith = new SubWithContractNewProperties
{
JustAProperty = "JustAProperty!",
Virtual = "Virtual!",
VirtualWithDataMember = "VirtualWithDataMember!",
WithDataMember = "WithDataMember!"
};
baseWith.JustAProperty = "JustAProperty2!";
baseWith.Virtual = "Virtual2!";
baseWith.VirtualWithDataMember = "VirtualWithDataMember2!";
baseWith.WithDataMember = "WithDataMember2!";
string json = AssertSerializeDeserializeEqual(baseWith);
Assert.AreEqual(@"{
""JustAProperty"": ""JustAProperty2!"",
""JustAProperty2"": ""JustAProperty!"",
""Virtual"": ""Virtual2!"",
""Virtual2"": ""Virtual!"",
""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"",
""VirtualWithDataMemberSub"": ""VirtualWithDataMember!"",
""WithDataMemberBase"": ""WithDataMember2!"",
""WithDataMemberSub"": ""WithDataMember!""
}", json);
}
[Test]
public void SubWithContractVirtualPropertiesTest()
{
BaseWithContract baseWith = new SubWithContractVirtualProperties
{
JustAProperty = "JustAProperty!",
Virtual = "Virtual!",
VirtualWithDataMember = "VirtualWithDataMember!",
WithDataMember = "WithDataMember!"
};
baseWith.JustAProperty = "JustAProperty2!";
baseWith.Virtual = "Virtual2!";
baseWith.VirtualWithDataMember = "VirtualWithDataMember2!";
baseWith.WithDataMember = "WithDataMember2!";
string json = AssertSerializeDeserializeEqual(baseWith);
Assert.AreEqual(@"{
""JustAProperty"": ""JustAProperty2!"",
""Virtual"": ""Virtual2!"",
""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"",
""VirtualWithDataMemberSub"": ""VirtualWithDataMember!"",
""WithDataMemberBase"": ""WithDataMember2!""
}", json);
}
private string AssertSerializeDeserializeEqual(object o)
{
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer s = new DataContractJsonSerializer(o.GetType());
s.WriteObject(ms, o);
var data = ms.ToArray();
JObject dataContractJson = JObject.Parse(Encoding.UTF8.GetString(data, 0, data.Length));
dataContractJson = new JObject(dataContractJson.Properties().OrderBy(p => p.Name));
JObject jsonNetJson = JObject.Parse(JsonConvert.SerializeObject(o));
jsonNetJson = new JObject(jsonNetJson.Properties().OrderBy(p => p.Name));
Console.WriteLine("Results for " + o.GetType().Name);
Console.WriteLine("DataContractJsonSerializer: " + dataContractJson);
Console.WriteLine("JsonDotNetSerializer : " + jsonNetJson);
Assert.AreEqual(dataContractJson.Count, jsonNetJson.Count);
foreach (KeyValuePair<string, JToken> property in dataContractJson)
{
Assert.IsTrue(JToken.DeepEquals(jsonNetJson[property.Key], property.Value), "Property not equal: " + property.Key);
}
return jsonNetJson.ToString();
}
#endif
[Test]
public void PersonTypedObjectDeserialization()
{
@@ -826,7 +1069,7 @@ keyword such as type of business.""
ConverableMembers c = JsonConvert.DeserializeObject<ConverableMembers>(json);
Assert.AreEqual("string", c.String);
Assert.AreEqual(double.MaxValue, c.Double);
#if !(NETFX_CORE || PORTABLE)
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
Assert.AreEqual(DBNull.Value, c.DBNull);
#endif
}
@@ -1645,7 +1888,7 @@ keyword such as type of business.""
Assert.AreEqual("titleId", n.FidOrder[n.FidOrder.Count - 1]);
}
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE || PORTABLE40)
[MetadataType(typeof(OptInClassMetadata))]
public class OptInClass
{
@@ -2196,7 +2439,7 @@ Path '', line 1, position 1.",
});
}
#if !(NET35 || NET20)
#if !(NET35 || NET20 || PORTABLE40)
[Test]
public void CannotDeserializeArrayIntoDynamic()
{
@@ -2313,7 +2556,7 @@ Path '', line 1, position 2.",
{
ContractResolver = new DefaultContractResolver
{
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
IgnoreSerializableAttribute = true
#endif
}
@@ -2332,7 +2575,7 @@ Path '', line 1, position 2.",
{
ContractResolver = new DefaultContractResolver
{
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
IgnoreSerializableAttribute = true
#endif
}
@@ -2350,9 +2593,9 @@ Path '', line 1, position 2.",
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:''}", new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
IgnoreSerializableAttribute = true
{
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
IgnoreSerializableAttribute = true
#endif
}
});
@@ -2370,7 +2613,7 @@ Path '', line 1, position 2.",
{
ContractResolver = new DefaultContractResolver
{
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
IgnoreSerializableAttribute = true
#endif
}
@@ -3041,7 +3284,7 @@ Path '', line 1, position 2.",
Assert.AreEqual("value", newModelStateDictionary["key"]);
}
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
public class ISerializableTestObject : ISerializable
{
internal string _stringValue;
@@ -3538,7 +3781,7 @@ To fix this error either change the environment to be fully trusted, change the
}
#endif
#if !(NET20 || SILVERLIGHT)
#if !(NET20 || SILVERLIGHT || PORTABLE40)
[Test]
public void SerializeDeserializeXNodeProperties()
{
@@ -3569,7 +3812,7 @@ To fix this error either change the environment to be fully trusted, change the
}
#endif
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
[Test]
public void SerializeDeserializeXmlNodeProperties()
{
@@ -4425,7 +4668,7 @@ To fix this error either change the environment to be fully trusted, change the
}", json);
}
#if !(NET35 || NET20)
#if !(NET35 || NET20 || PORTABLE40)
[Test]
public void SerializeExpandoObject()
{
@@ -5112,6 +5355,7 @@ To fix this error either change the environment to be fully trusted, change the
};
string result = JsonConvert.SerializeObject(cc);
Console.WriteLine(result);
Assert.AreEqual(@"{""differentVirtualMember"":""VirtualMember!"",""nonVirtualMember"":""NonVirtualMember!""}", result);
}
@@ -5430,7 +5674,7 @@ To fix this error either change the environment to be fully trusted, change the
}
}
#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE || PORTABLE40)
[Test]
public void SerializeException1()
{
@@ -5560,7 +5804,7 @@ To fix this error either change the environment to be fully trusted, change the
}
}
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE || PORTABLE40)
[Test]
public void DeserializeConcurrentDictionary()
{
@@ -6508,7 +6752,7 @@ Parameter name: value",
Assert.AreEqual("", s);
}
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
[Test]
public void SerializeAndDeserializeWithAttributes()
{
@@ -6674,7 +6918,7 @@ Parameter name: value",
});
}
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
[Test]
public void DeserializeException()
{
@@ -6712,7 +6956,7 @@ Parameter name: value",
}
#endif
[Test]
[Test]
public void AdditionalContentAfterFinish()
{
ExceptionAssert.Throws<JsonException>(
@@ -6930,7 +7174,7 @@ Parameter name: value",
Assert.AreEqual("[1.1,0.0,0.0]", json);
}
#if !(NET20 || NET35 || NET40 || SILVERLIGHT)
#if !(NET20 || NET35 || NET40 || SILVERLIGHT || PORTABLE40)
#if !PORTABLE
[Test]
public void DeserializeReadOnlyListWithBigInteger()
@@ -6992,7 +7236,7 @@ Parameter name: value",
obj = JsonConvert.DeserializeObject<MyTuple<int>>(json);
};
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
doStuff();
Assert.AreEqual(500, obj.Item1);
#else
@@ -7025,7 +7269,7 @@ Parameter name: value",
}
#endif
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || NET35 || NET20)
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || NET35 || NET20 || PORTABLE40)
[Test]
public void SerializeTupleWithSerializableAttribute()
{
@@ -7052,6 +7296,134 @@ Parameter name: value",
}
#endif
#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40)
public class PopulateReadOnlyTestClass
{
public IList<int> NonReadOnlyList { get; set; }
public IDictionary<string, int> NonReadOnlyDictionary { get; set; }
public IList<int> Array { get; set; }
public IList<int> List { get; set; }
public IDictionary<string, int> Dictionary { get; set; }
public IReadOnlyCollection<int> IReadOnlyCollection { get; set; }
public ReadOnlyCollection<int> ReadOnlyCollection { get; set; }
public IReadOnlyList<int> IReadOnlyList { get; set; }
public IReadOnlyDictionary<string, int> IReadOnlyDictionary { get; set; }
public ReadOnlyDictionary<string, int> ReadOnlyDictionary { get; set; }
public PopulateReadOnlyTestClass()
{
NonReadOnlyList = new List<int> { 1 };
NonReadOnlyDictionary = new Dictionary<string, int> { { "first", 2 } };
Array = new[] {3};
List = new ReadOnlyCollection<int>(new[] { 4 });
Dictionary = new ReadOnlyDictionary<string, int>(new Dictionary<string, int> { { "first", 5 } });
IReadOnlyCollection = new ReadOnlyCollection<int>(new[] { 6 });
ReadOnlyCollection = new ReadOnlyCollection<int>(new[] { 7 });
IReadOnlyList = new ReadOnlyCollection<int>(new[] { 8 });
IReadOnlyDictionary = new ReadOnlyDictionary<string, int>(new Dictionary<string, int> { { "first", 9 } });
ReadOnlyDictionary = new ReadOnlyDictionary<string, int>(new Dictionary<string, int> { { "first", 10 } });
}
}
[Test]
public void SerializeReadOnlyCollections()
{
PopulateReadOnlyTestClass c1 = new PopulateReadOnlyTestClass();
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
Assert.AreEqual(@"{
""NonReadOnlyList"": [
1
],
""NonReadOnlyDictionary"": {
""first"": 2
},
""Array"": [
3
],
""List"": [
4
],
""Dictionary"": {
""first"": 5
},
""IReadOnlyCollection"": [
6
],
""ReadOnlyCollection"": [
7
],
""IReadOnlyList"": [
8
],
""IReadOnlyDictionary"": {
""first"": 9
},
""ReadOnlyDictionary"": {
""first"": 10
}
}", json);
}
[Test]
public void PopulateReadOnlyCollections()
{
string json = @"{
""NonReadOnlyList"": [
11
],
""NonReadOnlyDictionary"": {
""first"": 12
},
""Array"": [
13
],
""List"": [
14
],
""Dictionary"": {
""first"": 15
},
""IReadOnlyCollection"": [
16
],
""ReadOnlyCollection"": [
17
],
""IReadOnlyList"": [
18
],
""IReadOnlyDictionary"": {
""first"": 19
},
""ReadOnlyDictionary"": {
""first"": 20
}
}";
var c2 = JsonConvert.DeserializeObject<PopulateReadOnlyTestClass>(json);
Assert.AreEqual(1, c2.NonReadOnlyDictionary.Count);
Assert.AreEqual(12, c2.NonReadOnlyDictionary["first"]);
Assert.AreEqual(2, c2.NonReadOnlyList.Count);
Assert.AreEqual(1, c2.NonReadOnlyList[0]);
Assert.AreEqual(11, c2.NonReadOnlyList[1]);
Assert.AreEqual(1, c2.Array.Count);
Assert.AreEqual(13, c2.Array[0]);
}
#endif
[Test]
public void SerializeArray2D()
{
@@ -7713,7 +8085,7 @@ Parameter name: value",
}", json);
}
#if !(PORTABLE || NET35 || NET20 || SILVERLIGHT)
#if !(PORTABLE || NET35 || NET20 || SILVERLIGHT || PORTABLE40)
[Test]
public void ReadTooLargeInteger()
{
@@ -7807,7 +8179,7 @@ Parameter name: value",
}
#endif
#if !(PORTABLE || NET20 || NET35)
#if !(NET20 || NET35 || PORTABLE40)
[Test]
public void HashSetInterface()
{
@@ -7880,7 +8252,7 @@ Parameter name: value",
Assert.AreEqual(1234567890.123456m, d);
}
#if !(PORTABLE || SILVERLIGHT || NETFX_CORE || WINDOWS_PHONE)
#if !(PORTABLE || SILVERLIGHT || NETFX_CORE || WINDOWS_PHONE || PORTABLE40)
[Test]
public void DontSerializeStaticFields()
{
@@ -7914,7 +8286,7 @@ Parameter name: value",
Assert.AreEqual("string!", l[0]);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
[Test]
public void SerializeBigInteger()
{
@@ -7928,7 +8300,7 @@ Parameter name: value",
}
#endif
#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE)
#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40)
[Test]
public void DeserializeReadOnlyListInterface()
{