-Fixed error when deserializing nullable types with no content

-Changed JsonReaders to set TokenType to JsonToken.None after finishing content
-Changed the ReadAsXXX methods to return null instead of throwing an error when there is no content
This commit is contained in:
James Newton-King
2012-02-19 01:25:47 +13:00
parent c876e1d9cc
commit 4f8832adc0
11 changed files with 240 additions and 26 deletions
@@ -112,7 +112,12 @@ namespace Newtonsoft.Json.Serialization
JsonConverter converter = GetConverter(contract, null);
if (reader.TokenType == JsonToken.None && !ReadForType(reader, contract, converter != null, false))
{
if (!contract.IsNullable)
throw new JsonSerializationException("No JSON content found and type '{0}' is not nullable.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
return null;
}
return CreateValueNonProperty(reader, objectType, contract, converter);
}
@@ -1114,21 +1119,23 @@ namespace Newtonsoft.Json.Serialization
return true;
case ReadType.ReadAsInt32:
reader.ReadAsInt32();
return true;
break;
case ReadType.ReadAsDecimal:
reader.ReadAsDecimal();
return true;
break;
case ReadType.ReadAsBytes:
reader.ReadAsBytes();
return true;
break;
#if !NET20
case ReadType.ReadAsDateTimeOffset:
reader.ReadAsDateTimeOffset();
return true;
break;
#endif
default:
throw new ArgumentOutOfRangeException();
}
return (reader.TokenType != JsonToken.None);
}
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, string id)