SimonM
SimonM

Reputation: 174

JsonConvert DeserializeObject can't find int members

I have an odd situation, my other classes are deserializing properly but this one class with three int values doesn't deserialize properly. Using the trace writer setting I see that Json.Net reports that it can't find the members, but I'm not sure why.

Class:

public class BroadcastMemoryResponse : BaseResponse
{
    public int FreeMemory { get; set; }

    [JsonProperty("Malloc_Count")]
    public int MallocCount { get; set; }

    [JsonProperty("Free_Count")]
    public int FreeCount { get; set; }
}

JSON:

{
  "ID": 100,
  "Service": "BroadcastMemory",
  "FreeMemory: ": 50508,
  "Malloc_Count: ": 10050409,
  "Free_Count: ": 10049533,
  "Status": "Success"
}

Note that the ID, Service and Status fields are in the BaseResponse class and these successfully deserialized (ID is an int and that deserializes properly). I use JsonProperties to remove the underscore in the JSON when it's mapped to C#. The class and properties are all public and the getters/setters are public so not sure what the problem is..?

Test deserialization code:

ITraceWriter traceWriter = new MemoryTraceWriter();
var settings = new JsonSerializerSettings { TraceWriter = traceWriter };
string str = "{\"ID\":100,\"Service\":\"BroadcastMemory\",\"FreeMemory: \":50508,\"Malloc_Count: \":10050409,\"Free_Count: \":10049533,\"Status\":\"Success\"}";
var deserializedObj = JsonConvert.DeserializeObject<BroadcastMemoryResponse>(str, settings);

Trace writer output:

2017-01-30T09:49:46.807 Info Started deserializing TcpClient.Core.Model.BroadcastMemoryResponse. Path 'ID', line 1, position 6.
2017-01-30T09:49:46.836 Verbose Could not find member 'FreeMemory: ' on TcpClient.Core.Model.BroadcastMemoryResponse. Path '['FreeMemory: ']', line 1, position 51.
2017-01-30T09:49:46.838 Verbose Could not find member 'Malloc_Count: ' on TcpClient.Core.Model.BroadcastMemoryResponse. Path '['Malloc_Count: ']', line 1, position 74.
2017-01-30T09:49:46.838 Verbose Could not find member 'Free_Count: ' on TcpClient.Core.Model.BroadcastMemoryResponse. Path '['Free_Count: ']', line 1, position 98.
2017-01-30T09:49:46.840 Info Finished deserializing TcpClient.Core.Model.BroadcastMemoryResponse. Path '', line 1, position 126.
2017-01-30T09:49:46.841 Verbose Deserialized JSON: 
{
  "ID": 100,
  "Service": "BroadcastMemory",
  "FreeMemory: ": 50508,
  "Malloc_Count: ": 10050409,
  "Free_Count: ": 10049533,
  "Status": "Success"
}

Upvotes: 2

Views: 1729

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149088

The problem is that in your Json, you have malformed property names, (e.g. "FreeMemory: " instead of "FreeMemory"). This line:

"{ … \"FreeMemory: \":50508,\"Malloc_Count: \":10050409,\"Free_Count: \":10049533, … }"

Should be:

"{ … \"FreeMemory\":50508,\"Malloc_Count\":10050409,\"Free_Count\":10049533, … }"

Alternatively, if you don't actually have control over the messages, you can simply change the JsonProperty attributes:

[JsonProperty("FreeMemory: ")]
public int FreeMemory { get; set; }

[JsonProperty("Malloc_Count: ")]
public int MallocCount { get; set; }

[JsonProperty("Free_Count: ")]
public int FreeCount { get; set; }

Upvotes: 4

Related Questions