Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Linq\JValue.cs

Symbol Coverage: 92.36% (133 of 144)

Branch Coverage: 94.85% (92 of 97)

Cyclomatic Complexity Avg: 2.97 Max:20

Code Lines: 150


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 value in JSON (string, integer, date, etc).
37
  /// </summary>
38
  public class JValue : JToken, IEquatable<JValue>
39
  {
40
    private JTokenType _valueType;
41
    private object _value;
42

  
43
 1489
    internal JValue(object value, JTokenType type)
44
    {
45
 1489
      _value = value;
46
 1489
      _valueType = type;
47
 1489
    }
48

  
49
    /// <summary>
50
    /// Initializes a new instance of the <see cref="JValue"/> class from another <see cref="JValue"/> object.
51
    /// </summary>
52
    /// <param name="other">A <see cref="JValue"/> object to copy from.</param>
53
 10
    public JValue(JValue other)
54
 10
      : this(other.Value, other.Type)
55
    {
56
 10
    }
57

  
58
    /// <summary>
59
    /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
60
    /// </summary>
61
    /// <param name="value">The value.</param>
62
 103
    public JValue(long value)
63
 103
      : this(value, JTokenType.Integer)
64
    {
65
 103
    }
66

  
67
    /// <summary>
68
    /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
69
    /// </summary>
70
    /// <param name="value">The value.</param>
71
 7
    public JValue(ulong value)
72
 7
      : this(value, JTokenType.Integer)
73
    {
74
 7
    }
75

  
76
    /// <summary>
77
    /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
78
    /// </summary>
79
    /// <param name="value">The value.</param>
80
 11
    public JValue(double value)
81
 11
      : this(value, JTokenType.Float)
82
    {
83
 11
    }
84

  
85
    /// <summary>
86
    /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
87
    /// </summary>
88
    /// <param name="value">The value.</param>
89
 9
    public JValue(DateTime value)
90
 9
      : this(value, JTokenType.Date)
91
    {
92
 9
    }
93

  
94
    /// <summary>
95
    /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
96
    /// </summary>
97
    /// <param name="value">The value.</param>
98
 17
    public JValue(bool value)
99
 17
      : this(value, JTokenType.Boolean)
100
    {
101
 17
    }
102

  
103
    /// <summary>
104
    /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
105
    /// </summary>
106
    /// <param name="value">The value.</param>
107
 44
    public JValue(string value)
108
 44
      : this(value, JTokenType.String)
109
    {
110
 44
    }
111

  
112
    /// <summary>
113
    /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
114
    /// </summary>
115
    /// <param name="value">The value.</param>
116
 1224
    public JValue(object value)
117
 1224
      : this(value, GetValueType(null, value))
118
    {
119
 1224
    }
120

  
121
    internal override bool DeepEquals(JToken node)
122
    {
123
 128
      JValue other = node as JValue;
124
 128
      if (other == null)
125
 0
        return false;
126

  
127
 128
      return ValuesEquals(this, other);
128
 128
    }
129

  
130
    /// <summary>
131
    /// Gets a value indicating whether this token has childen tokens.
132
    /// </summary>
133
    /// <value>
134
    /// 	<c>true</c> if this token has child values; otherwise, <c>false</c>.
135
    /// </value>
136
    public override bool HasValues
137
    {
138
 1
      get { return false; }
139
    }
140

  
141
    private static bool Compare(JTokenType valueType, object objA, object objB)
142
    {
143
 146
      if (objA == null && objB == null)
144
 23
        return true;
145
 123
      if (objA == null || objB == null)
146
 0
        return false;
147

  
148
 123
      switch (valueType)
149
      {
150
        case JTokenType.Integer:
151
 55
          if (objA is ulong || objB is ulong)
152
 3
            return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).Equals(Convert.ToDecimal(objB, CultureInfo.InvariantCulture));
153
          else
154
 52
            return Convert.ToInt64(objA, CultureInfo.InvariantCulture).Equals(Convert.ToInt64(objB, CultureInfo.InvariantCulture));
155
        case JTokenType.Float:
156
 22
          return Convert.ToDouble(objA, CultureInfo.InvariantCulture).Equals(Convert.ToDouble(objB, CultureInfo.InvariantCulture));
157
        case JTokenType.Comment:
158
        case JTokenType.String:
159
        case JTokenType.Boolean:
160
        case JTokenType.Raw:
161
 39
          return objA.Equals(objB);
162
        case JTokenType.Date:
163
 3
          return objA.Equals(objB);
164
        case JTokenType.Bytes:
165
 4
          byte[] b1 = objA as byte[];
166
 4
          byte[] b2 = objB as byte[];
167
 4
          if (b1 == null || b2 == null)
168
 0
            return false;
169

  
170
 4
          return MiscellaneousUtils.ByteArrayCompare(b1, b2);
171
        default:
172
 0
          throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
173
      }
174
 146
    }
175

  
176
    internal override JToken CloneToken()
177
    {
178
 9
      return new JValue(this);
179
 9
    }
180

  
181
    /// <summary>
182
    /// Creates a <see cref="JValue"/> comment with the given value.
183
    /// </summary>
184
    /// <param name="value">The value.</param>
185
    /// <returns>A <see cref="JValue"/> comment with the given value.</returns>
186
    public static JValue CreateComment(string value)
187
    {
188
 3
      return new JValue(value, JTokenType.Comment);
189
 3
    }
190

  
191
    /// <summary>
192
    /// Creates a <see cref="JValue"/> string with the given value.
193
    /// </summary>
194
    /// <param name="value">The value.</param>
195
    /// <returns>A <see cref="JValue"/> string with the given value.</returns>
196
    public static JValue CreateString(string value)
197
    {
198
 1
      return new JValue(value, JTokenType.String);
199
 1
    }
200

  
201
    private static JTokenType GetValueType(JTokenType? current, object value)
202
    {
203
 1231
      if (value == null)
204
 109
        return JTokenType.Null;
205
 1122
      else if (value == DBNull.Value)
206
 2
        return JTokenType.Null;
207
 1120
      else if (value is string)
208
 564
        return GetStringValueType(current);
209
 556
      else if (value is long || value is int || value is short || value is sbyte
210
 556
        || value is ulong || value is uint || value is ushort || value is byte)
211
 414
        return JTokenType.Integer;
212
 142
      else if (value is Enum)
213
 2
        return JTokenType.Integer;
214
 140
      else if (value is double || value is float || value is decimal)
215
 78
        return JTokenType.Float;
216
 62
      else if (value is DateTime)
217
 20
        return JTokenType.Date;
218
#if !PocketPC && !NET20
219
 42
      else if (value is DateTimeOffset)
220
 9
        return JTokenType.Date;
221
#endif
222
 33
      else if (value is byte[])
223
 14
        return JTokenType.Bytes;
224
 19
      else if (value is bool)
225
 19
        return JTokenType.Boolean;
226

  
227
 0
      throw new ArgumentException("Could not determine JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
228
 1231
    }
229

  
230
    private static JTokenType GetStringValueType(JTokenType? current)
231
    {
232
 564
      if (current == null)
233
 561
        return JTokenType.String;
234

  
235
 3
      switch (current.Value)
236
      {
237
        case JTokenType.Comment:
238
        case JTokenType.String:
239
        case JTokenType.Raw:
240
 1
          return current.Value;
241
        default:
242
 2
          return JTokenType.String;
243
      }
244
 564
    }
245

  
246
    /// <summary>
247
    /// Gets the node type for this <see cref="JToken"/>.
248
    /// </summary>
249
    /// <value>The type.</value>
250
    public override JTokenType Type
251
    {
252
 946
      get { return _valueType; }
253
    }
254

  
255
    /// <summary>
256
    /// Gets or sets the underlying token value.
257
    /// </summary>
258
    /// <value>The underlying token value.</value>
259
    public object Value
260
    {
261
 473
      get { return _value; }
262
      set
263
      {
264
 8
        Type currentType = (_value != null) ? _value.GetType() : null;
265
 8
        Type newType = (value != null) ? value.GetType() : null;
266

  
267
 8
        if (currentType != newType)
268
 7
          _valueType = GetValueType(_valueType, value);
269

  
270
 8
        _value = value;
271
 8
      }
272
    }
273

  
274
    private static void WriteConvertableValue(JsonWriter writer, IList<JsonConverter> converters, Action<object> defaultWrite, object value)
275
    {
276
      JsonConverter matchingConverter;
277

  
278
 225
      if (value != null && ((matchingConverter = JsonSerializer.GetMatchingConverter(converters, value.GetType())) != null))
279
 5
        matchingConverter.WriteJson(writer, value, new JsonSerializer());
280
      else
281
 220
        defaultWrite(value);
282
 225
    }
283

  
284
    /// <summary>
285
    /// Writes this token to a <see cref="JsonWriter"/>.
286
    /// </summary>
287
    /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
288
    /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
289
    public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
290
    {
291
 259
      switch (_valueType)
292
      {
293
        case JTokenType.Comment:
294
 1
          writer.WriteComment(_value.ToString());
295
 1
          break;
296
        case JTokenType.Integer:
297
 34
          WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToInt64(v, CultureInfo.InvariantCulture)), _value);
298
 34
          break;
299
        case JTokenType.Float:
300
 14
          WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToDouble(v, CultureInfo.InvariantCulture)), _value);
301
 14
          break;
302
        case JTokenType.String:
303
 162
          WriteConvertableValue(writer, converters, v => writer.WriteValue((v != null) ? v.ToString() : null), _value);
304
 162
          break;
305
        case JTokenType.Boolean:
306
 4
          WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToBoolean(v, CultureInfo.InvariantCulture)), _value);
307
 4
          break;
308
        case JTokenType.Date:
309
 10
          WriteConvertableValue(writer, converters, v =>
310
 10
          {
311
 10
#if !PocketPC && !NET20
312
 5
            if (v is DateTimeOffset)
313
 0
              writer.WriteValue((DateTimeOffset)v);
314
 10
            else
315
 10
#endif
316
 5
            writer.WriteValue(Convert.ToDateTime(v, CultureInfo.InvariantCulture));
317
 10
          }, _value);
318
 10
          break;
319
        case JTokenType.Raw:
320
 13
          writer.WriteRawValue((_value != null) ? _value.ToString() : null);
321
 13
          break;
322
        case JTokenType.Bytes:
323
 1
          WriteConvertableValue(writer, converters, v => writer.WriteValue((byte[])v), _value);
324
 1
          break;
325
        case JTokenType.Null:
326
 18
          writer.WriteNull();
327
 18
          break;
328
        case JTokenType.Undefined:
329
 2
          writer.WriteUndefined();
330
 2
          break;
331
        default:
332
 0
          throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
333
      }
334
 259
    }
335

  
336
    internal override int GetDeepHashCode()
337
    {
338
 17
      int valueHashCode = (_value != null) ? _value.GetHashCode() : 0;
339
      
340
 17
      return _valueType.GetHashCode() ^ valueHashCode;
341
 17
    }
342

  
343
    private static bool ValuesEquals(JValue v1, JValue v2)
344
    {
345
 290
      return (v1 == v2|| (v1._valueType == v2._valueType && Compare(v1._valueType, v1._value, v2._value)));
346
 290
    }
347

  
348
    /// <summary>
349
    /// Indicates whether the current object is equal to another object of the same type.
350
    /// </summary>
351
    /// <returns>
352
    /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
353
    /// </returns>
354
    /// <param name="other">An object to compare with this object.</param>
355
    public bool Equals(JValue other)
356
    {
357
 162
      if (other == null)
358
 0
        return false;
359

  
360
 162
      return ValuesEquals(this, other);
361
 162
    }
362

  
363
    /// <summary>
364
    /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
365
    /// </summary>
366
    /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
367
    /// <returns>
368
    /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
369
    /// </returns>
370
    /// <exception cref="T:System.NullReferenceException">
371
    /// The <paramref name="obj"/> parameter is null.
372
    /// </exception>
373
    public override bool Equals(object obj)
374
    {
375
 162
      if (obj == null)
376
 0
        return false;
377

  
378
 162
      JValue otherValue = obj as JValue;
379
 162
      if (otherValue != null)
380
 162
        return Equals(otherValue);
381

  
382
 0
      return base.Equals(obj);
383
 162
    }
384

  
385
    /// <summary>
386
    /// Serves as a hash function for a particular type.
387
    /// </summary>
388
    /// <returns>
389
    /// A hash code for the current <see cref="T:System.Object"/>.
390
    /// </returns>
391
    public override int GetHashCode()
392
    {
393
 21
      if (_value == null)
394
 0
        return 0;
395

  
396
 21
      return _value.GetHashCode();
397
 21
    }
398
  }
399
}