Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Linq\ComponentModel\JPropertyDescriptor.cs

Symbol Coverage: 88.89% (24 of 27)

Branch Coverage: 84.62% (11 of 13)

Cyclomatic Complexity Avg: 1.09 Max:2

Code Lines: 25


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
#if !SILVERLIGHT
27
using System;
28
using System.ComponentModel;
29
using Newtonsoft.Json.Utilities;
30

  
31
namespace Newtonsoft.Json.Linq.ComponentModel
32
{
33
  /// <summary>
34
  /// Represents a view of a <see cref="JProperty"/>.
35
  /// </summary>
36
  public class JPropertyDescriptor : PropertyDescriptor
37
  {
38
    private readonly Type _propertyType;
39

  
40
    /// <summary>
41
    /// Initializes a new instance of the <see cref="JPropertyDescriptor"/> class.
42
    /// </summary>
43
    /// <param name="name">The name.</param>
44
    /// <param name="propertyType">Type of the property.</param>
45
 35
    public JPropertyDescriptor(string name, Type propertyType)
46
 35
      : base(name, null)
47
    {
48
 35
      ValidationUtils.ArgumentNotNull(name, "name");
49
 35
      ValidationUtils.ArgumentNotNull(propertyType, "propertyType");
50

  
51
 35
      _propertyType = propertyType;
52
 35
    }
53

  
54
    private static JObject CastInstance(object instance)
55
    {
56
 12
      return (JObject)instance;
57
 12
    }
58

  
59
    /// <summary>
60
    /// When overridden in a derived class, returns whether resetting an object changes its value.
61
    /// </summary>
62
    /// <returns>
63
    /// true if resetting the component changes its value; otherwise, false.
64
    /// </returns>
65
    /// <param name="component">The component to test for reset capability. 
66
    ///                 </param>
67
    public override bool CanResetValue(object component)
68
    {
69
 4
      return false;
70
 4
    }
71

  
72
    /// <summary>
73
    /// When overridden in a derived class, gets the current value of the property on a component.
74
    /// </summary>
75
    /// <returns>
76
    /// The value of a property for a given component.
77
    /// </returns>
78
    /// <param name="component">The component with the property for which to retrieve the value. 
79
    ///                 </param>
80
    public override object GetValue(object component)
81
    {
82
 11
      JToken token = CastInstance(component)[Name];
83
      //if (token is JValue)
84
      //  return ((JValue) token).Value;
85

  
86
 11
      return token;
87
 11
    }
88

  
89
    /// <summary>
90
    /// When overridden in a derived class, resets the value for this property of the component to the default value.
91
    /// </summary>
92
    /// <param name="component">The component with the property value that is to be reset to the default value. 
93
    ///                 </param>
94
    public override void ResetValue(object component)
95
    {
96
 1
    }
97

  
98
    /// <summary>
99
    /// When overridden in a derived class, sets the value of the component to a different value.
100
    /// </summary>
101
    /// <param name="component">The component with the property value that is to be set. 
102
    ///                 </param><param name="value">The new value. 
103
    ///                 </param>
104
    public override void SetValue(object component, object value)
105
    {
106
 1
      JToken token = (value is JToken) ? (JToken) value : new JValue(value);
107

  
108
 1
      CastInstance(component)[Name] = token;
109
 1
    }
110

  
111
    /// <summary>
112
    /// When overridden in a derived class, determines a value indicating whether the value of this property needs to be persisted.
113
    /// </summary>
114
    /// <returns>
115
    /// true if the property should be persisted; otherwise, false.
116
    /// </returns>
117
    /// <param name="component">The component with the property to be examined for persistence. 
118
    ///                 </param>
119
    public override bool ShouldSerializeValue(object component)
120
    {
121
 4
      return false;
122
 4
    }
123

  
124
    /// <summary>
125
    /// When overridden in a derived class, gets the type of the component this property is bound to.
126
    /// </summary>
127
    /// <returns>
128
    /// A <see cref="T:System.Type"/> that represents the type of component this property is bound to. When the <see cref="M:System.ComponentModel.PropertyDescriptor.GetValue(System.Object)"/> or <see cref="M:System.ComponentModel.PropertyDescriptor.SetValue(System.Object,System.Object)"/> methods are invoked, the object specified might be an instance of this type.
129
    /// </returns>
130
    public override Type ComponentType
131
    {
132
 4
      get { return typeof(JObject); }
133
    }
134

  
135
    /// <summary>
136
    /// When overridden in a derived class, gets a value indicating whether this property is read-only.
137
    /// </summary>
138
    /// <returns>
139
    /// true if the property is read-only; otherwise, false.
140
    /// </returns>
141
    public override bool IsReadOnly
142
    {
143
 1
      get { return false; }
144
    }
145

  
146
    /// <summary>
147
    /// When overridden in a derived class, gets the type of the property.
148
    /// </summary>
149
    /// <returns>
150
    /// A <see cref="T:System.Type"/> that represents the type of the property.
151
    /// </returns>
152
    public override Type PropertyType
153
    {
154
 5
      get { return _propertyType; }
155
    }
156

  
157
    /// <summary>
158
    /// Gets the hash code for the name of the member.
159
    /// </summary>
160
    /// <value></value>
161
    /// <returns>
162
    /// The hash code for the name of the member.
163
    /// </returns>
164
    protected override int NameHashCode
165
    {
166
      get
167
      {
168
        // override property to fix up an error in its documentation
169
 0
        int nameHashCode = base.NameHashCode;
170
 0
        return nameHashCode;
171
 0
      }
172
    }
173
  }
174
}
175
#endif