-Fixed exception when loading JObject with multiple properties of the same name. Last property will be used
-Removed redundant ValidateObject method from JContainer
This commit is contained in:
@@ -1380,5 +1380,19 @@ Parameter name: arrayIndex")]
|
|||||||
|
|
||||||
Assert.AreEqual(3, (int)o[key]);
|
Assert.AreEqual(3, (int)o[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ParseMultipleProperties()
|
||||||
|
{
|
||||||
|
string json = @"{
|
||||||
|
""Name"": ""Name1"",
|
||||||
|
""Name"": ""Name2""
|
||||||
|
}";
|
||||||
|
|
||||||
|
JObject o = JObject.Parse(json);
|
||||||
|
string value = (string)o["Name"];
|
||||||
|
|
||||||
|
Assert.AreEqual("Name2", value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,14 +131,6 @@ namespace Newtonsoft.Json.Linq
|
|||||||
return Load(jsonReader);
|
return Load(jsonReader);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void ValidateObject(JToken o, JToken previous)
|
|
||||||
{
|
|
||||||
ValidationUtils.ArgumentNotNull(o, "o");
|
|
||||||
|
|
||||||
if (o.Type == JTokenType.Property)
|
|
||||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a <see cref="JArray"/> from an object.
|
/// Creates a <see cref="JArray"/> from an object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -118,17 +118,6 @@ namespace Newtonsoft.Json.Linq
|
|||||||
return new JConstructor(this);
|
return new JConstructor(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void ValidateObject(JToken o, JToken previous)
|
|
||||||
{
|
|
||||||
ValidationUtils.ArgumentNotNull(o, "o");
|
|
||||||
|
|
||||||
switch (o.Type)
|
|
||||||
{
|
|
||||||
case JTokenType.Property:
|
|
||||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes this token to a <see cref="JsonWriter"/>.
|
/// Writes this token to a <see cref="JsonWriter"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -609,15 +609,6 @@ namespace Newtonsoft.Json.Linq
|
|||||||
ClearItems();
|
ClearItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal abstract void ValidateObject(JToken o, JToken previous);
|
|
||||||
|
|
||||||
internal void AddObjectSkipNotify(JToken o)
|
|
||||||
{
|
|
||||||
ValidateObject(o, this);
|
|
||||||
|
|
||||||
Add(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void ReadContentFrom(JsonReader r)
|
internal void ReadContentFrom(JsonReader r)
|
||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(r, "r");
|
ValidationUtils.ArgumentNotNull(r, "r");
|
||||||
@@ -643,7 +634,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
case JsonToken.StartArray:
|
case JsonToken.StartArray:
|
||||||
JArray a = new JArray();
|
JArray a = new JArray();
|
||||||
a.SetLineInfo(lineInfo);
|
a.SetLineInfo(lineInfo);
|
||||||
parent.AddObjectSkipNotify(a);
|
parent.Add(a);
|
||||||
parent = a;
|
parent = a;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -656,7 +647,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
case JsonToken.StartObject:
|
case JsonToken.StartObject:
|
||||||
JObject o = new JObject();
|
JObject o = new JObject();
|
||||||
o.SetLineInfo(lineInfo);
|
o.SetLineInfo(lineInfo);
|
||||||
parent.AddObjectSkipNotify(o);
|
parent.Add(o);
|
||||||
parent = o;
|
parent = o;
|
||||||
break;
|
break;
|
||||||
case JsonToken.EndObject:
|
case JsonToken.EndObject:
|
||||||
@@ -668,7 +659,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
case JsonToken.StartConstructor:
|
case JsonToken.StartConstructor:
|
||||||
JConstructor constructor = new JConstructor(r.Value.ToString());
|
JConstructor constructor = new JConstructor(r.Value.ToString());
|
||||||
constructor.SetLineInfo(constructor);
|
constructor.SetLineInfo(constructor);
|
||||||
parent.AddObjectSkipNotify(constructor);
|
parent.Add(constructor);
|
||||||
parent = constructor;
|
parent = constructor;
|
||||||
break;
|
break;
|
||||||
case JsonToken.EndConstructor:
|
case JsonToken.EndConstructor:
|
||||||
@@ -684,27 +675,34 @@ namespace Newtonsoft.Json.Linq
|
|||||||
case JsonToken.Boolean:
|
case JsonToken.Boolean:
|
||||||
JValue v = new JValue(r.Value);
|
JValue v = new JValue(r.Value);
|
||||||
v.SetLineInfo(lineInfo);
|
v.SetLineInfo(lineInfo);
|
||||||
parent.AddObjectSkipNotify(v);
|
parent.Add(v);
|
||||||
break;
|
break;
|
||||||
case JsonToken.Comment:
|
case JsonToken.Comment:
|
||||||
v = JValue.CreateComment(r.Value.ToString());
|
v = JValue.CreateComment(r.Value.ToString());
|
||||||
v.SetLineInfo(lineInfo);
|
v.SetLineInfo(lineInfo);
|
||||||
parent.AddObjectSkipNotify(v);
|
parent.Add(v);
|
||||||
break;
|
break;
|
||||||
case JsonToken.Null:
|
case JsonToken.Null:
|
||||||
v = new JValue(null, JTokenType.Null);
|
v = new JValue(null, JTokenType.Null);
|
||||||
v.SetLineInfo(lineInfo);
|
v.SetLineInfo(lineInfo);
|
||||||
parent.AddObjectSkipNotify(v);
|
parent.Add(v);
|
||||||
break;
|
break;
|
||||||
case JsonToken.Undefined:
|
case JsonToken.Undefined:
|
||||||
v = new JValue(null, JTokenType.Undefined);
|
v = new JValue(null, JTokenType.Undefined);
|
||||||
v.SetLineInfo(lineInfo);
|
v.SetLineInfo(lineInfo);
|
||||||
parent.AddObjectSkipNotify(v);
|
parent.Add(v);
|
||||||
break;
|
break;
|
||||||
case JsonToken.PropertyName:
|
case JsonToken.PropertyName:
|
||||||
JProperty property = new JProperty(r.Value.ToString());
|
string propertyName = r.Value.ToString();
|
||||||
|
JProperty property = new JProperty(propertyName);
|
||||||
property.SetLineInfo(lineInfo);
|
property.SetLineInfo(lineInfo);
|
||||||
parent.AddObjectSkipNotify(property);
|
JObject parentObject = (JObject) parent;
|
||||||
|
// handle multiple properties with the same name in JSON
|
||||||
|
JProperty existingPropertyWithName = parentObject.Property(propertyName);
|
||||||
|
if (existingPropertyWithName == null)
|
||||||
|
parent.Add(property);
|
||||||
|
else
|
||||||
|
existingPropertyWithName.Replace(property);
|
||||||
parent = property;
|
parent = property;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
public JProperty Property(string name)
|
public JProperty Property(string name)
|
||||||
{
|
{
|
||||||
return Properties()
|
return Properties()
|
||||||
.Where(p => string.Compare(p.Name, name, StringComparison.Ordinal) == 0)
|
.Where(p => string.Equals(p.Name, name, StringComparison.Ordinal))
|
||||||
.SingleOrDefault();
|
.SingleOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,14 +298,6 @@ namespace Newtonsoft.Json.Linq
|
|||||||
return (JObject)token;
|
return (JObject)token;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void ValidateObject(JToken o, JToken previous)
|
|
||||||
{
|
|
||||||
ValidationUtils.ArgumentNotNull(o, "o");
|
|
||||||
|
|
||||||
if (o.Type != JTokenType.Property)
|
|
||||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes this token to a <see cref="JsonWriter"/>.
|
/// Writes this token to a <see cref="JsonWriter"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -218,14 +218,6 @@ namespace Newtonsoft.Json.Linq
|
|||||||
: CreateFromContent(content);
|
: CreateFromContent(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void ValidateObject(JToken o, JToken previous)
|
|
||||||
{
|
|
||||||
ValidationUtils.ArgumentNotNull(o, "o");
|
|
||||||
|
|
||||||
if (o.Type == JTokenType.Property)
|
|
||||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes this token to a <see cref="JsonWriter"/>.
|
/// Writes this token to a <see cref="JsonWriter"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user