diff options
author | Jonathan Pryor <jonpryor@vt.edu> | 2012-01-05 01:31:55 +0400 |
---|---|---|
committer | Jonathan Pryor <jonpryor@vt.edu> | 2012-01-05 01:35:02 +0400 |
commit | e35e45293b847cea01bbba5a3efbb5cd37bc776b (patch) | |
tree | 3dc30b2224c26aac83cb31cce66f194c9de65698 /mcs/class/System.ServiceModel.Web | |
parent | ff344b1c44dcabcc3493e667c929acf0e8a3f67a (diff) |
[System.ServiceModel.Web] Support `null` as value for `DateTime?` field.
Fixes assistly #5863.
Diffstat (limited to 'mcs/class/System.ServiceModel.Web')
2 files changed, 20 insertions, 3 deletions
diff --git a/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs b/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs index 3a5e57ff125..d15a4733fc8 100644 --- a/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs +++ b/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs @@ -72,9 +72,12 @@ namespace System.Runtime.Serialization.Json if (serialized_object_count ++ == serializer.MaxItemsInObjectGraph) throw SerializationError (String.Format ("The object graph exceeded the maximum object count '{0}' specified in the serializer", serializer.MaxItemsInObjectGraph)); - if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) + bool nullable = false; + if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) { + nullable = true; type = Nullable.GetUnderlyingType (type); - + } + bool isNull = reader.GetAttribute ("type") == "null"; switch (Type.GetTypeCode (type)) { @@ -122,8 +125,11 @@ namespace System.Runtime.Serialization.Json case TypeCode.DateTime: // it does not use ReadElementContentAsDateTime(). Different string format. var s = reader.ReadElementContentAsString (); - if (s.Length < 2 || !s.StartsWith ("/Date(", StringComparison.Ordinal) || !s.EndsWith (")/", StringComparison.Ordinal)) + if (s.Length < 2 || !s.StartsWith ("/Date(", StringComparison.Ordinal) || !s.EndsWith (")/", StringComparison.Ordinal)) { + if (nullable) + return null; throw new XmlException ("Invalid JSON DateTime format. The value format should be '/Date(UnixTime)/'"); + } // The date can contain [SIGN]LONG, [SIGN]LONG+HOURSMINUTES or [SIGN]LONG-HOURSMINUTES // the format for HOURSMINUTES is DDDD 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 2a75876fd0d..7a37a5b4e70 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 @@ -1385,6 +1385,17 @@ namespace MonoTests.System.Runtime.Serialization.Json DateTest t = serializer.ReadObject(inputStream) as DateTest; Assert.AreEqual (634129344000000000, t.ShouldHaveValue.Value.Ticks, "#1"); } + + [Test] + public void NullableFieldsShouldSupportNullValue () + { + string json = @"{""should_have_value"":null}"; + var inputStream = new MemoryStream (Encoding.UTF8.GetBytes (json)); + DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DateTest)); + Console.WriteLine ("# serializer assembly: {0}", serializer.GetType ().Assembly.Location); + DateTest t = serializer.ReadObject (inputStream) as DateTest; + Assert.AreEqual (false, t.ShouldHaveValue.HasValue, "#2"); + } [Test] public void DeserializeNullMember () |