1

I have the following JSON I would like to deserialize to a C# model.

{
  "$type": "TSNT",
  "name": "name",
  "ticket": {
    "$type": "notifyType",
    "$values": [
      {
        "$type": "test",
        "customerName": "atoms"
      },
      {
        "$type": "test2",
        "customerName": "atoms2"
      }
    ]
  },
  "foo": "bar"
}

On Calling the following I get the error: 'Cannot deserialize the current JSON array because the type requires a JSON object to deserialize correctly.'.

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(ss);

public class Value
{
    [JsonProperty("$type")]
    public string Type { get; set; }
    public string customerName { get; set; }
}

public class Ticket
{
    [JsonProperty("$type")]
    public string Type { get; set; }

    [JsonProperty("$values")]
    public List<Value> Values { get; set; }
}

public class Root
{
    [JsonProperty("$type")]
    public string Type { get; set; }
    public string name { get; set; }
    public Ticket ticket { get; set; }
    public string foo { get; set; }
}

I've tried to decorate the Ticket with [JsonArray] which then throws the error: 'Cannot create and populate list type Ticket. Path 'ticket.$values''

Please could someone explain to me where I'm going wrong?

atoms
  • 2,993
  • 2
  • 22
  • 43
  • Is that your **full** JSON? I suspect an outer array object as root. – Charlieface Apr 01 '21 at 10:58
  • yeah it is. Tried to create a minimum working example. If you run that it will reproduce the error mentioned. Have found others with issues as array object @ root but I dont think applied to my case – atoms Apr 01 '21 at 10:58
  • 1
    haven't really looked into your code, but I do know json.net uses $type to say what .net type it should be deserialized to, you may have to set some options to turn that off – Keith Nicholas Apr 01 '21 at 11:01
  • @KeithNicholas thanks ill look into that – atoms Apr 01 '21 at 11:02
  • check https://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm – Keith Nicholas Apr 01 '21 at 11:02
  • or maybe https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_TypeNameHandling.htm – Keith Nicholas Apr 01 '21 at 11:03
  • Thanks @KeithNicholas unfortuantly setting the property hasnt made a difference. Will keep looking – atoms Apr 01 '21 at 11:05
  • Looks like `$id`, `$type` and `$value` have special handling, it tells JsonConvert what the actual types are and which reference is which, to be able to rebuild the full object graph with cross-references. It's expecting the root object to have `List`, and the class `Value` should not exist at all. – Charlieface Apr 01 '21 at 11:15
  • Thanks @Charlieface. It still fails when I specify `new JsonSerializerSettings(){TypeNameHandling = TypeNameHandling.None})]` If I remove the class Value, it wont have anywhere to store the 'customerName' property? – atoms Apr 01 '21 at 11:17
  • 2
    Does this answer your question? [deserialize string that was serialized with TypeNameHandling.All](https://stackoverflow.com/questions/47802385/deserialize-string-that-was-serialized-with-typenamehandling-all) See also this fiddle https://dotnetfiddle.net/Bx3rr6 – Charlieface Apr 01 '21 at 11:19
  • @Charlieface if I change teh type to List it no longer throws the error. The List values property in the Ticket class contains two entries, but all values are empty. Any idea? Thanks will check out that answer – atoms Apr 01 '21 at 11:20
  • @Charlieface I undertand now. Thank you for your time! If you wanted to put the fiddle as an answer and the post you linked to would be happy to accept. Appreciate the insight! – atoms Apr 01 '21 at 11:30

0 Answers0