#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections;
#if HAVE_CONCURRENT_DICTIONARY
using System.Collections.Concurrent;
#endif
using LC.Newtonsoft.Json.Schema;
using System.Collections.Generic;
using System.ComponentModel;
#if HAVE_DYNAMIC
using System.Dynamic;
#endif
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
#if HAVE_CAS
using System.Security.Permissions;
#endif
using LC.Newtonsoft.Json.Converters;
using LC.Newtonsoft.Json.Utilities;
using LC.Newtonsoft.Json.Linq;
using System.Runtime.CompilerServices;
#if !HAVE_LINQ
using LC.Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using LC.Newtonsoft.Json.Serialization;
namespace LC.Newtonsoft.Json.Serialization
{
///
/// Used by to resolve a for a given .
///
public class DefaultContractResolver : IContractResolver
{
private static readonly IContractResolver _instance = new DefaultContractResolver();
// Json.NET Schema requires a property
internal static IContractResolver Instance => _instance;
private static readonly string[] BlacklistedTypeNames =
{
"System.IO.DriveInfo",
"System.IO.FileInfo",
"System.IO.DirectoryInfo"
};
private static readonly JsonConverter[] BuiltInConverters =
{
#if HAVE_ENTITY_FRAMEWORK
new EntityKeyMemberConverter(),
#endif
#if HAVE_DYNAMIC
new ExpandoObjectConverter(),
#endif
#if (HAVE_XML_DOCUMENT || HAVE_XLINQ)
new XmlNodeConverter(),
#endif
#if HAVE_ADO_NET
new BinaryConverter(),
new DataSetConverter(),
new DataTableConverter(),
#endif
#if HAVE_FSHARP_TYPES
new DiscriminatedUnionConverter(),
#endif
new KeyValuePairConverter(),
#pragma warning disable 618
new BsonObjectIdConverter(),
#pragma warning restore 618
new RegexConverter()
};
private readonly DefaultJsonNameTable _nameTable = new DefaultJsonNameTable();
private readonly ThreadSafeStore _contractCache;
///
/// Gets a value indicating whether members are being get and set using dynamic code generation.
/// This value is determined by the runtime permissions available.
///
///
/// true if using dynamic code generation; otherwise, false.
///
public bool DynamicCodeGeneration => JsonTypeReflector.DynamicCodeGeneration;
#if !PORTABLE
///
/// Gets or sets the default members search flags.
///
/// The default members search flags.
[Obsolete("DefaultMembersSearchFlags is obsolete. To modify the members serialized inherit from DefaultContractResolver and override the GetSerializableMembers method instead.")]
public BindingFlags DefaultMembersSearchFlags { get; set; }
#else
private readonly BindingFlags DefaultMembersSearchFlags;
#endif
///
/// Gets or sets a value indicating whether compiler generated members should be serialized.
///
///
/// true if serialized compiler generated members; otherwise, false.
///
public bool SerializeCompilerGeneratedMembers { get; set; }
#if HAVE_BINARY_SERIALIZATION
///
/// Gets or sets a value indicating whether to ignore the interface when serializing and deserializing types.
///
///
/// true if the interface will be ignored when serializing and deserializing types; otherwise, false.
///
public bool IgnoreSerializableInterface { get; set; }
///
/// Gets or sets a value indicating whether to ignore the attribute when serializing and deserializing types.
///
///
/// true if the attribute will be ignored when serializing and deserializing types; otherwise, false.
///
public bool IgnoreSerializableAttribute { get; set; }
#endif
///
/// Gets or sets a value indicating whether to ignore IsSpecified members when serializing and deserializing types.
///
///
/// true if the IsSpecified members will be ignored when serializing and deserializing types; otherwise, false.
///
public bool IgnoreIsSpecifiedMembers { get; set; }
///
/// Gets or sets a value indicating whether to ignore ShouldSerialize members when serializing and deserializing types.
///
///
/// true if the ShouldSerialize members will be ignored when serializing and deserializing types; otherwise, false.
///
public bool IgnoreShouldSerializeMembers { get; set; }
///
/// Gets or sets the naming strategy used to resolve how property names and dictionary keys are serialized.
///
/// The naming strategy used to resolve how property names and dictionary keys are serialized.
public NamingStrategy NamingStrategy { get; set; }
///
/// Initializes a new instance of the class.
///
public DefaultContractResolver()
{
#if HAVE_BINARY_SERIALIZATION
IgnoreSerializableAttribute = true;
#endif
#pragma warning disable 618
DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public;
#pragma warning restore 618
_contractCache = new ThreadSafeStore(CreateContract);
}
///
/// Resolves the contract for a given type.
///
/// The type to resolve a contract for.
/// The contract for a given type.
public virtual JsonContract ResolveContract(Type type)
{
ValidationUtils.ArgumentNotNull(type, nameof(type));
return _contractCache.Get(type);
}
private static bool FilterMembers(MemberInfo member)
{
if (member is PropertyInfo property)
{
if (ReflectionUtils.IsIndexedProperty(property))
{
return false;
}
return !ReflectionUtils.IsByRefLikeType(property.PropertyType);
}
else if (member is FieldInfo field)
{
return !ReflectionUtils.IsByRefLikeType(field.FieldType);
}
return true;
}
///
/// Gets the serializable members for the type.
///
/// The type to get serializable members for.
/// The serializable members for the type.
protected virtual List GetSerializableMembers(Type objectType)
{
bool ignoreSerializableAttribute;
#if HAVE_BINARY_SERIALIZATION
ignoreSerializableAttribute = IgnoreSerializableAttribute;
#else
ignoreSerializableAttribute = true;
#endif
MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
IEnumerable allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.Where(FilterMembers);
List serializableMembers = new List();
if (memberSerialization != MemberSerialization.Fields)
{
#if HAVE_DATA_CONTRACTS
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);
#endif
#pragma warning disable 618
List defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
.Where(FilterMembers).ToList();
#pragma warning restore 618
foreach (MemberInfo member in allMembers)
{
// exclude members that are compiler generated if set
if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof(CompilerGeneratedAttribute), true))
{
if (defaultMembers.Contains(member))
{
// add all members that are found by default member search
serializableMembers.Add(member);
}
else
{
// add members that are explicitly marked with JsonProperty/DataMember attribute
// or are a field if serializing just fields
if (JsonTypeReflector.GetAttribute(member) != null)
{
serializableMembers.Add(member);
}
else if (JsonTypeReflector.GetAttribute(member) != null)
{
serializableMembers.Add(member);
}
#if HAVE_DATA_CONTRACTS
else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute(member) != null)
{
serializableMembers.Add(member);
}
#endif
else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
{
serializableMembers.Add(member);
}
}
}
}
#if HAVE_DATA_CONTRACTS
// don't include EntityKey on entities objects... this is a bit hacky
if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", false, out _))
{
serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList();
}
#endif
// don't include TargetSite on non-serializable exceptions
// MemberBase is problematic to serialize. Large, self referencing instances, etc
if (typeof(Exception).IsAssignableFrom(objectType))
{
serializableMembers = serializableMembers.Where(m => !string.Equals(m.Name, "TargetSite", StringComparison.Ordinal)).ToList();
}
}
else
{
// serialize all fields
foreach (MemberInfo member in allMembers)
{
if (member is FieldInfo field && !field.IsStatic)
{
serializableMembers.Add(member);
}
}
}
return serializableMembers;
}
#if HAVE_DATA_CONTRACTS
private bool ShouldSerializeEntityMember(MemberInfo memberInfo)
{
if (memberInfo is PropertyInfo propertyInfo)
{
if (propertyInfo.PropertyType.IsGenericType() && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName == "System.Data.Objects.DataClasses.EntityReference`1")
{
return false;
}
}
return true;
}
#endif
///
/// Creates a for the given type.
///
/// Type of the object.
/// A for the given type.
protected virtual JsonObjectContract CreateObjectContract(Type objectType)
{
JsonObjectContract contract = new JsonObjectContract(objectType);
InitializeContract(contract);
bool ignoreSerializableAttribute;
#if HAVE_BINARY_SERIALIZATION
ignoreSerializableAttribute = IgnoreSerializableAttribute;
#else
ignoreSerializableAttribute = true;
#endif
contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(contract.NonNullableUnderlyingType, ignoreSerializableAttribute);
contract.Properties.AddRange(CreateProperties(contract.NonNullableUnderlyingType, contract.MemberSerialization));
Func extensionDataNameResolver = null;
JsonObjectAttribute attribute = JsonTypeReflector.GetCachedAttribute(contract.NonNullableUnderlyingType);
if (attribute != null)
{
contract.ItemRequired = attribute._itemRequired;
contract.ItemNullValueHandling = attribute._itemNullValueHandling;
contract.MissingMemberHandling = attribute._missingMemberHandling;
if (attribute.NamingStrategyType != null)
{
NamingStrategy namingStrategy = JsonTypeReflector.GetContainerNamingStrategy(attribute);
extensionDataNameResolver = s => namingStrategy.GetDictionaryKey(s);
}
}
if (extensionDataNameResolver == null)
{
extensionDataNameResolver = ResolveExtensionDataName;
}
contract.ExtensionDataNameResolver = extensionDataNameResolver;
if (contract.IsInstantiable)
{
ConstructorInfo overrideConstructor = GetAttributeConstructor(contract.NonNullableUnderlyingType);
// check if a JsonConstructorAttribute has been defined and use that
if (overrideConstructor != null)
{
contract.OverrideCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(overrideConstructor);
contract.CreatorParameters.AddRange(CreateConstructorParameters(overrideConstructor, contract.Properties));
}
else if (contract.MemberSerialization == MemberSerialization.Fields)
{
#if HAVE_BINARY_FORMATTER
// mimic DataContractSerializer behaviour when populating fields by overriding default creator to create an uninitialized object
// note that this is only possible when the application is fully trusted so fall back to using the default constructor (if available) in partial trust
if (JsonTypeReflector.FullyTrusted)
{
contract.DefaultCreator = contract.GetUninitializedObject;
}
#endif
}
else if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
{
ConstructorInfo constructor = GetParameterizedConstructor(contract.NonNullableUnderlyingType);
if (constructor != null)
{
contract.ParameterizedCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(constructor);
contract.CreatorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
}
}
else if (contract.NonNullableUnderlyingType.IsValueType())
{
// value types always have default constructor
// check whether there is a constructor that matches with non-writable properties on value type
ConstructorInfo constructor = GetImmutableConstructor(contract.NonNullableUnderlyingType, contract.Properties);
if (constructor != null)
{
contract.OverrideCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(constructor);
contract.CreatorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
}
}
}
MemberInfo extensionDataMember = GetExtensionDataMemberForType(contract.NonNullableUnderlyingType);
if (extensionDataMember != null)
{
SetExtensionDataDelegates(contract, extensionDataMember);
}
// serializing DirectoryInfo without ISerializable will stackoverflow
// https://github.com/JamesNK/Newtonsoft.Json/issues/1541
if (Array.IndexOf(BlacklistedTypeNames, objectType.FullName) != -1)
{
contract.OnSerializingCallbacks.Add(ThrowUnableToSerializeError);
}
return contract;
}
private static void ThrowUnableToSerializeError(object o, StreamingContext context)
{
throw new JsonSerializationException("Unable to serialize instance of '{0}'.".FormatWith(CultureInfo.InvariantCulture, o.GetType()));
}
private MemberInfo GetExtensionDataMemberForType(Type type)
{
IEnumerable members = GetClassHierarchyForType(type).SelectMany(baseType =>
{
IList m = new List();
m.AddRange(baseType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
m.AddRange(baseType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
return m;
});
MemberInfo extensionDataMember = members.LastOrDefault(m =>
{
MemberTypes memberType = m.MemberType();
if (memberType != MemberTypes.Property && memberType != MemberTypes.Field)
{
return false;
}
// last instance of attribute wins on type if there are multiple
if (!m.IsDefined(typeof(JsonExtensionDataAttribute), false))
{
return false;
}
if (!ReflectionUtils.CanReadMemberValue(m, true))
{
throw new JsonException("Invalid extension data attribute on '{0}'. Member '{1}' must have a getter.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(m.DeclaringType), m.Name));
}
Type t = ReflectionUtils.GetMemberUnderlyingType(m);
if (ReflectionUtils.ImplementsGenericDefinition(t, typeof(IDictionary<,>), out Type dictionaryType))
{
Type keyType = dictionaryType.GetGenericArguments()[0];
Type valueType = dictionaryType.GetGenericArguments()[1];
if (keyType.IsAssignableFrom(typeof(string)) && valueType.IsAssignableFrom(typeof(JToken)))
{
return true;
}
}
throw new JsonException("Invalid extension data attribute on '{0}'. Member '{1}' type must implement IDictionary.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(m.DeclaringType), m.Name));
});
return extensionDataMember;
}
private static void SetExtensionDataDelegates(JsonObjectContract contract, MemberInfo member)
{
JsonExtensionDataAttribute extensionDataAttribute = ReflectionUtils.GetAttribute(member);
if (extensionDataAttribute == null)
{
return;
}
Type t = ReflectionUtils.GetMemberUnderlyingType(member);
ReflectionUtils.ImplementsGenericDefinition(t, typeof(IDictionary<,>), out Type dictionaryType);
Type keyType = dictionaryType.GetGenericArguments()[0];
Type valueType = dictionaryType.GetGenericArguments()[1];
Type createdType;
// change type to a class if it is the base interface so it can be instantiated if needed
if (ReflectionUtils.IsGenericDefinition(t, typeof(IDictionary<,>)))
{
createdType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType);
}
else
{
createdType = t;
}
Func