无法使用Json.NET 8.0.1反序列化具有字节数组属性的对象

Lar*_*ael 5 c# serialization json.net

在升级代码库以使用Json.NET 8.0.1之后,一些反序列化会失败.使用Json.NET 7.0.1一切正常.显然,它是byte[]导致问题的类型属性的反序列化.如果我删除该byte[]属性它工作正常.我可以使用这个简单的控制台应用程序重现行为:

internal class Program
{
    private static void Main(string[] args)
    {
        Dictionary<string, Account> accounts;
        var jsonSerializerSettings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Objects,
            TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
        };

        using (var streamReader = new StreamReader("accounts.json"))
        {
            var json = streamReader.ReadToEnd();
            accounts = JsonConvert.DeserializeObject<Dictionary<string, Account>>(json, jsonSerializerSettings);
        }

        foreach (var account in accounts)
        {
            Debug.WriteLine(account.Value.Name);
        }
    }
}

internal class Account
{
    public string Id { get; set; }

    public string Name { get; set; }

    public byte[] EncryptedPassword { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

accounts.json文件如下所示:

{
    "$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[ConsoleApplication1.Account, ConsoleApplication1]], mscorlib",
    "lars.michael": {
        "$type": "ConsoleApplication1.Account, ConsoleApplication1",
        "EncryptedPassword": {
            "$type": "System.Byte[], mscorlib",
            "$value": "cGFzc3dvcmQ="
        },
        "Name": "Lars Michael",
        "Id": "lars.michael"
    },
    "john.doe": {
        "$type": "ConsoleApplication1.Account, ConsoleApplication1",
        "EncryptedPassword": {
            "$type": "System.Byte[], mscorlib",
            "$value": "cGFzc3dvcmQ="
        },
        "Name": "John Doe",
        "Id": "john.doe"
    }
}
Run Code Online (Sandbox Code Playgroud)

这可能是Json.NET 8.0.1中的一个错误,还是可以通过调整来解决这个问题JsonSerializerSettings

如果有人试图重现这一点,请确保将accounts.json文件中的程序集名称与控制台应用程序的程序集名称同步(在本例中ConsoleApplication1).

dbc*_*dbc 2

更新

已在更改集70120ce中修复,该更改集将包含在 Json.NET 8.0.2 中。

原答案

已确认——这似乎是一种回归。考虑以下简单的测试类:

internal class HasByteArray
{
    public byte[] EncryptedPassword { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试使用以下命令来回班级TypeNameHandling.Objects

    private static void TestSimple()
    {
        var test = new HasByteArray { EncryptedPassword = Convert.FromBase64String("cGFzc3dvcmQ=") };
        try
        {
            TestRoundTrip(test);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }

    private static void TestRoundTrip<T>(T item)
    {
        var jsonSerializerSettings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Objects,
            TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
        };

        TestRoundTrip<T>(item, jsonSerializerSettings);
    }

    private static void TestRoundTrip<T>(T item, JsonSerializerSettings jsonSerializerSettings)
    {
        var json = JsonConvert.SerializeObject(item, Formatting.Indented, jsonSerializerSettings);
        Debug.WriteLine(json);

        var item2 = JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);

        var json2 = JsonConvert.SerializeObject(item2, Formatting.Indented, jsonSerializerSettings);

        Debug.WriteLine(json2);

        if (!JToken.DeepEquals(JToken.Parse(json), JToken.Parse(json2)))
            throw new InvalidOperationException("Round Trip Failed");
    }
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

Newtonsoft.Json.JsonSerializationException: Additional text found in JSON string after finishing deserializing object.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 196
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonSerializer.cs:line 823
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonSerializer.cs:line 802
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonConvert.cs:line 863
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonConvert.cs:line 820
   at Question34654184.TestClass.TestRoundTrip[T](T item, JsonSerializerSettings jsonSerializerSettings)
   at Question34654184.TestClass.TestRoundTrip[T](T item)
   at Question34654184.TestClass.TestSimple()
Run Code Online (Sandbox Code Playgroud)

Json 7.0 中不会出现该异常。您应该报告问题

同时,您可以使用以下转换器来解决该问题:

public class ByteArrayConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(byte[]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var token = JToken.Load(reader);
        if (token == null)
            return null;
        switch (token.Type)
        {
            case JTokenType.Null:
                return null;
            case JTokenType.String:
                return Convert.FromBase64String((string)token);
            case JTokenType.Object:
                {
                    var value = (string)token["$value"];
                    return value == null ? null : Convert.FromBase64String(value);
                }
            default:
                throw new JsonSerializationException("Unknown byte array format");
        }
    }

    public override bool CanWrite { get { return false; } } // Use the default implementation for serialization, which is not broken.

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

有设置

            var jsonSerializerSettings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Objects,
                TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
                Converters = new [] { new ByteArrayConverter() },
            };
Run Code Online (Sandbox Code Playgroud)