Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Serialization\JsonContract.cs

Symbol Coverage: 100.00% (20 of 20)

Branch Coverage: 100.00% (35 of 35)

Cyclomatic Complexity Avg: 1.17 Max:2

Code Lines: 20


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

  
31
namespace Newtonsoft.Json.Serialization
32
{
33
  /// <summary>
34
  /// Contract details for a <see cref="Type"/> used by the <see cref="JsonSerializer"/>.
35
  /// </summary>
36
  public abstract class JsonContract
37
  {
38
    /// <summary>
39
    /// Gets the underlying type for the contract.
40
    /// </summary>
41
    /// <value>The underlying type for the contract.</value>
42
    public Type UnderlyingType { get; private set; }
43

  
44
    /// <summary>
45
    /// Gets or sets the type created during deserialization.
46
    /// </summary>
47
    /// <value>The type created during deserialization.</value>
48
    public Type CreatedType { get; set; }
49

  
50
    /// <summary>
51
    /// Gets or sets whether this type contract is serialized as a reference.
52
    /// </summary>
53
    /// <value>Whether this type contract is serialized as a reference.</value>
54
    public bool? IsReference { get; set; }
55

  
56
    /// <summary>
57
    /// Gets or sets the default <see cref="JsonConverter" /> for this contract.
58
    /// </summary>
59
    /// <value>The converter.</value>
60
    public JsonConverter Converter { get; set; }
61

  
62
    // internally specified JsonConverter's to override default behavour
63
    // checked for after passed in converters and attribute specified converters
64
    internal JsonConverter InternalConverter { get; set; }
65

  
66
#if !PocketPC
67
    /// <summary>
68
    /// Gets or sets the method called immediately after deserialization of the object.
69
    /// </summary>
70
    /// <value>The method called immediately after deserialization of the object.</value>
71
    public MethodInfo OnDeserialized { get; set; }
72
    /// <summary>
73
    /// Gets or sets the method called during deserialization of the object.
74
    /// </summary>
75
    /// <value>The method called during deserialization of the object.</value>
76
    public MethodInfo OnDeserializing { get; set; }
77
    /// <summary>
78
    /// Gets or sets the method called after serialization of the object graph.
79
    /// </summary>
80
    /// <value>The method called after serialization of the object graph.</value>
81
    public MethodInfo OnSerialized { get; set; }
82
    /// <summary>
83
    /// Gets or sets the method called before serialization of the object.
84
    /// </summary>
85
    /// <value>The method called before serialization of the object.</value>
86
    public MethodInfo OnSerializing { get; set; }
87
#endif
88

  
89
    /// <summary>
90
    /// Gets or sets the default creator method used to create the object.
91
    /// </summary>
92
    /// <value>The default creator method used to create the object.</value>
93
    public Func<object> DefaultCreator { get; set; }
94

  
95
    /// <summary>
96
    /// Gets or sets a value indicating whether [default creator non public].
97
    /// </summary>
98
    /// <value><c>true</c> if the default object creator is non-public; otherwise, <c>false</c>.</value>
99
    public bool DefaultCreatorNonPublic { get; set; }
100

  
101
    /// <summary>
102
    /// Gets or sets the method called when an error is thrown during the serialization of the object.
103
    /// </summary>
104
    /// <value>The method called when an error is thrown during the serialization of the object.</value>
105
    public MethodInfo OnError { get; set; }
106

  
107
    internal void InvokeOnSerializing(object o, StreamingContext context)
108
    {
109
#if !PocketPC
110
 173611
      if (OnSerializing != null)
111
 6
        OnSerializing.Invoke(o, new object[] { context });
112
#endif
113
 173611
    }
114

  
115
    internal void InvokeOnSerialized(object o, StreamingContext context)
116
    {
117
#if !PocketPC
118
 173604
      if (OnSerialized != null)
119
 5
        OnSerialized.Invoke(o, new object[] { context });
120
#endif
121
 173604
    }
122

  
123
    internal void InvokeOnDeserializing(object o, StreamingContext context)
124
    {
125
#if !PocketPC
126
 70352
      if (OnDeserializing != null)
127
 9
        OnDeserializing.Invoke(o, new object[] { context });
128
#endif
129
 70352
    }
130

  
131
    internal void InvokeOnDeserialized(object o, StreamingContext context)
132
    {
133
#if !PocketPC
134
 70335
      if (OnDeserialized != null)
135
 9
        OnDeserialized.Invoke(o, new object[] { context });
136
#endif
137
 70335
    }
138

  
139
    internal void InvokeOnError(object o, StreamingContext context, ErrorContext errorContext)
140
    {
141
 36
      if (OnError != null)
142
 15
        OnError.Invoke(o, new object[] { context, errorContext });
143
 36
    }
144

  
145
 310
    internal JsonContract(Type underlyingType)
146
    {
147
 310
      ValidationUtils.ArgumentNotNull(underlyingType, "underlyingType");
148

  
149
 310
      UnderlyingType = underlyingType;
150
 310
      CreatedType = underlyingType;
151
 310
    }
152
  }
153
}