in src/Elastic.Apm/Libraries/Newtonsoft.Json/Serialization/DefaultContractResolver.cs [1314:1465]
private void SetPropertySettingsFromAttributes(JsonProperty property, object attributeProvider, string name, Type declaringType,
MemberSerialization memberSerialization, out bool allowNonPublicAccess
)
{
#if HAVE_DATA_CONTRACTS
DataContractAttribute? dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
MemberInfo? memberInfo = attributeProvider as MemberInfo;
DataMemberAttribute? dataMemberAttribute;
if (dataContractAttribute != null && memberInfo != null)
{
dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)memberInfo);
}
else
{
dataMemberAttribute = null;
}
#endif
var propertyAttribute = JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(attributeProvider);
var requiredAttribute = JsonTypeReflector.GetAttribute<JsonRequiredAttribute>(attributeProvider);
string mappedName;
bool hasSpecifiedName;
if (propertyAttribute?.PropertyName != null)
{
mappedName = propertyAttribute.PropertyName;
hasSpecifiedName = true;
}
#if HAVE_DATA_CONTRACTS
else if (dataMemberAttribute?.Name != null)
{
mappedName = dataMemberAttribute.Name;
hasSpecifiedName = true;
}
#endif
else
{
mappedName = name;
hasSpecifiedName = false;
}
var containerAttribute = JsonTypeReflector.GetAttribute<JsonContainerAttribute>(declaringType);
NamingStrategy? namingStrategy;
if (propertyAttribute?.NamingStrategyType != null)
namingStrategy =
JsonTypeReflector.CreateNamingStrategyInstance(propertyAttribute.NamingStrategyType, propertyAttribute.NamingStrategyParameters);
else if (containerAttribute?.NamingStrategyType != null)
namingStrategy = JsonTypeReflector.GetContainerNamingStrategy(containerAttribute);
else
namingStrategy = NamingStrategy;
if (namingStrategy != null)
property.PropertyName = namingStrategy.GetPropertyName(mappedName, hasSpecifiedName);
else
property.PropertyName = ResolvePropertyName(mappedName);
property.UnderlyingName = name;
var hasMemberAttribute = false;
if (propertyAttribute != null)
{
property._required = propertyAttribute._required;
property.Order = propertyAttribute._order;
property.DefaultValueHandling = propertyAttribute._defaultValueHandling;
hasMemberAttribute = true;
property.NullValueHandling = propertyAttribute._nullValueHandling;
property.ReferenceLoopHandling = propertyAttribute._referenceLoopHandling;
property.ObjectCreationHandling = propertyAttribute._objectCreationHandling;
property.TypeNameHandling = propertyAttribute._typeNameHandling;
property.IsReference = propertyAttribute._isReference;
property.ItemIsReference = propertyAttribute._itemIsReference;
property.ItemConverter = propertyAttribute.ItemConverterType != null
? JsonTypeReflector.CreateJsonConverterInstance(propertyAttribute.ItemConverterType, propertyAttribute.ItemConverterParameters)
: null;
property.ItemReferenceLoopHandling = propertyAttribute._itemReferenceLoopHandling;
property.ItemTypeNameHandling = propertyAttribute._itemTypeNameHandling;
}
else
{
property.NullValueHandling = null;
property.ReferenceLoopHandling = null;
property.ObjectCreationHandling = null;
property.TypeNameHandling = null;
property.IsReference = null;
property.ItemIsReference = null;
property.ItemConverter = null;
property.ItemReferenceLoopHandling = null;
property.ItemTypeNameHandling = null;
#if HAVE_DATA_CONTRACTS
if (dataMemberAttribute != null)
{
property._required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
property.Order = (dataMemberAttribute.Order != -1) ? (int?)dataMemberAttribute.Order : null;
property.DefaultValueHandling =
(!dataMemberAttribute.EmitDefaultValue) ? (DefaultValueHandling?)DefaultValueHandling.Ignore : null;
hasMemberAttribute = true;
}
#endif
}
if (requiredAttribute != null)
{
property._required = Required.Always;
hasMemberAttribute = true;
}
property.HasMemberAttribute = hasMemberAttribute;
var hasJsonIgnoreAttribute =
JsonTypeReflector.GetAttribute<JsonIgnoreAttribute>(attributeProvider) != null
// automatically ignore extension data dictionary property if it is public
|| JsonTypeReflector.GetAttribute<JsonExtensionDataAttribute>(attributeProvider) != null
#if HAVE_NON_SERIALIZED_ATTRIBUTE
|| JsonTypeReflector.IsNonSerializable(attributeProvider)
#endif
;
if (memberSerialization != MemberSerialization.OptIn)
{
var hasIgnoreDataMemberAttribute = false;
#if HAVE_IGNORE_DATA_MEMBER_ATTRIBUTE
hasIgnoreDataMemberAttribute = (JsonTypeReflector.GetAttribute<IgnoreDataMemberAttribute>(attributeProvider) != null);
#endif
// ignored if it has JsonIgnore or NonSerialized or IgnoreDataMember attributes
property.Ignored = hasJsonIgnoreAttribute || hasIgnoreDataMemberAttribute;
}
else
{
// ignored if it has JsonIgnore/NonSerialized or does not have DataMember or JsonProperty attributes
property.Ignored = hasJsonIgnoreAttribute || !hasMemberAttribute;
}
// resolve converter for property
// the class type might have a converter but the property converter takes precedence
property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider);
var defaultValueAttribute = JsonTypeReflector.GetAttribute<DefaultValueAttribute>(attributeProvider);
if (defaultValueAttribute != null) property.DefaultValue = defaultValueAttribute.Value;
allowNonPublicAccess = false;
#pragma warning disable 618
if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) allowNonPublicAccess = true;
#pragma warning restore 618
if (hasMemberAttribute) allowNonPublicAccess = true;
if (memberSerialization == MemberSerialization.Fields) allowNonPublicAccess = true;
}