Class Json property serialization error

Hi I´m using v3.0.5 of the sdk
I have a class with multiple properties, one should be an actual Json
so I set the type as JObject (not sure if this is right option)

  [JsonProperty("jtd")]
  public JObject JtdContent { get; set; }

object stored in DB is perfect , and you can see below that jtd property is a well formed JSON

{
  "type": "jtd",
  "id": "prj::3::doc::98::jtd::2",
  "version": 2,
  "jtd": {
    "hola": "valor"
  },
  "revision": 0,
  "projectId": 3,
  "documentId": 98
}

when I try to retrieve back to code the deserialization fails

 var element = await collection.GetAsync(id);
 return element.ContentAs<T>(); //T is my class

Unexpected character encountered while parsing value: {. Path ‘jtd’, line 1, position 64.
{“type”:“jtd”,“id”:“prj::3::doc::98::jtd::2”,“version”:2,“jtd”:{“hola”:“valor”},“revision”:0,“projectId”:3,“documentId”:98}

you can see that position 64 is right where the json starts.

any help?
what type should me property be?

thanks in advance

I would try using the dynamic keyword as the type for that property. Under the covers that should act as a JObject.

1 Like

thanks for the quick answer, but already tried dynamic, and same result.
I’m looking now to have a custom deserializer for this class, haven’t finished yet

@afgonzal -

Your target class doesn’t match the structure of the JSON document. The easiest way to do this is just use a POCO:

 // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class Jtd    {
         public string hola { get; set; } 
     }

public class Root    {
    public string type { get; set; } 
    public string id { get; set; } 
    public int version { get; set; } 
    public Jtd jtd { get; set; } 
    public int revision { get; set; } 
    public int projectId { get; set; } 
    public int documentId { get; set; } 
}

only wrote the part of the class that I thought was relevant
but your answer helped me realize I had an error in my class

[JsonProperty(“jtd”)]
public dynamic JtdContent { get; set; }
and the error was a
public string jtd;

now is working as expected, thanks for taking the time

2 Likes