<PackageReference Include="Newtonsoft.Json" Version="6.0.5" />

JsonPropertyCollection

A collection of JsonProperty objects.
using Newtonsoft.Json.Utilities; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; namespace Newtonsoft.Json.Serialization { public class JsonPropertyCollection : KeyedCollection<string, JsonProperty> { private readonly Type _type; public JsonPropertyCollection(Type type) : base((IEqualityComparer<string>)StringComparer.Ordinal) { ValidationUtils.ArgumentNotNull(type, "type"); _type = type; } protected override string GetKeyForItem(JsonProperty item) { return item.PropertyName; } public void AddProperty(JsonProperty property) { if (Contains(property.PropertyName)) { if (property.Ignored) return; JsonProperty jsonProperty = base[property.PropertyName]; bool flag = true; if (jsonProperty.Ignored) { Remove(jsonProperty); flag = false; } else if ((object)property.DeclaringType != null && (object)jsonProperty.DeclaringType != null) { if (TypeExtensions.IsSubclassOf(property.DeclaringType, jsonProperty.DeclaringType)) { Remove(jsonProperty); flag = false; } if (TypeExtensions.IsSubclassOf(jsonProperty.DeclaringType, property.DeclaringType)) return; } if (flag) 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); } public JsonProperty GetClosestMatchProperty(string propertyName) { JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal); if (property == null) property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase); return property; } private new bool TryGetValue(string key, out JsonProperty item) { if (base.Dictionary == null) { item = null; return false; } return base.Dictionary.TryGetValue(key, out item); } public JsonProperty GetProperty(string propertyName, StringComparison comparisonType) { if (comparisonType == StringComparison.Ordinal) { if (TryGetValue(propertyName, out JsonProperty item)) return item; return null; } using (IEnumerator<JsonProperty> enumerator = GetEnumerator()) { while (enumerator.MoveNext()) { JsonProperty current = enumerator.Current; if (string.Equals(propertyName, current.PropertyName, comparisonType)) return current; } } return null; } } }