-Fixed jsonpath error when parsing a property that follows an escaped property
-Changed reader/writer/token path to escape property names containing special characters
This commit is contained in:
@@ -43,19 +43,19 @@ using System.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
[TestFixture]
|
||||
public class JsonValueConverterTests : TestFixtureBase
|
||||
{
|
||||
public class Computer
|
||||
[TestFixture]
|
||||
public class JsonValueConverterTests : TestFixtureBase
|
||||
{
|
||||
public string Cpu { get; set; }
|
||||
public List<string> Drives { get; set; }
|
||||
}
|
||||
public class Computer
|
||||
{
|
||||
public string Cpu { get; set; }
|
||||
public List<string> Drives { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteJson()
|
||||
{
|
||||
JsonObject o = JsonObject.Parse(@"{
|
||||
[Test]
|
||||
public void WriteJson()
|
||||
{
|
||||
JsonObject o = JsonObject.Parse(@"{
|
||||
""CPU"": ""Intel"",
|
||||
""Drives"": [
|
||||
""DVD read/writer"",
|
||||
@@ -63,21 +63,21 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
]
|
||||
}");
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw);
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw);
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
converter.WriteJson(writer, o, null);
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
converter.WriteJson(writer, o, null);
|
||||
|
||||
string json = sw.ToString();
|
||||
string json = sw.ToString();
|
||||
|
||||
Assert.AreEqual(@"{""Drives"":[""DVD read/writer"",""500 gigabyte hard drive""],""CPU"":""Intel""}", json);
|
||||
}
|
||||
Assert.AreEqual(@"{""Drives"":[""DVD read/writer"",""500 gigabyte hard drive""],""CPU"":""Intel""}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJson()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void ReadJson()
|
||||
{
|
||||
string json = @"{
|
||||
""CPU"": ""Intel"",
|
||||
""Drives"": [
|
||||
""DVD read/writer"",
|
||||
@@ -85,21 +85,21 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
]
|
||||
}";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonObject o = (JsonObject)converter.ReadJson(writer, typeof(JsonObject), null, null);
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonObject o = (JsonObject) converter.ReadJson(writer, typeof (JsonObject), null, null);
|
||||
|
||||
Assert.AreEqual(2, o.Count);
|
||||
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
|
||||
Assert.AreEqual("DVD read/writer", o.GetNamedArray("Drives")[0].GetString());
|
||||
Assert.AreEqual("500 gigabyte hard drive", o.GetNamedArray("Drives")[1].GetString());
|
||||
}
|
||||
Assert.AreEqual(2, o.Count);
|
||||
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
|
||||
Assert.AreEqual("DVD read/writer", o.GetNamedArray("Drives")[0].GetString());
|
||||
Assert.AreEqual("500 gigabyte hard drive", o.GetNamedArray("Drives")[1].GetString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJsonComments()
|
||||
{
|
||||
string json = @"{/*comment!*/
|
||||
[Test]
|
||||
public void ReadJsonComments()
|
||||
{
|
||||
string json = @"{/*comment!*/
|
||||
""CPU"": ""Intel"",/*comment!*/
|
||||
""Drives"": [/*comment!*/
|
||||
""DVD read/writer"",
|
||||
@@ -107,155 +107,156 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
]/*comment!*/
|
||||
}";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonObject o = (JsonObject)converter.ReadJson(writer, typeof(JsonObject), null, null);
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonObject o = (JsonObject) converter.ReadJson(writer, typeof (JsonObject), null, null);
|
||||
|
||||
Assert.AreEqual(2, o.Count);
|
||||
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
|
||||
Assert.AreEqual("DVD read/writer", o.GetNamedArray("Drives")[0].GetString());
|
||||
Assert.AreEqual("500 gigabyte hard drive", o.GetNamedArray("Drives")[1].GetString());
|
||||
}
|
||||
Assert.AreEqual(2, o.Count);
|
||||
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
|
||||
Assert.AreEqual("DVD read/writer", o.GetNamedArray("Drives")[0].GetString());
|
||||
Assert.AreEqual("500 gigabyte hard drive", o.GetNamedArray("Drives")[1].GetString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJsonNullValue()
|
||||
{
|
||||
string json = "null";
|
||||
[Test]
|
||||
public void ReadJsonNullValue()
|
||||
{
|
||||
string json = "null";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValue v = (JsonValue)converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValue v = (JsonValue) converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
|
||||
Assert.AreEqual(JsonValueType.Null, v.ValueType);
|
||||
}
|
||||
Assert.AreEqual(JsonValueType.Null, v.ValueType);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJsonUnsupportedValue()
|
||||
{
|
||||
string json = "undefined";
|
||||
[Test]
|
||||
public void ReadJsonUnsupportedValue()
|
||||
{
|
||||
string json = "undefined";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
}, "Unexpected or unsupported token: Undefined. Path '', line 1, position 9.");
|
||||
}
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected or unsupported token: Undefined. Path '', line 1, position 9.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJsonUnexpectedEndInArray()
|
||||
{
|
||||
string json = "[";
|
||||
[Test]
|
||||
public void ReadJsonUnexpectedEndInArray()
|
||||
{
|
||||
string json = "[";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
}, "Unexpected end. Path '', line 1, position 1.");
|
||||
}
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected end. Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJsonUnexpectedEndAfterComment()
|
||||
{
|
||||
string json = "[/*comment!*/";
|
||||
[Test]
|
||||
public void ReadJsonUnexpectedEndAfterComment()
|
||||
{
|
||||
string json = "[/*comment!*/";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
}, "Unexpected end. Path '', line 1, position 13.");
|
||||
}
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected end. Path '', line 1, position 13.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJsonUnexpectedEndInObject()
|
||||
{
|
||||
string json = "{'hi':";
|
||||
[Test]
|
||||
public void ReadJsonUnexpectedEndInObject()
|
||||
{
|
||||
string json = "{'hi':";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
}, "Unexpected end. Path 'hi', line 1, position 6.");
|
||||
}
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected end. Path 'hi', line 1, position 6.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadJsonBadJsonType()
|
||||
{
|
||||
string json = "null";
|
||||
[Test]
|
||||
public void ReadJsonBadJsonType()
|
||||
{
|
||||
string json = "null";
|
||||
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonObject), null, null);
|
||||
}, "Could not convert 'Windows.Data.Json.JsonValue' to 'Windows.Data.Json.JsonObject'. Path '', line 1, position 4.");
|
||||
}
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof (JsonObject), null, null);
|
||||
},
|
||||
"Could not convert 'Windows.Data.Json.JsonValue' to 'Windows.Data.Json.JsonObject'. Path '', line 1, position 4.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonConvertDeserialize()
|
||||
{
|
||||
string json = @"[
|
||||
[Test]
|
||||
public void JsonConvertDeserialize()
|
||||
{
|
||||
string json = @"[
|
||||
""DVD read/writer"",
|
||||
""500 gigabyte hard drive""
|
||||
]";
|
||||
|
||||
JsonArray a = JsonConvert.DeserializeObject<JsonArray>(json);
|
||||
JsonArray a = JsonConvert.DeserializeObject<JsonArray>(json);
|
||||
|
||||
Assert.AreEqual(2, a.Count);
|
||||
Assert.AreEqual("DVD read/writer", a[0].GetString());
|
||||
Assert.AreEqual("500 gigabyte hard drive", a[1].GetString());
|
||||
}
|
||||
Assert.AreEqual(2, a.Count);
|
||||
Assert.AreEqual("DVD read/writer", a[0].GetString());
|
||||
Assert.AreEqual("500 gigabyte hard drive", a[1].GetString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonConvertSerialize()
|
||||
{
|
||||
JsonArray a = JsonArray.Parse(@"[
|
||||
[Test]
|
||||
public void JsonConvertSerialize()
|
||||
{
|
||||
JsonArray a = JsonArray.Parse(@"[
|
||||
""DVD read/writer"",
|
||||
""500 gigabyte hard drive""
|
||||
]");
|
||||
|
||||
string json = JsonConvert.SerializeObject(a, Formatting.Indented);
|
||||
string json = JsonConvert.SerializeObject(a, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
Assert.AreEqual(@"[
|
||||
""DVD read/writer"",
|
||||
""500 gigabyte hard drive""
|
||||
]", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeDouble()
|
||||
{
|
||||
JsonObject o = new JsonObject();
|
||||
o["zero"] = JsonValue.CreateNumberValue(0);
|
||||
o["int"] = JsonValue.CreateNumberValue(1);
|
||||
o["smallfraction"] = JsonValue.CreateNumberValue(3.0000000000000009);
|
||||
o["double"] = JsonValue.CreateNumberValue(1.1);
|
||||
o["probablyint"] = JsonValue.CreateNumberValue(1.0);
|
||||
o["Epsilon"] = JsonValue.CreateNumberValue(double.Epsilon);
|
||||
o["MinValue"] = JsonValue.CreateNumberValue(double.MinValue);
|
||||
o["MaxValue"] = JsonValue.CreateNumberValue(double.MaxValue);
|
||||
o["NaN"] = JsonValue.CreateNumberValue(double.NaN);
|
||||
o["NegativeInfinity"] = JsonValue.CreateNumberValue(double.NegativeInfinity);
|
||||
o["PositiveInfinity"] = JsonValue.CreateNumberValue(double.PositiveInfinity);
|
||||
[Test]
|
||||
public void SerializeDouble()
|
||||
{
|
||||
JsonObject o = new JsonObject();
|
||||
o["zero"] = JsonValue.CreateNumberValue(0);
|
||||
o["int"] = JsonValue.CreateNumberValue(1);
|
||||
o["smallfraction"] = JsonValue.CreateNumberValue(3.0000000000000009);
|
||||
o["double"] = JsonValue.CreateNumberValue(1.1);
|
||||
o["probablyint"] = JsonValue.CreateNumberValue(1.0);
|
||||
o["Epsilon"] = JsonValue.CreateNumberValue(double.Epsilon);
|
||||
o["MinValue"] = JsonValue.CreateNumberValue(double.MinValue);
|
||||
o["MaxValue"] = JsonValue.CreateNumberValue(double.MaxValue);
|
||||
o["NaN"] = JsonValue.CreateNumberValue(double.NaN);
|
||||
o["NegativeInfinity"] = JsonValue.CreateNumberValue(double.NegativeInfinity);
|
||||
o["PositiveInfinity"] = JsonValue.CreateNumberValue(double.PositiveInfinity);
|
||||
|
||||
string json = JsonConvert.SerializeObject(o, Formatting.Indented);
|
||||
string json = JsonConvert.SerializeObject(o, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""PositiveInfinity"": ""Infinity"",
|
||||
""NegativeInfinity"": ""-Infinity"",
|
||||
""MinValue"": -1.7976931348623157E+308,
|
||||
@@ -268,12 +269,12 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
""smallfraction"": 3.0000000000000009,
|
||||
""probablyint"": 1
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializePerformance()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void DeserializePerformance()
|
||||
{
|
||||
string json = @"{
|
||||
""CPU"": ""Intel"",
|
||||
""Drives"": [
|
||||
""DVD read/writer"",
|
||||
@@ -281,46 +282,46 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
]
|
||||
}";
|
||||
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
JsonObject o = JsonObject.Parse(json);
|
||||
}
|
||||
timer.Stop();
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
JsonObject o = JsonObject.Parse(json);
|
||||
}
|
||||
timer.Stop();
|
||||
|
||||
string winrt = timer.Elapsed.TotalSeconds.ToString();
|
||||
string winrt = timer.Elapsed.TotalSeconds.ToString();
|
||||
|
||||
timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
JObject o = JObject.Parse(json);
|
||||
}
|
||||
timer.Stop();
|
||||
timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
JObject o = JObject.Parse(json);
|
||||
}
|
||||
timer.Stop();
|
||||
|
||||
string linq = timer.Elapsed.TotalSeconds.ToString();
|
||||
string linq = timer.Elapsed.TotalSeconds.ToString();
|
||||
|
||||
// warm up
|
||||
JsonConvert.DeserializeObject<Computer>(json);
|
||||
// warm up
|
||||
JsonConvert.DeserializeObject<Computer>(json);
|
||||
|
||||
timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
Computer o = JsonConvert.DeserializeObject<Computer>(json);
|
||||
}
|
||||
timer.Stop();
|
||||
timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
Computer o = JsonConvert.DeserializeObject<Computer>(json);
|
||||
}
|
||||
timer.Stop();
|
||||
|
||||
string jsonnet = timer.Elapsed.TotalSeconds.ToString();
|
||||
string jsonnet = timer.Elapsed.TotalSeconds.ToString();
|
||||
|
||||
throw new Exception(string.Format("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq));
|
||||
}
|
||||
Debug.WriteLine("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializePerformance()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void SerializePerformance()
|
||||
{
|
||||
string json = @"{
|
||||
""CPU"": ""Intel"",
|
||||
""Drives"": [
|
||||
""DVD read/writer"",
|
||||
@@ -328,46 +329,46 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
]
|
||||
}";
|
||||
|
||||
JsonObject o = JsonObject.Parse(json);
|
||||
JObject o1 = JObject.Parse(json);
|
||||
Computer o2 = JsonConvert.DeserializeObject<Computer>(json);
|
||||
JsonObject o = JsonObject.Parse(json);
|
||||
JObject o1 = JObject.Parse(json);
|
||||
Computer o2 = JsonConvert.DeserializeObject<Computer>(json);
|
||||
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
o.Stringify();
|
||||
}
|
||||
timer.Stop();
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
o.Stringify();
|
||||
}
|
||||
timer.Stop();
|
||||
|
||||
string winrt = timer.Elapsed.TotalSeconds.ToString();
|
||||
string winrt = timer.Elapsed.TotalSeconds.ToString();
|
||||
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
o1.ToString(Formatting.None);
|
||||
}
|
||||
timer.Stop();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
o1.ToString(Formatting.None);
|
||||
}
|
||||
timer.Stop();
|
||||
|
||||
string linq = timer.Elapsed.TotalSeconds.ToString();
|
||||
string linq = timer.Elapsed.TotalSeconds.ToString();
|
||||
|
||||
timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
JsonConvert.SerializeObject(o);
|
||||
}
|
||||
timer.Stop();
|
||||
timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
JsonConvert.SerializeObject(o);
|
||||
}
|
||||
timer.Stop();
|
||||
|
||||
string jsonnet = timer.Elapsed.TotalSeconds.ToString();
|
||||
string jsonnet = timer.Elapsed.TotalSeconds.ToString();
|
||||
|
||||
throw new Exception(string.Format("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq));
|
||||
}
|
||||
Debug.WriteLine("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseJson()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void ParseJson()
|
||||
{
|
||||
string json = @"{
|
||||
""channel"": {
|
||||
""title"": ""James Newton-King"",
|
||||
""link"": ""http://james.newtonking.com"",
|
||||
@@ -386,67 +387,74 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
}";
|
||||
|
||||
// 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"];
|
||||
}
|
||||
// Windows.Data.Json
|
||||
// -----------------
|
||||
JsonObject jsonObject = JsonObject.Parse(json);
|
||||
string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString();
|
||||
|
||||
[Test]
|
||||
public void CreateJson()
|
||||
{
|
||||
// Windows.Data.Json
|
||||
// -----------------
|
||||
JsonObject jsonObject = new JsonObject
|
||||
// LINQ to JSON
|
||||
// ------------
|
||||
JObject jObject = JObject.Parse(json);
|
||||
string itemTitle2 = (string) jObject["channel"]["item"][0]["title"];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateJson()
|
||||
{
|
||||
{"CPU", JsonValue.CreateStringValue("Intel")},
|
||||
{"Drives", new JsonArray {
|
||||
JsonValue.CreateStringValue("DVD read/writer"),
|
||||
JsonValue.CreateStringValue("500 gigabyte hard drive")
|
||||
}
|
||||
}
|
||||
};
|
||||
string json1 = jsonObject.Stringify();
|
||||
// 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
|
||||
// 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()
|
||||
{
|
||||
{"CPU", "Intel"},
|
||||
{"Drives", new JArray {
|
||||
"DVD read/writer",
|
||||
"500 gigabyte hard drive"
|
||||
}
|
||||
}
|
||||
};
|
||||
string json2 = jObject.ToString();
|
||||
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>();
|
||||
}
|
||||
}
|
||||
|
||||
[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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user