From be8d951c032227a59fbc5d9359ae53e6d9f1e5c1 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Sat, 3 Oct 2009 04:33:15 +0000 Subject: [PATCH] -Fixed exception when loading JObject with multiple properties of the same name. Last property will be used -Removed redundant ValidateObject method from JContainer --- .../Linq/JObjectTests.cs | 14 ++++++++ Src/Newtonsoft.Json/Linq/JArray.cs | 8 ----- Src/Newtonsoft.Json/Linq/JConstructor.cs | 11 ------ Src/Newtonsoft.Json/Linq/JContainer.cs | 34 +++++++++---------- Src/Newtonsoft.Json/Linq/JObject.cs | 10 +----- Src/Newtonsoft.Json/Linq/JProperty.cs | 8 ----- 6 files changed, 31 insertions(+), 54 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index c0602525..644bb283 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -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); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index 45b78e1a..82708cc6 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -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)); - } - /// /// Creates a from an object. /// diff --git a/Src/Newtonsoft.Json/Linq/JConstructor.cs b/Src/Newtonsoft.Json/Linq/JConstructor.cs index 51615b9b..296d1ae5 100644 --- a/Src/Newtonsoft.Json/Linq/JConstructor.cs +++ b/Src/Newtonsoft.Json/Linq/JConstructor.cs @@ -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)); - } - } - /// /// Writes this token to a . /// diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index fd232e8a..1045d191 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -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: diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 5f16d2ef..4d00b292 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -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)); - } - /// /// Writes this token to a . /// diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs index b434c872..ffd79851 100644 --- a/Src/Newtonsoft.Json/Linq/JProperty.cs +++ b/Src/Newtonsoft.Json/Linq/JProperty.cs @@ -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)); - } - /// /// Writes this token to a . ///