After upgrading a code base to use Json.NET 8.0.1, some deserialization stumbles. Using Json.NET 7.0.1 everything works fine. Apparently it is the deserialization of a property of type byte[] that causes the problem. If I remove the byte[] property it works fine. I can reproduce the behavior using this simple console application:
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; }
}
The accounts.json file looks like this:
{
"$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"
}
}
Is this possibly a bug in Json.NET 8.0.1 or can I maybe solve this by tweaking the JsonSerializerSettings?
If anyone is trying to reproduce this, make sure to synchronize the assembly name in the accounts.json file with the assembly name of the console application (in this case ConsoleApplication1).