-Fixed comments in JSON sometimes causing errors when deserializing
This commit is contained in:
@@ -4248,5 +4248,33 @@ keyword such as type of business.""
|
|||||||
set { }
|
set { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class CommentTestClass
|
||||||
|
{
|
||||||
|
public bool Indexed { get; set; }
|
||||||
|
public int StartYear { get; set; }
|
||||||
|
public IList<decimal> Values { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CommentTestClassTest()
|
||||||
|
{
|
||||||
|
string json = @"{""indexed"":true, ""startYear"":1939, ""values"":
|
||||||
|
[ 3000, /* 1940-1949 */
|
||||||
|
3000, 3600, 3600, 3600, 3600, 4200, 4200, 4200, 4200, 4800, /* 1950-1959 */
|
||||||
|
4800, 4800, 4800, 4800, 4800, 4800, 6600, 6600, 7800, 7800, /* 1960-1969 */
|
||||||
|
7800, 7800, 9000, 10800, 13200, 14100, 15300, 16500, 17700, 22900, /* 1970-1979 */
|
||||||
|
25900, 29700, 32400, 35700, 37800, 39600, 42000, 43800, 45000, 48000, /* 1980-1989 */
|
||||||
|
51300, 53400, 55500, 57600, 60600, 61200, 62700, 65400, 68400, 72600, /* 1990-1999 */
|
||||||
|
76200, 80400, 84900, 87000, 87900, 90000, 94200, 97500, 102000, 106800, /* 2000-2009 */
|
||||||
|
106800, 106800] /* 2010-2011 */
|
||||||
|
}";
|
||||||
|
|
||||||
|
CommentTestClass commentTestClass = JsonConvert.DeserializeObject<CommentTestClass>(json);
|
||||||
|
|
||||||
|
Assert.AreEqual(true, commentTestClass.Indexed);
|
||||||
|
Assert.AreEqual(1939, commentTestClass.StartYear);
|
||||||
|
Assert.AreEqual(63, commentTestClass.Values.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -322,8 +322,12 @@ namespace Newtonsoft.Json
|
|||||||
public override byte[] ReadAsBytes()
|
public override byte[] ReadAsBytes()
|
||||||
{
|
{
|
||||||
_readType = ReadType.ReadAsBytes;
|
_readType = ReadType.ReadAsBytes;
|
||||||
if (!ReadInternal())
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (!ReadInternal())
|
||||||
throw CreateJsonReaderException("Unexpected end when reading bytes: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
|
throw CreateJsonReaderException("Unexpected end when reading bytes: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
|
||||||
|
} while (TokenType == JsonToken.Comment);
|
||||||
|
|
||||||
if (TokenType == JsonToken.Null)
|
if (TokenType == JsonToken.Null)
|
||||||
return null;
|
return null;
|
||||||
@@ -340,9 +344,13 @@ namespace Newtonsoft.Json
|
|||||||
public override decimal? ReadAsDecimal()
|
public override decimal? ReadAsDecimal()
|
||||||
{
|
{
|
||||||
_readType = ReadType.ReadAsDecimal;
|
_readType = ReadType.ReadAsDecimal;
|
||||||
if (!ReadInternal())
|
|
||||||
throw CreateJsonReaderException("Unexpected end when reading decimal: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
|
do
|
||||||
|
{
|
||||||
|
if (!ReadInternal())
|
||||||
|
throw CreateJsonReaderException("Unexpected end when reading decimal: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
|
||||||
|
} while (TokenType == JsonToken.Comment);
|
||||||
|
|
||||||
if (TokenType == JsonToken.Null)
|
if (TokenType == JsonToken.Null)
|
||||||
return null;
|
return null;
|
||||||
if (TokenType == JsonToken.Float)
|
if (TokenType == JsonToken.Float)
|
||||||
@@ -366,8 +374,12 @@ namespace Newtonsoft.Json
|
|||||||
public override DateTimeOffset? ReadAsDateTimeOffset()
|
public override DateTimeOffset? ReadAsDateTimeOffset()
|
||||||
{
|
{
|
||||||
_readType = ReadType.ReadAsDateTimeOffset;
|
_readType = ReadType.ReadAsDateTimeOffset;
|
||||||
if (!ReadInternal())
|
|
||||||
throw CreateJsonReaderException("Unexpected end when reading date: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
|
do
|
||||||
|
{
|
||||||
|
if (!ReadInternal())
|
||||||
|
throw CreateJsonReaderException("Unexpected end when reading date: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
|
||||||
|
} while (TokenType == JsonToken.Comment);
|
||||||
|
|
||||||
if (TokenType == JsonToken.Null)
|
if (TokenType == JsonToken.Null)
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -634,6 +634,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case JsonToken.Comment:
|
||||||
|
break;
|
||||||
case JsonToken.EndObject:
|
case JsonToken.EndObject:
|
||||||
contract.InvokeOnDeserialized(dictionary.UnderlyingDictionary, Serializer.Context);
|
contract.InvokeOnDeserialized(dictionary.UnderlyingDictionary, Serializer.Context);
|
||||||
|
|
||||||
@@ -742,6 +744,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
|
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
|
||||||
break;
|
break;
|
||||||
|
case JsonToken.Comment:
|
||||||
|
break;
|
||||||
case JsonToken.EndObject:
|
case JsonToken.EndObject:
|
||||||
exit = true;
|
exit = true;
|
||||||
break;
|
break;
|
||||||
@@ -897,6 +901,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
reader.Skip();
|
reader.Skip();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case JsonToken.Comment:
|
||||||
|
break;
|
||||||
case JsonToken.EndObject:
|
case JsonToken.EndObject:
|
||||||
exit = true;
|
exit = true;
|
||||||
break;
|
break;
|
||||||
@@ -964,8 +970,14 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return reader.Read();
|
do
|
||||||
|
{
|
||||||
|
if (!reader.Read())
|
||||||
|
return false;
|
||||||
|
} while (reader.TokenType == JsonToken.Comment);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, string id)
|
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, string id)
|
||||||
|
|||||||
Reference in New Issue
Block a user