-Added set indexer to JToken
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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())); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user