diff options
author | Alexis Christoforides <alexis@thenull.net> | 2014-10-08 03:18:25 +0400 |
---|---|---|
committer | Alexis Christoforides <alexis@thenull.net> | 2014-10-08 03:24:41 +0400 |
commit | fbea92153684c2db093a44830818d226fdb17862 (patch) | |
tree | bba4c03b5d97c2fda0bbdd3212a1e12d4a60835c /mcs/class/System.ServiceModel.Web/Test | |
parent | 478f99f39832e29748d6e641608a38df41658d11 (diff) |
[tests] Add tests for Bug #23058 and polymorphism/known types in DataContractJsonSerializer.
Diffstat (limited to 'mcs/class/System.ServiceModel.Web/Test')
-rw-r--r-- | mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs b/mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs index 68d9fb6a083..8a11d70baf1 100644 --- a/mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs +++ b/mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs @@ -1191,6 +1191,15 @@ namespace MonoTests.System.Runtime.Serialization.Json return ser.ReadObject (xr); } + public T Deserialize<T>(string json) + { + var bytes = Encoding.Unicode.GetBytes (json); + using (MemoryStream stream = new MemoryStream (bytes)) { + var serializer = new DataContractJsonSerializer (typeof(T)); + return (T)serializer.ReadObject (stream); + } + } + [Test] public void IsStartObject () { @@ -1819,6 +1828,50 @@ namespace MonoTests.System.Runtime.Serialization.Json serializer.WriteObject (stream, o); } + // properly deserialize object with a polymorphic property (known derived type) + [Test] + public void Bug23058() + { + string serializedObj = @"{""PolymorphicProperty"":{""__type"":""KnownDerivedType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base"",""DerivedProperty"":""Derived 1""},""Name"":""Parent2""}"; + ParentType deserializedObj = Deserialize<ParentType> (serializedObj); + + Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.KnownDerivedType"); + Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base"); + Assert.AreEqual ((deserializedObj.PolymorphicProperty as KnownDerivedType).DerivedProperty, "Derived 1"); + Assert.AreEqual (deserializedObj.Name, "Parent2"); + } + + // properly deserialize object with a polymorphic property (base type with __type hint) + [Test] + public void DeserializeBaseTypePropHint() + { + string serializedObj = @"{""PolymorphicProperty"":{""__type"":""BaseType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base""},""Name"":""Parent2""}"; + ParentType deserializedObj = Deserialize<ParentType> (serializedObj); + + Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.BaseType"); + Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base"); + } + + // properly deserialize object with a polymorphic property (base type with __type hint) + [Test] + public void DeserializeBaseTypePropNoHint() + { + string serializedObj = @"{""PolymorphicProperty"":{""BaseTypeProperty"":""Base""},""Name"":""Parent2""}"; + ParentType deserializedObj = Deserialize<ParentType> (serializedObj); + + Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.BaseType"); + Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base"); + } + + // properly fail deserializing object with a polymorphic property (unknown derived type) + [ExpectedException (typeof (SerializationException))] + [Test] + public void FailDeserializingUnknownTypeProp() + { + string serializedObj = @"{""PolymorphicProperty"":{""__type"":""UnknownDerivedType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base"",""DerivedProperty"":""Derived 1""},""Name"":""Parent2""}"; + ParentType deserializedObj = Deserialize<ParentType> (serializedObj); + } + #endregion } @@ -2037,6 +2090,42 @@ namespace MonoTests.System.Runtime.Serialization.Json public long CodedServerTimeUTC { get; set; } public DateTime ServerTimeUTC { get; set; } } + + #region polymorphism test helper classes + + [DataContract] + [KnownType (typeof (KnownDerivedType))] + public class ParentType + { + [DataMember] + public string Name { get; set; } + + [DataMember] + public BaseType PolymorphicProperty { get; set; } + } + + [DataContract] + public class BaseType + { + [DataMember] + public string BaseTypeProperty { get; set; } + } + + [DataContract] + public class KnownDerivedType : BaseType + { + [DataMemberAttribute] + public string DerivedProperty { get; set; } + } + + [DataContract] + public class UnknownDerivedType : BaseType + { + [DataMember] + public string DerivedProperty { get; set; } + } + + #endregion } [DataContract] |