Subversion Repository Public Repository

ChrisCompleteCodeTrunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion


#if NETFX_CORE
using System;
using Newtonsoft.Json.Converters;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#endif
using Windows.Data.Json;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;

namespace Newtonsoft.Json.Tests.Converters
{
    [TestFixture]
    public class JsonValueConverterTests : TestFixtureBase
    {
        public class Computer
        {
            public string Cpu { get; set; }
            public List<string> Drives { get; set; }
        }

        [Test]
        public void WriteJson()
        {
            JsonObject o = JsonObject.Parse(@"{
  ""CPU"": ""Intel"",
  ""Drives"": [
    ""DVD read/writer"",
    ""500 gigabyte hard drive""
  ]
}");

            StringWriter sw = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);

            JsonValueConverter converter = new JsonValueConverter();
            converter.WriteJson(writer, o, null);

            string json = sw.ToString();

            Assert.AreEqual(@"{""Drives"":[""DVD read/writer"",""500 gigabyte hard drive""],""CPU"":""Intel""}", json);
        }

        [Test]
        public void ReadJson()
        {
            string json = @"{
  ""CPU"": ""Intel"",
  ""Drives"": [
    ""DVD read/writer"",
    ""500 gigabyte hard drive""
  ]
}";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            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());
        }

        [Test]
        public void ReadJsonComments()
        {
            string json = @"{/*comment!*/
  ""CPU"": ""Intel"",/*comment!*/
  ""Drives"": [/*comment!*/
    ""DVD read/writer"",
    /*comment!*/""500 gigabyte hard drive""
  ]/*comment!*/
}";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            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());
        }

        [Test]
        public void ReadJsonNullValue()
        {
            string json = "null";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            JsonValueConverter converter = new JsonValueConverter();
            JsonValue v = (JsonValue) converter.ReadJson(writer, typeof (JsonValue), null, null);

            Assert.AreEqual(JsonValueType.Null, v.ValueType);
        }

        [Test]
        public void ReadJsonUnsupportedValue()
        {
            string json = "undefined";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            JsonValueConverter converter = new JsonValueConverter();

            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 = "[";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            JsonValueConverter converter = new JsonValueConverter();

            ExceptionAssert.Throws<JsonException>(() =>
            {
                converter.ReadJson(writer, typeof (JsonValue), null, null);
            }, "Unexpected end when reading JSON. Path '', line 1, position 1.");
        }

        [Test]
        public void ReadJsonUnexpectedEndAfterComment()
        {
            string json = "[/*comment!*/";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            JsonValueConverter converter = new JsonValueConverter();

            ExceptionAssert.Throws<JsonException>(() =>
            {
                converter.ReadJson(writer, typeof (JsonValue), null, null);
            }, "Unexpected end when reading JSON. Path '', line 1, position 13.");
        }

        [Test]
        public void ReadJsonUnexpectedEndInObject()
        {
            string json = "{'hi':";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            JsonValueConverter converter = new JsonValueConverter();

            ExceptionAssert.Throws<JsonException>(() =>
            {
                converter.ReadJson(writer, typeof (JsonValue), null, null);
            }, "Unexpected end when reading JSON. Path 'hi', line 1, position 6.");
        }

        [Test]
        public void ReadJsonBadJsonType()
        {
            string json = "null";

            JsonTextReader writer = new JsonTextReader(new StringReader(json));

            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.");
        }

        [Test]
        public void JsonConvertDeserialize()
        {
            string json = @"[
  ""DVD read/writer"",
  ""500 gigabyte hard drive""
]";

            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());
        }

        [Test]
        public void JsonConvertSerialize()
        {
            JsonArray a = JsonArray.Parse(@"[
  ""DVD read/writer"",
  ""500 gigabyte hard drive""
]");

            string json = JsonConvert.SerializeObject(a, Formatting.Indented);

            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);

            string json = JsonConvert.SerializeObject(o, Formatting.Indented);

            Assert.AreEqual(@"{
  ""PositiveInfinity"": ""Infinity"",
  ""NegativeInfinity"": ""-Infinity"",
  ""MinValue"": -1.7976931348623157E+308,
  ""double"": 1.1,
  ""int"": 1,
  ""zero"": 0,
  ""Epsilon"": 4.94065645841247E-324,
  ""MaxValue"": 1.7976931348623157E+308,
  ""NaN"": ""NaN"",
  ""smallfraction"": 3.0000000000000009,
  ""probablyint"": 1
}", json);
        }

        [Test]
        public void DeserializePerformance()
        {
            string json = @"{
  ""CPU"": ""Intel"",
  ""Drives"": [
    ""DVD read/writer"",
    ""500 gigabyte hard drive""
  ]
}";

            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();

            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();

            // 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();

            string jsonnet = timer.Elapsed.TotalSeconds.ToString();

            Debug.WriteLine("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq);
        }

        [Test]
        public void SerializePerformance()
        {
            string json = @"{
  ""CPU"": ""Intel"",
  ""Drives"": [
    ""DVD read/writer"",
    ""500 gigabyte hard drive""
  ]
}";

            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();

            string winrt = timer.Elapsed.TotalSeconds.ToString();

            timer.Start();
            for (int i = 0; i < 100000; i++)
            {
                o1.ToString(Formatting.None);
            }
            timer.Stop();

            string linq = timer.Elapsed.TotalSeconds.ToString();

            timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < 100000; i++)
            {
                JsonConvert.SerializeObject(o);
            }
            timer.Stop();

            string jsonnet = timer.Elapsed.TotalSeconds.ToString();

            Debug.WriteLine("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq);
        }

        [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>();
        }
    }
}

#endif

Commits for ChrisCompleteCodeTrunk/M3Workflow/Libraries/Json90r1/Source/Src/Newtonsoft.Json.Tests/Converters/JsonValueConverterTests.cs

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000