Fix serializing types with struct ref properties (#1924)
This commit is contained in:
@@ -198,6 +198,25 @@ namespace Newtonsoft.Json.Serialization
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the serializable members for the type.
|
||||
/// </summary>
|
||||
@@ -215,7 +234,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
|
||||
|
||||
IEnumerable<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
|
||||
.Where(m => !ReflectionUtils.IsIndexedProperty(m));
|
||||
.Where(FilterMembers);
|
||||
|
||||
List<MemberInfo> serializableMembers = new List<MemberInfo>();
|
||||
|
||||
@@ -227,7 +246,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
#pragma warning disable 618
|
||||
List<MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
|
||||
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
|
||||
.Where(FilterMembers).ToList();
|
||||
#pragma warning restore 618
|
||||
|
||||
foreach (MemberInfo member in allMembers)
|
||||
|
||||
@@ -457,25 +457,24 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the member is an indexed property.
|
||||
/// </summary>
|
||||
/// <param name="member">The member.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the member is an indexed property; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool IsIndexedProperty(MemberInfo member)
|
||||
public static bool IsByRefLikeType(Type type)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(member, nameof(member));
|
||||
|
||||
if (member is PropertyInfo propertyInfo)
|
||||
{
|
||||
return IsIndexedProperty(propertyInfo);
|
||||
}
|
||||
else
|
||||
if (!type.IsValueType())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// IsByRefLike flag on type is not available in netstandard2.0
|
||||
Attribute[] attributes = GetAttributes(type, null, false);
|
||||
for (int i = 0; i < attributes.Length; i++)
|
||||
{
|
||||
if (string.Equals(attributes[i].GetType().FullName, "System.Runtime.CompilerServices.IsByRefLikeAttribute", StringComparison.Ordinal))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user