Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Utilities\ConvertUtils.cs

Symbol Coverage: 50.94% (81 of 159)

Branch Coverage: 51.20% (64 of 125)

Cyclomatic Complexity Avg: 3.13 Max:27

Code Lines: 163


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 System.Globalization;
31
using System.ComponentModel;
32
using Newtonsoft.Json.Serialization;
33
using System.Reflection;
34

  
35
#if !SILVERLIGHT
36
using System.Data.SqlTypes;
37
#endif
38

  
39
namespace Newtonsoft.Json.Utilities
40
{
41
  internal static class ConvertUtils
42
  {
43
    internal struct TypeConvertKey : IEquatable<TypeConvertKey>
44
    {
45
      private readonly Type _initialType;
46
      private readonly Type _targetType;
47

  
48
      public Type InitialType
49
      {
50
 7
        get { return _initialType; }
51
      }
52

  
53
      public Type TargetType
54
      {
55
 7
        get { return _targetType; }
56
      }
57

  
58
      public TypeConvertKey(Type initialType, Type targetType)
59
      {
60
 5
        _initialType = initialType;
61
 5
        _targetType = targetType;
62
 5
      }
63

  
64
      public override int GetHashCode()
65
      {
66
 17
        return _initialType.GetHashCode() ^ _targetType.GetHashCode();
67
 17
      }
68

  
69
      public override bool Equals(object obj)
70
      {
71
 0
        if (!(obj is TypeConvertKey))
72
 0
          return false;
73

  
74
 0
        return Equals((TypeConvertKey)obj);
75
 0
      }
76

  
77
      public bool Equals(TypeConvertKey other)
78
      {
79
 1
        return (_initialType == other._initialType && _targetType == other._targetType);
80
 1
      }
81
    }
82

  
83
 1
    private static readonly ThreadSafeStore<TypeConvertKey, Func<object, object>> CastConverters =
84
 1
      new ThreadSafeStore<TypeConvertKey, Func<object, object>>(CreateCastConverter);
85

  
86
    private static Func<object, object> CreateCastConverter(TypeConvertKey t)
87
    {
88
 4
      MethodInfo castMethodInfo = t.TargetType.GetMethod("op_Implicit", new[] { t.InitialType });
89
 4
      if (castMethodInfo == null)
90
 3
        castMethodInfo = t.TargetType.GetMethod("op_Explicit", new[] { t.InitialType });
91

  
92
 4
      if (castMethodInfo == null)
93
 3
        return null;
94

  
95
 1
      MethodCall<object, object> call = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(castMethodInfo);
96

  
97
 1
      return o => call(null, o);
98
 4
    }
99

  
100
    public static bool CanConvertType(Type initialType, Type targetType, bool allowTypeNameToString)
101
    {
102
 0
      ValidationUtils.ArgumentNotNull(initialType, "initialType");
103
 0
      ValidationUtils.ArgumentNotNull(targetType, "targetType");
104

  
105
 0
      if (ReflectionUtils.IsNullableType(targetType))
106
 0
        targetType = Nullable.GetUnderlyingType(targetType);
107

  
108
 0
      if (targetType == initialType)
109
 0
        return true;
110

  
111
 0
      if (typeof(IConvertible).IsAssignableFrom(initialType) && typeof(IConvertible).IsAssignableFrom(targetType))
112
      {
113
 0
        return true;
114
      }
115

  
116
#if !PocketPC && !NET20
117
 0
      if (initialType == typeof(DateTime) && targetType == typeof(DateTimeOffset))
118
 0
        return true;
119
#endif
120

  
121
 0
      if (initialType == typeof(Guid) && (targetType == typeof(Guid) || targetType == typeof(string)))
122
 0
        return true;
123

  
124
 0
      if (initialType == typeof(Type) && targetType == typeof(string))
125
 0
        return true;
126

  
127
#if !PocketPC
128
      // see if source or target types have a TypeConverter that converts between the two
129
 0
      TypeConverter toConverter = GetConverter(initialType);
130

  
131
 0
      if (toConverter != null && !IsComponentConverter(toConverter) && toConverter.CanConvertTo(targetType))
132
      {
133
 0
        if (allowTypeNameToString || toConverter.GetType() != typeof(TypeConverter))
134
 0
          return true;
135
      }
136

  
137
 0
      TypeConverter fromConverter = GetConverter(targetType);
138

  
139
 0
      if (fromConverter != null && !IsComponentConverter(fromConverter) && fromConverter.CanConvertFrom(initialType))
140
 0
        return true;
141
#endif
142

  
143
      // handle DBNull and INullable
144
 0
      if (initialType == typeof(DBNull))
145
      {
146
 0
        if (ReflectionUtils.IsNullable(targetType))
147
 0
          return true;
148
      }
149

  
150
 0
      return false;
151
 0
    }
152

  
153
    private static bool IsComponentConverter(TypeConverter converter)
154
    {
155
#if !SILVERLIGHT && !PocketPC
156
 0
      return (converter is ComponentConverter);
157
#else
158
      return false;
159
#endif
160
 0
    }
161

  
162
    #region Convert
163
    /// <summary>
164
    /// Converts the value to the specified type.
165
    /// </summary>
166
    /// <typeparam name="T">The type to convert the value to.</typeparam>
167
    /// <param name="initialValue">The value to convert.</param>
168
    /// <returns>The converted type.</returns>
169
    public static T Convert<T>(object initialValue)
170
    {
171
 0
      return Convert<T>(initialValue, CultureInfo.CurrentCulture);
172
 0
    }
173

  
174
    /// <summary>
175
    /// Converts the value to the specified type.
176
    /// </summary>
177
    /// <typeparam name="T">The type to convert the value to.</typeparam>
178
    /// <param name="initialValue">The value to convert.</param>
179
    /// <param name="culture">The culture to use when converting.</param>
180
    /// <returns>The converted type.</returns>
181
    public static T Convert<T>(object initialValue, CultureInfo culture)
182
    {
183
 0
      return (T)Convert(initialValue, culture, typeof(T));
184
 0
    }
185

  
186
    /// <summary>
187
    /// Converts the value to the specified type.
188
    /// </summary>
189
    /// <param name="initialValue">The value to convert.</param>
190
    /// <param name="culture">The culture to use when converting.</param>
191
    /// <param name="targetType">The type to convert the value to.</param>
192
    /// <returns>The converted type.</returns>
193
    public static object Convert(object initialValue, CultureInfo culture, Type targetType)
194
    {
195
 40239
      if (initialValue == null)
196
 1
        throw new ArgumentNullException("initialValue");
197

  
198
 40238
      if (ReflectionUtils.IsNullableType(targetType))
199
 8
        targetType = Nullable.GetUnderlyingType(targetType);
200

  
201
 40238
      Type initialType = initialValue.GetType();
202

  
203
 40238
      if (targetType == initialType)
204
 3
        return initialValue;
205

  
206
 40235
      if (initialValue is string && typeof(Type).IsAssignableFrom(targetType))
207
 2
        return Type.GetType((string) initialValue, true);
208

  
209
 40233
      if (targetType.IsInterface || targetType.IsGenericTypeDefinition || targetType.IsAbstract)
210
 0
        throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType");
211

  
212
      // use Convert.ChangeType if both types are IConvertible
213
 40233
      if (initialValue is IConvertible && typeof(IConvertible).IsAssignableFrom(targetType))
214
      {
215
 40209
        if (targetType.IsEnum)
216
        {
217
 11
          if (initialValue is string)
218
 2
            return Enum.Parse(targetType, initialValue.ToString(), true);
219
 9
          else if (IsInteger(initialValue))
220
 9
            return Enum.ToObject(targetType, initialValue);
221
        }
222
        
223
 40198
        return System.Convert.ChangeType(initialValue, targetType, culture);
224
      }
225

  
226
#if !PocketPC && !NET20
227
 24
      if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
228
 2
        return new DateTimeOffset((DateTime)initialValue);
229
#endif
230

  
231
 22
      if (initialValue is string)
232
      {
233
 22
        if (targetType == typeof (Guid))
234
 16
          return new Guid((string) initialValue);
235
 6
        if (targetType == typeof (Uri))
236
 1
          return new Uri((string) initialValue);
237
 5
        if (targetType == typeof (TimeSpan))
238
 1
          return TimeSpan.Parse((string) initialValue);
239
      }
240

  
241
#if !PocketPC
242
      // see if source or target types have a TypeConverter that converts between the two
243
 4
      TypeConverter toConverter = GetConverter(initialType);
244

  
245
 4
      if (toConverter != null && toConverter.CanConvertTo(targetType))
246
      {
247
#if !SILVERLIGHT
248
 0
        return toConverter.ConvertTo(null, culture, initialValue, targetType);
249
#else
250
        return toConverter.ConvertTo(initialValue, targetType);
251
#endif
252
      }
253

  
254
 4
      TypeConverter fromConverter = GetConverter(targetType);
255

  
256
 4
      if (fromConverter != null && fromConverter.CanConvertFrom(initialType))
257
      {
258
#if !SILVERLIGHT
259
 1
        return fromConverter.ConvertFrom(null, culture, initialValue);
260
#else
261
        return fromConverter.ConvertFrom(initialValue);
262
#endif
263
      }
264
#endif
265

  
266
      // handle DBNull and INullable
267
 3
      if (initialValue == DBNull.Value)
268
      {
269
 0
        if (ReflectionUtils.IsNullable(targetType))
270
 0
          return EnsureTypeAssignable(null, initialType, targetType);
271
        
272
 0
        throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
273
      }
274
#if !SILVERLIGHT
275
 3
      if (initialValue is INullable)
276
 0
        return EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType);
277
#endif
278

  
279
 3
      throw new Exception("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
280
 40233
    }
281
    #endregion
282

  
283
    #region TryConvert
284
    /// <summary>
285
    /// Converts the value to the specified type.
286
    /// </summary>
287
    /// <typeparam name="T">The type to convert the value to.</typeparam>
288
    /// <param name="initialValue">The value to convert.</param>
289
    /// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
290
    /// <returns>
291
    /// 	<c>true</c> if <c>initialValue</c> was converted successfully; otherwise, <c>false</c>.
292
    /// </returns>
293
    public static bool TryConvert<T>(object initialValue, out T convertedValue)
294
    {
295
 0
      return TryConvert(initialValue, CultureInfo.CurrentCulture, out convertedValue);
296
 0
    }
297

  
298
    /// <summary>
299
    /// Converts the value to the specified type.
300
    /// </summary>
301
    /// <typeparam name="T">The type to convert the value to.</typeparam>
302
    /// <param name="initialValue">The value to convert.</param>
303
    /// <param name="culture">The culture to use when converting.</param>
304
    /// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
305
    /// <returns>
306
    /// 	<c>true</c> if <c>initialValue</c> was converted successfully; otherwise, <c>false</c>.
307
    /// </returns>
308
    public static bool TryConvert<T>(object initialValue, CultureInfo culture, out T convertedValue)
309
    {
310
 0
      return MiscellaneousUtils.TryAction<T>(delegate
311
 0
      {
312
 0
        object tempConvertedValue;
313
 0
        TryConvert(initialValue, CultureInfo.CurrentCulture, typeof(T), out tempConvertedValue);
314
 0

  
315
 0
        return (T)tempConvertedValue;
316
 0
      }, out convertedValue);
317
 0
    }
318

  
319
    /// <summary>
320
    /// Converts the value to the specified type.
321
    /// </summary>
322
    /// <param name="initialValue">The value to convert.</param>
323
    /// <param name="culture">The culture to use when converting.</param>
324
    /// <param name="targetType">The type to convert the value to.</param>
325
    /// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
326
    /// <returns>
327
    /// 	<c>true</c> if <c>initialValue</c> was converted successfully; otherwise, <c>false</c>.
328
    /// </returns>
329
    public static bool TryConvert(object initialValue, CultureInfo culture, Type targetType, out object convertedValue)
330
    {
331
 40239
      return MiscellaneousUtils.TryAction<object>(delegate { return Convert(initialValue, culture, targetType); }, out convertedValue);
332
 40239
    }
333
    #endregion
334

  
335
    #region ConvertOrCast
336
    /// <summary>
337
    /// Converts the value to the specified type. If the value is unable to be converted, the
338
    /// value is checked whether it assignable to the specified type.
339
    /// </summary>
340
    /// <typeparam name="T">The type to convert or cast the value to.</typeparam>
341
    /// <param name="initialValue">The value to convert.</param>
342
    /// <returns>The converted type. If conversion was unsuccessful, the initial value is returned if assignable to the target type</returns>
343
    public static T ConvertOrCast<T>(object initialValue)
344
    {
345
 0
      return ConvertOrCast<T>(initialValue, CultureInfo.CurrentCulture);
346
 0
    }
347

  
348
    /// <summary>
349
    /// Converts the value to the specified type. If the value is unable to be converted, the
350
    /// value is checked whether it assignable to the specified type.
351
    /// </summary>
352
    /// <typeparam name="T">The type to convert or cast the value to.</typeparam>
353
    /// <param name="initialValue">The value to convert.</param>
354
    /// <param name="culture">The culture to use when converting.</param>
355
    /// <returns>The converted type. If conversion was unsuccessful, the initial value is returned if assignable to the target type</returns>
356
    public static T ConvertOrCast<T>(object initialValue, CultureInfo culture)
357
    {
358
 0
      return (T)ConvertOrCast(initialValue, culture, typeof(T));
359
 0
    }
360

  
361
    /// <summary>
362
    /// Converts the value to the specified type. If the value is unable to be converted, the
363
    /// value is checked whether it assignable to the specified type.
364
    /// </summary>
365
    /// <param name="initialValue">The value to convert.</param>
366
    /// <param name="culture">The culture to use when converting.</param>
367
    /// <param name="targetType">The type to convert or cast the value to.</param>
368
    /// <returns>
369
    /// The converted type. If conversion was unsuccessful, the initial value
370
    /// is returned if assignable to the target type.
371
    /// </returns>
372
    public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType)
373
    {
374
      object convertedValue;
375

  
376
 60310
      if (targetType == typeof(object))
377
 18
        return initialValue;
378

  
379
 60292
      if (initialValue == null && ReflectionUtils.IsNullable(targetType))
380
 20053
        return null;
381

  
382
 40239
      if (TryConvert(initialValue, culture, targetType, out convertedValue))
383
 40233
        return convertedValue;
384

  
385
 6
      return EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType);
386
 60306
    }
387
    #endregion
388

  
389
    #region TryConvertOrCast
390
    /// <summary>
391
    /// Converts the value to the specified type. If the value is unable to be converted, the
392
    /// value is checked whether it assignable to the specified type.
393
    /// </summary>
394
    /// <typeparam name="T">The type to convert the value to.</typeparam>
395
    /// <param name="initialValue">The value to convert.</param>
396
    /// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
397
    /// <returns>
398
    /// 	<c>true</c> if <c>initialValue</c> was converted successfully or is assignable; otherwise, <c>false</c>.
399
    /// </returns>
400
    public static bool TryConvertOrCast<T>(object initialValue, out T convertedValue)
401
    {
402
 0
      return TryConvertOrCast<T>(initialValue, CultureInfo.CurrentCulture, out convertedValue);
403
 0
    }
404

  
405
    /// <summary>
406
    /// Converts the value to the specified type. If the value is unable to be converted, the
407
    /// value is checked whether it assignable to the specified type.
408
    /// </summary>
409
    /// <typeparam name="T">The type to convert the value to.</typeparam>
410
    /// <param name="initialValue">The value to convert.</param>
411
    /// <param name="culture">The culture to use when converting.</param>
412
    /// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
413
    /// <returns>
414
    /// 	<c>true</c> if <c>initialValue</c> was converted successfully or is assignable; otherwise, <c>false</c>.
415
    /// </returns>
416
    public static bool TryConvertOrCast<T>(object initialValue, CultureInfo culture, out T convertedValue)
417
    {
418
 0
      return MiscellaneousUtils.TryAction<T>(delegate
419
 0
      {
420
 0
        object tempConvertedValue;
421
 0
        TryConvertOrCast(initialValue, CultureInfo.CurrentCulture, typeof(T), out tempConvertedValue);
422
 0

  
423
 0
        return (T)tempConvertedValue;
424
 0
      }, out convertedValue);
425
 0
    }
426

  
427
    /// <summary>
428
    /// Converts the value to the specified type. If the value is unable to be converted, the
429
    /// value is checked whether it assignable to the specified type.
430
    /// </summary>
431
    /// <param name="initialValue">The value to convert.</param>
432
    /// <param name="culture">The culture to use when converting.</param>
433
    /// <param name="targetType">The type to convert the value to.</param>
434
    /// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
435
    /// <returns>
436
    /// 	<c>true</c> if <c>initialValue</c> was converted successfully or is assignable; otherwise, <c>false</c>.
437
    /// </returns>
438
    public static bool TryConvertOrCast(object initialValue, CultureInfo culture, Type targetType, out object convertedValue)
439
    {
440
 0
      return MiscellaneousUtils.TryAction<object>(delegate { return ConvertOrCast(initialValue, culture, targetType); }, out convertedValue);
441
 0
    }
442
    #endregion
443

  
444
    private static object EnsureTypeAssignable(object value, Type initialType, Type targetType)
445
    {
446
 6
      Type valueType = (value != null) ? value.GetType() : null;
447

  
448
 6
      if (value != null)
449
      {
450
 5
        if (targetType.IsAssignableFrom(valueType))
451
 0
          return value;
452

  
453
 5
        Func<object, object> castConverter = CastConverters.Get(new TypeConvertKey(valueType, targetType));
454
 5
        if (castConverter != null)
455
 2
          return castConverter(value);
456
      }
457
      else
458
      {
459
 1
        if (ReflectionUtils.IsNullable(targetType))
460
 0
          return null;
461
      }
462

  
463
 4
      throw new Exception("Could not cast or convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, (initialType != null) ? initialType.ToString() : "{null}", targetType));
464
 2
    }
465

  
466
#if !SILVERLIGHT
467
    public static object ToValue(INullable nullableValue)
468
    {
469
 0
      if (nullableValue == null)
470
 0
        return null;
471
 0
      else if (nullableValue is SqlInt32)
472
 0
        return ToValue((SqlInt32)nullableValue);
473
 0
      else if (nullableValue is SqlInt64)
474
 0
        return ToValue((SqlInt64)nullableValue);
475
 0
      else if (nullableValue is SqlBoolean)
476
 0
        return ToValue((SqlBoolean)nullableValue);
477
 0
      else if (nullableValue is SqlString)
478
 0
        return ToValue((SqlString)nullableValue);
479
 0
      else if (nullableValue is SqlDateTime)
480
 0
        return ToValue((SqlDateTime)nullableValue);
481

  
482
 0
      throw new Exception("Unsupported INullable type: {0}".FormatWith(CultureInfo.InvariantCulture, nullableValue.GetType()));
483
 0
    }
484
#endif
485

  
486
#if !PocketPC
487
    internal static TypeConverter GetConverter(Type t)
488
    {
489
 218
      return JsonTypeReflector.GetTypeConverter(t);
490
 218
    }
491
#endif
492

  
493
    public static bool IsInteger(object value)
494
    {
495
 9
      switch (System.Convert.GetTypeCode(value))
496
      {
497
        case TypeCode.SByte:
498
        case TypeCode.Byte:
499
        case TypeCode.Int16:
500
        case TypeCode.UInt16:
501
        case TypeCode.Int32:
502
        case TypeCode.UInt32:
503
        case TypeCode.Int64:
504
        case TypeCode.UInt64:
505
 9
          return true;
506
        default:
507
 0
          return false;
508
      }
509
 9
    }
510
  }
511
}