且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-17 19:11:35

更新

已修复在变更集中

Fixed in change set 70120ce, which will be included in Json.NET 8.0.2.

原始答案

已确认-这似乎是回归.考虑以下简单的测试类:

Confirmed - this appears to be a regression. Consider the following simple test class:

internal class HasByteArray
{
    public byte[] EncryptedPassword { get; set; }
}

现在,如果我尝试使用TypeNameHandling.Objects往返课程:

Now if I try to round-trip the class with 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");
    }

我收到以下异常:

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()

在Json 7.0中不会发生该异常.您应该报告问题.

The exception does not occur in Json 7.0. You should report an issue.

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

In the meantime, you can use the following converter to work around the problem:

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();
    }
}

具有设置

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