Code Coverage Statistics for Source File
Newtonsoft.Json\Linq\JConstructor.cs
Symbol Coverage: 92.86% (52 of 56)
Branch Coverage: 81.82% (27 of 33)
Cyclomatic Complexity Avg: 1.67 Max:5
Code Lines: 54
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.Linq; |
|
29 |
using System.Text; |
|
30 |
using Newtonsoft.Json.Utilities; |
|
31 |
using System.Globalization; |
|
32 |
||
33 |
namespace Newtonsoft.Json.Linq |
|
34 |
{ |
|
35 |
/// <summary> |
|
36 |
/// Represents a JSON constructor. |
|
37 |
/// </summary> |
|
38 |
public class JConstructor : JContainer |
|
39 |
{ |
|
40 |
private string _name; |
|
41 |
||
42 |
/// <summary> |
|
43 |
/// Gets or sets the name of this constructor. |
|
44 |
/// </summary> |
|
45 |
/// <value>The constructor name.</value> |
|
46 |
public string Name |
|
47 |
{ |
|
48 |
8 |
get { return _name; }
|
49 |
1 |
set { _name = value; }
|
50 |
} |
|
51 |
||
52 |
/// <summary> |
|
53 |
/// Gets the node type for this <see cref="JToken"/>. |
|
54 |
/// </summary> |
|
55 |
/// <value>The type.</value> |
|
56 |
public override JTokenType Type |
|
57 |
{ |
|
58 |
4 |
get { return JTokenType.Constructor; }
|
59 |
} |
|
60 |
||
61 |
/// <summary> |
|
62 |
/// Initializes a new instance of the <see cref="JConstructor"/> class. |
|
63 |
/// </summary> |
|
64 |
2 |
public JConstructor()
|
65 |
{ |
|
66 |
2 |
}
|
67 |
||
68 |
/// <summary> |
|
69 |
/// Initializes a new instance of the <see cref="JConstructor"/> class from another <see cref="JConstructor"/> object. |
|
70 |
/// </summary> |
|
71 |
/// <param name="other">A <see cref="JConstructor"/> object to copy from.</param> |
|
72 |
2 |
public JConstructor(JConstructor other)
|
73 |
2 |
: base(other)
|
74 |
{ |
|
75 |
2 |
_name = other.Name;
|
76 |
2 |
}
|
77 |
||
78 |
/// <summary> |
|
79 |
/// Initializes a new instance of the <see cref="JConstructor"/> class with the specified name and content. |
|
80 |
/// </summary> |
|
81 |
/// <param name="name">The constructor name.</param> |
|
82 |
/// <param name="content">The contents of the constructor.</param> |
|
83 |
2 |
public JConstructor(string name, params object[] content)
|
84 |
2 |
: this(name, (object)content)
|
85 |
{ |
|
86 |
2 |
}
|
87 |
||
88 |
/// <summary> |
|
89 |
/// Initializes a new instance of the <see cref="JConstructor"/> class with the specified name and content. |
|
90 |
/// </summary> |
|
91 |
/// <param name="name">The constructor name.</param> |
|
92 |
/// <param name="content">The contents of the constructor.</param> |
|
93 |
3 |
public JConstructor(string name, object content)
|
94 |
3 |
: this(name)
|
95 |
{ |
|
96 |
3 |
Add(content);
|
97 |
3 |
}
|
98 |
||
99 |
/// <summary> |
|
100 |
/// Initializes a new instance of the <see cref="JConstructor"/> class with the specified name. |
|
101 |
/// </summary> |
|
102 |
/// <param name="name">The constructor name.</param> |
|
103 |
14 |
public JConstructor(string name)
|
104 |
{ |
|
105 |
14 |
ValidationUtils.ArgumentNotNullOrEmpty(name, "name");
|
106 |
||
107 |
14 |
_name = name;
|
108 |
14 |
}
|
109 |
||
110 |
internal override bool DeepEquals(JToken node) |
|
111 |
{ |
|
112 |
3 |
JConstructor c = node as JConstructor;
|
113 |
3 |
return (c != null && _name == c.Name && ContentsEqual(c));
|
114 |
3 |
}
|
115 |
||
116 |
internal override JToken CloneToken() |
|
117 |
{ |
|
118 |
2 |
return new JConstructor(this);
|
119 |
2 |
}
|
120 |
||
121 |
/// <summary> |
|
122 |
/// Writes this token to a <see cref="JsonWriter"/>. |
|
123 |
/// </summary> |
|
124 |
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param> |
|
125 |
/// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param> |
|
126 |
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters) |
|
127 |
{ |
|
128 |
3 |
writer.WriteStartConstructor(_name);
|
129 |
||
130 |
3 |
foreach (JToken token in Children())
|
131 |
{ |
|
132 |
5 |
token.WriteTo(writer, converters);
|
133 |
} |
|
134 |
||
135 |
3 |
writer.WriteEndConstructor();
|
136 |
3 |
}
|
137 |
||
138 |
/// <summary> |
|
139 |
/// Gets the <see cref="JToken"/> with the specified key. |
|
140 |
/// </summary> |
|
141 |
/// <value>The <see cref="JToken"/> with the specified key.</value> |
|
142 |
public override JToken this[object key] |
|
143 |
{ |
|
144 |
get |
|
145 |
{ |
|
146 |
2 |
ValidationUtils.ArgumentNotNull(key, "o");
|
147 |
||
148 |
2 |
if (!(key is int))
|
149 |
1 |
throw new ArgumentException("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
150 |
||
151 |
1 |
return GetItem((int)key);
|
152 |
1 |
}
|
153 |
set |
|
154 |
{ |
|
155 |
2 |
ValidationUtils.ArgumentNotNull(key, "o");
|
156 |
||
157 |
2 |
if (!(key is int))
|
158 |
1 |
throw new ArgumentException("Set JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
159 |
||
160 |
1 |
SetItem((int)key, value);
|
161 |
1 |
}
|
162 |
} |
|
163 |
||
164 |
internal override int GetDeepHashCode() |
|
165 |
{ |
|
166 |
4 |
return _name.GetHashCode() ^ ContentsHashCode();
|
167 |
4 |
}
|
168 |
||
169 |
/// <summary> |
|
170 |
/// Loads an <see cref="JConstructor"/> from a <see cref="JsonReader"/>. |
|
171 |
/// </summary> |
|
172 |
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param> |
|
173 |
/// <returns>A <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns> |
|
174 |
public static JConstructor Load(JsonReader reader) |
|
175 |
{ |
|
176 |
2 |
if (reader.TokenType == JsonToken.None)
|
177 |
{ |
|
178 |
0 |
if (!reader.Read())
|
179 |
0 |
throw new Exception("Error reading JConstructor from JsonReader.");
|
180 |
} |
|
181 |
||
182 |
2 |
if (reader.TokenType != JsonToken.StartConstructor)
|
183 |
0 |
throw new Exception("Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
184 |
||
185 |
2 |
JConstructor c = new JConstructor((string)reader.Value);
|
186 |
2 |
c.SetLineInfo(reader as IJsonLineInfo);
|
187 |
||
188 |
2 |
if (!reader.Read())
|
189 |
0 |
throw new Exception("Error reading JConstructor from JsonReader.");
|
190 |
||
191 |
2 |
c.ReadContentFrom(reader);
|
192 |
||
193 |
2 |
return c;
|
194 |
2 |
}
|
195 |
} |
|
196 |
} |