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)) {
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
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 ()