-JsonTextReader, BsonReader performance

-Fix dictionary key encoding when serializing
-Fix WrappedDictionary being set to a property when deserializing
-Fix Silverlight serialization security error
This commit is contained in:
JamesNK
2010-01-01 23:13:35 +00:00
parent 974178a162
commit 324c4e4bc5
25 changed files with 971 additions and 566 deletions
@@ -97,7 +97,11 @@ namespace Newtonsoft.Json.Tests.Converters
[Test]
public void SerializeCustomFormattedDateTime()
{
IsoDateTimeConverter converter = new IsoDateTimeConverter() { DateTimeFormat = "dd/MM/yyyy" };
IsoDateTimeConverter converter = new IsoDateTimeConverter
{
DateTimeFormat = "dd/MM/yyyy",
Culture = CultureInfo.InvariantCulture
};
string json = @"""09/12/2006""";
@@ -152,6 +152,9 @@ now brown cow?", '"', true);
result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true);
Assert.AreEqual(result, @"'Fred\'s ""cat"".'");
result = JavaScriptUtils.ToEscapedJavaScriptString("\u001farray\u003caddress");
Assert.AreEqual(result, @"""\u001farray<address""");
}
[Test]
+139 -1
View File
@@ -48,7 +48,7 @@ namespace Newtonsoft.Json.Tests
,{""t"":""CSCO"", ""n"":""Cisco Systems, Inc."", ""e"":""NASDAQ"", ""id"":""99624""}
,{""t"":""CVX"", ""n"":""Chevron Corporation"", ""e"":""NYSE"", ""id"":""667226""}
,{""t"":""TM"", ""n"":""Toyota Motor Corporation (ADR)"", ""e"":""NYSE"", ""id"":""655880""}
,{""t"":""JPM"", ""n"":""JPMorgan Chase \x26 Co."", ""e"":""NYSE"", ""id"":""665639""}
,{""t"":""JPM"", ""n"":""JPMorgan Chase \\x26 Co."", ""e"":""NYSE"", ""id"":""665639""}
,{""t"":""COP"", ""n"":""ConocoPhillips"", ""e"":""NYSE"", ""id"":""1691168""}
,{""t"":""LFC"", ""n"":""China Life Insurance Company Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""688679""}
,{""t"":""NOK"", ""n"":""Nokia Corporation (ADR)"", ""e"":""NYSE"", ""id"":""657729""}
@@ -138,6 +138,8 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
Assert.AreEqual(7, jsonReader.LineNumber);
Assert.AreEqual(1, jsonReader.LinePosition);
Assert.IsFalse(jsonReader.Read());
}
}
@@ -176,10 +178,12 @@ namespace Newtonsoft.Json.Tests
jsonReader.Read();
Assert.AreEqual(jsonReader.TokenType, JsonToken.Integer);
Assert.AreEqual(1, jsonReader.Value);
Assert.AreEqual(3, jsonReader.Depth);
jsonReader.Read();
Assert.AreEqual(jsonReader.TokenType, JsonToken.Integer);
Assert.AreEqual(2, jsonReader.Value);
Assert.AreEqual(3, jsonReader.Depth);
jsonReader.Read();
@@ -192,6 +196,140 @@ namespace Newtonsoft.Json.Tests
}
}
[Test]
[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: reader")]
public void NullTextReader()
{
new JsonTextReader(null);
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unterminated string. Expected delimiter: '. Line 1, position 3.")]
public void UnexpectedEndOfString()
{
JsonReader reader = new JsonTextReader(new StringReader("'hi"));
reader.Read();
}
[Test]
public void ReadNullTerminatorStrings()
{
JsonReader reader = new JsonTextReader(new StringReader("'h\0i'"));
Assert.IsTrue(reader.Read());
Assert.AreEqual("h\0i", reader.Value);
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end while parsing unicode character. Line 1, position 7.")]
public void UnexpectedEndOfHex()
{
JsonReader reader = new JsonTextReader(new StringReader(@"'h\u006"));
reader.Read();
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unterminated string. Expected delimiter: '. Line 1, position 3.")]
public void UnexpectedEndOfControlCharacter()
{
JsonReader reader = new JsonTextReader(new StringReader(@"'h\"));
reader.Read();
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected character encountered while parsing value: t. Line 1, position 1.")]
public void ReadBytesWithBadCharacter()
{
JsonReader reader = new JsonTextReader(new StringReader(@"true"));
reader.ReadAsBytes();
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unterminated string. Expected delimiter: '. Line 1, position 17.")]
public void ReadBytesWithUnexpectedEnd()
{
string helloWorld = "Hello world!";
byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);
JsonReader reader = new JsonTextReader(new StringReader(@"'" + Convert.ToBase64String(helloWorldData)));
reader.ReadAsBytes();
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end when reading bytes: Line 1, position 3.")]
public void ReadBytesNoStartWithUnexpectedEnd()
{
JsonReader reader = new JsonTextReader(new StringReader(@"[ "));
Assert.IsTrue(reader.Read());
reader.ReadAsBytes();
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end when parsing unquoted property name. Line 1, position 4.")]
public void UnexpectedEndWhenParsingUnquotedProperty()
{
JsonReader reader = new JsonTextReader(new StringReader(@"{aww"));
Assert.IsTrue(reader.Read());
reader.Read();
}
[Test]
public void ParsingQuotedPropertyWithControlCharacters()
{
JsonReader reader = new JsonTextReader(new StringReader(@"{'hi\r\nbye':1}"));
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual(@"hi
bye", reader.Value);
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
Assert.IsFalse(reader.Read());
}
[Test]
public void ReadBytesFollowingNumberInArray()
{
string helloWorld = "Hello world!";
byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);
JsonReader reader = new JsonTextReader(new StringReader(@"[1,'" + Convert.ToBase64String(helloWorldData) + @"']"));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
byte[] data = reader.ReadAsBytes();
Assert.AreEqual(helloWorldData, data);
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsFalse(reader.Read());
}
[Test]
public void ReadBytesFollowingNumberInObject()
{
string helloWorld = "Hello world!";
byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);
JsonReader reader = new JsonTextReader(new StringReader(@"{num:1,data:'" + Convert.ToBase64String(helloWorldData) + @"'}"));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsTrue(reader.Read());
byte[] data = reader.ReadAsBytes();
Assert.AreEqual(helloWorldData, data);
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
}
[Test]
public void ReadingEscapedStrings()
{
@@ -527,7 +527,7 @@ namespace Newtonsoft.Json.Tests
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Null, reader.TokenType);
Assert.AreEqual(@"Value null is not defined in enum. Line 1, position 6.", validationEventArgs.Message);
Assert.AreEqual(@"Value null is not defined in enum. Line 1, position 5.", validationEventArgs.Message);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
@@ -564,7 +564,7 @@ namespace Newtonsoft.Json.Tests
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 12.", validationEventArgs.Message);
Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 11.", validationEventArgs.Message);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
@@ -1005,7 +1005,7 @@ namespace Newtonsoft.Json.Tests
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Null, reader.TokenType);
Assert.AreEqual("Index 3 has not been defined and the schema does not allow additional items. Line 1, position 14.", validationEventArgs.Message);
Assert.AreEqual("Index 3 has not been defined and the schema does not allow additional items. Line 1, position 13.", validationEventArgs.Message);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
File diff suppressed because one or more lines are too long
@@ -27,6 +27,7 @@ using System;
using System.Collections.Generic;
#if !SILVERLIGHT && !PocketPC && !NET20
using System.ComponentModel.DataAnnotations;
using System.Web.Script.Serialization;
#endif
using System.Text;
using NUnit.Framework;
@@ -131,12 +132,21 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void DeserializeJavaScriptDate()
{
DateTime dateValue = new DateTime(2000, 3, 30);
DateTime dateValue = new DateTime(2010, 3, 30);
Dictionary<string, object> testDictionary = new Dictionary<string, object>();
testDictionary["date"] = dateValue;
string jsonText = JsonConvert.SerializeObject(testDictionary);
#if !PocketPC && !NET20
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Dictionary<string, object>));
serializer.WriteObject(ms, testDictionary);
byte[] data = ms.ToArray();
string output = Encoding.UTF8.GetString(data, 0, data.Length);
#endif
Dictionary<string, object> deserializedDictionary = (Dictionary<string, object>)JsonConvert.DeserializeObject(jsonText, typeof(Dictionary<string, object>));
DateTime deserializedDate = (DateTime)deserializedDictionary["date"];
@@ -403,6 +413,37 @@ keyword such as type of business.""
Assert.AreEqual(dataContractResult[0], jsonNetResult[0]);
}
[Test]
public void BackslashEqivilence()
{
string json = @"[""vvv\/vvv\tvvv\""vvv\bvvv\nvvv\rvvv\\vvv\fvvv""]";
#if !SILVERLIGHT
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
List<string> javaScriptSerializerResult = javaScriptSerializer.Deserialize<List<string>>(json);
#endif
DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(List<string>));
List<string> dataContractResult = (List<string>)s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
List<string> jsonNetResult = JsonConvert.DeserializeObject<List<string>>(json);
Assert.AreEqual(1, jsonNetResult.Count);
Assert.AreEqual(dataContractResult[0], jsonNetResult[0]);
#if !SILVERLIGHT
Assert.AreEqual(javaScriptSerializerResult[0], jsonNetResult[0]);
#endif
}
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = @"Bad JSON escape sequence: \j. Line 1, position 7.")]
public void InvalidBackslash()
{
string json = @"[""vvv\jvvv""]";
JsonConvert.DeserializeObject<List<string>>(json);
}
[Test]
public void DateTimeTest()
{
@@ -1068,9 +1109,6 @@ keyword such as type of business.""
]
}";
//JavaScriptSerializer serializer = new JavaScriptSerializer();
//GoogleMapGeocoderStructure jsonGoogleMapGeocoder = serializer.Deserialize<GoogleMapGeocoderStructure>(json);
GoogleMapGeocoderStructure jsonGoogleMapGeocoder = JsonConvert.DeserializeObject<GoogleMapGeocoderStructure>(json);
}
@@ -2051,5 +2089,39 @@ keyword such as type of business.""
PersonPropertyClass newPersonPropertyClass = JsonConvert.DeserializeObject<PersonPropertyClass>(json);
Assert.AreEqual(wagePerson.HourlyWage, ((WagePerson) newPersonPropertyClass.Person).HourlyWage);
}
public class ExistingValueClass
{
public Dictionary<string, string> Dictionary { get; set; }
public List<string> List { get; set; }
public ExistingValueClass()
{
Dictionary = new Dictionary<string, string>
{
{"existing", "yup"}
};
List = new List<string>
{
"existing"
};
}
}
[Test]
public void DeserializePopulateDictionaryAndList()
{
ExistingValueClass d = JsonConvert.DeserializeObject<ExistingValueClass>(@"{'Dictionary':{appended:'appended',existing:'new'}}");
Assert.IsNotNull(d);
Assert.IsNotNull(d.Dictionary);
Assert.AreEqual(typeof(Dictionary<string, string>), d.Dictionary.GetType());
Assert.AreEqual(typeof(List<string>), d.List.GetType());
Assert.AreEqual(2, d.Dictionary.Count);
Assert.AreEqual("new", d.Dictionary["existing"]);
Assert.AreEqual("appended", d.Dictionary["appended"]);
Assert.AreEqual(1, d.List.Count);
Assert.AreEqual("existing", d.List[0]);
}
}
}