~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
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
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
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.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.Collections.Generic;
 
28
using System.Collections.Specialized;
 
29
using System.ComponentModel;
 
30
using Newtonsoft.Json.Tests.TestObjects;
 
31
#if !NETFX_CORE
 
32
using NUnit.Framework;
 
33
#else
 
34
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
35
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
36
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
37
#endif
 
38
using Newtonsoft.Json.Linq;
 
39
using System.IO;
 
40
using System.Collections;
 
41
#if !PocketPC && !SILVERLIGHT && !NETFX_CORE
 
42
using System.Web.UI;
 
43
#endif
 
44
#if NET20
 
45
using Newtonsoft.Json.Utilities.LinqBridge;
 
46
#else
 
47
using System.Linq;
 
48
#endif
 
49
 
 
50
namespace Newtonsoft.Json.Tests.Linq
 
51
{
 
52
  [TestFixture]
 
53
  public class JObjectTests : TestFixtureBase
 
54
  {
 
55
    [Test]
 
56
    public void WritePropertyWithNoValue()
 
57
    {
 
58
      var o = new JObject();
 
59
      o.Add(new JProperty("novalue"));
 
60
 
 
61
      Assert.AreEqual(@"{
 
62
  ""novalue"": null
 
63
}", o.ToString());
 
64
    }
 
65
 
 
66
    [Test]
 
67
    public void Keys()
 
68
    {
 
69
      var o = new JObject();
 
70
      var d = (IDictionary<string, JToken>) o;
 
71
 
 
72
      Assert.AreEqual(0, d.Keys.Count);
 
73
 
 
74
      o["value"] = true;
 
75
 
 
76
      Assert.AreEqual(1, d.Keys.Count);
 
77
    }
 
78
 
 
79
    [Test]
 
80
    public void TryGetValue()
 
81
    {
 
82
      JObject o = new JObject();
 
83
      o.Add("PropertyNameValue", new JValue(1));
 
84
      Assert.AreEqual(1, o.Children().Count());
 
85
 
 
86
      JToken t;
 
87
      Assert.AreEqual(false, o.TryGetValue("sdf", out t));
 
88
      Assert.AreEqual(null, t);
 
89
 
 
90
      Assert.AreEqual(false, o.TryGetValue(null, out t));
 
91
      Assert.AreEqual(null, t);
 
92
 
 
93
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
 
94
      Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
 
95
    }
 
96
 
 
97
    [Test]
 
98
    public void DictionaryItemShouldSet()
 
99
    {
 
100
      JObject o = new JObject();
 
101
      o["PropertyNameValue"] = new JValue(1);
 
102
      Assert.AreEqual(1, o.Children().Count());
 
103
 
 
104
      JToken t;
 
105
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
 
106
      Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
 
107
 
 
108
      o["PropertyNameValue"] = new JValue(2);
 
109
      Assert.AreEqual(1, o.Children().Count());
 
110
 
 
111
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
 
112
      Assert.AreEqual(true, JToken.DeepEquals(new JValue(2), t));
 
113
 
 
114
      o["PropertyNameValue"] = null;
 
115
      Assert.AreEqual(1, o.Children().Count());
 
116
 
 
117
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
 
118
      Assert.AreEqual(true, JToken.DeepEquals(new JValue((object)null), t));
 
119
    }
 
120
 
 
121
    [Test]
 
122
    public void Remove()
 
123
    {
 
124
      JObject o = new JObject();
 
125
      o.Add("PropertyNameValue", new JValue(1));
 
126
      Assert.AreEqual(1, o.Children().Count());
 
127
 
 
128
      Assert.AreEqual(false, o.Remove("sdf"));
 
129
      Assert.AreEqual(false, o.Remove(null));
 
130
      Assert.AreEqual(true, o.Remove("PropertyNameValue"));
 
131
 
 
132
      Assert.AreEqual(0, o.Children().Count());
 
133
    }
 
134
 
 
135
    [Test]
 
136
    public void GenericCollectionRemove()
 
137
    {
 
138
      JValue v = new JValue(1);
 
139
      JObject o = new JObject();
 
140
      o.Add("PropertyNameValue", v);
 
141
      Assert.AreEqual(1, o.Children().Count());
 
142
 
 
143
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1))));
 
144
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2))));
 
145
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1))));
 
146
      Assert.AreEqual(true, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", v)));
 
147
 
 
148
      Assert.AreEqual(0, o.Children().Count());
 
149
    }
 
150
 
 
151
    [Test]
 
152
    public void DuplicatePropertyNameShouldThrow()
 
153
    {
 
154
      ExceptionAssert.Throws<ArgumentException>(
 
155
        "Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
 
156
        () =>
 
157
        {
 
158
          JObject o = new JObject();
 
159
          o.Add("PropertyNameValue", null);
 
160
          o.Add("PropertyNameValue", null);
 
161
        });
 
162
    }
 
163
 
 
164
    [Test]
 
165
    public void GenericDictionaryAdd()
 
166
    {
 
167
      JObject o = new JObject();
 
168
 
 
169
      o.Add("PropertyNameValue", new JValue(1));
 
170
      Assert.AreEqual(1, (int)o["PropertyNameValue"]);
 
171
 
 
172
      o.Add("PropertyNameValue1", null);
 
173
      Assert.AreEqual(null, ((JValue)o["PropertyNameValue1"]).Value);
 
174
 
 
175
      Assert.AreEqual(2, o.Children().Count());
 
176
    }
 
177
 
 
178
    [Test]
 
179
    public void GenericCollectionAdd()
 
180
    {
 
181
      JObject o = new JObject();
 
182
      ((ICollection<KeyValuePair<string,JToken>>)o).Add(new KeyValuePair<string,JToken>("PropertyNameValue", new JValue(1)));
 
183
 
 
184
      Assert.AreEqual(1, (int)o["PropertyNameValue"]);
 
185
      Assert.AreEqual(1, o.Children().Count());
 
186
    }
 
187
 
 
188
    [Test]
 
189
    public void GenericCollectionClear()
 
190
    {
 
191
      JObject o = new JObject();
 
192
      o.Add("PropertyNameValue", new JValue(1));
 
193
      Assert.AreEqual(1, o.Children().Count());
 
194
 
 
195
      JProperty p = (JProperty)o.Children().ElementAt(0);
 
196
 
 
197
      ((ICollection<KeyValuePair<string, JToken>>)o).Clear();
 
198
      Assert.AreEqual(0, o.Children().Count());
 
199
 
 
200
      Assert.AreEqual(null, p.Parent);
 
201
    }
 
202
 
 
203
    [Test]
 
204
    public void GenericCollectionContains()
 
205
    {
 
206
      JValue v = new JValue(1);
 
207
      JObject o = new JObject();
 
208
      o.Add("PropertyNameValue", v);
 
209
      Assert.AreEqual(1, o.Children().Count());
 
210
 
 
211
      bool contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1)));
 
212
      Assert.AreEqual(false, contains);
 
213
 
 
214
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", v));
 
215
      Assert.AreEqual(true, contains);
 
216
 
 
217
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2)));
 
218
      Assert.AreEqual(false, contains);
 
219
 
 
220
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1)));
 
221
      Assert.AreEqual(false, contains);
 
222
 
 
223
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(default(KeyValuePair<string, JToken>));
 
224
      Assert.AreEqual(false, contains);
 
225
    }
 
226
 
 
227
    [Test]
 
228
    public void GenericDictionaryContains()
 
229
    {
 
230
      JObject o = new JObject();
 
231
      o.Add("PropertyNameValue", new JValue(1));
 
232
      Assert.AreEqual(1, o.Children().Count());
 
233
 
 
234
      bool contains = ((IDictionary<string, JToken>)o).ContainsKey("PropertyNameValue");
 
235
      Assert.AreEqual(true, contains);
 
236
    }
 
237
 
 
238
    [Test]
 
239
    public void GenericCollectionCopyTo()
 
240
    {
 
241
      JObject o = new JObject();
 
242
      o.Add("PropertyNameValue", new JValue(1));
 
243
      o.Add("PropertyNameValue2", new JValue(2));
 
244
      o.Add("PropertyNameValue3", new JValue(3));
 
245
      Assert.AreEqual(3, o.Children().Count());
 
246
 
 
247
      KeyValuePair<string, JToken>[] a = new KeyValuePair<string,JToken>[5];
 
248
 
 
249
      ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(a, 1);
 
250
 
 
251
      Assert.AreEqual(default(KeyValuePair<string,JToken>), a[0]);
 
252
      
 
253
      Assert.AreEqual("PropertyNameValue", a[1].Key);
 
254
      Assert.AreEqual(1, (int)a[1].Value);
 
255
 
 
256
      Assert.AreEqual("PropertyNameValue2", a[2].Key);
 
257
      Assert.AreEqual(2, (int)a[2].Value);
 
258
 
 
259
      Assert.AreEqual("PropertyNameValue3", a[3].Key);
 
260
      Assert.AreEqual(3, (int)a[3].Value);
 
261
 
 
262
      Assert.AreEqual(default(KeyValuePair<string, JToken>), a[4]);
 
263
    }
 
264
 
 
265
    [Test]
 
266
    public void GenericCollectionCopyToNullArrayShouldThrow()
 
267
    {
 
268
      ExceptionAssert.Throws<ArgumentException>(
 
269
        @"Value cannot be null.
 
270
Parameter name: array",
 
271
        () =>
 
272
        {
 
273
          JObject o = new JObject();
 
274
          ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
 
275
        });
 
276
    }
 
277
 
 
278
    [Test]
 
279
    public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
 
280
    {
 
281
      ExceptionAssert.Throws<ArgumentOutOfRangeException>(
 
282
        @"arrayIndex is less than 0.
 
283
Parameter name: arrayIndex",
 
284
        () =>
 
285
        {
 
286
          JObject o = new JObject();
 
287
          ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], -1);
 
288
        });
 
289
    }
 
290
 
 
291
    [Test]
 
292
    public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
 
293
    {
 
294
      ExceptionAssert.Throws<ArgumentException>(
 
295
        @"arrayIndex is equal to or greater than the length of array.",
 
296
        () =>
 
297
        {
 
298
          JObject o = new JObject();
 
299
          ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], 1);
 
300
        });
 
301
    }
 
302
 
 
303
    [Test]
 
304
    public void GenericCollectionCopyToInsufficientArrayCapacity()
 
305
    {
 
306
      ExceptionAssert.Throws<ArgumentException>(
 
307
        @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.",
 
308
        () =>
 
309
        {
 
310
          JObject o = new JObject();
 
311
          o.Add("PropertyNameValue", new JValue(1));
 
312
          o.Add("PropertyNameValue2", new JValue(2));
 
313
          o.Add("PropertyNameValue3", new JValue(3));
 
314
 
 
315
          ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
 
316
        });
 
317
    }
 
318
 
 
319
    [Test]
 
320
    public void FromObjectRaw()
 
321
    {
 
322
      PersonRaw raw = new PersonRaw
 
323
      {
 
324
        FirstName = "FirstNameValue",
 
325
        RawContent = new JRaw("[1,2,3,4,5]"),
 
326
        LastName = "LastNameValue"
 
327
      };
 
328
 
 
329
      JObject o = JObject.FromObject(raw);
 
330
 
 
331
      Assert.AreEqual("FirstNameValue", (string)o["first_name"]);
 
332
      Assert.AreEqual(JTokenType.Raw, ((JValue)o["RawContent"]).Type);
 
333
      Assert.AreEqual("[1,2,3,4,5]", (string)o["RawContent"]);
 
334
      Assert.AreEqual("LastNameValue", (string)o["last_name"]);
 
335
    }
 
336
 
 
337
    [Test]
 
338
    public void JTokenReader()
 
339
    {
 
340
      PersonRaw raw = new PersonRaw
 
341
      {
 
342
        FirstName = "FirstNameValue",
 
343
        RawContent = new JRaw("[1,2,3,4,5]"),
 
344
        LastName = "LastNameValue"
 
345
      };
 
346
 
 
347
      JObject o = JObject.FromObject(raw);
 
348
 
 
349
      JsonReader reader = new JTokenReader(o);
 
350
 
 
351
      Assert.IsTrue(reader.Read());
 
352
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
 
353
 
 
354
      Assert.IsTrue(reader.Read());
 
355
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
 
356
 
 
357
      Assert.IsTrue(reader.Read());
 
358
      Assert.AreEqual(JsonToken.String, reader.TokenType);
 
359
 
 
360
      Assert.IsTrue(reader.Read());
 
361
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
 
362
 
 
363
      Assert.IsTrue(reader.Read());
 
364
      Assert.AreEqual(JsonToken.Raw, reader.TokenType);
 
365
 
 
366
      Assert.IsTrue(reader.Read());
 
367
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
 
368
 
 
369
      Assert.IsTrue(reader.Read());
 
370
      Assert.AreEqual(JsonToken.String, reader.TokenType);
 
371
 
 
372
      Assert.IsTrue(reader.Read());
 
373
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
 
374
 
 
375
      Assert.IsFalse(reader.Read());
 
376
    }
 
377
 
 
378
    [Test]
 
379
    public void DeserializeFromRaw()
 
380
    {
 
381
      PersonRaw raw = new PersonRaw
 
382
      {
 
383
        FirstName = "FirstNameValue",
 
384
        RawContent = new JRaw("[1,2,3,4,5]"),
 
385
        LastName = "LastNameValue"
 
386
      };
 
387
 
 
388
      JObject o = JObject.FromObject(raw);
 
389
 
 
390
      JsonReader reader = new JTokenReader(o);
 
391
      JsonSerializer serializer = new JsonSerializer();
 
392
      raw = (PersonRaw)serializer.Deserialize(reader, typeof(PersonRaw));
 
393
 
 
394
      Assert.AreEqual("FirstNameValue", raw.FirstName);
 
395
      Assert.AreEqual("LastNameValue", raw.LastName);
 
396
      Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Value);
 
397
    }
 
398
 
 
399
    [Test]
 
400
    public void Parse_ShouldThrowOnUnexpectedToken()
 
401
    {
 
402
      ExceptionAssert.Throws<JsonReaderException>("Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.",
 
403
        () =>
 
404
        {
 
405
          string json = @"[""prop""]";
 
406
          JObject.Parse(json);
 
407
        });
 
408
    }
 
409
 
 
410
    [Test]
 
411
    public void ParseJavaScriptDate()
 
412
    {
 
413
      string json = @"[new Date(1207285200000)]";
 
414
 
 
415
      JArray a = (JArray)JsonConvert.DeserializeObject(json);
 
416
      JValue v = (JValue)a[0];
 
417
 
 
418
      Assert.AreEqual(JsonConvert.ConvertJavaScriptTicksToDateTime(1207285200000), (DateTime)v);
 
419
    }
 
420
 
 
421
    [Test]
 
422
    public void GenericValueCast()
 
423
    {
 
424
      string json = @"{""foo"":true}";
 
425
      JObject o = (JObject)JsonConvert.DeserializeObject(json);
 
426
      bool? value = o.Value<bool?>("foo");
 
427
      Assert.AreEqual(true, value);
 
428
 
 
429
      json = @"{""foo"":null}"; 
 
430
      o = (JObject)JsonConvert.DeserializeObject(json);
 
431
      value = o.Value<bool?>("foo");
 
432
      Assert.AreEqual(null, value);
 
433
    }
 
434
 
 
435
    [Test]
 
436
    public void Blog()
 
437
    {
 
438
      ExceptionAssert.Throws<JsonReaderException>(
 
439
        "Invalid property identifier character: ]. Path 'name', line 3, position 5.",
 
440
        () =>
 
441
        {
 
442
          JObject.Parse(@"{
 
443
    ""name"": ""James"",
 
444
    ]!#$THIS IS: BAD JSON![{}}}}]
 
445
  }");
 
446
        });
 
447
    }
 
448
 
 
449
    [Test]
 
450
    public void RawChildValues()
 
451
    {
 
452
      JObject o = new JObject();
 
453
      o["val1"] = new JRaw("1");
 
454
      o["val2"] = new JRaw("1");
 
455
 
 
456
      string json = o.ToString();
 
457
 
 
458
      Assert.AreEqual(@"{
 
459
  ""val1"": 1,
 
460
  ""val2"": 1
 
461
}", json);
 
462
    }
 
463
 
 
464
    [Test]
 
465
    public void Iterate()
 
466
    {
 
467
      JObject o = new JObject();
 
468
      o.Add("PropertyNameValue1", new JValue(1));
 
469
      o.Add("PropertyNameValue2", new JValue(2));
 
470
 
 
471
      JToken t = o;
 
472
 
 
473
      int i = 1;
 
474
      foreach (JProperty property in t)
 
475
      {
 
476
        Assert.AreEqual("PropertyNameValue" + i, property.Name);
 
477
        Assert.AreEqual(i, (int)property.Value);
 
478
 
 
479
        i++;
 
480
      }
 
481
    }
 
482
 
 
483
    [Test]
 
484
    public void KeyValuePairIterate()
 
485
    {
 
486
      JObject o = new JObject();
 
487
      o.Add("PropertyNameValue1", new JValue(1));
 
488
      o.Add("PropertyNameValue2", new JValue(2));
 
489
 
 
490
      int i = 1;
 
491
      foreach (KeyValuePair<string, JToken> pair in o)
 
492
      {
 
493
        Assert.AreEqual("PropertyNameValue" + i, pair.Key);
 
494
        Assert.AreEqual(i, (int)pair.Value);
 
495
 
 
496
        i++;
 
497
      }
 
498
    }
 
499
 
 
500
    [Test]
 
501
    public void WriteObjectNullStringValue()
 
502
    {
 
503
      string s = null;
 
504
      JValue v = new JValue(s);
 
505
      Assert.AreEqual(null, v.Value);
 
506
      Assert.AreEqual(JTokenType.String, v.Type);
 
507
 
 
508
      JObject o = new JObject();
 
509
      o["title"] = v;
 
510
 
 
511
      string output = o.ToString();
 
512
      
 
513
      Assert.AreEqual(@"{
 
514
  ""title"": null
 
515
}", output);
 
516
    }
 
517
 
 
518
    [Test]
 
519
    public void Example()
 
520
    {
 
521
      string json = @"{
 
522
        ""Name"": ""Apple"",
 
523
        ""Expiry"": new Date(1230422400000),
 
524
        ""Price"": 3.99,
 
525
        ""Sizes"": [
 
526
          ""Small"",
 
527
          ""Medium"",
 
528
          ""Large""
 
529
        ]
 
530
      }";
 
531
 
 
532
      JObject o = JObject.Parse(json);
 
533
 
 
534
      string name = (string)o["Name"];
 
535
      // Apple
 
536
 
 
537
      JArray sizes = (JArray)o["Sizes"];
 
538
 
 
539
      string smallest = (string)sizes[0];
 
540
      // Small
 
541
 
 
542
      Console.WriteLine(name);
 
543
      Console.WriteLine(smallest);
 
544
    }
 
545
 
 
546
    [Test]
 
547
    public void DeserializeClassManually()
 
548
    {
 
549
      string jsonText = @"{
 
550
  ""short"":
 
551
  {
 
552
    ""original"":""http://www.foo.com/"",
 
553
    ""short"":""krehqk"",
 
554
    ""error"":
 
555
    {
 
556
      ""code"":0,
 
557
      ""msg"":""No action taken""
 
558
    }
 
559
  }
 
560
}";
 
561
 
 
562
      JObject json = JObject.Parse(jsonText);
 
563
 
 
564
      Shortie shortie = new Shortie
 
565
                        {
 
566
                          Original = (string)json["short"]["original"],
 
567
                          Short = (string)json["short"]["short"],
 
568
                          Error = new ShortieException
 
569
                                  {
 
570
                                    Code = (int)json["short"]["error"]["code"],
 
571
                                    ErrorMessage = (string)json["short"]["error"]["msg"]
 
572
                                  }
 
573
                        };
 
574
 
 
575
      Console.WriteLine(shortie.Original);
 
576
      // http://www.foo.com/
 
577
 
 
578
      Console.WriteLine(shortie.Error.ErrorMessage);
 
579
      // No action taken
 
580
 
 
581
      Assert.AreEqual("http://www.foo.com/", shortie.Original);
 
582
      Assert.AreEqual("krehqk", shortie.Short);
 
583
      Assert.AreEqual(null, shortie.Shortened);
 
584
      Assert.AreEqual(0, shortie.Error.Code);
 
585
      Assert.AreEqual("No action taken", shortie.Error.ErrorMessage);
 
586
    }
 
587
 
 
588
    [Test]
 
589
    public void JObjectContainingHtml()
 
590
    {
 
591
      JObject o = new JObject();
 
592
      o["rc"] = new JValue(200);
 
593
      o["m"] = new JValue("");
 
594
      o["o"] = new JValue(@"<div class='s1'>
 
595
    <div class='avatar'>                    
 
596
        <a href='asdf'>asdf</a><br />
 
597
        <strong>0</strong>
 
598
    </div>
 
599
    <div class='sl'>
 
600
        <p>
 
601
            444444444
 
602
        </p>
 
603
    </div>
 
604
    <div class='clear'>
 
605
    </div>                        
 
606
</div>");
 
607
 
 
608
      Assert.AreEqual(@"{
 
609
  ""rc"": 200,
 
610
  ""m"": """",
 
611
  ""o"": ""<div class='s1'>\r\n    <div class='avatar'>                    \r\n        <a href='asdf'>asdf</a><br />\r\n        <strong>0</strong>\r\n    </div>\r\n    <div class='sl'>\r\n        <p>\r\n            444444444\r\n        </p>\r\n    </div>\r\n    <div class='clear'>\r\n    </div>                        \r\n</div>""
 
612
}", o.ToString());
 
613
    }
 
614
 
 
615
    [Test]
 
616
    public void ImplicitValueConversions()
 
617
    {
 
618
      JObject moss = new JObject();
 
619
      moss["FirstName"] = new JValue("Maurice");
 
620
      moss["LastName"] = new JValue("Moss");
 
621
      moss["BirthDate"] = new JValue(new DateTime(1977, 12, 30));
 
622
      moss["Department"] = new JValue("IT");
 
623
      moss["JobTitle"] = new JValue("Support");
 
624
 
 
625
      Console.WriteLine(moss.ToString());
 
626
      //{
 
627
      //  "FirstName": "Maurice",
 
628
      //  "LastName": "Moss",
 
629
      //  "BirthDate": "\/Date(252241200000+1300)\/",
 
630
      //  "Department": "IT",
 
631
      //  "JobTitle": "Support"
 
632
      //}
 
633
 
 
634
 
 
635
      JObject jen = new JObject();
 
636
      jen["FirstName"] = "Jen";
 
637
      jen["LastName"] = "Barber";
 
638
      jen["BirthDate"] = new DateTime(1978, 3, 15);
 
639
      jen["Department"] = "IT";
 
640
      jen["JobTitle"] = "Manager";
 
641
 
 
642
      Console.WriteLine(jen.ToString());
 
643
      //{
 
644
      //  "FirstName": "Jen",
 
645
      //  "LastName": "Barber",
 
646
      //  "BirthDate": "\/Date(258721200000+1300)\/",
 
647
      //  "Department": "IT",
 
648
      //  "JobTitle": "Manager"
 
649
      //}
 
650
    }
 
651
 
 
652
    [Test]
 
653
    public void ReplaceJPropertyWithJPropertyWithSameName()
 
654
    {
 
655
      JProperty p1 = new JProperty("Test1", 1);
 
656
      JProperty p2 = new JProperty("Test2", "Two");
 
657
 
 
658
      JObject o = new JObject(p1, p2);
 
659
      IList l = o;
 
660
      Assert.AreEqual(p1, l[0]);
 
661
      Assert.AreEqual(p2, l[1]);
 
662
 
 
663
      JProperty p3 = new JProperty("Test1", "III");
 
664
 
 
665
      p1.Replace(p3);
 
666
      Assert.AreEqual(null, p1.Parent);
 
667
      Assert.AreEqual(l, p3.Parent);
 
668
 
 
669
      Assert.AreEqual(p3, l[0]);
 
670
      Assert.AreEqual(p2, l[1]);
 
671
 
 
672
      Assert.AreEqual(2, l.Count);
 
673
      Assert.AreEqual(2, o.Properties().Count());
 
674
 
 
675
      JProperty p4 = new JProperty("Test4", "IV");
 
676
 
 
677
      p2.Replace(p4);
 
678
      Assert.AreEqual(null, p2.Parent);
 
679
      Assert.AreEqual(l, p4.Parent);
 
680
 
 
681
      Assert.AreEqual(p3, l[0]);
 
682
      Assert.AreEqual(p4, l[1]);
 
683
    }
 
684
 
 
685
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
 
686
    [Test]
 
687
    public void PropertyChanging()
 
688
    {
 
689
      object changing = null;
 
690
      object changed = null;
 
691
      int changingCount = 0;
 
692
      int changedCount = 0;
 
693
 
 
694
      JObject o = new JObject();
 
695
      o.PropertyChanging += (sender, args) =>
 
696
        {
 
697
          JObject s = (JObject) sender;
 
698
          changing = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
 
699
          changingCount++;
 
700
        };
 
701
      o.PropertyChanged += (sender, args) =>
 
702
      {
 
703
        JObject s = (JObject)sender;
 
704
        changed = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
 
705
        changedCount++;
 
706
      };
 
707
 
 
708
      o["StringValue"] = "value1";
 
709
      Assert.AreEqual(null, changing);
 
710
      Assert.AreEqual("value1", changed);
 
711
      Assert.AreEqual("value1", (string)o["StringValue"]);
 
712
      Assert.AreEqual(1, changingCount);
 
713
      Assert.AreEqual(1, changedCount);
 
714
 
 
715
      o["StringValue"] = "value1";
 
716
      Assert.AreEqual(1, changingCount);
 
717
      Assert.AreEqual(1, changedCount);
 
718
 
 
719
      o["StringValue"] = "value2";
 
720
      Assert.AreEqual("value1", changing);
 
721
      Assert.AreEqual("value2", changed);
 
722
      Assert.AreEqual("value2", (string)o["StringValue"]);
 
723
      Assert.AreEqual(2, changingCount);
 
724
      Assert.AreEqual(2, changedCount);
 
725
 
 
726
      o["StringValue"] = null;
 
727
      Assert.AreEqual("value2", changing);
 
728
      Assert.AreEqual(null, changed);
 
729
      Assert.AreEqual(null, (string)o["StringValue"]);
 
730
      Assert.AreEqual(3, changingCount);
 
731
      Assert.AreEqual(3, changedCount);
 
732
 
 
733
      o["NullValue"] = null;
 
734
      Assert.AreEqual(null, changing);
 
735
      Assert.AreEqual(null, changed);
 
736
      Assert.AreEqual(new JValue((object)null), o["NullValue"]);
 
737
      Assert.AreEqual(4, changingCount);
 
738
      Assert.AreEqual(4, changedCount);
 
739
 
 
740
      o["NullValue"] = null;
 
741
      Assert.AreEqual(4, changingCount);
 
742
      Assert.AreEqual(4, changedCount);
 
743
    }
 
744
#endif
 
745
 
 
746
    [Test]
 
747
    public void PropertyChanged()
 
748
    {
 
749
      object changed = null;
 
750
      int changedCount = 0;
 
751
 
 
752
      JObject o = new JObject();
 
753
      o.PropertyChanged += (sender, args) =>
 
754
      {
 
755
        JObject s = (JObject)sender;
 
756
        changed = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
 
757
        changedCount++;
 
758
      };
 
759
 
 
760
      o["StringValue"] = "value1";
 
761
      Assert.AreEqual("value1", changed);
 
762
      Assert.AreEqual("value1", (string)o["StringValue"]);
 
763
      Assert.AreEqual(1, changedCount);
 
764
 
 
765
      o["StringValue"] = "value1";
 
766
      Assert.AreEqual(1, changedCount);
 
767
 
 
768
      o["StringValue"] = "value2";
 
769
      Assert.AreEqual("value2", changed);
 
770
      Assert.AreEqual("value2", (string)o["StringValue"]);
 
771
      Assert.AreEqual(2, changedCount);
 
772
 
 
773
      o["StringValue"] = null;
 
774
      Assert.AreEqual(null, changed);
 
775
      Assert.AreEqual(null, (string)o["StringValue"]);
 
776
      Assert.AreEqual(3, changedCount);
 
777
 
 
778
      o["NullValue"] = null;
 
779
      Assert.AreEqual(null, changed);
 
780
      Assert.AreEqual(new JValue((object)null), o["NullValue"]);
 
781
      Assert.AreEqual(4, changedCount);
 
782
 
 
783
      o["NullValue"] = null;
 
784
      Assert.AreEqual(4, changedCount);
 
785
    }
 
786
 
 
787
    [Test]
 
788
    public void IListContains()
 
789
    {
 
790
      JProperty p = new JProperty("Test", 1);
 
791
      IList l = new JObject(p);
 
792
 
 
793
      Assert.IsTrue(l.Contains(p));
 
794
      Assert.IsFalse(l.Contains(new JProperty("Test", 1)));
 
795
    }
 
796
 
 
797
    [Test]
 
798
    public void IListIndexOf()
 
799
    {
 
800
      JProperty p = new JProperty("Test", 1);
 
801
      IList l = new JObject(p);
 
802
 
 
803
      Assert.AreEqual(0, l.IndexOf(p));
 
804
      Assert.AreEqual(-1, l.IndexOf(new JProperty("Test", 1)));
 
805
    }
 
806
 
 
807
    [Test]
 
808
    public void IListClear()
 
809
    {
 
810
      JProperty p = new JProperty("Test", 1);
 
811
      IList l = new JObject(p);
 
812
 
 
813
      Assert.AreEqual(1, l.Count);
 
814
 
 
815
      l.Clear();
 
816
 
 
817
      Assert.AreEqual(0, l.Count);
 
818
    }
 
819
 
 
820
    [Test]
 
821
    public void IListCopyTo()
 
822
    {
 
823
      JProperty p1 = new JProperty("Test1", 1);
 
824
      JProperty p2 = new JProperty("Test2", "Two");
 
825
      IList l = new JObject(p1, p2);
 
826
 
 
827
      object[] a = new object[l.Count];
 
828
 
 
829
      l.CopyTo(a, 0);
 
830
 
 
831
      Assert.AreEqual(p1, a[0]);
 
832
      Assert.AreEqual(p2, a[1]);
 
833
    }
 
834
 
 
835
    [Test]
 
836
    public void IListAdd()
 
837
    {
 
838
      JProperty p1 = new JProperty("Test1", 1);
 
839
      JProperty p2 = new JProperty("Test2", "Two");
 
840
      IList l = new JObject(p1, p2);
 
841
 
 
842
      JProperty p3 = new JProperty("Test3", "III");
 
843
 
 
844
      l.Add(p3);
 
845
 
 
846
      Assert.AreEqual(3, l.Count);
 
847
      Assert.AreEqual(p3, l[2]);
 
848
    }
 
849
 
 
850
    [Test]
 
851
    public void IListAddBadToken()
 
852
    {
 
853
      ExceptionAssert.Throws<ArgumentException>(
 
854
      "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
 
855
      () =>
 
856
      {
 
857
        JProperty p1 = new JProperty("Test1", 1);
 
858
        JProperty p2 = new JProperty("Test2", "Two");
 
859
        IList l = new JObject(p1, p2);
 
860
 
 
861
        l.Add(new JValue("Bad!"));
 
862
      });
 
863
    }
 
864
 
 
865
    [Test]
 
866
    public void IListAddBadValue()
 
867
    {
 
868
      ExceptionAssert.Throws<ArgumentException>(
 
869
      "Argument is not a JToken.",
 
870
      () =>
 
871
      {
 
872
        JProperty p1 = new JProperty("Test1", 1);
 
873
        JProperty p2 = new JProperty("Test2", "Two");
 
874
        IList l = new JObject(p1, p2);
 
875
 
 
876
        l.Add("Bad!");
 
877
      });
 
878
    }
 
879
 
 
880
    [Test]
 
881
    public void IListAddPropertyWithExistingName()
 
882
    {
 
883
      ExceptionAssert.Throws<ArgumentException>(
 
884
      "Can not add property Test2 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
 
885
      () =>
 
886
      {
 
887
        JProperty p1 = new JProperty("Test1", 1);
 
888
        JProperty p2 = new JProperty("Test2", "Two");
 
889
        IList l = new JObject(p1, p2);
 
890
 
 
891
        JProperty p3 = new JProperty("Test2", "II");
 
892
 
 
893
        l.Add(p3);
 
894
      });
 
895
    }
 
896
 
 
897
    [Test]
 
898
    public void IListRemove()
 
899
    {
 
900
      JProperty p1 = new JProperty("Test1", 1);
 
901
      JProperty p2 = new JProperty("Test2", "Two");
 
902
      IList l = new JObject(p1, p2);
 
903
 
 
904
      JProperty p3 = new JProperty("Test3", "III");
 
905
 
 
906
      // won't do anything
 
907
      l.Remove(p3);
 
908
      Assert.AreEqual(2, l.Count);
 
909
 
 
910
      l.Remove(p1);
 
911
      Assert.AreEqual(1, l.Count);
 
912
      Assert.IsFalse(l.Contains(p1));
 
913
      Assert.IsTrue(l.Contains(p2));
 
914
 
 
915
      l.Remove(p2);
 
916
      Assert.AreEqual(0, l.Count);
 
917
      Assert.IsFalse(l.Contains(p2));
 
918
      Assert.AreEqual(null, p2.Parent);
 
919
    }
 
920
 
 
921
    [Test]
 
922
    public void IListRemoveAt()
 
923
    {
 
924
      JProperty p1 = new JProperty("Test1", 1);
 
925
      JProperty p2 = new JProperty("Test2", "Two");
 
926
      IList l = new JObject(p1, p2);
 
927
 
 
928
      // won't do anything
 
929
      l.RemoveAt(0);
 
930
 
 
931
      l.Remove(p1);
 
932
      Assert.AreEqual(1, l.Count);
 
933
 
 
934
      l.Remove(p2);
 
935
      Assert.AreEqual(0, l.Count);
 
936
    }
 
937
 
 
938
    [Test]
 
939
    public void IListInsert()
 
940
    {
 
941
      JProperty p1 = new JProperty("Test1", 1);
 
942
      JProperty p2 = new JProperty("Test2", "Two");
 
943
      IList l = new JObject(p1, p2);
 
944
 
 
945
      JProperty p3 = new JProperty("Test3", "III");
 
946
 
 
947
      l.Insert(1, p3);
 
948
      Assert.AreEqual(l, p3.Parent);
 
949
 
 
950
      Assert.AreEqual(p1, l[0]);
 
951
      Assert.AreEqual(p3, l[1]);
 
952
      Assert.AreEqual(p2, l[2]);
 
953
    }
 
954
 
 
955
    [Test]
 
956
    public void IListIsReadOnly()
 
957
    {
 
958
      IList l = new JObject();
 
959
      Assert.IsFalse(l.IsReadOnly);
 
960
    }
 
961
 
 
962
    [Test]
 
963
    public void IListIsFixedSize()
 
964
    {
 
965
      IList l = new JObject();
 
966
      Assert.IsFalse(l.IsFixedSize);
 
967
    }
 
968
 
 
969
    [Test]
 
970
    public void IListSetItem()
 
971
    {
 
972
      JProperty p1 = new JProperty("Test1", 1);
 
973
      JProperty p2 = new JProperty("Test2", "Two");
 
974
      IList l = new JObject(p1, p2);
 
975
 
 
976
      JProperty p3 = new JProperty("Test3", "III");
 
977
 
 
978
      l[0] = p3;
 
979
 
 
980
      Assert.AreEqual(p3, l[0]);
 
981
      Assert.AreEqual(p2, l[1]);
 
982
    }
 
983
 
 
984
    [Test]
 
985
    public void IListSetItemAlreadyExists()
 
986
    {
 
987
      ExceptionAssert.Throws<ArgumentException>(
 
988
      "Can not add property Test3 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
 
989
      () =>
 
990
      {
 
991
        JProperty p1 = new JProperty("Test1", 1);
 
992
        JProperty p2 = new JProperty("Test2", "Two");
 
993
        IList l = new JObject(p1, p2);
 
994
 
 
995
        JProperty p3 = new JProperty("Test3", "III");
 
996
 
 
997
        l[0] = p3;
 
998
        l[1] = p3;
 
999
      });
 
1000
    }
 
1001
 
 
1002
    [Test]
 
1003
    public void IListSetItemInvalid()
 
1004
    {
 
1005
      ExceptionAssert.Throws<ArgumentException>(
 
1006
      @"Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
 
1007
      () =>
 
1008
      {
 
1009
        JProperty p1 = new JProperty("Test1", 1);
 
1010
        JProperty p2 = new JProperty("Test2", "Two");
 
1011
        IList l = new JObject(p1, p2);
 
1012
 
 
1013
        l[0] = new JValue(true);
 
1014
      });
 
1015
    }
 
1016
 
 
1017
    [Test]
 
1018
    public void IListSyncRoot()
 
1019
    {
 
1020
      JProperty p1 = new JProperty("Test1", 1);
 
1021
      JProperty p2 = new JProperty("Test2", "Two");
 
1022
      IList l = new JObject(p1, p2);
 
1023
 
 
1024
      Assert.IsNotNull(l.SyncRoot);
 
1025
    }
 
1026
 
 
1027
    [Test]
 
1028
    public void IListIsSynchronized()
 
1029
    {
 
1030
      JProperty p1 = new JProperty("Test1", 1);
 
1031
      JProperty p2 = new JProperty("Test2", "Two");
 
1032
      IList l = new JObject(p1, p2);
 
1033
 
 
1034
      Assert.IsFalse(l.IsSynchronized);
 
1035
    }
 
1036
 
 
1037
    [Test]
 
1038
    public void GenericListJTokenContains()
 
1039
    {
 
1040
      JProperty p = new JProperty("Test", 1);
 
1041
      IList<JToken> l = new JObject(p);
 
1042
 
 
1043
      Assert.IsTrue(l.Contains(p));
 
1044
      Assert.IsFalse(l.Contains(new JProperty("Test", 1)));
 
1045
    }
 
1046
 
 
1047
    [Test]
 
1048
    public void GenericListJTokenIndexOf()
 
1049
    {
 
1050
      JProperty p = new JProperty("Test", 1);
 
1051
      IList<JToken> l = new JObject(p);
 
1052
 
 
1053
      Assert.AreEqual(0, l.IndexOf(p));
 
1054
      Assert.AreEqual(-1, l.IndexOf(new JProperty("Test", 1)));
 
1055
    }
 
1056
 
 
1057
    [Test]
 
1058
    public void GenericListJTokenClear()
 
1059
    {
 
1060
      JProperty p = new JProperty("Test", 1);
 
1061
      IList<JToken> l = new JObject(p);
 
1062
 
 
1063
      Assert.AreEqual(1, l.Count);
 
1064
 
 
1065
      l.Clear();
 
1066
 
 
1067
      Assert.AreEqual(0, l.Count);
 
1068
    }
 
1069
 
 
1070
    [Test]
 
1071
    public void GenericListJTokenCopyTo()
 
1072
    {
 
1073
      JProperty p1 = new JProperty("Test1", 1);
 
1074
      JProperty p2 = new JProperty("Test2", "Two");
 
1075
      IList<JToken> l = new JObject(p1, p2);
 
1076
 
 
1077
      JToken[] a = new JToken[l.Count];
 
1078
 
 
1079
      l.CopyTo(a, 0);
 
1080
 
 
1081
      Assert.AreEqual(p1, a[0]);
 
1082
      Assert.AreEqual(p2, a[1]);
 
1083
    }
 
1084
 
 
1085
    [Test]
 
1086
    public void GenericListJTokenAdd()
 
1087
    {
 
1088
      JProperty p1 = new JProperty("Test1", 1);
 
1089
      JProperty p2 = new JProperty("Test2", "Two");
 
1090
      IList<JToken> l = new JObject(p1, p2);
 
1091
 
 
1092
      JProperty p3 = new JProperty("Test3", "III");
 
1093
 
 
1094
      l.Add(p3);
 
1095
 
 
1096
      Assert.AreEqual(3, l.Count);
 
1097
      Assert.AreEqual(p3, l[2]);
 
1098
    }
 
1099
 
 
1100
    [Test]
 
1101
    public void GenericListJTokenAddBadToken()
 
1102
    {
 
1103
      ExceptionAssert.Throws<ArgumentException>("Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
 
1104
      () =>
 
1105
      {
 
1106
        JProperty p1 = new JProperty("Test1", 1);
 
1107
        JProperty p2 = new JProperty("Test2", "Two");
 
1108
        IList<JToken> l = new JObject(p1, p2);
 
1109
 
 
1110
        l.Add(new JValue("Bad!"));
 
1111
      });
 
1112
    }
 
1113
 
 
1114
    [Test]
 
1115
    public void GenericListJTokenAddBadValue()
 
1116
    {
 
1117
      ExceptionAssert.Throws<ArgumentException>("Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
 
1118
        () =>
 
1119
        {
 
1120
          JProperty p1 = new JProperty("Test1", 1);
 
1121
          JProperty p2 = new JProperty("Test2", "Two");
 
1122
          IList<JToken> l = new JObject(p1, p2);
 
1123
 
 
1124
          // string is implicitly converted to JValue
 
1125
          l.Add("Bad!");
 
1126
        });
 
1127
    }
 
1128
 
 
1129
    [Test]
 
1130
    public void GenericListJTokenAddPropertyWithExistingName()
 
1131
    {
 
1132
      ExceptionAssert.Throws<ArgumentException>("Can not add property Test2 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
 
1133
        () =>
 
1134
        {
 
1135
          JProperty p1 = new JProperty("Test1", 1);
 
1136
          JProperty p2 = new JProperty("Test2", "Two");
 
1137
          IList<JToken> l = new JObject(p1, p2);
 
1138
 
 
1139
          JProperty p3 = new JProperty("Test2", "II");
 
1140
 
 
1141
          l.Add(p3);
 
1142
        });
 
1143
    }
 
1144
 
 
1145
    [Test]
 
1146
    public void GenericListJTokenRemove()
 
1147
    {
 
1148
      JProperty p1 = new JProperty("Test1", 1);
 
1149
      JProperty p2 = new JProperty("Test2", "Two");
 
1150
      IList<JToken> l = new JObject(p1, p2);
 
1151
 
 
1152
      JProperty p3 = new JProperty("Test3", "III");
 
1153
 
 
1154
      // won't do anything
 
1155
      Assert.IsFalse(l.Remove(p3));
 
1156
      Assert.AreEqual(2, l.Count);
 
1157
 
 
1158
      Assert.IsTrue(l.Remove(p1));
 
1159
      Assert.AreEqual(1, l.Count);
 
1160
      Assert.IsFalse(l.Contains(p1));
 
1161
      Assert.IsTrue(l.Contains(p2));
 
1162
 
 
1163
      Assert.IsTrue(l.Remove(p2));
 
1164
      Assert.AreEqual(0, l.Count);
 
1165
      Assert.IsFalse(l.Contains(p2));
 
1166
      Assert.AreEqual(null, p2.Parent);
 
1167
    }
 
1168
 
 
1169
    [Test]
 
1170
    public void GenericListJTokenRemoveAt()
 
1171
    {
 
1172
      JProperty p1 = new JProperty("Test1", 1);
 
1173
      JProperty p2 = new JProperty("Test2", "Two");
 
1174
      IList<JToken> l = new JObject(p1, p2);
 
1175
 
 
1176
      // won't do anything
 
1177
      l.RemoveAt(0);
 
1178
 
 
1179
      l.Remove(p1);
 
1180
      Assert.AreEqual(1, l.Count);
 
1181
 
 
1182
      l.Remove(p2);
 
1183
      Assert.AreEqual(0, l.Count);
 
1184
    }
 
1185
 
 
1186
    [Test]
 
1187
    public void GenericListJTokenInsert()
 
1188
    {
 
1189
      JProperty p1 = new JProperty("Test1", 1);
 
1190
      JProperty p2 = new JProperty("Test2", "Two");
 
1191
      IList<JToken> l = new JObject(p1, p2);
 
1192
 
 
1193
      JProperty p3 = new JProperty("Test3", "III");
 
1194
 
 
1195
      l.Insert(1, p3);
 
1196
      Assert.AreEqual(l, p3.Parent);
 
1197
 
 
1198
      Assert.AreEqual(p1, l[0]);
 
1199
      Assert.AreEqual(p3, l[1]);
 
1200
      Assert.AreEqual(p2, l[2]);
 
1201
    }
 
1202
 
 
1203
    [Test]
 
1204
    public void GenericListJTokenIsReadOnly()
 
1205
    {
 
1206
      IList<JToken> l = new JObject();
 
1207
      Assert.IsFalse(l.IsReadOnly);
 
1208
    }
 
1209
 
 
1210
    [Test]
 
1211
    public void GenericListJTokenSetItem()
 
1212
    {
 
1213
      JProperty p1 = new JProperty("Test1", 1);
 
1214
      JProperty p2 = new JProperty("Test2", "Two");
 
1215
      IList<JToken> l = new JObject(p1, p2);
 
1216
 
 
1217
      JProperty p3 = new JProperty("Test3", "III");
 
1218
 
 
1219
      l[0] = p3;
 
1220
 
 
1221
      Assert.AreEqual(p3, l[0]);
 
1222
      Assert.AreEqual(p2, l[1]);
 
1223
    }
 
1224
 
 
1225
    [Test]
 
1226
    public void GenericListJTokenSetItemAlreadyExists()
 
1227
    {
 
1228
      ExceptionAssert.Throws<ArgumentException>("Can not add property Test3 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
 
1229
      () =>
 
1230
      {
 
1231
        JProperty p1 = new JProperty("Test1", 1);
 
1232
        JProperty p2 = new JProperty("Test2", "Two");
 
1233
        IList<JToken> l = new JObject(p1, p2);
 
1234
 
 
1235
        JProperty p3 = new JProperty("Test3", "III");
 
1236
 
 
1237
        l[0] = p3;
 
1238
        l[1] = p3;
 
1239
      });
 
1240
    }
 
1241
 
 
1242
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
1243
    [Test]
 
1244
    public void IBindingListSortDirection()
 
1245
    {
 
1246
      IBindingList l = new JObject();
 
1247
      Assert.AreEqual(ListSortDirection.Ascending, l.SortDirection);
 
1248
    }
 
1249
 
 
1250
    [Test]
 
1251
    public void IBindingListSortProperty()
 
1252
    {
 
1253
      IBindingList l = new JObject();
 
1254
      Assert.AreEqual(null, l.SortProperty);
 
1255
    }
 
1256
 
 
1257
    [Test]
 
1258
    public void IBindingListSupportsChangeNotification()
 
1259
    {
 
1260
      IBindingList l = new JObject();
 
1261
      Assert.AreEqual(true, l.SupportsChangeNotification);
 
1262
    }
 
1263
 
 
1264
    [Test]
 
1265
    public void IBindingListSupportsSearching()
 
1266
    {
 
1267
      IBindingList l = new JObject();
 
1268
      Assert.AreEqual(false, l.SupportsSearching);
 
1269
    }
 
1270
 
 
1271
    [Test]
 
1272
    public void IBindingListSupportsSorting()
 
1273
    {
 
1274
      IBindingList l = new JObject();
 
1275
      Assert.AreEqual(false, l.SupportsSorting);
 
1276
    }
 
1277
 
 
1278
    [Test]
 
1279
    public void IBindingListAllowEdit()
 
1280
    {
 
1281
      IBindingList l = new JObject();
 
1282
      Assert.AreEqual(true, l.AllowEdit);
 
1283
    }
 
1284
 
 
1285
    [Test]
 
1286
    public void IBindingListAllowNew()
 
1287
    {
 
1288
      IBindingList l = new JObject();
 
1289
      Assert.AreEqual(true, l.AllowNew);
 
1290
    }
 
1291
 
 
1292
    [Test]
 
1293
    public void IBindingListAllowRemove()
 
1294
    {
 
1295
      IBindingList l = new JObject();
 
1296
      Assert.AreEqual(true, l.AllowRemove);
 
1297
    }
 
1298
 
 
1299
    [Test]
 
1300
    public void IBindingListAddIndex()
 
1301
    {
 
1302
      IBindingList l = new JObject();
 
1303
      // do nothing
 
1304
      l.AddIndex(null);
 
1305
    }
 
1306
 
 
1307
    [Test]
 
1308
    public void IBindingListApplySort()
 
1309
    {
 
1310
      ExceptionAssert.Throws<NotSupportedException>(
 
1311
        "Specified method is not supported.",
 
1312
        () =>
 
1313
        {
 
1314
          IBindingList l = new JObject();
 
1315
          l.ApplySort(null, ListSortDirection.Ascending);
 
1316
        });
 
1317
    }
 
1318
 
 
1319
    [Test]
 
1320
    public void IBindingListRemoveSort()
 
1321
    {
 
1322
      ExceptionAssert.Throws<NotSupportedException>(
 
1323
        "Specified method is not supported.",
 
1324
        () =>
 
1325
        {
 
1326
          IBindingList l = new JObject();
 
1327
          l.RemoveSort();
 
1328
        });
 
1329
    }
 
1330
 
 
1331
    [Test]
 
1332
    public void IBindingListRemoveIndex()
 
1333
    {
 
1334
      IBindingList l = new JObject();
 
1335
      // do nothing
 
1336
      l.RemoveIndex(null);
 
1337
    }
 
1338
 
 
1339
    [Test]
 
1340
    public void IBindingListFind()
 
1341
    {
 
1342
      ExceptionAssert.Throws<NotSupportedException>(
 
1343
        "Specified method is not supported.",
 
1344
        () =>
 
1345
        {
 
1346
          IBindingList l = new JObject();
 
1347
          l.Find(null, null);
 
1348
        });
 
1349
    }
 
1350
 
 
1351
    [Test]
 
1352
    public void IBindingListIsSorted()
 
1353
    {
 
1354
      IBindingList l = new JObject();
 
1355
      Assert.AreEqual(false, l.IsSorted);
 
1356
    }
 
1357
 
 
1358
    [Test]
 
1359
    public void IBindingListAddNew()
 
1360
    {
 
1361
      ExceptionAssert.Throws<JsonException>(
 
1362
        "Could not determine new value to add to 'Newtonsoft.Json.Linq.JObject'.",
 
1363
        () =>
 
1364
        {
 
1365
          IBindingList l = new JObject();
 
1366
          l.AddNew();
 
1367
        });
 
1368
    }
 
1369
 
 
1370
    [Test]
 
1371
    public void IBindingListAddNewWithEvent()
 
1372
    {
 
1373
      JObject o = new JObject();
 
1374
      o._addingNew += (s, e) => e.NewObject = new JProperty("Property!");
 
1375
 
 
1376
      IBindingList l = o;
 
1377
      object newObject = l.AddNew();
 
1378
      Assert.IsNotNull(newObject);
 
1379
 
 
1380
      JProperty p = (JProperty) newObject;
 
1381
      Assert.AreEqual("Property!", p.Name);
 
1382
      Assert.AreEqual(o, p.Parent);
 
1383
    }
 
1384
 
 
1385
    [Test]
 
1386
    public void ITypedListGetListName()
 
1387
    {
 
1388
      JProperty p1 = new JProperty("Test1", 1);
 
1389
      JProperty p2 = new JProperty("Test2", "Two");
 
1390
      ITypedList l = new JObject(p1, p2);
 
1391
 
 
1392
      Assert.AreEqual(string.Empty, l.GetListName(null));
 
1393
    }
 
1394
 
 
1395
    [Test]
 
1396
    public void ITypedListGetItemProperties()
 
1397
    {
 
1398
      JProperty p1 = new JProperty("Test1", 1);
 
1399
      JProperty p2 = new JProperty("Test2", "Two");
 
1400
      ITypedList l = new JObject(p1, p2);
 
1401
 
 
1402
      PropertyDescriptorCollection propertyDescriptors = l.GetItemProperties(null);
 
1403
      Assert.IsNull(propertyDescriptors);
 
1404
    }
 
1405
 
 
1406
    [Test]
 
1407
    public void ListChanged()
 
1408
    {
 
1409
      JProperty p1 = new JProperty("Test1", 1);
 
1410
      JProperty p2 = new JProperty("Test2", "Two");
 
1411
      JObject o = new JObject(p1, p2);
 
1412
 
 
1413
      ListChangedType? changedType = null;
 
1414
      int? index = null;
 
1415
      
 
1416
      o.ListChanged += (s, a) =>
 
1417
        {
 
1418
          changedType = a.ListChangedType;
 
1419
          index = a.NewIndex;
 
1420
        };
 
1421
 
 
1422
      JProperty p3 = new JProperty("Test3", "III");
 
1423
 
 
1424
      o.Add(p3);
 
1425
      Assert.AreEqual(changedType, ListChangedType.ItemAdded);
 
1426
      Assert.AreEqual(index, 2);
 
1427
      Assert.AreEqual(p3, ((IList<JToken>)o)[index.Value]);
 
1428
 
 
1429
      JProperty p4 = new JProperty("Test4", "IV");
 
1430
 
 
1431
      ((IList<JToken>) o)[index.Value] = p4;
 
1432
      Assert.AreEqual(changedType, ListChangedType.ItemChanged);
 
1433
      Assert.AreEqual(index, 2);
 
1434
      Assert.AreEqual(p4, ((IList<JToken>)o)[index.Value]);
 
1435
      Assert.IsFalse(((IList<JToken>)o).Contains(p3));
 
1436
      Assert.IsTrue(((IList<JToken>)o).Contains(p4));
 
1437
 
 
1438
      o["Test1"] = 2;
 
1439
      Assert.AreEqual(changedType, ListChangedType.ItemChanged);
 
1440
      Assert.AreEqual(index, 0);
 
1441
      Assert.AreEqual(2, (int)o["Test1"]);
 
1442
    }
 
1443
#endif
 
1444
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
 
1445
    [Test]
 
1446
    public void CollectionChanged()
 
1447
    {
 
1448
      JProperty p1 = new JProperty("Test1", 1);
 
1449
      JProperty p2 = new JProperty("Test2", "Two");
 
1450
      JObject o = new JObject(p1, p2);
 
1451
 
 
1452
      NotifyCollectionChangedAction? changedType = null;
 
1453
      int? index = null;
 
1454
 
 
1455
      o._collectionChanged += (s, a) =>
 
1456
      {
 
1457
        changedType = a.Action;
 
1458
        index = a.NewStartingIndex;
 
1459
      };
 
1460
 
 
1461
      JProperty p3 = new JProperty("Test3", "III");
 
1462
 
 
1463
      o.Add(p3);
 
1464
      Assert.AreEqual(changedType, NotifyCollectionChangedAction.Add);
 
1465
      Assert.AreEqual(index, 2);
 
1466
      Assert.AreEqual(p3, ((IList<JToken>)o)[index.Value]);
 
1467
 
 
1468
      JProperty p4 = new JProperty("Test4", "IV");
 
1469
 
 
1470
      ((IList<JToken>)o)[index.Value] = p4;
 
1471
      Assert.AreEqual(changedType, NotifyCollectionChangedAction.Replace);
 
1472
      Assert.AreEqual(index, 2);
 
1473
      Assert.AreEqual(p4, ((IList<JToken>)o)[index.Value]);
 
1474
      Assert.IsFalse(((IList<JToken>)o).Contains(p3));
 
1475
      Assert.IsTrue(((IList<JToken>)o).Contains(p4));
 
1476
 
 
1477
      o["Test1"] = 2;
 
1478
      Assert.AreEqual(changedType, NotifyCollectionChangedAction.Replace);
 
1479
      Assert.AreEqual(index, 0);
 
1480
      Assert.AreEqual(2, (int)o["Test1"]);
 
1481
    }
 
1482
#endif
 
1483
 
 
1484
    [Test]
 
1485
    public void GetGeocodeAddress()
 
1486
    {
 
1487
      string json = @"{
 
1488
  ""name"": ""Address: 435 North Mulford Road Rockford, IL 61107"",
 
1489
  ""Status"": {
 
1490
    ""code"": 200,
 
1491
    ""request"": ""geocode""
 
1492
  },
 
1493
  ""Placemark"": [ {
 
1494
    ""id"": ""p1"",
 
1495
    ""address"": ""435 N Mulford Rd, Rockford, IL 61107, USA"",
 
1496
    ""AddressDetails"": {
 
1497
   ""Accuracy"" : 8,
 
1498
   ""Country"" : {
 
1499
      ""AdministrativeArea"" : {
 
1500
         ""AdministrativeAreaName"" : ""IL"",
 
1501
         ""SubAdministrativeArea"" : {
 
1502
            ""Locality"" : {
 
1503
               ""LocalityName"" : ""Rockford"",
 
1504
               ""PostalCode"" : {
 
1505
                  ""PostalCodeNumber"" : ""61107""
 
1506
               },
 
1507
               ""Thoroughfare"" : {
 
1508
                  ""ThoroughfareName"" : ""435 N Mulford Rd""
 
1509
               }
 
1510
            },
 
1511
            ""SubAdministrativeAreaName"" : ""Winnebago""
 
1512
         }
 
1513
      },
 
1514
      ""CountryName"" : ""USA"",
 
1515
      ""CountryNameCode"" : ""US""
 
1516
   }
 
1517
},
 
1518
    ""ExtendedData"": {
 
1519
      ""LatLonBox"": {
 
1520
        ""north"": 42.2753076,
 
1521
        ""south"": 42.2690124,
 
1522
        ""east"": -88.9964645,
 
1523
        ""west"": -89.0027597
 
1524
      }
 
1525
    },
 
1526
    ""Point"": {
 
1527
      ""coordinates"": [ -88.9995886, 42.2721596, 0 ]
 
1528
    }
 
1529
  } ]
 
1530
}";
 
1531
 
 
1532
      JObject o = JObject.Parse(json);
 
1533
 
 
1534
      string searchAddress = (string)o["Placemark"][0]["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"]["Locality"]["Thoroughfare"]["ThoroughfareName"];
 
1535
      Assert.AreEqual("435 N Mulford Rd", searchAddress);
 
1536
    }
 
1537
 
 
1538
    [Test]
 
1539
    public void SetValueWithInvalidPropertyName()
 
1540
    {
 
1541
      ExceptionAssert.Throws<ArgumentException>("Set JObject values with invalid key value: 0. Object property name expected.",
 
1542
        () =>
 
1543
        {
 
1544
          JObject o = new JObject();
 
1545
          o[0] = new JValue(3);
 
1546
        });
 
1547
    }
 
1548
 
 
1549
    [Test]
 
1550
    public void SetValue()
 
1551
    {
 
1552
      object key = "TestKey";
 
1553
 
 
1554
      JObject o = new JObject();
 
1555
      o[key] = new JValue(3);
 
1556
 
 
1557
      Assert.AreEqual(3, (int)o[key]);
 
1558
    }
 
1559
 
 
1560
    [Test]
 
1561
    public void ParseMultipleProperties()
 
1562
    {
 
1563
      string json = @"{
 
1564
        ""Name"": ""Name1"",
 
1565
        ""Name"": ""Name2""
 
1566
      }";
 
1567
 
 
1568
      JObject o = JObject.Parse(json);
 
1569
      string value = (string)o["Name"];
 
1570
 
 
1571
      Assert.AreEqual("Name2", value);
 
1572
    }
 
1573
 
 
1574
#if !(NETFX_CORE || PORTABLE)
 
1575
    [Test]
 
1576
    public void WriteObjectNullDBNullValue()
 
1577
    {
 
1578
      DBNull dbNull = DBNull.Value;
 
1579
      JValue v = new JValue(dbNull);
 
1580
      Assert.AreEqual(DBNull.Value, v.Value);
 
1581
      Assert.AreEqual(JTokenType.Null, v.Type);
 
1582
 
 
1583
      JObject o = new JObject();
 
1584
      o["title"] = v;
 
1585
 
 
1586
      string output = o.ToString();
 
1587
      
 
1588
      Assert.AreEqual(@"{
 
1589
  ""title"": null
 
1590
}", output);
 
1591
    }
 
1592
#endif
 
1593
 
 
1594
    [Test]
 
1595
    public void InvalidValueCastExceptionMessage()
 
1596
    {
 
1597
      ExceptionAssert.Throws<ArgumentException>("Can not convert Object to String.",
 
1598
        () =>
 
1599
        {
 
1600
          string json = @"{
 
1601
  ""responseData"": {}, 
 
1602
  ""responseDetails"": null, 
 
1603
  ""responseStatus"": 200
 
1604
}";
 
1605
 
 
1606
          JObject o = JObject.Parse(json);
 
1607
 
 
1608
          string name = (string)o["responseData"];
 
1609
        });
 
1610
    }
 
1611
 
 
1612
    [Test]
 
1613
    public void InvalidPropertyValueCastExceptionMessage()
 
1614
    {
 
1615
      ExceptionAssert.Throws<ArgumentException>("Can not convert Object to String.",
 
1616
      () =>
 
1617
      {
 
1618
        string json = @"{
 
1619
  ""responseData"": {}, 
 
1620
  ""responseDetails"": null, 
 
1621
  ""responseStatus"": 200
 
1622
}";
 
1623
 
 
1624
        JObject o = JObject.Parse(json);
 
1625
 
 
1626
        string name = (string)o.Property("responseData");
 
1627
      });
 
1628
    }
 
1629
 
 
1630
    [Test]
 
1631
    public void NumberTooBigForInt64()
 
1632
    {
 
1633
      ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Path 'code', line 1, position 30.",
 
1634
        () =>
 
1635
        {
 
1636
          string json = @"{""code"": 307953220000517141511}";
 
1637
 
 
1638
          JObject.Parse(json);
 
1639
        });
 
1640
    }
 
1641
 
 
1642
    [Test]
 
1643
    public void ParseIncomplete()
 
1644
    {
 
1645
      ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject. Path 'foo', line 1, position 6.",
 
1646
        () =>
 
1647
        {
 
1648
          JObject.Parse("{ foo:");
 
1649
        });
 
1650
    }
 
1651
 
 
1652
    [Test]
 
1653
    public void LoadFromNestedObject()
 
1654
    {
 
1655
      string jsonText = @"{
 
1656
  ""short"":
 
1657
  {
 
1658
    ""error"":
 
1659
    {
 
1660
      ""code"":0,
 
1661
      ""msg"":""No action taken""
 
1662
    }
 
1663
  }
 
1664
}";
 
1665
 
 
1666
      JsonReader reader = new JsonTextReader(new StringReader(jsonText));
 
1667
      reader.Read();
 
1668
      reader.Read();
 
1669
      reader.Read();
 
1670
      reader.Read();
 
1671
      reader.Read();
 
1672
 
 
1673
      JObject o = (JObject)JToken.ReadFrom(reader);
 
1674
      Assert.IsNotNull(o);
 
1675
      Assert.AreEqual(@"{
 
1676
  ""code"": 0,
 
1677
  ""msg"": ""No action taken""
 
1678
}", o.ToString(Formatting.Indented));
 
1679
    }
 
1680
 
 
1681
    [Test]
 
1682
    public void LoadFromNestedObjectIncomplete()
 
1683
    {
 
1684
      ExceptionAssert.Throws<JsonReaderException>("Unexpected end of content while loading JObject. Path 'short.error.code', line 6, position 15.",
 
1685
      () =>
 
1686
      {
 
1687
        string jsonText = @"{
 
1688
  ""short"":
 
1689
  {
 
1690
    ""error"":
 
1691
    {
 
1692
      ""code"":0";
 
1693
 
 
1694
        JsonReader reader = new JsonTextReader(new StringReader(jsonText));
 
1695
        reader.Read();
 
1696
        reader.Read();
 
1697
        reader.Read();
 
1698
        reader.Read();
 
1699
        reader.Read();
 
1700
 
 
1701
        JToken.ReadFrom(reader);
 
1702
      });
 
1703
    }
 
1704
 
 
1705
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
1706
    [Test]
 
1707
    public void GetProperties()
 
1708
    {
 
1709
      JObject o = JObject.Parse("{'prop1':12,'prop2':'hi!','prop3':null,'prop4':[1,2,3]}");
 
1710
 
 
1711
      ICustomTypeDescriptor descriptor = o;
 
1712
 
 
1713
      PropertyDescriptorCollection properties = descriptor.GetProperties();
 
1714
      Assert.AreEqual(4, properties.Count);
 
1715
 
 
1716
      PropertyDescriptor prop1 = properties[0];
 
1717
      Assert.AreEqual("prop1", prop1.Name);
 
1718
      Assert.AreEqual(typeof(long), prop1.PropertyType);
 
1719
      Assert.AreEqual(typeof(JObject), prop1.ComponentType);
 
1720
      Assert.AreEqual(false, prop1.CanResetValue(o));
 
1721
      Assert.AreEqual(false, prop1.ShouldSerializeValue(o));
 
1722
 
 
1723
      PropertyDescriptor prop2 = properties[1];
 
1724
      Assert.AreEqual("prop2", prop2.Name);
 
1725
      Assert.AreEqual(typeof(string), prop2.PropertyType);
 
1726
      Assert.AreEqual(typeof(JObject), prop2.ComponentType);
 
1727
      Assert.AreEqual(false, prop2.CanResetValue(o));
 
1728
      Assert.AreEqual(false, prop2.ShouldSerializeValue(o));
 
1729
 
 
1730
      PropertyDescriptor prop3 = properties[2];
 
1731
      Assert.AreEqual("prop3", prop3.Name);
 
1732
      Assert.AreEqual(typeof(object), prop3.PropertyType);
 
1733
      Assert.AreEqual(typeof(JObject), prop3.ComponentType);
 
1734
      Assert.AreEqual(false, prop3.CanResetValue(o));
 
1735
      Assert.AreEqual(false, prop3.ShouldSerializeValue(o));
 
1736
 
 
1737
      PropertyDescriptor prop4 = properties[3];
 
1738
      Assert.AreEqual("prop4", prop4.Name);
 
1739
      Assert.AreEqual(typeof(JArray), prop4.PropertyType);
 
1740
      Assert.AreEqual(typeof(JObject), prop4.ComponentType);
 
1741
      Assert.AreEqual(false, prop4.CanResetValue(o));
 
1742
      Assert.AreEqual(false, prop4.ShouldSerializeValue(o));
 
1743
    }
 
1744
#endif
 
1745
    [Test]
 
1746
    public void ParseEmptyObjectWithComment()
 
1747
    {
 
1748
      JObject o = JObject.Parse("{ /* A Comment */ }");
 
1749
      Assert.AreEqual(0, o.Count);
 
1750
    }
 
1751
 
 
1752
    [Test]
 
1753
    public void FromObjectTimeSpan()
 
1754
    {
 
1755
      JValue v = (JValue)JToken.FromObject(TimeSpan.FromDays(1));
 
1756
      Assert.AreEqual(v.Value, TimeSpan.FromDays(1));
 
1757
 
 
1758
      Assert.AreEqual("1.00:00:00", v.ToString());
 
1759
    }
 
1760
 
 
1761
    [Test]
 
1762
    public void FromObjectUri()
 
1763
    {
 
1764
      JValue v = (JValue)JToken.FromObject(new Uri("http://www.stuff.co.nz"));
 
1765
      Assert.AreEqual(v.Value, new Uri("http://www.stuff.co.nz"));
 
1766
 
 
1767
      Assert.AreEqual("http://www.stuff.co.nz/", v.ToString());
 
1768
    }
 
1769
 
 
1770
    [Test]
 
1771
    public void FromObjectGuid()
 
1772
    {
 
1773
      JValue v = (JValue)JToken.FromObject(new Guid("9065ACF3-C820-467D-BE50-8D4664BEAF35"));
 
1774
      Assert.AreEqual(v.Value, new Guid("9065ACF3-C820-467D-BE50-8D4664BEAF35"));
 
1775
 
 
1776
      Assert.AreEqual("9065acf3-c820-467d-be50-8d4664beaf35", v.ToString());
 
1777
    }
 
1778
 
 
1779
    [Test]
 
1780
    public void ParseAdditionalContent()
 
1781
    {
 
1782
      ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Path '', line 10, position 2.",
 
1783
      () =>
 
1784
      {
 
1785
        string json = @"{
 
1786
""Name"": ""Apple"",
 
1787
""Expiry"": new Date(1230422400000),
 
1788
""Price"": 3.99,
 
1789
""Sizes"": [
 
1790
""Small"",
 
1791
""Medium"",
 
1792
""Large""
 
1793
]
 
1794
}, 987987";
 
1795
 
 
1796
        JObject o = JObject.Parse(json);
 
1797
      });
 
1798
    }
 
1799
 
 
1800
    [Test]
 
1801
    public void DeepEqualsIgnoreOrder()
 
1802
    {
 
1803
      JObject o1 = new JObject(
 
1804
        new JProperty("null", null),
 
1805
        new JProperty("integer", 1),
 
1806
        new JProperty("string", "string!"),
 
1807
        new JProperty("decimal", 0.5m),
 
1808
        new JProperty("array", new JArray(1, 2)));
 
1809
 
 
1810
      Assert.IsTrue(o1.DeepEquals(o1));
 
1811
 
 
1812
      JObject o2 = new JObject(
 
1813
        new JProperty("null", null),
 
1814
        new JProperty("string", "string!"),
 
1815
        new JProperty("decimal", 0.5m),
 
1816
        new JProperty("integer", 1),
 
1817
        new JProperty("array", new JArray(1, 2)));
 
1818
 
 
1819
      Assert.IsTrue(o1.DeepEquals(o2));
 
1820
 
 
1821
      JObject o3 = new JObject(
 
1822
        new JProperty("null", null),
 
1823
        new JProperty("string", "string!"),
 
1824
        new JProperty("decimal", 0.5m),
 
1825
        new JProperty("integer", 2),
 
1826
        new JProperty("array", new JArray(1, 2)));
 
1827
 
 
1828
      Assert.IsFalse(o1.DeepEquals(o3));
 
1829
 
 
1830
      JObject o4 = new JObject(
 
1831
        new JProperty("null", null),
 
1832
        new JProperty("string", "string!"),
 
1833
        new JProperty("decimal", 0.5m),
 
1834
        new JProperty("integer", 1),
 
1835
        new JProperty("array", new JArray(2, 1)));
 
1836
 
 
1837
      Assert.IsFalse(o1.DeepEquals(o4));
 
1838
 
 
1839
      JObject o5 = new JObject(
 
1840
        new JProperty("null", null),
 
1841
        new JProperty("string", "string!"),
 
1842
        new JProperty("decimal", 0.5m),
 
1843
        new JProperty("integer", 1));
 
1844
 
 
1845
      Assert.IsFalse(o1.DeepEquals(o5));
 
1846
 
 
1847
      Assert.IsFalse(o1.DeepEquals(null));
 
1848
    }
 
1849
 
 
1850
    [Test]
 
1851
    public void ToListOnEmptyObject()
 
1852
    {
 
1853
      JObject o = JObject.Parse(@"{}");
 
1854
      IList<JToken> l1 = o.ToList<JToken>();
 
1855
      Assert.AreEqual(0, l1.Count);
 
1856
 
 
1857
      IList<KeyValuePair<string, JToken>> l2 = o.ToList<KeyValuePair<string, JToken>>();
 
1858
      Assert.AreEqual(0, l2.Count);
 
1859
 
 
1860
      o = JObject.Parse(@"{'hi':null}");
 
1861
      
 
1862
      l1 = o.ToList<JToken>();
 
1863
      Assert.AreEqual(1, l1.Count);
 
1864
 
 
1865
      l2 = o.ToList<KeyValuePair<string, JToken>>();
 
1866
      Assert.AreEqual(1, l2.Count);
 
1867
    }
 
1868
 
 
1869
    [Test]
 
1870
    public void EmptyObjectDeepEquals()
 
1871
    {
 
1872
      Assert.IsTrue(JToken.DeepEquals(new JObject(), new JObject()));
 
1873
 
 
1874
      JObject a = new JObject();
 
1875
      JObject b = new JObject();
 
1876
 
 
1877
      b.Add("hi", "bye");
 
1878
      b.Remove("hi");
 
1879
 
 
1880
      Assert.IsTrue(JToken.DeepEquals(a, b));
 
1881
      Assert.IsTrue(JToken.DeepEquals(b, a));
 
1882
    }
 
1883
 
 
1884
    [Test]
 
1885
    public void GetValueBlogExample()
 
1886
    {
 
1887
      JObject o = JObject.Parse(@"{
 
1888
        'name': 'Lower',
 
1889
        'NAME': 'Upper'
 
1890
      }");
 
1891
 
 
1892
      string exactMatch = (string)o.GetValue("NAME", StringComparison.OrdinalIgnoreCase);
 
1893
      // Upper
 
1894
 
 
1895
      string ignoreCase = (string)o.GetValue("Name", StringComparison.OrdinalIgnoreCase);
 
1896
      // Lower
 
1897
 
 
1898
      Assert.AreEqual("Upper", exactMatch);
 
1899
      Assert.AreEqual("Lower", ignoreCase);
 
1900
    }
 
1901
 
 
1902
    [Test]
 
1903
    public void GetValue()
 
1904
    {
 
1905
      JObject a = new JObject();
 
1906
      a["Name"] = "Name!";
 
1907
      a["name"] = "name!";
 
1908
      a["title"] = "Title!";
 
1909
 
 
1910
      Assert.AreEqual(null, a.GetValue("NAME", StringComparison.Ordinal));
 
1911
      Assert.AreEqual(null, a.GetValue("NAME"));
 
1912
      Assert.AreEqual(null, a.GetValue("TITLE"));
 
1913
      Assert.AreEqual("Name!", (string)a.GetValue("NAME", StringComparison.OrdinalIgnoreCase));
 
1914
      Assert.AreEqual("name!", (string)a.GetValue("name", StringComparison.Ordinal));
 
1915
      Assert.AreEqual(null, a.GetValue(null, StringComparison.Ordinal));
 
1916
      Assert.AreEqual(null, a.GetValue(null));
 
1917
 
 
1918
      JToken v;
 
1919
      Assert.IsFalse(a.TryGetValue("NAME", StringComparison.Ordinal, out v));
 
1920
      Assert.AreEqual(null, v);
 
1921
 
 
1922
      Assert.IsFalse(a.TryGetValue("NAME", out v));
 
1923
      Assert.IsFalse(a.TryGetValue("TITLE", out v));
 
1924
 
 
1925
      Assert.IsTrue(a.TryGetValue("NAME", StringComparison.OrdinalIgnoreCase, out v));
 
1926
      Assert.AreEqual("Name!", (string)v);
 
1927
 
 
1928
      Assert.IsTrue(a.TryGetValue("name", StringComparison.Ordinal, out v));
 
1929
      Assert.AreEqual("name!", (string)v);
 
1930
 
 
1931
      Assert.IsFalse(a.TryGetValue(null, StringComparison.Ordinal, out v));
 
1932
    }
 
1933
  }
 
1934
}
 
 
b'\\ No newline at end of file'