Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Linq\ComponentModel\JTypeDescriptor.cs

Symbol Coverage: 48.72% (19 of 39)

Branch Coverage: 54.55% (12 of 22)

Cyclomatic Complexity Avg: 1.36 Max:4

Code Lines: 37


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.Collections.Generic;
29
using System.ComponentModel;
30
using Newtonsoft.Json.Utilities;
31

  
32
namespace Newtonsoft.Json.Linq.ComponentModel
33
{
34
  /// <summary>
35
  /// Represents a view of a <see cref="JObject"/>.
36
  /// </summary>
37
  public class JTypeDescriptor : ICustomTypeDescriptor
38
  {
39
    private readonly JObject _value;
40

  
41
    /// <summary>
42
    /// Initializes a new instance of the <see cref="JTypeDescriptor"/> class.
43
    /// </summary>
44
    /// <param name="value">The value.</param>
45
 12
    public JTypeDescriptor(JObject value)
46
    {
47
 12
      ValidationUtils.ArgumentNotNull(value, "value");
48
 12
      _value = value;
49
 12
    }
50

  
51
    /// <summary>
52
    /// Returns the properties for this instance of a component.
53
    /// </summary>
54
    /// <returns>
55
    /// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"/> that represents the properties for this component instance.
56
    /// </returns>
57
    public virtual PropertyDescriptorCollection GetProperties()
58
    {
59
 11
      return GetProperties(null);
60
 11
    }
61

  
62
    private static Type GetTokenPropertyType(JToken token)
63
    {
64
 29
      if (token is JValue)
65
      {
66
 20
        JValue v = (JValue) token;
67
 20
        return (v.Value != null) ? v.Value.GetType() : typeof (object);
68
      }
69

  
70
 9
      return token.GetType();
71
 29
    }
72

  
73
    /// <summary>
74
    /// Returns the properties for this instance of a component using the attribute array as a filter.
75
    /// </summary>
76
    /// <param name="attributes">An array of type <see cref="T:System.Attribute"/> that is used as a filter.</param>
77
    /// <returns>
78
    /// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"/> that represents the filtered properties for this component instance.
79
    /// </returns>
80
    public virtual PropertyDescriptorCollection GetProperties(Attribute[] attributes)
81
    {
82
 11
      PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null);
83

  
84
 11
      if (_value != null)
85
      {
86
 11
        foreach (KeyValuePair<string, JToken> propertyValue in _value)
87
        {
88
 29
          descriptors.Add(new JPropertyDescriptor(propertyValue.Key, GetTokenPropertyType(propertyValue.Value)));
89
        }
90
      }
91

  
92
 11
      return descriptors;
93
 11
    }
94

  
95
    /// <summary>
96
    /// Returns a collection of custom attributes for this instance of a component.
97
    /// </summary>
98
    /// <returns>
99
    /// An <see cref="T:System.ComponentModel.AttributeCollection"/> containing the attributes for this object.
100
    /// </returns>
101
    public AttributeCollection GetAttributes()
102
    {
103
 0
      return AttributeCollection.Empty;
104
 0
    }
105

  
106
    /// <summary>
107
    /// Returns the class name of this instance of a component.
108
    /// </summary>
109
    /// <returns>
110
    /// The class name of the object, or null if the class does not have a name.
111
    /// </returns>
112
    public string GetClassName()
113
    {
114
 0
      return null;
115
 0
    }
116

  
117
    /// <summary>
118
    /// Returns the name of this instance of a component.
119
    /// </summary>
120
    /// <returns>
121
    /// The name of the object, or null if the object does not have a name.
122
    /// </returns>
123
    public string GetComponentName()
124
    {
125
 0
      return null;
126
 0
    }
127

  
128
    /// <summary>
129
    /// Returns a type converter for this instance of a component.
130
    /// </summary>
131
    /// <returns>
132
    /// A <see cref="T:System.ComponentModel.TypeConverter"/> that is the converter for this object, or null if there is no <see cref="T:System.ComponentModel.TypeConverter"/> for this object.
133
    /// </returns>
134
    public TypeConverter GetConverter()
135
    {
136
 0
      return new TypeConverter();
137
 0
    }
138

  
139
    /// <summary>
140
    /// Returns the default event for this instance of a component.
141
    /// </summary>
142
    /// <returns>
143
    /// An <see cref="T:System.ComponentModel.EventDescriptor"/> that represents the default event for this object, or null if this object does not have events.
144
    /// </returns>
145
    public EventDescriptor GetDefaultEvent()
146
    {
147
 0
      return null;
148
 0
    }
149

  
150
    /// <summary>
151
    /// Returns the default property for this instance of a component.
152
    /// </summary>
153
    /// <returns>
154
    /// A <see cref="T:System.ComponentModel.PropertyDescriptor"/> that represents the default property for this object, or null if this object does not have properties.
155
    /// </returns>
156
    public PropertyDescriptor GetDefaultProperty()
157
    {
158
 0
      return null;
159
 0
    }
160

  
161
    /// <summary>
162
    /// Returns an editor of the specified type for this instance of a component.
163
    /// </summary>
164
    /// <param name="editorBaseType">A <see cref="T:System.Type"/> that represents the editor for this object.</param>
165
    /// <returns>
166
    /// An <see cref="T:System.Object"/> of the specified type that is the editor for this object, or null if the editor cannot be found.
167
    /// </returns>
168
    public object GetEditor(Type editorBaseType)
169
    {
170
 0
      return null;
171
 0
    }
172

  
173
    /// <summary>
174
    /// Returns the events for this instance of a component using the specified attribute array as a filter.
175
    /// </summary>
176
    /// <param name="attributes">An array of type <see cref="T:System.Attribute"/> that is used as a filter.</param>
177
    /// <returns>
178
    /// An <see cref="T:System.ComponentModel.EventDescriptorCollection"/> that represents the filtered events for this component instance.
179
    /// </returns>
180
    public EventDescriptorCollection GetEvents(Attribute[] attributes)
181
    {
182
 0
      return EventDescriptorCollection.Empty;
183
 0
    }
184

  
185
    /// <summary>
186
    /// Returns the events for this instance of a component.
187
    /// </summary>
188
    /// <returns>
189
    /// An <see cref="T:System.ComponentModel.EventDescriptorCollection"/> that represents the events for this component instance.
190
    /// </returns>
191
    public EventDescriptorCollection GetEvents()
192
    {
193
 0
      return EventDescriptorCollection.Empty;
194
 0
    }
195

  
196
    /// <summary>
197
    /// Returns an object that contains the property described by the specified property descriptor.
198
    /// </summary>
199
    /// <param name="pd">A <see cref="T:System.ComponentModel.PropertyDescriptor"/> that represents the property whose owner is to be found.</param>
200
    /// <returns>
201
    /// An <see cref="T:System.Object"/> that represents the owner of the specified property.
202
    /// </returns>
203
    public object GetPropertyOwner(PropertyDescriptor pd)
204
    {
205
 0
      return null;
206
 0
    }
207
  }
208
}
209
#endif