-Documentation
This commit is contained in:
@@ -35,6 +35,8 @@ using Windows.Data.Json;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Converters
|
||||
@@ -265,7 +267,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}", json);
|
||||
}
|
||||
|
||||
//[Test]
|
||||
[Test]
|
||||
public void DeserializePerformance()
|
||||
{
|
||||
Stopwatch timer = new Stopwatch();
|
||||
@@ -321,7 +323,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Console.WriteLine(jsonnet);
|
||||
}
|
||||
|
||||
//[Test]
|
||||
[Test]
|
||||
public void SerializePerformance()
|
||||
{
|
||||
JsonObject o = JsonObject.Parse(@"{
|
||||
@@ -373,5 +375,89 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Console.WriteLine(winrt);
|
||||
Console.WriteLine(jsonnet);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseJson()
|
||||
{
|
||||
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""
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}";
|
||||
|
||||
// Windows.Data.Json
|
||||
// -----------------
|
||||
JsonObject jsonObject = JsonObject.Parse(json);
|
||||
string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString();
|
||||
|
||||
// LINQ to JSON
|
||||
// ------------
|
||||
JObject jObject = JObject.Parse(json);
|
||||
string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateJson()
|
||||
{
|
||||
// Windows.Data.Json
|
||||
// -----------------
|
||||
JsonObject jsonObject = new JsonObject
|
||||
{
|
||||
{"CPU", JsonValue.CreateStringValue("Intel")},
|
||||
{"Drives", new JsonArray {
|
||||
JsonValue.CreateStringValue("DVD read/writer"),
|
||||
JsonValue.CreateStringValue("500 gigabyte hard drive")
|
||||
}
|
||||
}
|
||||
};
|
||||
string json1 = jsonObject.Stringify();
|
||||
|
||||
// LINQ to JSON
|
||||
// ------------
|
||||
JObject jObject = new JObject
|
||||
{
|
||||
{"CPU", "Intel"},
|
||||
{"Drives", new JArray {
|
||||
"DVD read/writer",
|
||||
"500 gigabyte hard drive"
|
||||
}
|
||||
}
|
||||
};
|
||||
string json2 = jObject.ToString();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Converting()
|
||||
{
|
||||
JsonObject jsonObject = new JsonObject
|
||||
{
|
||||
{"CPU", JsonValue.CreateStringValue("Intel")},
|
||||
{"Drives", new JsonArray {
|
||||
JsonValue.CreateStringValue("DVD read/writer"),
|
||||
JsonValue.CreateStringValue("500 gigabyte hard drive")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// convert Windows.Data.Json to LINQ to JSON
|
||||
JObject o = JObject.FromObject(jsonObject);
|
||||
|
||||
// convert LINQ to JSON to Windows.Data.Json
|
||||
JArray a = (JArray)o["Drives"];
|
||||
JsonArray jsonArray = a.ToObject<JsonArray>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user