2 // Copyright (c) 2007 James Newton-King
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
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
27 using System.Reflection;
28 using System.Runtime.Serialization;
29 using Newtonsoft.Json.Utilities;
31 namespace Newtonsoft.Json.Serialization
34 /// Contract details for a <see cref="Type"/> used by the <see cref="JsonSerializer"/>.
36 public abstract class JsonContract
39 /// Gets the underlying type for the contract.
41 /// <value>The underlying type for the contract.</value>
42 public Type UnderlyingType { get; private set; }
45 /// Gets or sets the type created during deserialization.
47 /// <value>The type created during deserialization.</value>
48 public Type CreatedType { get; set; }
51 /// Gets or sets whether this type contract is serialized as a reference.
53 /// <value>Whether this type contract is serialized as a reference.</value>
54 public bool? IsReference { get; set; }
57 /// Gets or sets the default <see cref="JsonConverter" /> for this contract.
59 /// <value>The converter.</value>
60 public JsonConverter Converter { get; set; }
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; }
68 /// Gets or sets the method called immediately after deserialization of the object.
70 /// <value>The method called immediately after deserialization of the object.</value>
71 public MethodInfo OnDeserialized { get; set; }
73 /// Gets or sets the method called during deserialization of the object.
75 /// <value>The method called during deserialization of the object.</value>
76 public MethodInfo OnDeserializing { get; set; }
78 /// Gets or sets the method called after serialization of the object graph.
80 /// <value>The method called after serialization of the object graph.</value>
81 public MethodInfo OnSerialized { get; set; }
83 /// Gets or sets the method called before serialization of the object.
85 /// <value>The method called before serialization of the object.</value>
86 public MethodInfo OnSerializing { get; set; }
90 /// Gets or sets the default creator method used to create the object.
92 /// <value>The default creator method used to create the object.</value>
93 public Func<object> DefaultCreator { get; set; }
96 /// Gets or sets a value indicating whether [default creator non public].
98 /// <value><c>true</c> if the default object creator is non-public; otherwise, <c>false</c>.</value>
99 public bool DefaultCreatorNonPublic { get; set; }
102 /// Gets or sets the method called when an error is thrown during the serialization of the object.
104 /// <value>The method called when an error is thrown during the serialization of the object.</value>
105 public MethodInfo OnError { get; set; }
107 internal void InvokeOnSerializing(object o, StreamingContext context)
110 if (OnSerializing != null)
111 OnSerializing.Invoke(o, new object[] { context });
115 internal void InvokeOnSerialized(object o, StreamingContext context)
118 if (OnSerialized != null)
119 OnSerialized.Invoke(o, new object[] { context });
123 internal void InvokeOnDeserializing(object o, StreamingContext context)
126 if (OnDeserializing != null)
127 OnDeserializing.Invoke(o, new object[] { context });
131 internal void InvokeOnDeserialized(object o, StreamingContext context)
134 if (OnDeserialized != null)
135 OnDeserialized.Invoke(o, new object[] { context });
139 internal void InvokeOnError(object o, StreamingContext context, ErrorContext errorContext)
142 OnError.Invoke(o, new object[] { context, errorContext });
145 internal JsonContract(Type underlyingType)
147 ValidationUtils.ArgumentNotNull(underlyingType, "underlyingType");
149 UnderlyingType = underlyingType;
150 CreatedType = underlyingType;