-Improve ignore case get property performance

This commit is contained in:
James Newton-King
2015-11-14 10:46:16 +13:00
parent 8e1a59b8e6
commit f3822513df
@@ -38,6 +38,7 @@ namespace Newtonsoft.Json.Serialization
public class JsonPropertyCollection : KeyedCollection<string, JsonProperty> public class JsonPropertyCollection : KeyedCollection<string, JsonProperty>
{ {
private readonly Type _type; private readonly Type _type;
private readonly List<JsonProperty> _list;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class. /// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class.
@@ -48,6 +49,9 @@ namespace Newtonsoft.Json.Serialization
{ {
ValidationUtils.ArgumentNotNull(type, "type"); ValidationUtils.ArgumentNotNull(type, "type");
_type = type; _type = type;
// foreach over List<T> to avoid boxing the Enumerator
_list = (List<JsonProperty>)Items;
} }
/// <summary> /// <summary>
@@ -70,7 +74,9 @@ namespace Newtonsoft.Json.Serialization
{ {
// don't overwrite existing property with ignored property // don't overwrite existing property with ignored property
if (property.Ignored) if (property.Ignored)
{
return; return;
}
JsonProperty existingProperty = this[property.PropertyName]; JsonProperty existingProperty = this[property.PropertyName];
bool duplicateProperty = true; bool duplicateProperty = true;
@@ -102,7 +108,9 @@ namespace Newtonsoft.Json.Serialization
} }
if (duplicateProperty) 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)); 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); Add(property);
@@ -119,7 +127,9 @@ namespace Newtonsoft.Json.Serialization
{ {
JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal); JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
if (property == null) if (property == null)
{
property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase); property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
}
return property; return property;
} }
@@ -148,13 +158,16 @@ namespace Newtonsoft.Json.Serialization
{ {
JsonProperty property; JsonProperty property;
if (TryGetValue(propertyName, out property)) if (TryGetValue(propertyName, out property))
{
return property; return property;
}
return null; 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)) if (string.Equals(propertyName, property.PropertyName, comparisonType))
{ {
return property; return property;