-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>
{
private readonly Type _type;
private readonly List<JsonProperty> _list;
/// <summary>
/// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class.
@@ -48,6 +49,9 @@ namespace Newtonsoft.Json.Serialization
{
ValidationUtils.ArgumentNotNull(type, "type");
_type = type;
// foreach over List<T> to avoid boxing the Enumerator
_list = (List<JsonProperty>)Items;
}
/// <summary>
@@ -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;