2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Collections.Generic;
29
using System.Collections.ObjectModel;
30
using Newtonsoft.Json.Utilities;
31
using System.Globalization;
33
namespace Newtonsoft.Json.Serialization
36
/// A collection of <see cref="JsonProperty"/> objects.
38
public class JsonPropertyCollection : KeyedCollection<string, JsonProperty>
40
private readonly Type _type;
43
/// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class.
45
/// <param name="type">The type.</param>
46
public JsonPropertyCollection(Type type)
47
: base(StringComparer.Ordinal)
49
ValidationUtils.ArgumentNotNull(type, "type");
54
/// When implemented in a derived class, extracts the key from the specified element.
56
/// <param name="item">The element from which to extract the key.</param>
57
/// <returns>The key for the specified element.</returns>
58
protected override string GetKeyForItem(JsonProperty item)
60
return item.PropertyName;
64
/// Adds a <see cref="JsonProperty"/> object.
66
/// <param name="property">The property to add to the collection.</param>
67
public void AddProperty(JsonProperty property)
69
if (Contains(property.PropertyName))
71
// don't overwrite existing property with ignored property
75
JsonProperty existingProperty = this[property.PropertyName];
76
bool duplicateProperty = true;
78
if (existingProperty.Ignored)
80
// remove ignored property so it can be replaced in collection
81
Remove(existingProperty);
82
duplicateProperty = false;
85
if (property.DeclaringType != null && existingProperty.DeclaringType != null)
87
if (property.DeclaringType.IsSubclassOf(existingProperty.DeclaringType))
89
// current property is on a derived class and hides the existing
90
Remove(existingProperty);
91
duplicateProperty = false;
93
if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType))
95
// current property is hidden by the existing so don't add it
100
if (duplicateProperty)
101
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));
108
/// Gets the closest matching <see cref="JsonProperty"/> object.
109
/// First attempts to get an exact case match of propertyName and then
110
/// a case insensitive match.
112
/// <param name="propertyName">Name of the property.</param>
113
/// <returns>A matching property if found.</returns>
114
public JsonProperty GetClosestMatchProperty(string propertyName)
116
JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
117
if (property == null)
118
property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
123
private bool TryGetValue(string key, out JsonProperty item)
125
if (Dictionary == null)
127
item = default(JsonProperty);
131
return Dictionary.TryGetValue(key, out item);
136
/// Gets a property by property name.
138
/// <param name="propertyName">The name of the property to get.</param>
139
/// <param name="comparisonType">Type property name string comparison.</param>
140
/// <returns>A matching property if found.</returns>
141
public JsonProperty GetProperty(string propertyName, StringComparison comparisonType)
143
// KeyedCollection has an ordinal comparer
144
if (comparisonType == StringComparison.Ordinal)
146
JsonProperty property;
147
if (TryGetValue(propertyName, out property))
153
foreach (JsonProperty property in this)
155
if (string.Equals(propertyName, property.PropertyName, comparisonType))
b'\\ No newline at end of file'