Sau khi nâng cấp một cơ sở mã để sử dụng Json.NET 8.0.1, một số sterbles deserialization. Sử dụng Json.NET 7.0.1 mọi thứ hoạt động tốt. Rõ ràng nó là deserialization của một tài sản của loại byte[]
gây ra vấn đề. Nếu tôi loại bỏ các tài sản byte[]
nó hoạt động tốt. Tôi có thể mô phỏng hành vi sử dụng giao diện điều khiển này ứng dụng đơn giản:Không thể deserialize một đối tượng với một thuộc tính mảng byte bằng cách sử dụng Json.NET 8.0.1
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; }
}
File accounts.json
trông như thế này:
{
"$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"
}
}
Đây có phải là có thể là một lỗi trong Json.NET 8.0.1 hoặc tôi có thể có thể giải quyết việc này bằng cách tinh chỉnh JsonSerializerSettings
?
Nếu có ai đang cố gắng tái tạo điều này, hãy đảm bảo đồng bộ tên lắp ráp trong tệp accounts.json
với tên lắp ráp của ứng dụng bảng điều khiển (trong trường hợp này là ConsoleApplication1
).
mát, nhờ xác nhận và thực hiện giải pháp. Tôi đã báo cáo sự cố trên GitHub. –