-Changed JavaScriptConvert.DeserializeObject to check for addition content after deserializing object

This commit is contained in:
JamesNK
2008-09-04 08:12:36 +00:00
parent 7f4656a583
commit 7b0f3a7ccc
3 changed files with 49 additions and 1 deletions
@@ -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);
}
}
}
+3
View File
@@ -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;
+13 -1
View File
@@ -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()