JSON.NET Deserialization "Hangs" (Mono)
mono-core-2.6.4-2.13.x86_64 Json.NET 3.5 Release 8
I call the deserializer and it works - I can see the objects created (deserialized) but then the deserializer never exits/returns.
List<ListEntry> listed_entries = JsonConvert.DeserializeObject<List<ListEntry>>(payload);
Console.WriteLine("Deserialization complete");
ListEntry objects are created, then the program 'hangs'. "Deserialization complete" never appears.
The JSON payload is 1,938K of:
[{"entityName": "Contact", "version": 27, "displayName": "", "objectId": 11446184}, {"entityName": "Contact", "version": 33, "displayName": "", "objectId": 10148760}, {"entityName": "Contact", "version": 35, "displayName": "", "objectId": 12695703}, {"entityName": "Contact", "version": 33, "displayName": "", "objectId": 7575210},
... ]
with no trailing newline. No complex; it decodes to an object of:
public class ListEntry
{
public ListEntry ()
{
Console.WriteLine("Created ListEntry");
}
[JsonPropertyAttribute("entityName")]
public string EntityName { get; set; }
[JsonPropertyAttribute("objectId")]
public int 开发者_如何学编程Objectid { get; set; }
[JsonPropertyAttribute("version")]
public int Version { set; get; }
[JsonPropertyAttribute("displayname")]
public string DisplayName { set; get; }
}
I found the problem; in one of the JSON elements there was a "version" attribute of "null". This, of course, cannot deserialze to a .NET "int". The exception in the deserialization thread doesn't seem to propagate to the main thread. Anyway, modifying the serialization object resolved the issue:
[JsonPropertyAttribute("version")]
public int? Version
{
set
{
if (value == null)
this.version = 0;
else
this.version = (int)value;
}
get { return this.version; }
}
The proper way to work with nullable types:
[JsonPropertyAttribute("version")]
public int? Version
{
set
{
this.version = value ?? default(int);
}
get
{
return this.version;
}
}
精彩评论