2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using Newtonsoft.Json.Converters;
29
using NUnit.Framework;
31
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
32
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
33
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
34
using Windows.Data.Json;
36
using System.Diagnostics;
37
using Newtonsoft.Json.Linq;
38
using System.Collections.Generic;
42
namespace Newtonsoft.Json.Tests.Converters
45
public class JsonValueConverterTests : TestFixtureBase
49
public string Cpu { get; set; }
50
public List<string> Drives { get; set; }
54
public void WriteJson()
56
JsonObject o = JsonObject.Parse(@"{
60
""500 gigabyte hard drive""
64
StringWriter sw = new StringWriter();
65
JsonTextWriter writer = new JsonTextWriter(sw);
67
JsonValueConverter converter = new JsonValueConverter();
68
converter.WriteJson(writer, o, null);
70
string json = sw.ToString();
72
Assert.AreEqual(@"{""Drives"":[""DVD read/writer"",""500 gigabyte hard drive""],""CPU"":""Intel""}", json);
76
public void ReadJson()
82
""500 gigabyte hard drive""
86
JsonTextReader writer = new JsonTextReader(new StringReader(json));
88
JsonValueConverter converter = new JsonValueConverter();
89
JsonObject o = (JsonObject)converter.ReadJson(writer, typeof(JsonObject), null, null);
91
Assert.AreEqual(2, o.Count);
92
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
93
Assert.AreEqual("DVD read/writer", o.GetNamedArray("Drives")[0].GetString());
94
Assert.AreEqual("500 gigabyte hard drive", o.GetNamedArray("Drives")[1].GetString());
98
public void ReadJsonComments()
100
string json = @"{/*comment!*/
101
""CPU"": ""Intel"",/*comment!*/
102
""Drives"": [/*comment!*/
104
/*comment!*/""500 gigabyte hard drive""
108
JsonTextReader writer = new JsonTextReader(new StringReader(json));
110
JsonValueConverter converter = new JsonValueConverter();
111
JsonObject o = (JsonObject)converter.ReadJson(writer, typeof(JsonObject), null, null);
113
Assert.AreEqual(2, o.Count);
114
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
115
Assert.AreEqual("DVD read/writer", o.GetNamedArray("Drives")[0].GetString());
116
Assert.AreEqual("500 gigabyte hard drive", o.GetNamedArray("Drives")[1].GetString());
120
public void ReadJsonNullValue()
122
string json = "null";
124
JsonTextReader writer = new JsonTextReader(new StringReader(json));
126
JsonValueConverter converter = new JsonValueConverter();
127
JsonValue v = (JsonValue)converter.ReadJson(writer, typeof(JsonValue), null, null);
129
Assert.AreEqual(JsonValueType.Null, v.ValueType);
133
public void ReadJsonUnsupportedValue()
135
string json = "undefined";
137
JsonTextReader writer = new JsonTextReader(new StringReader(json));
139
JsonValueConverter converter = new JsonValueConverter();
141
ExceptionAssert.Throws<JsonException>("Unexpected or unsupported token: Undefined. Path '', line 1, position 9.",
144
converter.ReadJson(writer, typeof(JsonValue), null, null);
149
public void ReadJsonUnexpectedEndInArray()
153
JsonTextReader writer = new JsonTextReader(new StringReader(json));
155
JsonValueConverter converter = new JsonValueConverter();
157
ExceptionAssert.Throws<JsonException>("Unexpected end. Path '', line 1, position 1.",
160
converter.ReadJson(writer, typeof(JsonValue), null, null);
165
public void ReadJsonUnexpectedEndAfterComment()
167
string json = "[/*comment!*/";
169
JsonTextReader writer = new JsonTextReader(new StringReader(json));
171
JsonValueConverter converter = new JsonValueConverter();
173
ExceptionAssert.Throws<JsonException>("Unexpected end. Path '', line 1, position 13.",
176
converter.ReadJson(writer, typeof(JsonValue), null, null);
181
public void ReadJsonUnexpectedEndInObject()
183
string json = "{'hi':";
185
JsonTextReader writer = new JsonTextReader(new StringReader(json));
187
JsonValueConverter converter = new JsonValueConverter();
189
ExceptionAssert.Throws<JsonException>("Unexpected end. Path 'hi', line 1, position 6.",
192
converter.ReadJson(writer, typeof(JsonValue), null, null);
197
public void ReadJsonBadJsonType()
199
string json = "null";
201
JsonTextReader writer = new JsonTextReader(new StringReader(json));
203
JsonValueConverter converter = new JsonValueConverter();
205
ExceptionAssert.Throws<JsonException>("Could not convert 'Windows.Data.Json.JsonValue' to 'Windows.Data.Json.JsonObject'. Path '', line 1, position 4.",
208
converter.ReadJson(writer, typeof(JsonObject), null, null);
213
public void JsonConvertDeserialize()
217
""500 gigabyte hard drive""
220
JsonArray a = JsonConvert.DeserializeObject<JsonArray>(json);
222
Assert.AreEqual(2, a.Count);
223
Assert.AreEqual("DVD read/writer", a[0].GetString());
224
Assert.AreEqual("500 gigabyte hard drive", a[1].GetString());
228
public void JsonConvertSerialize()
230
JsonArray a = JsonArray.Parse(@"[
232
""500 gigabyte hard drive""
235
string json = JsonConvert.SerializeObject(a, Formatting.Indented);
239
""500 gigabyte hard drive""
244
public void SerializeDouble()
246
JsonObject o = new JsonObject();
247
o["zero"] = JsonValue.CreateNumberValue(0);
248
o["int"] = JsonValue.CreateNumberValue(1);
249
o["smallfraction"] = JsonValue.CreateNumberValue(3.0000000000000009);
250
o["double"] = JsonValue.CreateNumberValue(1.1);
251
o["probablyint"] = JsonValue.CreateNumberValue(1.0);
252
o["Epsilon"] = JsonValue.CreateNumberValue(double.Epsilon);
253
o["MinValue"] = JsonValue.CreateNumberValue(double.MinValue);
254
o["MaxValue"] = JsonValue.CreateNumberValue(double.MaxValue);
255
o["NaN"] = JsonValue.CreateNumberValue(double.NaN);
256
o["NegativeInfinity"] = JsonValue.CreateNumberValue(double.NegativeInfinity);
257
o["PositiveInfinity"] = JsonValue.CreateNumberValue(double.PositiveInfinity);
259
string json = JsonConvert.SerializeObject(o, Formatting.Indented);
262
""PositiveInfinity"": Infinity,
263
""NegativeInfinity"": -Infinity,
264
""MinValue"": -1.7976931348623157E+308,
268
""Epsilon"": 4.94065645841247E-324,
269
""MaxValue"": 1.7976931348623157E+308,
271
""smallfraction"": 3.0000000000000009,
277
public void DeserializePerformance()
283
""500 gigabyte hard drive""
287
Stopwatch timer = new Stopwatch();
289
for (int i = 0; i < 100000; i++)
291
JsonObject o = JsonObject.Parse(json);
295
string winrt = timer.Elapsed.TotalSeconds.ToString();
297
timer = new Stopwatch();
299
for (int i = 0; i < 100000; i++)
301
JObject o = JObject.Parse(json);
305
string linq = timer.Elapsed.TotalSeconds.ToString();
308
JsonConvert.DeserializeObject<Computer>(json);
310
timer = new Stopwatch();
312
for (int i = 0; i < 100000; i++)
314
Computer o = JsonConvert.DeserializeObject<Computer>(json);
318
string jsonnet = timer.Elapsed.TotalSeconds.ToString();
320
throw new Exception(string.Format("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq));
321
Console.WriteLine(winrt);
322
Console.WriteLine(jsonnet);
326
public void SerializePerformance()
332
""500 gigabyte hard drive""
336
JsonObject o = JsonObject.Parse(json);
337
JObject o1 = JObject.Parse(json);
338
Computer o2 = JsonConvert.DeserializeObject<Computer>(json);
340
Stopwatch timer = new Stopwatch();
342
for (int i = 0; i < 100000; i++)
348
string winrt = timer.Elapsed.TotalSeconds.ToString();
351
for (int i = 0; i < 100000; i++)
353
o1.ToString(Formatting.None);
357
string linq = timer.Elapsed.TotalSeconds.ToString();
359
timer = new Stopwatch();
361
for (int i = 0; i < 100000; i++)
363
JsonConvert.SerializeObject(o);
367
string jsonnet = timer.Elapsed.TotalSeconds.ToString();
369
throw new Exception(string.Format("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq));
370
Console.WriteLine(winrt);
371
Console.WriteLine(jsonnet);
375
public void ParseJson()
379
""title"": ""James Newton-King"",
380
""link"": ""http://james.newtonking.com"",
381
""description"": ""James Newton-King's blog."",
384
""title"": ""Json.NET 1.3 + New license + Now on CodePlex"",
385
""description"": ""Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex"",
386
""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
398
JsonObject jsonObject = JsonObject.Parse(json);
399
string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString();
403
JObject jObject = JObject.Parse(json);
404
string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];
408
public void CreateJson()
412
JsonObject jsonObject = new JsonObject
414
{"CPU", JsonValue.CreateStringValue("Intel")},
415
{"Drives", new JsonArray {
416
JsonValue.CreateStringValue("DVD read/writer"),
417
JsonValue.CreateStringValue("500 gigabyte hard drive")
421
string json1 = jsonObject.Stringify();
425
JObject jObject = new JObject
428
{"Drives", new JArray {
430
"500 gigabyte hard drive"
434
string json2 = jObject.ToString();
438
public void Converting()
440
JsonObject jsonObject = new JsonObject
442
{"CPU", JsonValue.CreateStringValue("Intel")},
443
{"Drives", new JsonArray {
444
JsonValue.CreateStringValue("DVD read/writer"),
445
JsonValue.CreateStringValue("500 gigabyte hard drive")
450
// convert Windows.Data.Json to LINQ to JSON
451
JObject o = JObject.FromObject(jsonObject);
453
// convert LINQ to JSON to Windows.Data.Json
454
JArray a = (JArray)o["Drives"];
455
JsonArray jsonArray = a.ToObject<JsonArray>();
b'\\ No newline at end of file'