-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
@@ -83,6 +83,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -172,6 +173,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
@@ -220,6 +222,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -261,6 +264,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -322,6 +326,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
string decodedString = Encoding.UTF8.GetString(encodedStringData, 0, encodedStringData.Length); string decodedString = Encoding.UTF8.GetString(encodedStringData, 0, encodedStringData.Length);
Assert.AreEqual("Hello world!", decodedString); Assert.AreEqual("Hello world!", decodedString);
@@ -360,6 +365,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -418,6 +424,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -492,6 +499,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -520,6 +528,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -548,6 +557,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -576,6 +586,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -622,6 +633,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -680,6 +692,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -722,6 +735,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -746,6 +760,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -796,6 +811,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -1107,6 +1123,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -1158,6 +1175,7 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read()); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
+14 -12
View File
@@ -553,12 +553,13 @@ Parameter name: reader")]
} }
[Test] [Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end when reading bytes. Line 1, position 3.")]
public void ReadBytesNoStartWithUnexpectedEnd() public void ReadBytesNoStartWithUnexpectedEnd()
{ {
JsonReader reader = new JsonTextReader(new StringReader(@"[ ")); JsonReader reader = new JsonTextReader(new StringReader(@"[ "));
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
reader.ReadAsBytes();
Assert.IsNull(reader.ReadAsBytes());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -909,11 +910,11 @@ bye", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType); Assert.AreEqual(typeof(string), reader.ValueType);
Assert.AreEqual(20000, reader.Value.ToString().Length); Assert.AreEqual(20000, reader.Value.ToString().Length);
reader.Read(); Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
reader.Read(); Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -1128,21 +1129,21 @@ bye", reader.Value);
} }
[Test] [Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end when reading decimal. Line 0, position 0.")]
public void ReadAsDecimalNoContent() public void ReadAsDecimalNoContent()
{ {
JsonTextReader reader = new JsonTextReader(new StringReader(@"")); JsonTextReader reader = new JsonTextReader(new StringReader(@""));
reader.ReadAsDecimal(); Assert.IsNull(reader.ReadAsDecimal());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end when reading bytes. Line 0, position 0.")]
public void ReadAsBytesNoContent() public void ReadAsBytesNoContent()
{ {
JsonTextReader reader = new JsonTextReader(new StringReader(@"")); JsonTextReader reader = new JsonTextReader(new StringReader(@""));
reader.ReadAsBytes(); Assert.IsNull(reader.ReadAsBytes());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -1156,12 +1157,12 @@ bye", reader.Value);
#if !NET20 #if !NET20
[Test] [Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end when reading date. Line 0, position 0.")]
public void ReadAsDateTimeOffsetNoContent() public void ReadAsDateTimeOffsetNoContent()
{ {
JsonTextReader reader = new JsonTextReader(new StringReader(@"")); JsonTextReader reader = new JsonTextReader(new StringReader(@""));
reader.ReadAsDateTimeOffset(); Assert.IsNull(reader.ReadAsDateTimeOffset());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
#endif #endif
@@ -1171,7 +1172,8 @@ bye", reader.Value);
{ {
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()")); JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
reader.ReadAsDecimal(); Assert.IsNull(reader.ReadAsDecimal());
Assert.AreEqual(JsonToken.None, reader.TokenType);
} }
[Test] [Test]
@@ -1453,5 +1453,75 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual(2, values[1]); Assert.AreEqual(2, values[1]);
Assert.AreEqual(3, values[2]); Assert.AreEqual(3, values[2]);
} }
[Test]
public void ReadAsInt32InArray()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""integer""
},
""maxItems"":1
}";
string json = "[1,2]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
reader.Read();
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.AreEqual("Array item count 2 exceeds maximum count of 1. Line 1, position 5.", validationEventArgs.Message);
}
[Test]
public void ReadAsInt32InArrayIncomplete()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""integer""
},
""maxItems"":1
}";
string json = "[1,2";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
reader.Read();
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.None, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
}
} }
} }
@@ -97,6 +97,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType); Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);
Assert.IsFalse(jsonReader.Read()); Assert.IsFalse(jsonReader.Read());
Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
} }
using (JsonReader jsonReader = new JTokenReader(o.Property("Test2"))) using (JsonReader jsonReader = new JTokenReader(o.Property("Test2")))
@@ -110,6 +111,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value); Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);
Assert.IsFalse(jsonReader.Read()); Assert.IsFalse(jsonReader.Read());
Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
} }
} }
@@ -255,6 +257,9 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(0, lineInfo.LineNumber); Assert.AreEqual(0, lineInfo.LineNumber);
Assert.AreEqual(0, lineInfo.LinePosition); Assert.AreEqual(0, lineInfo.LinePosition);
Assert.AreEqual(false, lineInfo.HasLineInfo()); Assert.AreEqual(false, lineInfo.HasLineInfo());
jsonReader.Read();
Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
} }
} }
@@ -284,6 +289,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType); Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);
Assert.IsFalse(jsonReader.Read()); Assert.IsFalse(jsonReader.Read());
Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
} }
} }
@@ -5180,7 +5180,7 @@ keyword such as type of business.""
} }
[Test] [Test]
[ExpectedException(typeof (JsonReaderException), ExpectedMessage = "Unexpected end when reading integer. Line 2, position 18.")] [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Unexpected end when setting PreProperty's value. Line 2, position 18.")]
public void DeserializeUnexpectedEndInt() public void DeserializeUnexpectedEndInt()
{ {
string json = @"{ string json = @"{
@@ -5298,6 +5298,80 @@ keyword such as type of business.""
Assert.AreEqual(dict["k1"], dict2["k1"]); Assert.AreEqual(dict["k1"], dict2["k1"]);
} }
[Test]
public void DeserializeEmptyStrings()
{
object v = JsonConvert.DeserializeObject<double?>("");
Assert.IsNull(v);
v = JsonConvert.DeserializeObject<char?>("");
Assert.IsNull(v);
v = JsonConvert.DeserializeObject<int?>("");
Assert.IsNull(v);
v = JsonConvert.DeserializeObject<decimal?>("");
Assert.IsNull(v);
v = JsonConvert.DeserializeObject<DateTime?>("");
Assert.IsNull(v);
v = JsonConvert.DeserializeObject<DateTimeOffset?>("");
Assert.IsNull(v);
v = JsonConvert.DeserializeObject<byte[]>("");
Assert.IsNull(v);
}
public class Sdfsdf
{
public double Id { get; set; }
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "No JSON content found and type 'System.Double' is not nullable.")]
public void DeserializeDoubleFromEmptyString()
{
JsonConvert.DeserializeObject<double>("");
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "No JSON content found and type 'System.StringComparison' is not nullable.")]
public void DeserializeEnumFromEmptyString()
{
JsonConvert.DeserializeObject<StringComparison>("");
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "No JSON content found and type 'System.Int32' is not nullable.")]
public void DeserializeInt32FromEmptyString()
{
JsonConvert.DeserializeObject<int>("");
}
[Test]
public void DeserializeByteArrayFromEmptyString()
{
byte[] b = JsonConvert.DeserializeObject<byte[]>("");
Assert.IsNull(b);
}
[Test]
[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: value")]
public void DeserializeDoubleFromNullString()
{
JsonConvert.DeserializeObject<double>(null);
}
[Test]
[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: value")]
public void DeserializeFromNullString()
{
JsonConvert.DeserializeObject(null);
}
} }
public class Widget1 public class Widget1
+17 -3
View File
@@ -350,26 +350,40 @@ namespace Newtonsoft.Json.Bson
{ {
try try
{ {
bool success;
switch (_bsonReaderState) switch (_bsonReaderState)
{ {
case BsonReaderState.Normal: case BsonReaderState.Normal:
return ReadNormal(); success = ReadNormal();
break;
case BsonReaderState.ReferenceStart: case BsonReaderState.ReferenceStart:
case BsonReaderState.ReferenceRef: case BsonReaderState.ReferenceRef:
case BsonReaderState.ReferenceId: case BsonReaderState.ReferenceId:
return ReadReference(); success = ReadReference();
break;
case BsonReaderState.CodeWScopeStart: case BsonReaderState.CodeWScopeStart:
case BsonReaderState.CodeWScopeCode: case BsonReaderState.CodeWScopeCode:
case BsonReaderState.CodeWScopeScope: case BsonReaderState.CodeWScopeScope:
case BsonReaderState.CodeWScopeScopeObject: case BsonReaderState.CodeWScopeScopeObject:
case BsonReaderState.CodeWScopeScopeEnd: case BsonReaderState.CodeWScopeScopeEnd:
return ReadCodeWScope(); success = ReadCodeWScope();
break;
default: default:
throw CreateReaderException(this, "Unexpected state: {0}".FormatWith(CultureInfo.InvariantCulture, _bsonReaderState)); throw CreateReaderException(this, "Unexpected state: {0}".FormatWith(CultureInfo.InvariantCulture, _bsonReaderState));
} }
if (!success)
{
SetToken(JsonToken.None);
return false;
}
return true;
} }
catch (EndOfStreamException) catch (EndOfStreamException)
{ {
SetToken(JsonToken.None);
return false; return false;
} }
} }
+2
View File
@@ -722,6 +722,8 @@ namespace Newtonsoft.Json
/// <returns>The deserialized object from the JSON string.</returns> /// <returns>The deserialized object from the JSON string.</returns>
public static object DeserializeObject(string value, Type type, JsonSerializerSettings settings) public static object DeserializeObject(string value, Type type, JsonSerializerSettings settings)
{ {
ValidationUtils.ArgumentNotNull(value, "value");
StringReader sr = new StringReader(value); StringReader sr = new StringReader(value);
JsonSerializer jsonSerializer = JsonSerializer.Create(settings); JsonSerializer jsonSerializer = JsonSerializer.Create(settings);
+23 -5
View File
@@ -327,7 +327,13 @@ namespace Newtonsoft.Json
public override bool Read() public override bool Read()
{ {
_readType = ReadType.Read; _readType = ReadType.Read;
return ReadInternal(); if (!ReadInternal())
{
SetToken(JsonToken.None);
return false;
}
return true;
} }
private bool IsWrappedInTypeObject() private bool IsWrappedInTypeObject()
@@ -371,7 +377,10 @@ namespace Newtonsoft.Json
do do
{ {
if (!ReadInternal()) if (!ReadInternal())
throw CreateReaderException(this, "Unexpected end when reading bytes."); {
SetToken(JsonToken.None);
return null;
}
} while (TokenType == JsonToken.Comment); } while (TokenType == JsonToken.Comment);
if (IsWrappedInTypeObject()) if (IsWrappedInTypeObject())
@@ -431,7 +440,10 @@ namespace Newtonsoft.Json
do do
{ {
if (!ReadInternal()) if (!ReadInternal())
throw CreateReaderException(this, "Unexpected end when reading decimal."); {
SetToken(JsonToken.None);
return null;
}
} while (TokenType == JsonToken.Comment); } while (TokenType == JsonToken.Comment);
if (TokenType == JsonToken.Float) if (TokenType == JsonToken.Float)
@@ -471,7 +483,10 @@ namespace Newtonsoft.Json
do do
{ {
if (!ReadInternal()) if (!ReadInternal())
throw CreateReaderException(this, "Unexpected end when reading integer."); {
SetToken(JsonToken.None);
return null;
}
} while (TokenType == JsonToken.Comment); } while (TokenType == JsonToken.Comment);
if (TokenType == JsonToken.Integer) if (TokenType == JsonToken.Integer)
@@ -512,7 +527,10 @@ namespace Newtonsoft.Json
do do
{ {
if (!ReadInternal()) if (!ReadInternal())
throw CreateReaderException(this, "Unexpected end when reading date."); {
SetToken(JsonToken.None);
return null;
}
} while (TokenType == JsonToken.Comment); } while (TokenType == JsonToken.Comment);
if (TokenType == JsonToken.Date) if (TokenType == JsonToken.Date)
@@ -504,6 +504,9 @@ namespace Newtonsoft.Json
case JsonToken.Bytes: case JsonToken.Bytes:
// these have no equivalent in JSON schema // these have no equivalent in JSON schema
break; break;
case JsonToken.None:
// no content, do nothing
break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
+1 -1
View File
@@ -246,7 +246,7 @@ namespace Newtonsoft.Json.Linq
private bool ReadToEnd() private bool ReadToEnd()
{ {
//CurrentState = State.Finished; SetToken(JsonToken.None);
return false; return false;
} }
@@ -112,7 +112,12 @@ namespace Newtonsoft.Json.Serialization
JsonConverter converter = GetConverter(contract, null); JsonConverter converter = GetConverter(contract, null);
if (reader.TokenType == JsonToken.None && !ReadForType(reader, contract, converter != null, false)) 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 null;
}
return CreateValueNonProperty(reader, objectType, contract, converter); return CreateValueNonProperty(reader, objectType, contract, converter);
} }
@@ -1114,21 +1119,23 @@ namespace Newtonsoft.Json.Serialization
return true; return true;
case ReadType.ReadAsInt32: case ReadType.ReadAsInt32:
reader.ReadAsInt32(); reader.ReadAsInt32();
return true; break;
case ReadType.ReadAsDecimal: case ReadType.ReadAsDecimal:
reader.ReadAsDecimal(); reader.ReadAsDecimal();
return true; break;
case ReadType.ReadAsBytes: case ReadType.ReadAsBytes:
reader.ReadAsBytes(); reader.ReadAsBytes();
return true; break;
#if !NET20 #if !NET20
case ReadType.ReadAsDateTimeOffset: case ReadType.ReadAsDateTimeOffset:
reader.ReadAsDateTimeOffset(); reader.ReadAsDateTimeOffset();
return true; break;
#endif #endif
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
return (reader.TokenType != JsonToken.None);
} }
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, string id) private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, string id)