-Changed JavaScriptConvert.DeserializeObject to check for addition content after deserializing object
This commit is contained in:
@@ -173,5 +173,38 @@ now brown cow?", '"', true);
|
||||
value = 'c';
|
||||
Assert.AreEqual(@"""c""", JavaScriptConvert.ToString(value));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "After parsing a value an unexpected character was encoutered: t")]
|
||||
public void TestInvalidStrings()
|
||||
{
|
||||
string orig = @"this is a string ""that has quotes"" ";
|
||||
|
||||
string serialized = JavaScriptConvert.SerializeObject(orig);
|
||||
|
||||
// *** Make string invalid by stripping \" \"
|
||||
serialized = serialized.Replace(@"\""", "\"");
|
||||
|
||||
string result = JavaScriptConvert.DeserializeObject<string>(serialized);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeValueObjects()
|
||||
{
|
||||
int i = JavaScriptConvert.DeserializeObject<int>("1");
|
||||
Assert.AreEqual(1, i);
|
||||
|
||||
DateTimeOffset d = JavaScriptConvert.DeserializeObject<DateTimeOffset>(@"""\/Date(-59011455539000+0000)\/""");
|
||||
Assert.AreEqual(new DateTimeOffset(new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Utc)), d);
|
||||
|
||||
bool b = JavaScriptConvert.DeserializeObject<bool>("true");
|
||||
Assert.AreEqual(true, b);
|
||||
|
||||
object n = JavaScriptConvert.DeserializeObject<object>("null");
|
||||
Assert.AreEqual(null, n);
|
||||
|
||||
object u = JavaScriptConvert.DeserializeObject<object>("undefined");
|
||||
Assert.AreEqual(null, u);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -546,6 +546,9 @@ namespace Newtonsoft.Json
|
||||
using (JsonReader jsonReader = new JsonTextReader(sr))
|
||||
{
|
||||
deserializedValue = jsonSerializer.Deserialize(jsonReader, type);
|
||||
|
||||
if (jsonReader.Read())
|
||||
throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object.");
|
||||
}
|
||||
|
||||
return deserializedValue;
|
||||
|
||||
@@ -592,6 +592,10 @@ namespace Newtonsoft.Json
|
||||
|
||||
} while (!end && MoveNext());
|
||||
|
||||
// hit the end of the reader before the number ended. clear the last number value
|
||||
if (!end)
|
||||
ClearCurrentChar();
|
||||
|
||||
string number = _buffer.ToString();
|
||||
object numberValue;
|
||||
JsonToken numberType;
|
||||
@@ -678,9 +682,17 @@ namespace Newtonsoft.Json
|
||||
bool match = MatchValue(value);
|
||||
|
||||
if (!noTrailingNonSeperatorCharacters)
|
||||
{
|
||||
return match;
|
||||
}
|
||||
else
|
||||
return (match && (!MoveNext() || CurrentIsSeperator()));
|
||||
{
|
||||
bool matchAndNoTrainingNonSeperatorCharacters = (match && (!MoveNext() || CurrentIsSeperator()));
|
||||
if (!CurrentIsSeperator())
|
||||
ClearCurrentChar();
|
||||
|
||||
return matchAndNoTrainingNonSeperatorCharacters;
|
||||
}
|
||||
}
|
||||
|
||||
private bool CurrentIsSeperator()
|
||||
|
||||
Reference in New Issue
Block a user