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.ComponentModel;
28
using System.Globalization;
30
using System.Reflection;
31
using System.Runtime.Serialization;
32
using System.Security.Permissions;
33
using Newtonsoft.Json.Utilities;
35
namespace Newtonsoft.Json.Serialization
37
#if !SILVERLIGHT && !PocketPC && !NET20
38
internal interface IMetadataTypeAttribute
40
Type MetadataClassType { get; }
44
internal static class JsonTypeReflector
46
public const string IdPropertyName = "$id";
47
public const string RefPropertyName = "$ref";
48
public const string TypePropertyName = "$type";
49
public const string ArrayValuesPropertyName = "$values";
51
public const string ShouldSerializePrefix = "ShouldSerialize";
53
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> JsonConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetJsonConverterTypeFromAttribute);
54
#if !SILVERLIGHT && !PocketPC && !NET20
55
private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
57
private const string MetadataTypeAttributeTypeName =
58
"System.ComponentModel.DataAnnotations.MetadataTypeAttribute, System.ComponentModel.DataAnnotations, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
59
private static Type _cachedMetadataTypeAttributeType;
62
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> TypeConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetTypeConverterTypeFromAttribute);
64
private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
66
TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
67
if (converterAttribute == null)
70
return Type.GetType(converterAttribute.ConverterTypeName);
73
private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider)
75
return TypeConverterTypeCache.Get(attributeProvider);
79
public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
81
return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type);
84
public static JsonObjectAttribute GetJsonObjectAttribute(Type type)
86
return GetJsonContainerAttribute(type) as JsonObjectAttribute;
89
public static JsonArrayAttribute GetJsonArrayAttribute(Type type)
91
return GetJsonContainerAttribute(type) as JsonArrayAttribute;
94
#if !PocketPC && !NET20
95
public static DataContractAttribute GetDataContractAttribute(Type type)
97
return CachedAttributeGetter<DataContractAttribute>.GetAttribute(type);
101
public static MemberSerialization GetObjectMemberSerialization(Type objectType)
103
JsonObjectAttribute objectAttribute = GetJsonObjectAttribute(objectType);
105
if (objectAttribute == null)
107
#if !PocketPC && !NET20
108
DataContractAttribute dataContractAttribute = GetDataContractAttribute(objectType);
110
if (dataContractAttribute != null)
111
return MemberSerialization.OptIn;
114
return MemberSerialization.OptOut;
117
return objectAttribute.MemberSerialization;
120
private static Type GetJsonConverterType(ICustomAttributeProvider attributeProvider)
122
return JsonConverterTypeCache.Get(attributeProvider);
125
private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
127
JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
128
return (converterAttribute != null)
129
? converterAttribute.ConverterType
133
public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
135
Type converterType = GetJsonConverterType(attributeProvider);
137
if (converterType != null)
139
JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType);
141
if (!memberConverter.CanConvert(targetConvertedType))
142
throw new JsonSerializationException("JsonConverter {0} on {1} is not compatible with member type {2}.".FormatWith(CultureInfo.InvariantCulture, memberConverter.GetType().Name, attributeProvider, targetConvertedType.Name));
144
return memberConverter;
151
public static TypeConverter GetTypeConverter(Type type)
154
return TypeDescriptor.GetConverter(type);
156
Type converterType = GetTypeConverterType(type);
158
if (converterType != null)
159
return (TypeConverter)ReflectionUtils.CreateInstance(converterType);
166
#if !SILVERLIGHT && !PocketPC && !NET20
167
private static Type GetAssociatedMetadataType(Type type)
169
return AssociatedMetadataTypesCache.Get(type);
172
private static Type GetAssociateMetadataTypeFromAttribute(Type type)
174
Type metadataTypeAttributeType = GetMetadataTypeAttributeType();
175
if (metadataTypeAttributeType == null)
178
object attribute = type.GetCustomAttributes(metadataTypeAttributeType, true).SingleOrDefault();
179
if (attribute == null)
182
IMetadataTypeAttribute metadataTypeAttribute = (DynamicCodeGeneration)
183
? DynamicWrapper.CreateWrapper<IMetadataTypeAttribute>(attribute)
184
: new LateBoundMetadataTypeAttribute(attribute);
186
return metadataTypeAttribute.MetadataClassType;
189
private static Type GetMetadataTypeAttributeType()
191
// always attempt to get the metadata type attribute type
192
// the assembly may have been loaded since last time
193
if (_cachedMetadataTypeAttributeType == null)
195
Type metadataTypeAttributeType = Type.GetType(MetadataTypeAttributeTypeName);
197
if (metadataTypeAttributeType != null)
198
_cachedMetadataTypeAttributeType = metadataTypeAttributeType;
203
return _cachedMetadataTypeAttributeType;
206
private static T GetAttribute<T>(Type type) where T : Attribute
208
Type metadataType = GetAssociatedMetadataType(type);
209
if (metadataType != null)
211
T attribute = ReflectionUtils.GetAttribute<T>(metadataType, true);
212
if (attribute != null)
216
return ReflectionUtils.GetAttribute<T>(type, true);
219
private static T GetAttribute<T>(MemberInfo memberInfo) where T : Attribute
221
Type metadataType = GetAssociatedMetadataType(memberInfo.DeclaringType);
222
if (metadataType != null)
224
MemberInfo metadataTypeMemberInfo = metadataType.GetMember(memberInfo.Name,
225
memberInfo.MemberType,
226
BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).SingleOrDefault();
228
if (metadataTypeMemberInfo != null)
230
T attribute = ReflectionUtils.GetAttribute<T>(metadataTypeMemberInfo, true);
231
if (attribute != null)
236
return ReflectionUtils.GetAttribute<T>(memberInfo, true);
239
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
241
Type type = attributeProvider as Type;
243
return GetAttribute<T>(type);
245
MemberInfo memberInfo = attributeProvider as MemberInfo;
246
if (memberInfo != null)
247
return GetAttribute<T>(memberInfo);
249
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
252
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
254
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
258
private static bool? _dynamicCodeGeneration;
260
public static bool DynamicCodeGeneration
264
if (_dynamicCodeGeneration == null)
266
#if !PocketPC && !SILVERLIGHT
269
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
270
new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess).Demand();
271
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
272
_dynamicCodeGeneration = true;
276
_dynamicCodeGeneration = false;
279
_dynamicCodeGeneration = false;
283
return _dynamicCodeGeneration.Value;
287
public static ReflectionDelegateFactory ReflectionDelegateFactory
291
#if !PocketPC && !SILVERLIGHT
292
if (DynamicCodeGeneration)
293
return DynamicReflectionDelegateFactory.Instance;
296
return LateBoundReflectionDelegateFactory.Instance;
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.ComponentModel;
28
using System.Globalization;
29
using System.Reflection;
30
#if !(NETFX_CORE || PORTABLE)
31
using System.Security.Permissions;
33
using Newtonsoft.Json.Utilities;
34
#if NETFX_CORE || PORTABLE
35
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
38
using Newtonsoft.Json.Utilities.LinqBridge;
42
using System.Runtime.Serialization;
44
namespace Newtonsoft.Json.Serialization
46
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
47
internal interface IMetadataTypeAttribute
49
Type MetadataClassType { get; }
53
internal static class JsonTypeReflector
55
public const string IdPropertyName = "$id";
56
public const string RefPropertyName = "$ref";
57
public const string TypePropertyName = "$type";
58
public const string ValuePropertyName = "$value";
59
public const string ArrayValuesPropertyName = "$values";
61
public const string ShouldSerializePrefix = "ShouldSerialize";
62
public const string SpecifiedPostfix = "Specified";
64
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> JsonConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetJsonConverterTypeFromAttribute);
65
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
66
private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
68
private const string MetadataTypeAttributeTypeName =
69
"System.ComponentModel.DataAnnotations.MetadataTypeAttribute, System.ComponentModel.DataAnnotations, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
70
private static Type _cachedMetadataTypeAttributeType;
73
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> TypeConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetTypeConverterTypeFromAttribute);
75
private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
77
TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
78
if (converterAttribute == null)
81
return Type.GetType(converterAttribute.ConverterTypeName);
84
private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider)
86
return TypeConverterTypeCache.Get(attributeProvider);
90
public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
92
return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type.GetCustomAttributeProvider());
95
public static JsonObjectAttribute GetJsonObjectAttribute(Type type)
97
return GetJsonContainerAttribute(type) as JsonObjectAttribute;
100
public static JsonArrayAttribute GetJsonArrayAttribute(Type type)
102
return GetJsonContainerAttribute(type) as JsonArrayAttribute;
105
public static JsonDictionaryAttribute GetJsonDictionaryAttribute(Type type)
107
return GetJsonContainerAttribute(type) as JsonDictionaryAttribute;
110
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
111
public static SerializableAttribute GetSerializableAttribute(Type type)
113
return CachedAttributeGetter<SerializableAttribute>.GetAttribute(type.GetCustomAttributeProvider());
117
#if !PocketPC && !NET20
118
public static DataContractAttribute GetDataContractAttribute(Type type)
120
// DataContractAttribute does not have inheritance
121
Type currentType = type;
123
while (currentType != null)
125
DataContractAttribute result = CachedAttributeGetter<DataContractAttribute>.GetAttribute(currentType.GetCustomAttributeProvider());
129
currentType = currentType.BaseType();
135
public static DataMemberAttribute GetDataMemberAttribute(MemberInfo memberInfo)
137
// DataMemberAttribute does not have inheritance
139
// can't override a field
140
if (memberInfo.MemberType() == MemberTypes.Field)
141
return CachedAttributeGetter<DataMemberAttribute>.GetAttribute(memberInfo.GetCustomAttributeProvider());
143
// search property and then search base properties if nothing is returned and the property is virtual
144
PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
145
DataMemberAttribute result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(propertyInfo.GetCustomAttributeProvider());
148
if (propertyInfo.IsVirtual())
150
Type currentType = propertyInfo.DeclaringType;
152
while (result == null && currentType != null)
154
PropertyInfo baseProperty = (PropertyInfo)ReflectionUtils.GetMemberInfoFromType(currentType, propertyInfo);
155
if (baseProperty != null && baseProperty.IsVirtual())
156
result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(baseProperty.GetCustomAttributeProvider());
158
currentType = currentType.BaseType();
167
public static MemberSerialization GetObjectMemberSerialization(Type objectType, bool ignoreSerializableAttribute)
169
JsonObjectAttribute objectAttribute = GetJsonObjectAttribute(objectType);
170
if (objectAttribute != null)
171
return objectAttribute.MemberSerialization;
173
#if !PocketPC && !NET20
174
DataContractAttribute dataContractAttribute = GetDataContractAttribute(objectType);
175
if (dataContractAttribute != null)
176
return MemberSerialization.OptIn;
179
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
180
if (!ignoreSerializableAttribute)
182
SerializableAttribute serializableAttribute = GetSerializableAttribute(objectType);
183
if (serializableAttribute != null)
184
return MemberSerialization.Fields;
189
return MemberSerialization.OptOut;
192
private static Type GetJsonConverterType(ICustomAttributeProvider attributeProvider)
194
return JsonConverterTypeCache.Get(attributeProvider);
197
private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
199
JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
200
return (converterAttribute != null)
201
? converterAttribute.ConverterType
205
public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
207
object provider = null;
208
#if !(NETFX_CORE || PORTABLE)
209
provider = attributeProvider as MemberInfo;
211
provider = attributeProvider.UnderlyingObject;
214
Type converterType = GetJsonConverterType(attributeProvider);
216
if (converterType != null)
218
JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType);
220
return memberConverter;
226
#if !(NETFX_CORE || PORTABLE)
228
public static TypeConverter GetTypeConverter(Type type)
231
return TypeDescriptor.GetConverter(type);
233
Type converterType = GetTypeConverterType(type);
235
if (converterType != null)
236
return (TypeConverter)ReflectionUtils.CreateInstance(converterType);
244
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
245
private static Type GetAssociatedMetadataType(Type type)
247
return AssociatedMetadataTypesCache.Get(type);
250
private static Type GetAssociateMetadataTypeFromAttribute(Type type)
252
Type metadataTypeAttributeType = GetMetadataTypeAttributeType();
253
if (metadataTypeAttributeType == null)
256
object attribute = type.GetCustomAttributes(metadataTypeAttributeType, true).SingleOrDefault();
257
if (attribute == null)
260
IMetadataTypeAttribute metadataTypeAttribute = (DynamicCodeGeneration)
261
? DynamicWrapper.CreateWrapper<IMetadataTypeAttribute>(attribute)
262
: new LateBoundMetadataTypeAttribute(attribute);
264
return metadataTypeAttribute.MetadataClassType;
267
private static Type GetMetadataTypeAttributeType()
269
// always attempt to get the metadata type attribute type
270
// the assembly may have been loaded since last time
271
if (_cachedMetadataTypeAttributeType == null)
273
Type metadataTypeAttributeType = Type.GetType(MetadataTypeAttributeTypeName);
275
if (metadataTypeAttributeType != null)
276
_cachedMetadataTypeAttributeType = metadataTypeAttributeType;
281
return _cachedMetadataTypeAttributeType;
285
private static T GetAttribute<T>(Type type) where T : Attribute
289
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
290
Type metadataType = GetAssociatedMetadataType(type);
291
if (metadataType != null)
293
attribute = ReflectionUtils.GetAttribute<T>(metadataType, true);
294
if (attribute != null)
299
attribute = ReflectionUtils.GetAttribute<T>(type.GetCustomAttributeProvider(), true);
300
if (attribute != null)
303
foreach (Type typeInterface in type.GetInterfaces())
305
attribute = ReflectionUtils.GetAttribute<T>(typeInterface.GetCustomAttributeProvider(), true);
306
if (attribute != null)
313
private static T GetAttribute<T>(MemberInfo memberInfo) where T : Attribute
317
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
318
Type metadataType = GetAssociatedMetadataType(memberInfo.DeclaringType);
319
if (metadataType != null)
321
MemberInfo metadataTypeMemberInfo = ReflectionUtils.GetMemberInfoFromType(metadataType, memberInfo);
323
if (metadataTypeMemberInfo != null)
325
attribute = ReflectionUtils.GetAttribute<T>(metadataTypeMemberInfo, true);
326
if (attribute != null)
332
attribute = ReflectionUtils.GetAttribute<T>(memberInfo.GetCustomAttributeProvider(), true);
333
if (attribute != null)
336
if (memberInfo.DeclaringType != null)
338
foreach (Type typeInterface in memberInfo.DeclaringType.GetInterfaces())
340
MemberInfo interfaceTypeMemberInfo = ReflectionUtils.GetMemberInfoFromType(typeInterface, memberInfo);
342
if (interfaceTypeMemberInfo != null)
344
attribute = ReflectionUtils.GetAttribute<T>(interfaceTypeMemberInfo.GetCustomAttributeProvider(), true);
345
if (attribute != null)
354
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
356
object provider = null;
357
#if !(NETFX_CORE || PORTABLE)
358
provider = attributeProvider;
360
provider = attributeProvider.UnderlyingObject;
363
Type type = provider as Type;
365
return GetAttribute<T>(type);
367
MemberInfo memberInfo = provider as MemberInfo;
368
if (memberInfo != null)
369
return GetAttribute<T>(memberInfo);
371
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
374
private static bool? _dynamicCodeGeneration;
375
private static bool? _fullyTrusted;
378
internal static void SetFullyTrusted(bool fullyTrusted)
380
_fullyTrusted = fullyTrusted;
383
internal static void SetDynamicCodeGeneration(bool dynamicCodeGeneration)
385
_dynamicCodeGeneration = dynamicCodeGeneration;
389
public static bool DynamicCodeGeneration
393
if (_dynamicCodeGeneration == null)
395
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
398
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
399
new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess).Demand();
400
new SecurityPermission(SecurityPermissionFlag.SkipVerification).Demand();
401
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
402
new SecurityPermission(PermissionState.Unrestricted).Demand();
403
_dynamicCodeGeneration = true;
407
_dynamicCodeGeneration = false;
410
_dynamicCodeGeneration = false;
414
return _dynamicCodeGeneration.Value;
418
public static bool FullyTrusted
422
if (_fullyTrusted == null)
424
#if (NETFX_CORE || SILVERLIGHT || PORTABLE)
425
_fullyTrusted = false;
426
#elif !(NET20 || NET35)
427
AppDomain appDomain = AppDomain.CurrentDomain;
429
_fullyTrusted = appDomain.IsHomogenous && appDomain.IsFullyTrusted;
433
new SecurityPermission(PermissionState.Unrestricted).Demand();
434
_fullyTrusted = true;
438
_fullyTrusted = false;
443
return _fullyTrusted.Value;
447
public static ReflectionDelegateFactory ReflectionDelegateFactory
451
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
452
if (DynamicCodeGeneration)
453
return DynamicReflectionDelegateFactory.Instance;
456
return LateBoundReflectionDelegateFactory.Instance;
b'\\ No newline at end of file'