-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:
JamesNK
2009-10-03 04:33:15 +00:00
parent 33ac3a9c44
commit be8d951c03
6 changed files with 31 additions and 54 deletions
@@ -1380,5 +1380,19 @@ Parameter name: arrayIndex")]
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);
}
}
}
-8
View File
@@ -131,14 +131,6 @@ namespace Newtonsoft.Json.Linq
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>
/// Creates a <see cref="JArray"/> from an object.
/// </summary>
-11
View File
@@ -118,17 +118,6 @@ namespace Newtonsoft.Json.Linq
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>
/// Writes this token to a <see cref="JsonWriter"/>.
/// </summary>
+16 -18
View File
@@ -609,15 +609,6 @@ namespace Newtonsoft.Json.Linq
ClearItems();
}
internal abstract void ValidateObject(JToken o, JToken previous);
internal void AddObjectSkipNotify(JToken o)
{
ValidateObject(o, this);
Add(o);
}
internal void ReadContentFrom(JsonReader r)
{
ValidationUtils.ArgumentNotNull(r, "r");
@@ -643,7 +634,7 @@ namespace Newtonsoft.Json.Linq
case JsonToken.StartArray:
JArray a = new JArray();
a.SetLineInfo(lineInfo);
parent.AddObjectSkipNotify(a);
parent.Add(a);
parent = a;
break;
@@ -656,7 +647,7 @@ namespace Newtonsoft.Json.Linq
case JsonToken.StartObject:
JObject o = new JObject();
o.SetLineInfo(lineInfo);
parent.AddObjectSkipNotify(o);
parent.Add(o);
parent = o;
break;
case JsonToken.EndObject:
@@ -668,7 +659,7 @@ namespace Newtonsoft.Json.Linq
case JsonToken.StartConstructor:
JConstructor constructor = new JConstructor(r.Value.ToString());
constructor.SetLineInfo(constructor);
parent.AddObjectSkipNotify(constructor);
parent.Add(constructor);
parent = constructor;
break;
case JsonToken.EndConstructor:
@@ -684,27 +675,34 @@ namespace Newtonsoft.Json.Linq
case JsonToken.Boolean:
JValue v = new JValue(r.Value);
v.SetLineInfo(lineInfo);
parent.AddObjectSkipNotify(v);
parent.Add(v);
break;
case JsonToken.Comment:
v = JValue.CreateComment(r.Value.ToString());
v.SetLineInfo(lineInfo);
parent.AddObjectSkipNotify(v);
parent.Add(v);
break;
case JsonToken.Null:
v = new JValue(null, JTokenType.Null);
v.SetLineInfo(lineInfo);
parent.AddObjectSkipNotify(v);
parent.Add(v);
break;
case JsonToken.Undefined:
v = new JValue(null, JTokenType.Undefined);
v.SetLineInfo(lineInfo);
parent.AddObjectSkipNotify(v);
parent.Add(v);
break;
case JsonToken.PropertyName:
JProperty property = new JProperty(r.Value.ToString());
string propertyName = r.Value.ToString();
JProperty property = new JProperty(propertyName);
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;
break;
default:
+1 -9
View File
@@ -157,7 +157,7 @@ namespace Newtonsoft.Json.Linq
public JProperty Property(string name)
{
return Properties()
.Where(p => string.Compare(p.Name, name, StringComparison.Ordinal) == 0)
.Where(p => string.Equals(p.Name, name, StringComparison.Ordinal))
.SingleOrDefault();
}
@@ -298,14 +298,6 @@ namespace Newtonsoft.Json.Linq
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>
/// Writes this token to a <see cref="JsonWriter"/>.
/// </summary>
-8
View File
@@ -218,14 +218,6 @@ namespace Newtonsoft.Json.Linq
: 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>
/// Writes this token to a <see cref="JsonWriter"/>.
/// </summary>