From 2fb34b22bb6ffc6c12c8decb087e579b3a9b5e6e Mon Sep 17 00:00:00 2001 From: JamesNK Date: Sun, 2 Mar 2008 03:18:05 +0000 Subject: [PATCH] Tests --- .../JsonSerializerTest.cs | 27 +-- Src/Newtonsoft.Json.Tests/LinqToJsonTest.cs | 178 ++++++++++++++++-- .../Newtonsoft.Json.Tests.csproj | 6 +- .../XmlNodeConverterTest.cs | 2 +- 4 files changed, 183 insertions(+), 30 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index 423b7257..5680279b 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -36,6 +36,8 @@ using System.Collections.ObjectModel; using System.Runtime.Serialization.Json; using System.Net.Mail; using System.Web.Script.Serialization; +using Newtonsoft.Json.Linq; +using System.Linq; namespace Newtonsoft.Json.Tests { @@ -239,11 +241,14 @@ namespace Newtonsoft.Json.Tests [Test] public void TypedHashtableDeserialization() { - string value = @"{""Name"":""Orange"", ""Hash"":{""Expiry"":""01/24/2010 12:00:00""}}"; + string value = @"{""Name"":""Orange"", ""Hash"":{""Expiry"":""01/24/2010 12:00:00"",""UntypedArray"":[""01/24/2010 12:00:00""]}}"; TypedSubHashtable p = JavaScriptConvert.DeserializeObject(value, typeof(TypedSubHashtable)) as TypedSubHashtable; Assert.AreEqual("01/24/2010 12:00:00", p.Hash["Expiry"].ToString()); + Assert.AreEqual(@"[ + ""01/24/2010 12:00:00"" +]", p.Hash["UntypedArray"].ToString()); } public class GetOnlyPropertyClass @@ -421,12 +426,12 @@ keyword such as type of business."" ""torrentc"": ""1816000723"" }"; - JavaScriptObject o = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonText); - Assert.AreEqual(4, o.Count); + JObject o = (JObject)JavaScriptConvert.DeserializeObject(jsonText); + Assert.AreEqual(4, o.Children().Count()); - JavaScriptArray torrentsArray = (JavaScriptArray)o["torrents"]; - JavaScriptArray nestedTorrentsArray = (JavaScriptArray)torrentsArray[0]; - Assert.AreEqual(nestedTorrentsArray.Count, 19); + JToken torrentsArray = (JToken)o["torrents"]; + JToken nestedTorrentsArray = (JToken)torrentsArray[0]; + Assert.AreEqual(nestedTorrentsArray.Children().Count(), 19); } public class JsonPropertyClass @@ -602,7 +607,7 @@ keyword such as type of business."" object deserializedValue; - using (JsonReader jsonReader = new JsonReader(new StringReader(output))) + using (JsonReader jsonReader = new JsonTextReader(new StringReader(output))) { deserializedValue = jsonSerializer.Deserialize(jsonReader, typeof(ProductShort)); } @@ -732,7 +737,7 @@ keyword such as type of business."" Assert.AreEqual(@"[{""Name"":""Test1"",""Expiry"":""\/Date(946684800000)\/"",""Price"":0,""Sizes"":null},{""Name"":""Test2"",""Expiry"":""\/Date(946684800000)\/"",""Price"":0,""Sizes"":null},{""Name"":""Test3"",""Expiry"":""\/Date(946684800000)\/"",""Price"":0,""Sizes"":null}]", sw.GetStringBuilder().ToString()); - ProductCollection collectionNew = (ProductCollection)jsonSerializer.Deserialize(new JsonReader(new StringReader(sw.GetStringBuilder().ToString())), typeof(ProductCollection)); + ProductCollection collectionNew = (ProductCollection)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(sw.GetStringBuilder().ToString())), typeof(ProductCollection)); CollectionAssert.AreEqual(collection, collectionNew); } @@ -752,13 +757,13 @@ keyword such as type of business."" Assert.AreEqual(@"{""Color"":2,""Establised"":""\/Date(1264122061000+0000)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""Expiry"":""\/Date(949532490000)\/"",""Price"":0},{""Name"":""Alien"",""Expiry"":""\/Date(946684800000)\/"",""Price"":0}]}", sw.GetStringBuilder().ToString()); - Store s2 = (Store)jsonSerializer.Deserialize(new JsonReader(new StringReader("{}")), typeof(Store)); + Store s2 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader("{}")), typeof(Store)); Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s2.Escape); - Store s3 = (Store)jsonSerializer.Deserialize(new JsonReader(new StringReader(@"{""Escape"":null}")), typeof(Store)); + Store s3 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(@"{""Escape"":null}")), typeof(Store)); Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s3.Escape); - Store s4 = (Store)jsonSerializer.Deserialize(new JsonReader(new StringReader(@"{""Color"":2,""Establised"":""\/Date(1264071600000+1300)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""Expiry"":""\/Date(949485690000+1300)\/"",""Price"":0},{""Name"":""Alien"",""Expiry"":""\/Date(946638000000)\/"",""Price"":0}]}")), typeof(Store)); + Store s4 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(@"{""Color"":2,""Establised"":""\/Date(1264071600000+1300)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""Expiry"":""\/Date(949485690000+1300)\/"",""Price"":0},{""Name"":""Alien"",""Expiry"":""\/Date(946638000000)\/"",""Price"":0}]}")), typeof(Store)); Assert.AreEqual(s1.Establised, s3.Establised); } diff --git a/Src/Newtonsoft.Json.Tests/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/LinqToJsonTest.cs index 9426abc5..a67f7b84 100644 --- a/Src/Newtonsoft.Json.Tests/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/LinqToJsonTest.cs @@ -31,6 +31,7 @@ using NUnit.Framework; using Newtonsoft.Json.Linq; using System.Xml; using System.IO; +using Newtonsoft.Json.Converters; namespace Newtonsoft.Json.Tests { @@ -167,14 +168,10 @@ keyword such as type of business."" JObject o = JObject.Parse(json); - List resultObjects = o.PropertyValue("results") - .Children().ToList(); + List resultObjects = o["results"].Children().ToList(); - List resultUrls = o.PropertyValue("results") - .Children() - .PropertyValues("url") - .ToList(); + List resultUrls = o["results"].Children().Values("url").ToList(); List expectedUrls = new List() { "http://www.google.com/", "http://news.google.com/", "http://groups.google.com/", "http://maps.google.com/" }; @@ -205,7 +202,7 @@ keyword such as type of business."" ] }", o.ToString()); - JArray list = o.PropertyValue("Drives"); + JArray list = o.Value("Drives"); Assert.AreEqual(@"[ ""DVD read/writer"", @@ -399,10 +396,8 @@ keyword such as type of business."" //} var postTitles = - from p in rss.PropertyValue("channel") - .PropertyValue("item") - .Children() - select p.PropertyValue("title"); + from p in rss["channel"]["item"].Children() + select p.Value("title"); foreach (var item in postTitles) { @@ -413,11 +408,7 @@ keyword such as type of business."" //Json.NET 1.3 + New license + Now on CodePlex var categories = - from c in rss.PropertyValue("channel") - .PropertyValue("item") - .Children() - .PropertyValues("category") - .Children() + from c in rss["channel"]["item"].Children()["category"].Values() group c by c into g orderby g.Count() descending select new { Category = g.Key, Count = g.Count() }; @@ -431,5 +422,160 @@ keyword such as type of business."" //LINQ - Count: 1 //CodePlex - Count: 1 } + + [Test] + public void BasicQuerying() + { + string json = @"{ + ""channel"": { + ""title"": ""James Newton-King"", + ""link"": ""http://james.newtonking.com"", + ""description"": ""James Newton-King's blog."", + ""item"": [ + { + ""title"": ""Json.NET 1.3 + New license + Now on CodePlex"", + ""description"": ""Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex"", + ""link"": ""http://james.newtonking.com/projects/json-net.aspx"", + ""category"": [ + ""Json.NET"", + ""CodePlex"" + ] + }, + { + ""title"": ""LINQ to JSON beta"", + ""description"": ""Annoucing LINQ to JSON"", + ""link"": ""http://james.newtonking.com/projects/json-net.aspx"", + ""category"": [ + ""Json.NET"", + ""LINQ"" + ] + } + ] + } + }"; + + JObject o = JObject.Parse(json); + + Assert.AreEqual(null, o["purple"]); + Assert.AreEqual(null, o.Value("purple")); + + Assert.IsInstanceOfType(typeof(JArray), o["channel"]["item"]); + + Assert.AreEqual(2, o["channel"]["item"].Children()["title"].Count()); + Assert.AreEqual(0, o["channel"]["item"].Children()["monkey"].Count()); + } + + [Test] + [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Accessed JObject values with invalid key value: 0. Object property name expected.")] + public void JObjectIntIndex() + { + JObject o = new JObject(); + Assert.AreEqual(null, o[0]); + } + + [Test] + [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Accessed JArray values with invalid key value: ""purple"". Array position index expected.")] + public void JArrayStringIndex() + { + JArray a = new JArray(); + Assert.AreEqual(null, a["purple"]); + } + + [Test] + [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Accessed JConstructor values with invalid key value: ""purple"". Argument position index expected.")] + public void JConstructorStringIndex() + { + JConstructor c = new JConstructor(); + Assert.AreEqual(null, c["purple"]); + } + + [Test] + public void ToStringJsonConverter() + { + JObject o = + new JObject( + new JProperty("Test1", new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc)), + new JProperty("Test2", new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0))), + new JProperty("Test3", "Test3Value"), + new JProperty("Test4", null) + ); + + JsonSerializer serializer = new JsonSerializer(); + serializer.Converters.Add(new JavaScriptDateTimeConverter()); + StringWriter sw = new StringWriter(); + JsonWriter writer = new JsonTextWriter(sw); + writer.Formatting = Formatting.Indented; + serializer.Serialize(writer, o); + + string json = sw.ToString(); + + Assert.AreEqual(@"{ + ""Test1"": new Date( + 971586305000 + ), + ""Test2"": new Date( + 971546045000 + ), + ""Test3"": ""Test3Value"", + ""Test4"": null +}", json); + } + + [Test] + public void DateTimeOffset() + { + List testDates = new List { + new DateTimeOffset(new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Utc)), + new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero), + new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.FromHours(13)), + new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.FromHours(-3.5)), + }; + + JsonSerializer jsonSerializer = new JsonSerializer(); + + JsonTokenWriter jsonWriter; + using (jsonWriter = new JsonTokenWriter()) + { + jsonSerializer.Serialize(jsonWriter, testDates); + } + + Assert.AreEqual(4, jsonWriter.Token.Children().Count()); + } + + [Test] + public void FromObject() + { + List posts = GetPosts(); + + JObject o = JObject.FromObject(new + { + channel = new + { + title = "James Newton-King", + link = "http://james.newtonking.com", + description = "James Newton-King's blog.", + item = + from p in posts + orderby p.Title + select new + { + title = p.Title, + description = p.Description, + link = p.Link, + category = p.Categories + } + } + }); + + Console.WriteLine(o.ToString()); + Assert.IsInstanceOfType(typeof(JObject), o); + Assert.IsInstanceOfType(typeof(JObject), o["channel"]); + Assert.AreEqual("James Newton-King", (string)o["channel"]["title"]); + Assert.AreEqual(2, o["channel"]["item"].Children().Count()); + + JArray a = JArray.FromObject(new List() { 0, 1, 2, 3, 4 }); + Assert.IsInstanceOfType(typeof(JArray), a); + Assert.AreEqual(5, a.Count()); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index 2af957f5..767389b3 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -66,13 +66,15 @@ + + - + - + diff --git a/Src/Newtonsoft.Json.Tests/XmlNodeConverterTest.cs b/Src/Newtonsoft.Json.Tests/XmlNodeConverterTest.cs index 65ef870d..b5d93b0e 100644 --- a/Src/Newtonsoft.Json.Tests/XmlNodeConverterTest.cs +++ b/Src/Newtonsoft.Json.Tests/XmlNodeConverterTest.cs @@ -53,7 +53,7 @@ namespace Newtonsoft.Json.Tests StringWriter sw = new StringWriter(); - using (JsonWriter jsonWriter = new JsonWriter(sw)) + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented;