Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Serialization\DefaultSerializationBinder.cs

Symbol Coverage: 81.82% (9 of 11)

Branch Coverage: 71.43% (5 of 7)

Cyclomatic Complexity Avg: 2.00 Max:4

Code Lines: 11


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.Runtime.Serialization;
28
using System.Reflection;
29
using System.Globalization;
30
using Newtonsoft.Json.Utilities;
31

  
32
namespace Newtonsoft.Json.Serialization
33
{
34
  /// <summary>
35
  /// The default serialization binder used when resolving and loading classes from type names.
36
  /// </summary>
37
  public class DefaultSerializationBinder : SerializationBinder
38
  {
39
 1
    internal static readonly DefaultSerializationBinder Instance = new DefaultSerializationBinder();
40

  
41
    /// <summary>
42
    /// When overridden in a derived class, controls the binding of a serialized object to a type.
43
    /// </summary>
44
    /// <param name="assemblyName">Specifies the <see cref="T:System.Reflection.Assembly"/> name of the serialized object.</param>
45
    /// <param name="typeName">Specifies the <see cref="T:System.Type"/> name of the serialized object.</param>
46
    /// <returns>
47
    /// The type of the object the formatter creates a new instance of.
48
    /// </returns>
49
    public override Type BindToType(string assemblyName, string typeName)
50
    {
51
 13
      if (assemblyName != null)
52
      {
53
        Assembly assembly;
54

  
55
#if !SILVERLIGHT && !PocketPC
56
        // look, I don't like using obsolete methods as much as you do but this is the only way
57
        // Assembly.Load won't check the GAC for a partial name
58
#pragma warning disable 618,612
59
 12
        assembly = Assembly.LoadWithPartialName(assemblyName);
60
#pragma warning restore 618,612
61
#else
62
        assembly = Assembly.Load(assemblyName);
63
#endif
64

  
65
 12
        if (assembly == null)
66
 0
          throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, assemblyName));
67

  
68
 12
        Type type = assembly.GetType(typeName);
69
 12
        if (type == null)
70
 0
          throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));
71

  
72
 12
        return type;
73
      }
74
      else
75
      {
76
 1
        return Type.GetType(typeName);
77
      }
78
 13
    }
79
  }
80
}