This commit is contained in:
JamesNK
2008-03-02 03:18:05 +00:00
parent 38f0c6b81d
commit 2fb34b22bb
4 changed files with 183 additions and 30 deletions
+16 -11
View File
@@ -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);
}
+162 -16
View File
@@ -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<JObject> resultObjects = o.PropertyValue<JArray>("results")
.Children<JObject>().ToList();
List<JObject> resultObjects = o["results"].Children<JObject>().ToList();
List<string> resultUrls = o.PropertyValue<JArray>("results")
.Children<JObject>()
.PropertyValues<string>("url")
.ToList();
List<string> resultUrls = o["results"].Children().Values<string>("url").ToList();
List<string> expectedUrls = new List<string>() { "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<JArray>("Drives");
JArray list = o.Value<JArray>("Drives");
Assert.AreEqual(@"[
""DVD read/writer"",
@@ -399,10 +396,8 @@ keyword such as type of business.""
//}
var postTitles =
from p in rss.PropertyValue<JObject>("channel")
.PropertyValue<JArray>("item")
.Children<JObject>()
select p.PropertyValue<string>("title");
from p in rss["channel"]["item"].Children()
select p.Value<string>("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<JObject>("channel")
.PropertyValue<JArray>("item")
.Children<JObject>()
.PropertyValues<JArray>("category")
.Children<string>()
from c in rss["channel"]["item"].Children()["category"].Values<string>()
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<string>("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<DateTimeOffset> testDates = new List<DateTimeOffset> {
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<Post> 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<int>() { 0, 1, 2, 3, 4 });
Assert.IsInstanceOfType(typeof(JArray), a);
Assert.AreEqual(5, a.Count());
}
}
}
@@ -66,13 +66,15 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="JsonTokenReaderTest.cs" />
<Compile Include="JsonTokenWriterTest.cs" />
<Compile Include="DateTimeTestClass.cs" />
<Compile Include="IsoDateTimeConverterTests.cs" />
<Compile Include="JavaScriptConvertTest.cs" />
<Compile Include="JavaScriptDateTimeConverterTests.cs" />
<Compile Include="JsonReaderTest.cs" />
<Compile Include="JsonTextReaderTest.cs" />
<Compile Include="JsonSerializerTest.cs" />
<Compile Include="JsonWriterTest.cs" />
<Compile Include="JsonTextWriterTest.cs" />
<Compile Include="LinqToJsonTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestFixtureBase.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;