From f3822513df73cf485d1e5596d20ff9d5406f6b5b Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sat, 14 Nov 2015 10:46:16 +1300 Subject: [PATCH] -Improve ignore case get property performance --- .../Serialization/JsonPropertyCollection.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs index 7f208254..3da25180 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs @@ -38,6 +38,7 @@ namespace Newtonsoft.Json.Serialization public class JsonPropertyCollection : KeyedCollection { private readonly Type _type; + private readonly List _list; /// /// Initializes a new instance of the class. @@ -48,6 +49,9 @@ namespace Newtonsoft.Json.Serialization { ValidationUtils.ArgumentNotNull(type, "type"); _type = type; + + // foreach over List to avoid boxing the Enumerator + _list = (List)Items; } /// @@ -70,7 +74,9 @@ namespace Newtonsoft.Json.Serialization { // don't overwrite existing property with ignored property if (property.Ignored) + { return; + } JsonProperty existingProperty = this[property.PropertyName]; bool duplicateProperty = true; @@ -102,7 +108,9 @@ namespace Newtonsoft.Json.Serialization } if (duplicateProperty) + { throw new JsonSerializationException("A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _type)); + } } Add(property); @@ -119,7 +127,9 @@ namespace Newtonsoft.Json.Serialization { JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal); if (property == null) + { property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase); + } return property; } @@ -148,13 +158,16 @@ namespace Newtonsoft.Json.Serialization { JsonProperty property; if (TryGetValue(propertyName, out property)) + { return property; + } return null; } - foreach (JsonProperty property in this) + for (int i = 0; i < _list.Count; i++) { + JsonProperty property = _list[i]; if (string.Equals(propertyName, property.PropertyName, comparisonType)) { return property;