I'm struggling to convert a json to an c# object.
My json is generated by an other app (in typescript) here is the structure of the object used to generate my json :
With this structure i'm able to create json like:
{
EQNode: { key: "userid", value: 5}
}
Or more complexe :
{
AndNode: [
{
EQNode: { key: "userid", value: 5 }
},
{
LikeNode: { key: "username", value: "foo" }
}
]
}
Or even more complexe :
{
AndNode: [
{
EQNode: { key: "userid", value: 5 }
},
{
OrNode: [
{
LikeNode: { key: "username", value: "foo" }
},
{
LikeNode: { key: "email", value: "foo@bar.fr" }
},
]
}
]
}
I'have managed to parse my json using this model :
public class EQNode
{
[JsonProperty("key")]
public string key { get; set; }
[JsonProperty("value")]
public int value { get; set; }
}
public class LikeNode
{
[JsonProperty("key")]
public string key { get; set; }
[JsonProperty("value")]
public string value { get; set; }
}
public class GTNode
{
[JsonProperty("key")]
public string key { get; set; }
[JsonProperty("value")]
public int value { get; set; }
}
public class LTNode
{
[JsonProperty("key")]
public string key { get; set; }
[JsonProperty("value")]
public int value { get; set; }
}
public class OrNode
{
[JsonProperty("EQNode")]
public EQNode EQ { get; set; }
}
public class AndNode
{
[JsonProperty("EQNode")]
public EQNode EQ { get; set; }
[JsonProperty("LikeNode")]
public LikeNode LIKE { get; set; }
[JsonProperty("GTNode")]
public GTNode GT { get; set; }
[JsonProperty("LTNode")]
public LTNode LT { get; set; }
[JsonProperty("OrNode")]
public List<OrNode> OR { get; set; }
[JsonProperty("AndNode")]
public List<OrNode> And { get; set; }
}
public class RootObject
{
[JsonProperty("AndNode")]
public List<AndNode> And { get; set; }
[JsonProperty("OrNode")]
public List<OrNode> OR { get; set; }
[JsonProperty("EQNode")]
public EQNode EQ { get; set; }
[JsonProperty("LikeNode")]
public LikeNode LIKE { get; set; }
[JsonProperty("GTNode")]
public GTNode GT { get; set; }
[JsonProperty("LTNode")]
public LTNode LT { get; set; }
}
But this seems a little "hard coded" to me. Is there a way to have a nice / better model structure for my model ?