-Fixed serializing FileInfo/DirectoryInfo on .NET Core with JsonConverter

This commit is contained in:
James Newton-King
2018-02-22 16:16:22 +13:00
parent f2b6ea4966
commit 8bb337debb
2 changed files with 98 additions and 7 deletions
@@ -313,13 +313,6 @@ namespace Newtonsoft.Json.Serialization
/// <returns>A <see cref="JsonObjectContract"/> for the given type.</returns>
protected virtual JsonObjectContract CreateObjectContract(Type objectType)
{
// serializing DirectoryInfo without ISerializable will stackoverflow
// https://github.com/JamesNK/Newtonsoft.Json/issues/1541
if (Array.IndexOf(BlacklistedTypeNames, objectType.FullName) != -1)
{
throw new JsonSerializationException("Unable to serialize instance of '{0}'.".FormatWith(CultureInfo.InvariantCulture, objectType));
}
JsonObjectContract contract = new JsonObjectContract(objectType);
InitializeContract(contract);
@@ -404,9 +397,21 @@ namespace Newtonsoft.Json.Serialization
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<MemberInfo> members = GetClassHierarchyForType(type).SelectMany(baseType =>