Code Coverage Statistics for Source File
Newtonsoft.Json\Serialization\JsonPropertyCollection.cs
Symbol Coverage: 96.30% (26 of 27)
Branch Coverage: 93.75% (15 of 16)
Cyclomatic Complexity Avg: 2.40 Max:4
Code Lines: 26
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
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 |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
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. |
|
24 |
#endregion |
|
25 |
||
26 |
using System; |
|
27 |
using System.Collections.Generic; |
|
28 |
using System.Text; |
|
29 |
using System.Collections.ObjectModel; |
|
30 |
using Newtonsoft.Json.Utilities; |
|
31 |
using System.Globalization; |
|
32 |
||
33 |
namespace Newtonsoft.Json.Serialization |
|
34 |
{ |
|
35 |
/// <summary> |
|
36 |
/// A collection of <see cref="JsonProperty"/> objects. |
|
37 |
/// </summary> |
|
38 |
public class JsonPropertyCollection : KeyedCollection<string, JsonProperty> |
|
39 |
{ |
|
40 |
private readonly JsonObjectContract _contract; |
|
41 |
||
42 |
/// <summary> |
|
43 |
/// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class. |
|
44 |
/// </summary> |
|
45 |
/// <param name="contract">The contract.</param> |
|
46 |
348 |
public JsonPropertyCollection(JsonObjectContract contract)
|
47 |
{ |
|
48 |
348 |
ValidationUtils.ArgumentNotNull(contract, "contract");
|
49 |
348 |
_contract = contract;
|
50 |
348 |
}
|
51 |
||
52 |
/// <summary> |
|
53 |
/// When implemented in a derived class, extracts the key from the specified element. |
|
54 |
/// </summary> |
|
55 |
/// <param name="item">The element from which to extract the key.</param> |
|
56 |
/// <returns>The key for the specified element.</returns> |
|
57 |
protected override string GetKeyForItem(JsonProperty item) |
|
58 |
{ |
|
59 |
1191 |
return item.PropertyName;
|
60 |
1191 |
}
|
61 |
||
62 |
/// <summary> |
|
63 |
/// Adds a <see cref="JsonProperty"/> object. |
|
64 |
/// </summary> |
|
65 |
/// <param name="property">The property to add to the collection.</param> |
|
66 |
public void AddProperty(JsonProperty property) |
|
67 |
{ |
|
68 |
598 |
if (Contains(property.PropertyName))
|
69 |
{ |
|
70 |
// don't overwrite existing property with ignored property |
|
71 |
5 |
if (property.Ignored)
|
72 |
4 |
return;
|
73 |
||
74 |
1 |
JsonProperty existingProperty = this[property.PropertyName];
|
75 |
||
76 |
1 |
if (!existingProperty.Ignored)
|
77 |
1 |
throw new JsonSerializationException(
|
78 |
1 |
"A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _contract.UnderlyingType));
|
79 |
||
80 |
// remove ignored property so it can be replaced in collection |
|
81 |
0 |
Remove(existingProperty);
|
82 |
} |
|
83 |
||
84 |
593 |
Add(property);
|
85 |
597 |
}
|
86 |
||
87 |
/// <summary> |
|
88 |
/// Gets the closest matching <see cref="JsonProperty"/> object. |
|
89 |
/// First attempts to get an exact case match of propertyName and then |
|
90 |
/// a case insensitive match. |
|
91 |
/// </summary> |
|
92 |
/// <param name="propertyName">Name of the property.</param> |
|
93 |
/// <returns>A matching property if found.</returns> |
|
94 |
public JsonProperty GetClosestMatchProperty(string propertyName) |
|
95 |
{ |
|
96 |
160686 |
JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
|
97 |
160686 |
if (property == null)
|
98 |
72 |
property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
|
99 |
||
100 |
160686 |
return property;
|
101 |
160686 |
}
|
102 |
||
103 |
/// <summary> |
|
104 |
/// Gets a property by property name. |
|
105 |
/// </summary> |
|
106 |
/// <param name="propertyName">The name of the property to get.</param> |
|
107 |
/// <param name="comparisonType">Type property name string comparison.</param> |
|
108 |
/// <returns>A matching property if found.</returns> |
|
109 |
public JsonProperty GetProperty(string propertyName, StringComparison comparisonType) |
|
110 |
{ |
|
111 |
160758 |
foreach (JsonProperty property in this)
|
112 |
{ |
|
113 |
462128 |
if (string.Equals(propertyName, property.PropertyName, comparisonType))
|
114 |
{ |
|
115 |
160647 |
return property;
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
111 |
return null;
|
120 |
160758 |
}
|
121 |
} |
|
122 |
} |