From fac4f481b3231735e0aaabedc04da8349737a4fd Mon Sep 17 00:00:00 2001 From: JamesNK Date: Fri, 18 Sep 2009 21:38:50 +0000 Subject: [PATCH] -Added set indexer to JToken --- Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs | 19 +++++ .../Linq/JConstructorTests.cs | 21 ++++++ .../Linq/JObjectTests.cs | 73 +++++++++++++++++++ Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs | 8 ++ Src/Newtonsoft.Json/Linq/JArray.cs | 9 +++ Src/Newtonsoft.Json/Linq/JConstructor.cs | 9 +++ Src/Newtonsoft.Json/Linq/JObject.cs | 10 +++ Src/Newtonsoft.Json/Linq/JToken.cs | 1 + 8 files changed, 150 insertions(+) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs index 9906dac6..8ab36e32 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs @@ -396,5 +396,24 @@ Parameter name: index")] Assert.AreEqual(2, (int)a[1]); Assert.AreNotSame(a, a[2]); } + + [Test] + [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Set JArray values with invalid key value: ""badvalue"". Array position index expected.")] + public void SetValueWithInvalidIndex() + { + JArray a = new JArray(); + a["badvalue"] = new JValue(3); + } + + [Test] + public void SetValue() + { + object key = 0; + + JArray a = new JArray((object)null); + a[key] = new JValue(3); + + Assert.AreEqual(3, (int)a[key]); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs index dc5dedb6..4bf4f33c 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs @@ -44,5 +44,26 @@ namespace Newtonsoft.Json.Tests.Linq i++; } } + + [Test] + [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Set JConstructor values with invalid key value: ""badvalue"". Argument position index expected.")] + public void SetValueWithInvalidIndex() + { + JConstructor c = new JConstructor(); + c["badvalue"] = new JValue(3); + } + + [Test] + public void SetValue() + { + object key = 0; + + JConstructor c = new JConstructor(); + c.Name = "con"; + c.Add(null); + c[key] = new JValue(3); + + Assert.AreEqual(3, (int)c[key]); + } } } diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 78174e01..c0602525 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -1307,5 +1307,78 @@ Parameter name: arrayIndex")] Assert.AreEqual(2, (int)o["Test1"]); } #endif + + [Test] + public void GetGeocodeAddress() + { + string json = @"{ + ""name"": ""Address: 435 North Mulford Road Rockford, IL 61107"", + ""Status"": { + ""code"": 200, + ""request"": ""geocode"" + }, + ""Placemark"": [ { + ""id"": ""p1"", + ""address"": ""435 N Mulford Rd, Rockford, IL 61107, USA"", + ""AddressDetails"": { + ""Accuracy"" : 8, + ""Country"" : { + ""AdministrativeArea"" : { + ""AdministrativeAreaName"" : ""IL"", + ""SubAdministrativeArea"" : { + ""Locality"" : { + ""LocalityName"" : ""Rockford"", + ""PostalCode"" : { + ""PostalCodeNumber"" : ""61107"" + }, + ""Thoroughfare"" : { + ""ThoroughfareName"" : ""435 N Mulford Rd"" + } + }, + ""SubAdministrativeAreaName"" : ""Winnebago"" + } + }, + ""CountryName"" : ""USA"", + ""CountryNameCode"" : ""US"" + } +}, + ""ExtendedData"": { + ""LatLonBox"": { + ""north"": 42.2753076, + ""south"": 42.2690124, + ""east"": -88.9964645, + ""west"": -89.0027597 + } + }, + ""Point"": { + ""coordinates"": [ -88.9995886, 42.2721596, 0 ] + } + } ] +}"; + + JObject o = JObject.Parse(json); + + string searchAddress = (string)o["Placemark"][0]["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"]["Locality"]["Thoroughfare"]["ThoroughfareName"]; + Assert.AreEqual("435 N Mulford Rd", searchAddress); + } + + [Test] + [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Set JObject values with invalid key value: 0. Object property name expected.")] + public void SetValueWithInvalidPropertyName() + { + JObject o = new JObject(); + o[0] = new JValue(3); + } + + [Test] + public void SetValue() + { + object key = "TestKey"; + + JObject o = new JObject(); + o[key] = new JValue(3); + + Assert.AreEqual(3, (int)o[key]); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs index e83cce30..0490cd5c 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs @@ -160,5 +160,13 @@ namespace Newtonsoft.Json.Tests.Linq { Assert.IsFalse((new JValue(5L)).HasValues); } + + [Test] + [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot set child value on Newtonsoft.Json.Linq.JValue.")] + public void SetValue() + { + JToken t = new JValue(5L); + t[0] = new JValue(3); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index abb3481e..45b78e1a 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -197,6 +197,15 @@ namespace Newtonsoft.Json.Linq return GetItem((int)key); } + set + { + ValidationUtils.ArgumentNotNull(key, "o"); + + if (!(key is int)) + throw new ArgumentException("Set JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key))); + + SetItem((int)key, value); + } } /// diff --git a/Src/Newtonsoft.Json/Linq/JConstructor.cs b/Src/Newtonsoft.Json/Linq/JConstructor.cs index d3545eb3..51615b9b 100644 --- a/Src/Newtonsoft.Json/Linq/JConstructor.cs +++ b/Src/Newtonsoft.Json/Linq/JConstructor.cs @@ -161,6 +161,15 @@ namespace Newtonsoft.Json.Linq return GetItem((int)key); } + set + { + ValidationUtils.ArgumentNotNull(key, "o"); + + if (!(key is int)) + throw new ArgumentException("Set JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key))); + + SetItem((int)key, value); + } } internal override int GetDeepHashCode() diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 631e3f13..5f16d2ef 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -186,6 +186,16 @@ namespace Newtonsoft.Json.Linq return this[propertyName]; } + set + { + ValidationUtils.ArgumentNotNull(key, "o"); + + string propertyName = key as string; + if (propertyName == null) + throw new ArgumentException("Set JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key))); + + this[propertyName] = value; + } } /// diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index aef1391e..eec3db6c 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -234,6 +234,7 @@ namespace Newtonsoft.Json.Linq public virtual JToken this[object key] { get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); } + set { throw new InvalidOperationException("Cannot set child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); } } ///