-Fixed comments in JSON sometimes causing errors when deserializing

This commit is contained in:
JamesNK
2011-03-26 01:25:10 +00:00
parent dffe08021d
commit b476a8d0fc
3 changed files with 60 additions and 8 deletions
@@ -4248,5 +4248,33 @@ keyword such as type of business.""
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);
}
}
}
+18 -6
View File
@@ -322,8 +322,12 @@ namespace Newtonsoft.Json
public override byte[] ReadAsBytes()
{
_readType = ReadType.ReadAsBytes;
if (!ReadInternal())
do
{
if (!ReadInternal())
throw CreateJsonReaderException("Unexpected end when reading bytes: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
} while (TokenType == JsonToken.Comment);
if (TokenType == JsonToken.Null)
return null;
@@ -340,9 +344,13 @@ namespace Newtonsoft.Json
public override decimal? 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)
return null;
if (TokenType == JsonToken.Float)
@@ -366,8 +374,12 @@ namespace Newtonsoft.Json
public override DateTimeOffset? 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)
return null;
@@ -634,6 +634,8 @@ namespace Newtonsoft.Json.Serialization
throw;
}
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
contract.InvokeOnDeserialized(dictionary.UnderlyingDictionary, Serializer.Context);
@@ -742,6 +744,8 @@ namespace Newtonsoft.Json.Serialization
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
exit = true;
break;
@@ -897,6 +901,8 @@ namespace Newtonsoft.Json.Serialization
reader.Skip();
}
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
exit = true;
break;
@@ -964,8 +970,14 @@ namespace Newtonsoft.Json.Serialization
return true;
}
#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)