0

I'm having issues deserializating a property w/ JSON.net's DeserializeObject. The JSON is properly formatted and can be extracted using DeserializeObject and accessing the dynamic property, but when I deserialize from my model, the property is always null.

I'm using v10 of JSON.net.

The property in question is defined as such:

    [JsonProperty(PropertyName = "email")]
    public List<string> EmailAddresses { get; set; }  

The Json is below:

  "representative" : {
    "id" : 16,
    "username" : "jb",
    "firstName" : "James",
    "lastName" : "Bond",
    "emailAddress" : "demo@backstopsolutions.com"
  },
  "email" : [ "test213@example.com", "test312@backstopsolutions.com" ],
  "permissionTag" : "Public",
  "website" : null,

When I deserialize the object w/ it's type, i.e.

var restResponse = await client.ExecuteTaskAsync<T>(request, cancelToken);

the property is null.

I can do the below, but I'd much prefer to figure out why the deserialization is failing.

dynamic jsonResponse = JsonConvert.DeserializeObject(restResponse.Content);
var tagsJArray = jsonResponse.email as JArray;
if (tagsJArray != null)
{
    restResponse.Data.EmailAddresses = tagsJArray.ToObject<List<string>>();
}

I assume I'm missing a JsonProperty attribute, but nothing I've tried has worked. Can anyone help shed some light on this?

Thanks in advance.

Rey
  • 3,663
  • 3
  • 32
  • 55
  • this may help you: https://stackoverflow.com/a/30147559/1498401 and live example: https://dotnetfiddle.net/ayZBlL – tinamou Jun 27 '17 at 21:50
  • 2
    Can you extend your question to a full [mcve] showing your root type and how you construct `client`? I tried to reproduce your problem [here](https://dotnetfiddle.net/xJ2u7F) and everything seems to work fine. – dbc Jun 27 '17 at 21:52
  • Are you getting null just on the email property or all of the other properties too? – Arman Peiravi Jun 27 '17 at 21:59
  • I'm working on the minimal complete and verifiable example per dbc(thanks, btw). Arman - all other properties are deserializing without issue. – thenorthsider23 Jun 27 '17 at 22:05
  • As seen in dbc's example, it works fine. Perhaps it's your specific version of Newtonsoft.JSON? What happens if you use string[] instead of List? – Arman Peiravi Jun 27 '17 at 22:08
  • 2
    Oy, is your `client` actually [`RestSharp.RestClient`](https://github.com/restsharp/RestSharp/blob/master/RestSharp/RestClient.Async.cs#L258)? Because RestSharp [doesn't use Json.NET by default](https://github.com/restsharp/RestSharp/blob/master/readme.txt) so `[JsonProperty(...)]` will do nothing. – dbc Jun 27 '17 at 22:16
  • dbc got it. We were using the wrong set of attributes and because most of our rest properties match the model names, we didn't notice at first. Thank you for insight despite me failing to provide a good question. I'll add an answer and close the question in case this helps someone else in the future. – thenorthsider23 Jun 28 '17 at 14:34

2 Answers2

0

If the name of the class that contains the EmailAddresses property is Example, then you should deserialize your JSON as below:

var restResponse = await client.ExecuteTaskAsync<Example>(request, cancelToken);
Christos
  • 53,228
  • 8
  • 76
  • 108
0

As usual, this was a problem of misunderstanding. The client I was using had recently switched to RestSharp.RestClient, but we did not update our attributes and they were being ignored. Once we switch to the proper set of attributes, the field is properly deserialized.