~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Type _type;
 
41
 
 
42
    /// <summary>
 
43
    /// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class.
 
44
    /// </summary>
 
45
    /// <param name="type">The type.</param>
 
46
    public JsonPropertyCollection(Type type)
 
47
      : base(StringComparer.Ordinal)
 
48
    {
 
49
      ValidationUtils.ArgumentNotNull(type, "type");
 
50
      _type = type;
 
51
    }
 
52
 
 
53
    /// <summary>
 
54
    /// When implemented in a derived class, extracts the key from the specified element.
 
55
    /// </summary>
 
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)
 
59
    {
 
60
      return item.PropertyName;
 
61
    }
 
62
 
 
63
    /// <summary>
 
64
    /// Adds a <see cref="JsonProperty"/> object.
 
65
    /// </summary>
 
66
    /// <param name="property">The property to add to the collection.</param>
 
67
    public void AddProperty(JsonProperty property)
 
68
    {
 
69
      if (Contains(property.PropertyName))
 
70
      {
 
71
        // don't overwrite existing property with ignored property
 
72
        if (property.Ignored)
 
73
          return;
 
74
 
 
75
        JsonProperty existingProperty = this[property.PropertyName];
 
76
        bool duplicateProperty = true;
 
77
 
 
78
        if (existingProperty.Ignored)
 
79
        {
 
80
          // remove ignored property so it can be replaced in collection
 
81
          Remove(existingProperty);
 
82
          duplicateProperty = false;
 
83
        }
 
84
 
 
85
        if (property.DeclaringType != null && existingProperty.DeclaringType != null)
 
86
        {
 
87
          if (property.DeclaringType.IsSubclassOf(existingProperty.DeclaringType))
 
88
          {
 
89
            // current property is on a derived class and hides the existing
 
90
            Remove(existingProperty);
 
91
            duplicateProperty = false;
 
92
          }
 
93
          if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType))
 
94
          {
 
95
            // current property is hidden by the existing so don't add it
 
96
            return;
 
97
          }
 
98
        }
 
99
 
 
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));
 
102
      }
 
103
 
 
104
      Add(property);
 
105
    }
 
106
 
 
107
    /// <summary>
 
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.
 
111
    /// </summary>
 
112
    /// <param name="propertyName">Name of the property.</param>
 
113
    /// <returns>A matching property if found.</returns>
 
114
    public JsonProperty GetClosestMatchProperty(string propertyName)
 
115
    {
 
116
      JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
 
117
      if (property == null)
 
118
        property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
 
119
 
 
120
      return property;
 
121
    }
 
122
 
 
123
    private bool TryGetValue(string key, out JsonProperty item)
 
124
    {
 
125
      if (Dictionary == null)
 
126
      {
 
127
        item = default(JsonProperty);
 
128
        return false;
 
129
      }
 
130
 
 
131
      return Dictionary.TryGetValue(key, out item);
 
132
    }
 
133
 
 
134
 
 
135
    /// <summary>
 
136
    /// Gets a property by property name.
 
137
    /// </summary>
 
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)
 
142
    {
 
143
      // KeyedCollection has an ordinal comparer
 
144
      if (comparisonType == StringComparison.Ordinal)
 
145
      {
 
146
        JsonProperty property;
 
147
        if (TryGetValue(propertyName, out property))
 
148
          return property;
 
149
 
 
150
        return null;
 
151
      }
 
152
 
 
153
      foreach (JsonProperty property in this)
 
154
      {
 
155
        if (string.Equals(propertyName, property.PropertyName, comparisonType))
 
156
        {
 
157
          return property;
 
158
        }
 
159
      }
 
160
 
 
161
      return null;
 
162
    }
 
163
  }
 
164
}
 
 
b'\\ No newline at end of file'