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 System.Collections.Generic;
28
using System.Collections.Specialized;
29
using System.ComponentModel;
30
using Newtonsoft.Json.Tests.TestObjects;
32
using NUnit.Framework;
34
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
35
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
36
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
38
using Newtonsoft.Json.Linq;
40
using System.Collections;
41
#if !PocketPC && !SILVERLIGHT && !NETFX_CORE
45
using Newtonsoft.Json.Utilities.LinqBridge;
50
namespace Newtonsoft.Json.Tests.Linq
53
public class JObjectTests : TestFixtureBase
56
public void WritePropertyWithNoValue()
58
var o = new JObject();
59
o.Add(new JProperty("novalue"));
69
var o = new JObject();
70
var d = (IDictionary<string, JToken>) o;
72
Assert.AreEqual(0, d.Keys.Count);
76
Assert.AreEqual(1, d.Keys.Count);
80
public void TryGetValue()
82
JObject o = new JObject();
83
o.Add("PropertyNameValue", new JValue(1));
84
Assert.AreEqual(1, o.Children().Count());
87
Assert.AreEqual(false, o.TryGetValue("sdf", out t));
88
Assert.AreEqual(null, t);
90
Assert.AreEqual(false, o.TryGetValue(null, out t));
91
Assert.AreEqual(null, t);
93
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
94
Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
98
public void DictionaryItemShouldSet()
100
JObject o = new JObject();
101
o["PropertyNameValue"] = new JValue(1);
102
Assert.AreEqual(1, o.Children().Count());
105
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
106
Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
108
o["PropertyNameValue"] = new JValue(2);
109
Assert.AreEqual(1, o.Children().Count());
111
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
112
Assert.AreEqual(true, JToken.DeepEquals(new JValue(2), t));
114
o["PropertyNameValue"] = null;
115
Assert.AreEqual(1, o.Children().Count());
117
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
118
Assert.AreEqual(true, JToken.DeepEquals(new JValue((object)null), t));
124
JObject o = new JObject();
125
o.Add("PropertyNameValue", new JValue(1));
126
Assert.AreEqual(1, o.Children().Count());
128
Assert.AreEqual(false, o.Remove("sdf"));
129
Assert.AreEqual(false, o.Remove(null));
130
Assert.AreEqual(true, o.Remove("PropertyNameValue"));
132
Assert.AreEqual(0, o.Children().Count());
136
public void GenericCollectionRemove()
138
JValue v = new JValue(1);
139
JObject o = new JObject();
140
o.Add("PropertyNameValue", v);
141
Assert.AreEqual(1, o.Children().Count());
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)));
148
Assert.AreEqual(0, o.Children().Count());
152
public void DuplicatePropertyNameShouldThrow()
154
ExceptionAssert.Throws<ArgumentException>(
155
"Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
158
JObject o = new JObject();
159
o.Add("PropertyNameValue", null);
160
o.Add("PropertyNameValue", null);
165
public void GenericDictionaryAdd()
167
JObject o = new JObject();
169
o.Add("PropertyNameValue", new JValue(1));
170
Assert.AreEqual(1, (int)o["PropertyNameValue"]);
172
o.Add("PropertyNameValue1", null);
173
Assert.AreEqual(null, ((JValue)o["PropertyNameValue1"]).Value);
175
Assert.AreEqual(2, o.Children().Count());
179
public void GenericCollectionAdd()
181
JObject o = new JObject();
182
((ICollection<KeyValuePair<string,JToken>>)o).Add(new KeyValuePair<string,JToken>("PropertyNameValue", new JValue(1)));
184
Assert.AreEqual(1, (int)o["PropertyNameValue"]);
185
Assert.AreEqual(1, o.Children().Count());
189
public void GenericCollectionClear()
191
JObject o = new JObject();
192
o.Add("PropertyNameValue", new JValue(1));
193
Assert.AreEqual(1, o.Children().Count());
195
JProperty p = (JProperty)o.Children().ElementAt(0);
197
((ICollection<KeyValuePair<string, JToken>>)o).Clear();
198
Assert.AreEqual(0, o.Children().Count());
200
Assert.AreEqual(null, p.Parent);
204
public void GenericCollectionContains()
206
JValue v = new JValue(1);
207
JObject o = new JObject();
208
o.Add("PropertyNameValue", v);
209
Assert.AreEqual(1, o.Children().Count());
211
bool contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1)));
212
Assert.AreEqual(false, contains);
214
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", v));
215
Assert.AreEqual(true, contains);
217
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2)));
218
Assert.AreEqual(false, contains);
220
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1)));
221
Assert.AreEqual(false, contains);
223
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(default(KeyValuePair<string, JToken>));
224
Assert.AreEqual(false, contains);
228
public void GenericDictionaryContains()
230
JObject o = new JObject();
231
o.Add("PropertyNameValue", new JValue(1));
232
Assert.AreEqual(1, o.Children().Count());
234
bool contains = ((IDictionary<string, JToken>)o).ContainsKey("PropertyNameValue");
235
Assert.AreEqual(true, contains);
239
public void GenericCollectionCopyTo()
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());
247
KeyValuePair<string, JToken>[] a = new KeyValuePair<string,JToken>[5];
249
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(a, 1);
251
Assert.AreEqual(default(KeyValuePair<string,JToken>), a[0]);
253
Assert.AreEqual("PropertyNameValue", a[1].Key);
254
Assert.AreEqual(1, (int)a[1].Value);
256
Assert.AreEqual("PropertyNameValue2", a[2].Key);
257
Assert.AreEqual(2, (int)a[2].Value);
259
Assert.AreEqual("PropertyNameValue3", a[3].Key);
260
Assert.AreEqual(3, (int)a[3].Value);
262
Assert.AreEqual(default(KeyValuePair<string, JToken>), a[4]);
266
public void GenericCollectionCopyToNullArrayShouldThrow()
268
ExceptionAssert.Throws<ArgumentException>(
269
@"Value cannot be null.
270
Parameter name: array",
273
JObject o = new JObject();
274
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
279
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
281
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
282
@"arrayIndex is less than 0.
283
Parameter name: arrayIndex",
286
JObject o = new JObject();
287
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], -1);
292
public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
294
ExceptionAssert.Throws<ArgumentException>(
295
@"arrayIndex is equal to or greater than the length of array.",
298
JObject o = new JObject();
299
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], 1);
304
public void GenericCollectionCopyToInsufficientArrayCapacity()
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.",
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));
315
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
320
public void FromObjectRaw()
322
PersonRaw raw = new PersonRaw
324
FirstName = "FirstNameValue",
325
RawContent = new JRaw("[1,2,3,4,5]"),
326
LastName = "LastNameValue"
329
JObject o = JObject.FromObject(raw);
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"]);
338
public void JTokenReader()
340
PersonRaw raw = new PersonRaw
342
FirstName = "FirstNameValue",
343
RawContent = new JRaw("[1,2,3,4,5]"),
344
LastName = "LastNameValue"
347
JObject o = JObject.FromObject(raw);
349
JsonReader reader = new JTokenReader(o);
351
Assert.IsTrue(reader.Read());
352
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
354
Assert.IsTrue(reader.Read());
355
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
357
Assert.IsTrue(reader.Read());
358
Assert.AreEqual(JsonToken.String, reader.TokenType);
360
Assert.IsTrue(reader.Read());
361
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
363
Assert.IsTrue(reader.Read());
364
Assert.AreEqual(JsonToken.Raw, reader.TokenType);
366
Assert.IsTrue(reader.Read());
367
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
369
Assert.IsTrue(reader.Read());
370
Assert.AreEqual(JsonToken.String, reader.TokenType);
372
Assert.IsTrue(reader.Read());
373
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
375
Assert.IsFalse(reader.Read());
379
public void DeserializeFromRaw()
381
PersonRaw raw = new PersonRaw
383
FirstName = "FirstNameValue",
384
RawContent = new JRaw("[1,2,3,4,5]"),
385
LastName = "LastNameValue"
388
JObject o = JObject.FromObject(raw);
390
JsonReader reader = new JTokenReader(o);
391
JsonSerializer serializer = new JsonSerializer();
392
raw = (PersonRaw)serializer.Deserialize(reader, typeof(PersonRaw));
394
Assert.AreEqual("FirstNameValue", raw.FirstName);
395
Assert.AreEqual("LastNameValue", raw.LastName);
396
Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Value);
400
public void Parse_ShouldThrowOnUnexpectedToken()
402
ExceptionAssert.Throws<JsonReaderException>("Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.",
405
string json = @"[""prop""]";
411
public void ParseJavaScriptDate()
413
string json = @"[new Date(1207285200000)]";
415
JArray a = (JArray)JsonConvert.DeserializeObject(json);
416
JValue v = (JValue)a[0];
418
Assert.AreEqual(JsonConvert.ConvertJavaScriptTicksToDateTime(1207285200000), (DateTime)v);
422
public void GenericValueCast()
424
string json = @"{""foo"":true}";
425
JObject o = (JObject)JsonConvert.DeserializeObject(json);
426
bool? value = o.Value<bool?>("foo");
427
Assert.AreEqual(true, value);
429
json = @"{""foo"":null}";
430
o = (JObject)JsonConvert.DeserializeObject(json);
431
value = o.Value<bool?>("foo");
432
Assert.AreEqual(null, value);
438
ExceptionAssert.Throws<JsonReaderException>(
439
"Invalid property identifier character: ]. Path 'name', line 3, position 5.",
444
]!#$THIS IS: BAD JSON![{}}}}]
450
public void RawChildValues()
452
JObject o = new JObject();
453
o["val1"] = new JRaw("1");
454
o["val2"] = new JRaw("1");
456
string json = o.ToString();
465
public void Iterate()
467
JObject o = new JObject();
468
o.Add("PropertyNameValue1", new JValue(1));
469
o.Add("PropertyNameValue2", new JValue(2));
474
foreach (JProperty property in t)
476
Assert.AreEqual("PropertyNameValue" + i, property.Name);
477
Assert.AreEqual(i, (int)property.Value);
484
public void KeyValuePairIterate()
486
JObject o = new JObject();
487
o.Add("PropertyNameValue1", new JValue(1));
488
o.Add("PropertyNameValue2", new JValue(2));
491
foreach (KeyValuePair<string, JToken> pair in o)
493
Assert.AreEqual("PropertyNameValue" + i, pair.Key);
494
Assert.AreEqual(i, (int)pair.Value);
501
public void WriteObjectNullStringValue()
504
JValue v = new JValue(s);
505
Assert.AreEqual(null, v.Value);
506
Assert.AreEqual(JTokenType.String, v.Type);
508
JObject o = new JObject();
511
string output = o.ToString();
519
public void Example()
523
""Expiry"": new Date(1230422400000),
532
JObject o = JObject.Parse(json);
534
string name = (string)o["Name"];
537
JArray sizes = (JArray)o["Sizes"];
539
string smallest = (string)sizes[0];
542
Console.WriteLine(name);
543
Console.WriteLine(smallest);
547
public void DeserializeClassManually()
549
string jsonText = @"{
552
""original"":""http://www.foo.com/"",
553
""short"":""krehqk"",
557
""msg"":""No action taken""
562
JObject json = JObject.Parse(jsonText);
564
Shortie shortie = new Shortie
566
Original = (string)json["short"]["original"],
567
Short = (string)json["short"]["short"],
568
Error = new ShortieException
570
Code = (int)json["short"]["error"]["code"],
571
ErrorMessage = (string)json["short"]["error"]["msg"]
575
Console.WriteLine(shortie.Original);
576
// http://www.foo.com/
578
Console.WriteLine(shortie.Error.ErrorMessage);
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);
589
public void JObjectContainingHtml()
591
JObject o = new JObject();
592
o["rc"] = new JValue(200);
593
o["m"] = new JValue("");
594
o["o"] = new JValue(@"<div class='s1'>
596
<a href='asdf'>asdf</a><br />
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>""
616
public void ImplicitValueConversions()
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");
625
Console.WriteLine(moss.ToString());
627
// "FirstName": "Maurice",
628
// "LastName": "Moss",
629
// "BirthDate": "\/Date(252241200000+1300)\/",
630
// "Department": "IT",
631
// "JobTitle": "Support"
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";
642
Console.WriteLine(jen.ToString());
644
// "FirstName": "Jen",
645
// "LastName": "Barber",
646
// "BirthDate": "\/Date(258721200000+1300)\/",
647
// "Department": "IT",
648
// "JobTitle": "Manager"
653
public void ReplaceJPropertyWithJPropertyWithSameName()
655
JProperty p1 = new JProperty("Test1", 1);
656
JProperty p2 = new JProperty("Test2", "Two");
658
JObject o = new JObject(p1, p2);
660
Assert.AreEqual(p1, l[0]);
661
Assert.AreEqual(p2, l[1]);
663
JProperty p3 = new JProperty("Test1", "III");
666
Assert.AreEqual(null, p1.Parent);
667
Assert.AreEqual(l, p3.Parent);
669
Assert.AreEqual(p3, l[0]);
670
Assert.AreEqual(p2, l[1]);
672
Assert.AreEqual(2, l.Count);
673
Assert.AreEqual(2, o.Properties().Count());
675
JProperty p4 = new JProperty("Test4", "IV");
678
Assert.AreEqual(null, p2.Parent);
679
Assert.AreEqual(l, p4.Parent);
681
Assert.AreEqual(p3, l[0]);
682
Assert.AreEqual(p4, l[1]);
685
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
687
public void PropertyChanging()
689
object changing = null;
690
object changed = null;
691
int changingCount = 0;
692
int changedCount = 0;
694
JObject o = new JObject();
695
o.PropertyChanging += (sender, args) =>
697
JObject s = (JObject) sender;
698
changing = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
701
o.PropertyChanged += (sender, args) =>
703
JObject s = (JObject)sender;
704
changed = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
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);
715
o["StringValue"] = "value1";
716
Assert.AreEqual(1, changingCount);
717
Assert.AreEqual(1, changedCount);
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);
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);
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);
740
o["NullValue"] = null;
741
Assert.AreEqual(4, changingCount);
742
Assert.AreEqual(4, changedCount);
747
public void PropertyChanged()
749
object changed = null;
750
int changedCount = 0;
752
JObject o = new JObject();
753
o.PropertyChanged += (sender, args) =>
755
JObject s = (JObject)sender;
756
changed = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
760
o["StringValue"] = "value1";
761
Assert.AreEqual("value1", changed);
762
Assert.AreEqual("value1", (string)o["StringValue"]);
763
Assert.AreEqual(1, changedCount);
765
o["StringValue"] = "value1";
766
Assert.AreEqual(1, changedCount);
768
o["StringValue"] = "value2";
769
Assert.AreEqual("value2", changed);
770
Assert.AreEqual("value2", (string)o["StringValue"]);
771
Assert.AreEqual(2, changedCount);
773
o["StringValue"] = null;
774
Assert.AreEqual(null, changed);
775
Assert.AreEqual(null, (string)o["StringValue"]);
776
Assert.AreEqual(3, changedCount);
778
o["NullValue"] = null;
779
Assert.AreEqual(null, changed);
780
Assert.AreEqual(new JValue((object)null), o["NullValue"]);
781
Assert.AreEqual(4, changedCount);
783
o["NullValue"] = null;
784
Assert.AreEqual(4, changedCount);
788
public void IListContains()
790
JProperty p = new JProperty("Test", 1);
791
IList l = new JObject(p);
793
Assert.IsTrue(l.Contains(p));
794
Assert.IsFalse(l.Contains(new JProperty("Test", 1)));
798
public void IListIndexOf()
800
JProperty p = new JProperty("Test", 1);
801
IList l = new JObject(p);
803
Assert.AreEqual(0, l.IndexOf(p));
804
Assert.AreEqual(-1, l.IndexOf(new JProperty("Test", 1)));
808
public void IListClear()
810
JProperty p = new JProperty("Test", 1);
811
IList l = new JObject(p);
813
Assert.AreEqual(1, l.Count);
817
Assert.AreEqual(0, l.Count);
821
public void IListCopyTo()
823
JProperty p1 = new JProperty("Test1", 1);
824
JProperty p2 = new JProperty("Test2", "Two");
825
IList l = new JObject(p1, p2);
827
object[] a = new object[l.Count];
831
Assert.AreEqual(p1, a[0]);
832
Assert.AreEqual(p2, a[1]);
836
public void IListAdd()
838
JProperty p1 = new JProperty("Test1", 1);
839
JProperty p2 = new JProperty("Test2", "Two");
840
IList l = new JObject(p1, p2);
842
JProperty p3 = new JProperty("Test3", "III");
846
Assert.AreEqual(3, l.Count);
847
Assert.AreEqual(p3, l[2]);
851
public void IListAddBadToken()
853
ExceptionAssert.Throws<ArgumentException>(
854
"Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
857
JProperty p1 = new JProperty("Test1", 1);
858
JProperty p2 = new JProperty("Test2", "Two");
859
IList l = new JObject(p1, p2);
861
l.Add(new JValue("Bad!"));
866
public void IListAddBadValue()
868
ExceptionAssert.Throws<ArgumentException>(
869
"Argument is not a JToken.",
872
JProperty p1 = new JProperty("Test1", 1);
873
JProperty p2 = new JProperty("Test2", "Two");
874
IList l = new JObject(p1, p2);
881
public void IListAddPropertyWithExistingName()
883
ExceptionAssert.Throws<ArgumentException>(
884
"Can not add property Test2 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
887
JProperty p1 = new JProperty("Test1", 1);
888
JProperty p2 = new JProperty("Test2", "Two");
889
IList l = new JObject(p1, p2);
891
JProperty p3 = new JProperty("Test2", "II");
898
public void IListRemove()
900
JProperty p1 = new JProperty("Test1", 1);
901
JProperty p2 = new JProperty("Test2", "Two");
902
IList l = new JObject(p1, p2);
904
JProperty p3 = new JProperty("Test3", "III");
908
Assert.AreEqual(2, l.Count);
911
Assert.AreEqual(1, l.Count);
912
Assert.IsFalse(l.Contains(p1));
913
Assert.IsTrue(l.Contains(p2));
916
Assert.AreEqual(0, l.Count);
917
Assert.IsFalse(l.Contains(p2));
918
Assert.AreEqual(null, p2.Parent);
922
public void IListRemoveAt()
924
JProperty p1 = new JProperty("Test1", 1);
925
JProperty p2 = new JProperty("Test2", "Two");
926
IList l = new JObject(p1, p2);
932
Assert.AreEqual(1, l.Count);
935
Assert.AreEqual(0, l.Count);
939
public void IListInsert()
941
JProperty p1 = new JProperty("Test1", 1);
942
JProperty p2 = new JProperty("Test2", "Two");
943
IList l = new JObject(p1, p2);
945
JProperty p3 = new JProperty("Test3", "III");
948
Assert.AreEqual(l, p3.Parent);
950
Assert.AreEqual(p1, l[0]);
951
Assert.AreEqual(p3, l[1]);
952
Assert.AreEqual(p2, l[2]);
956
public void IListIsReadOnly()
958
IList l = new JObject();
959
Assert.IsFalse(l.IsReadOnly);
963
public void IListIsFixedSize()
965
IList l = new JObject();
966
Assert.IsFalse(l.IsFixedSize);
970
public void IListSetItem()
972
JProperty p1 = new JProperty("Test1", 1);
973
JProperty p2 = new JProperty("Test2", "Two");
974
IList l = new JObject(p1, p2);
976
JProperty p3 = new JProperty("Test3", "III");
980
Assert.AreEqual(p3, l[0]);
981
Assert.AreEqual(p2, l[1]);
985
public void IListSetItemAlreadyExists()
987
ExceptionAssert.Throws<ArgumentException>(
988
"Can not add property Test3 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
991
JProperty p1 = new JProperty("Test1", 1);
992
JProperty p2 = new JProperty("Test2", "Two");
993
IList l = new JObject(p1, p2);
995
JProperty p3 = new JProperty("Test3", "III");
1003
public void IListSetItemInvalid()
1005
ExceptionAssert.Throws<ArgumentException>(
1006
@"Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
1009
JProperty p1 = new JProperty("Test1", 1);
1010
JProperty p2 = new JProperty("Test2", "Two");
1011
IList l = new JObject(p1, p2);
1013
l[0] = new JValue(true);
1018
public void IListSyncRoot()
1020
JProperty p1 = new JProperty("Test1", 1);
1021
JProperty p2 = new JProperty("Test2", "Two");
1022
IList l = new JObject(p1, p2);
1024
Assert.IsNotNull(l.SyncRoot);
1028
public void IListIsSynchronized()
1030
JProperty p1 = new JProperty("Test1", 1);
1031
JProperty p2 = new JProperty("Test2", "Two");
1032
IList l = new JObject(p1, p2);
1034
Assert.IsFalse(l.IsSynchronized);
1038
public void GenericListJTokenContains()
1040
JProperty p = new JProperty("Test", 1);
1041
IList<JToken> l = new JObject(p);
1043
Assert.IsTrue(l.Contains(p));
1044
Assert.IsFalse(l.Contains(new JProperty("Test", 1)));
1048
public void GenericListJTokenIndexOf()
1050
JProperty p = new JProperty("Test", 1);
1051
IList<JToken> l = new JObject(p);
1053
Assert.AreEqual(0, l.IndexOf(p));
1054
Assert.AreEqual(-1, l.IndexOf(new JProperty("Test", 1)));
1058
public void GenericListJTokenClear()
1060
JProperty p = new JProperty("Test", 1);
1061
IList<JToken> l = new JObject(p);
1063
Assert.AreEqual(1, l.Count);
1067
Assert.AreEqual(0, l.Count);
1071
public void GenericListJTokenCopyTo()
1073
JProperty p1 = new JProperty("Test1", 1);
1074
JProperty p2 = new JProperty("Test2", "Two");
1075
IList<JToken> l = new JObject(p1, p2);
1077
JToken[] a = new JToken[l.Count];
1081
Assert.AreEqual(p1, a[0]);
1082
Assert.AreEqual(p2, a[1]);
1086
public void GenericListJTokenAdd()
1088
JProperty p1 = new JProperty("Test1", 1);
1089
JProperty p2 = new JProperty("Test2", "Two");
1090
IList<JToken> l = new JObject(p1, p2);
1092
JProperty p3 = new JProperty("Test3", "III");
1096
Assert.AreEqual(3, l.Count);
1097
Assert.AreEqual(p3, l[2]);
1101
public void GenericListJTokenAddBadToken()
1103
ExceptionAssert.Throws<ArgumentException>("Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
1106
JProperty p1 = new JProperty("Test1", 1);
1107
JProperty p2 = new JProperty("Test2", "Two");
1108
IList<JToken> l = new JObject(p1, p2);
1110
l.Add(new JValue("Bad!"));
1115
public void GenericListJTokenAddBadValue()
1117
ExceptionAssert.Throws<ArgumentException>("Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
1120
JProperty p1 = new JProperty("Test1", 1);
1121
JProperty p2 = new JProperty("Test2", "Two");
1122
IList<JToken> l = new JObject(p1, p2);
1124
// string is implicitly converted to JValue
1130
public void GenericListJTokenAddPropertyWithExistingName()
1132
ExceptionAssert.Throws<ArgumentException>("Can not add property Test2 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
1135
JProperty p1 = new JProperty("Test1", 1);
1136
JProperty p2 = new JProperty("Test2", "Two");
1137
IList<JToken> l = new JObject(p1, p2);
1139
JProperty p3 = new JProperty("Test2", "II");
1146
public void GenericListJTokenRemove()
1148
JProperty p1 = new JProperty("Test1", 1);
1149
JProperty p2 = new JProperty("Test2", "Two");
1150
IList<JToken> l = new JObject(p1, p2);
1152
JProperty p3 = new JProperty("Test3", "III");
1154
// won't do anything
1155
Assert.IsFalse(l.Remove(p3));
1156
Assert.AreEqual(2, l.Count);
1158
Assert.IsTrue(l.Remove(p1));
1159
Assert.AreEqual(1, l.Count);
1160
Assert.IsFalse(l.Contains(p1));
1161
Assert.IsTrue(l.Contains(p2));
1163
Assert.IsTrue(l.Remove(p2));
1164
Assert.AreEqual(0, l.Count);
1165
Assert.IsFalse(l.Contains(p2));
1166
Assert.AreEqual(null, p2.Parent);
1170
public void GenericListJTokenRemoveAt()
1172
JProperty p1 = new JProperty("Test1", 1);
1173
JProperty p2 = new JProperty("Test2", "Two");
1174
IList<JToken> l = new JObject(p1, p2);
1176
// won't do anything
1180
Assert.AreEqual(1, l.Count);
1183
Assert.AreEqual(0, l.Count);
1187
public void GenericListJTokenInsert()
1189
JProperty p1 = new JProperty("Test1", 1);
1190
JProperty p2 = new JProperty("Test2", "Two");
1191
IList<JToken> l = new JObject(p1, p2);
1193
JProperty p3 = new JProperty("Test3", "III");
1196
Assert.AreEqual(l, p3.Parent);
1198
Assert.AreEqual(p1, l[0]);
1199
Assert.AreEqual(p3, l[1]);
1200
Assert.AreEqual(p2, l[2]);
1204
public void GenericListJTokenIsReadOnly()
1206
IList<JToken> l = new JObject();
1207
Assert.IsFalse(l.IsReadOnly);
1211
public void GenericListJTokenSetItem()
1213
JProperty p1 = new JProperty("Test1", 1);
1214
JProperty p2 = new JProperty("Test2", "Two");
1215
IList<JToken> l = new JObject(p1, p2);
1217
JProperty p3 = new JProperty("Test3", "III");
1221
Assert.AreEqual(p3, l[0]);
1222
Assert.AreEqual(p2, l[1]);
1226
public void GenericListJTokenSetItemAlreadyExists()
1228
ExceptionAssert.Throws<ArgumentException>("Can not add property Test3 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
1231
JProperty p1 = new JProperty("Test1", 1);
1232
JProperty p2 = new JProperty("Test2", "Two");
1233
IList<JToken> l = new JObject(p1, p2);
1235
JProperty p3 = new JProperty("Test3", "III");
1242
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
1244
public void IBindingListSortDirection()
1246
IBindingList l = new JObject();
1247
Assert.AreEqual(ListSortDirection.Ascending, l.SortDirection);
1251
public void IBindingListSortProperty()
1253
IBindingList l = new JObject();
1254
Assert.AreEqual(null, l.SortProperty);
1258
public void IBindingListSupportsChangeNotification()
1260
IBindingList l = new JObject();
1261
Assert.AreEqual(true, l.SupportsChangeNotification);
1265
public void IBindingListSupportsSearching()
1267
IBindingList l = new JObject();
1268
Assert.AreEqual(false, l.SupportsSearching);
1272
public void IBindingListSupportsSorting()
1274
IBindingList l = new JObject();
1275
Assert.AreEqual(false, l.SupportsSorting);
1279
public void IBindingListAllowEdit()
1281
IBindingList l = new JObject();
1282
Assert.AreEqual(true, l.AllowEdit);
1286
public void IBindingListAllowNew()
1288
IBindingList l = new JObject();
1289
Assert.AreEqual(true, l.AllowNew);
1293
public void IBindingListAllowRemove()
1295
IBindingList l = new JObject();
1296
Assert.AreEqual(true, l.AllowRemove);
1300
public void IBindingListAddIndex()
1302
IBindingList l = new JObject();
1308
public void IBindingListApplySort()
1310
ExceptionAssert.Throws<NotSupportedException>(
1311
"Specified method is not supported.",
1314
IBindingList l = new JObject();
1315
l.ApplySort(null, ListSortDirection.Ascending);
1320
public void IBindingListRemoveSort()
1322
ExceptionAssert.Throws<NotSupportedException>(
1323
"Specified method is not supported.",
1326
IBindingList l = new JObject();
1332
public void IBindingListRemoveIndex()
1334
IBindingList l = new JObject();
1336
l.RemoveIndex(null);
1340
public void IBindingListFind()
1342
ExceptionAssert.Throws<NotSupportedException>(
1343
"Specified method is not supported.",
1346
IBindingList l = new JObject();
1352
public void IBindingListIsSorted()
1354
IBindingList l = new JObject();
1355
Assert.AreEqual(false, l.IsSorted);
1359
public void IBindingListAddNew()
1361
ExceptionAssert.Throws<JsonException>(
1362
"Could not determine new value to add to 'Newtonsoft.Json.Linq.JObject'.",
1365
IBindingList l = new JObject();
1371
public void IBindingListAddNewWithEvent()
1373
JObject o = new JObject();
1374
o._addingNew += (s, e) => e.NewObject = new JProperty("Property!");
1377
object newObject = l.AddNew();
1378
Assert.IsNotNull(newObject);
1380
JProperty p = (JProperty) newObject;
1381
Assert.AreEqual("Property!", p.Name);
1382
Assert.AreEqual(o, p.Parent);
1386
public void ITypedListGetListName()
1388
JProperty p1 = new JProperty("Test1", 1);
1389
JProperty p2 = new JProperty("Test2", "Two");
1390
ITypedList l = new JObject(p1, p2);
1392
Assert.AreEqual(string.Empty, l.GetListName(null));
1396
public void ITypedListGetItemProperties()
1398
JProperty p1 = new JProperty("Test1", 1);
1399
JProperty p2 = new JProperty("Test2", "Two");
1400
ITypedList l = new JObject(p1, p2);
1402
PropertyDescriptorCollection propertyDescriptors = l.GetItemProperties(null);
1403
Assert.IsNull(propertyDescriptors);
1407
public void ListChanged()
1409
JProperty p1 = new JProperty("Test1", 1);
1410
JProperty p2 = new JProperty("Test2", "Two");
1411
JObject o = new JObject(p1, p2);
1413
ListChangedType? changedType = null;
1416
o.ListChanged += (s, a) =>
1418
changedType = a.ListChangedType;
1422
JProperty p3 = new JProperty("Test3", "III");
1425
Assert.AreEqual(changedType, ListChangedType.ItemAdded);
1426
Assert.AreEqual(index, 2);
1427
Assert.AreEqual(p3, ((IList<JToken>)o)[index.Value]);
1429
JProperty p4 = new JProperty("Test4", "IV");
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));
1439
Assert.AreEqual(changedType, ListChangedType.ItemChanged);
1440
Assert.AreEqual(index, 0);
1441
Assert.AreEqual(2, (int)o["Test1"]);
1444
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
1446
public void CollectionChanged()
1448
JProperty p1 = new JProperty("Test1", 1);
1449
JProperty p2 = new JProperty("Test2", "Two");
1450
JObject o = new JObject(p1, p2);
1452
NotifyCollectionChangedAction? changedType = null;
1455
o._collectionChanged += (s, a) =>
1457
changedType = a.Action;
1458
index = a.NewStartingIndex;
1461
JProperty p3 = new JProperty("Test3", "III");
1464
Assert.AreEqual(changedType, NotifyCollectionChangedAction.Add);
1465
Assert.AreEqual(index, 2);
1466
Assert.AreEqual(p3, ((IList<JToken>)o)[index.Value]);
1468
JProperty p4 = new JProperty("Test4", "IV");
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));
1478
Assert.AreEqual(changedType, NotifyCollectionChangedAction.Replace);
1479
Assert.AreEqual(index, 0);
1480
Assert.AreEqual(2, (int)o["Test1"]);
1485
public void GetGeocodeAddress()
1488
""name"": ""Address: 435 North Mulford Road Rockford, IL 61107"",
1491
""request"": ""geocode""
1495
""address"": ""435 N Mulford Rd, Rockford, IL 61107, USA"",
1496
""AddressDetails"": {
1499
""AdministrativeArea"" : {
1500
""AdministrativeAreaName"" : ""IL"",
1501
""SubAdministrativeArea"" : {
1503
""LocalityName"" : ""Rockford"",
1505
""PostalCodeNumber"" : ""61107""
1507
""Thoroughfare"" : {
1508
""ThoroughfareName"" : ""435 N Mulford Rd""
1511
""SubAdministrativeAreaName"" : ""Winnebago""
1514
""CountryName"" : ""USA"",
1515
""CountryNameCode"" : ""US""
1520
""north"": 42.2753076,
1521
""south"": 42.2690124,
1522
""east"": -88.9964645,
1523
""west"": -89.0027597
1527
""coordinates"": [ -88.9995886, 42.2721596, 0 ]
1532
JObject o = JObject.Parse(json);
1534
string searchAddress = (string)o["Placemark"][0]["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"]["Locality"]["Thoroughfare"]["ThoroughfareName"];
1535
Assert.AreEqual("435 N Mulford Rd", searchAddress);
1539
public void SetValueWithInvalidPropertyName()
1541
ExceptionAssert.Throws<ArgumentException>("Set JObject values with invalid key value: 0. Object property name expected.",
1544
JObject o = new JObject();
1545
o[0] = new JValue(3);
1550
public void SetValue()
1552
object key = "TestKey";
1554
JObject o = new JObject();
1555
o[key] = new JValue(3);
1557
Assert.AreEqual(3, (int)o[key]);
1561
public void ParseMultipleProperties()
1564
""Name"": ""Name1"",
1568
JObject o = JObject.Parse(json);
1569
string value = (string)o["Name"];
1571
Assert.AreEqual("Name2", value);
1574
#if !(NETFX_CORE || PORTABLE)
1576
public void WriteObjectNullDBNullValue()
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);
1583
JObject o = new JObject();
1586
string output = o.ToString();
1595
public void InvalidValueCastExceptionMessage()
1597
ExceptionAssert.Throws<ArgumentException>("Can not convert Object to String.",
1601
""responseData"": {},
1602
""responseDetails"": null,
1603
""responseStatus"": 200
1606
JObject o = JObject.Parse(json);
1608
string name = (string)o["responseData"];
1613
public void InvalidPropertyValueCastExceptionMessage()
1615
ExceptionAssert.Throws<ArgumentException>("Can not convert Object to String.",
1619
""responseData"": {},
1620
""responseDetails"": null,
1621
""responseStatus"": 200
1624
JObject o = JObject.Parse(json);
1626
string name = (string)o.Property("responseData");
1631
public void NumberTooBigForInt64()
1633
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Path 'code', line 1, position 30.",
1636
string json = @"{""code"": 307953220000517141511}";
1638
JObject.Parse(json);
1643
public void ParseIncomplete()
1645
ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject. Path 'foo', line 1, position 6.",
1648
JObject.Parse("{ foo:");
1653
public void LoadFromNestedObject()
1655
string jsonText = @"{
1661
""msg"":""No action taken""
1666
JsonReader reader = new JsonTextReader(new StringReader(jsonText));
1673
JObject o = (JObject)JToken.ReadFrom(reader);
1674
Assert.IsNotNull(o);
1677
""msg"": ""No action taken""
1678
}", o.ToString(Formatting.Indented));
1682
public void LoadFromNestedObjectIncomplete()
1684
ExceptionAssert.Throws<JsonReaderException>("Unexpected end of content while loading JObject. Path 'short.error.code', line 6, position 15.",
1687
string jsonText = @"{
1694
JsonReader reader = new JsonTextReader(new StringReader(jsonText));
1701
JToken.ReadFrom(reader);
1705
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
1707
public void GetProperties()
1709
JObject o = JObject.Parse("{'prop1':12,'prop2':'hi!','prop3':null,'prop4':[1,2,3]}");
1711
ICustomTypeDescriptor descriptor = o;
1713
PropertyDescriptorCollection properties = descriptor.GetProperties();
1714
Assert.AreEqual(4, properties.Count);
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));
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));
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));
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));
1746
public void ParseEmptyObjectWithComment()
1748
JObject o = JObject.Parse("{ /* A Comment */ }");
1749
Assert.AreEqual(0, o.Count);
1753
public void FromObjectTimeSpan()
1755
JValue v = (JValue)JToken.FromObject(TimeSpan.FromDays(1));
1756
Assert.AreEqual(v.Value, TimeSpan.FromDays(1));
1758
Assert.AreEqual("1.00:00:00", v.ToString());
1762
public void FromObjectUri()
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"));
1767
Assert.AreEqual("http://www.stuff.co.nz/", v.ToString());
1771
public void FromObjectGuid()
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"));
1776
Assert.AreEqual("9065acf3-c820-467d-be50-8d4664beaf35", v.ToString());
1780
public void ParseAdditionalContent()
1782
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Path '', line 10, position 2.",
1786
""Name"": ""Apple"",
1787
""Expiry"": new Date(1230422400000),
1796
JObject o = JObject.Parse(json);
1801
public void DeepEqualsIgnoreOrder()
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)));
1810
Assert.IsTrue(o1.DeepEquals(o1));
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)));
1819
Assert.IsTrue(o1.DeepEquals(o2));
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)));
1828
Assert.IsFalse(o1.DeepEquals(o3));
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)));
1837
Assert.IsFalse(o1.DeepEquals(o4));
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));
1845
Assert.IsFalse(o1.DeepEquals(o5));
1847
Assert.IsFalse(o1.DeepEquals(null));
1851
public void ToListOnEmptyObject()
1853
JObject o = JObject.Parse(@"{}");
1854
IList<JToken> l1 = o.ToList<JToken>();
1855
Assert.AreEqual(0, l1.Count);
1857
IList<KeyValuePair<string, JToken>> l2 = o.ToList<KeyValuePair<string, JToken>>();
1858
Assert.AreEqual(0, l2.Count);
1860
o = JObject.Parse(@"{'hi':null}");
1862
l1 = o.ToList<JToken>();
1863
Assert.AreEqual(1, l1.Count);
1865
l2 = o.ToList<KeyValuePair<string, JToken>>();
1866
Assert.AreEqual(1, l2.Count);
1870
public void EmptyObjectDeepEquals()
1872
Assert.IsTrue(JToken.DeepEquals(new JObject(), new JObject()));
1874
JObject a = new JObject();
1875
JObject b = new JObject();
1880
Assert.IsTrue(JToken.DeepEquals(a, b));
1881
Assert.IsTrue(JToken.DeepEquals(b, a));
1885
public void GetValueBlogExample()
1887
JObject o = JObject.Parse(@"{
1892
string exactMatch = (string)o.GetValue("NAME", StringComparison.OrdinalIgnoreCase);
1895
string ignoreCase = (string)o.GetValue("Name", StringComparison.OrdinalIgnoreCase);
1898
Assert.AreEqual("Upper", exactMatch);
1899
Assert.AreEqual("Lower", ignoreCase);
1903
public void GetValue()
1905
JObject a = new JObject();
1906
a["Name"] = "Name!";
1907
a["name"] = "name!";
1908
a["title"] = "Title!";
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));
1919
Assert.IsFalse(a.TryGetValue("NAME", StringComparison.Ordinal, out v));
1920
Assert.AreEqual(null, v);
1922
Assert.IsFalse(a.TryGetValue("NAME", out v));
1923
Assert.IsFalse(a.TryGetValue("TITLE", out v));
1925
Assert.IsTrue(a.TryGetValue("NAME", StringComparison.OrdinalIgnoreCase, out v));
1926
Assert.AreEqual("Name!", (string)v);
1928
Assert.IsTrue(a.TryGetValue("name", StringComparison.Ordinal, out v));
1929
Assert.AreEqual("name!", (string)v);
1931
Assert.IsFalse(a.TryGetValue(null, StringComparison.Ordinal, out v));
b'\\ No newline at end of file'