title: Deserialize Json using C# with variable content
body: <p>I'm reposting this question because someone marked it as duplicate but it's not, so I detailed more to avoid the confusion.</p>
  
  <p>I'm creating an API in c# that my WebApp can reach to get some data. In my web app, i have a view with a list of (let's say) products. For now i have a route that return all my products stored in Database.</p>
  
  <p>I would like now to add the ability for the webapp to make complexe filters. I would like to be able in the webApp or with Postman for exemple, to reach my route (let's say) "/api/product" with a URlParam named "filters" that will contains all filters that the webapp need to apply.</p>
  
  <p>Now back to my WebApp (build with angular) I have a service that can create like a tree of filters (See class architecture below). (And yes this architecture could be improved)</p>
  
  <p><a href="https://i.stack.imgur.com/q44oK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q44oK.png" alt="enter image description here"></a></p>
  
  <p>The idea is that i can build filter like this : If i want to fetch all Product that have "foo" in their names and with a price gretter than 15$ :</p>
  
  <pre><code>let filter = new AndNode();
  filter.Add(new LikeNode("name", "foo"));
  filter.Add(new GTNode("price", 15));
  filter.generateJson();
  </code></pre>
  
  <p>Will result in this Json :</p>
  
  <pre><code>{
      "$and": [
          { 
              "$like": { "key": "name", "value": "foo" }
          },
          {
              "$gt": { "key": "price", "value": 15 }
          }
      ]
  }  
  </code></pre>
  
  <p>Now, this Json have 1 requirement : It should never contains more than one property at is first level. Because this is meant to be used to generate SQL Filters, it make sense that, the json must contains only one node on his first level : an EQ, LIKE, GT, or LT node, or if we have more than one filters an AND / OR nodes that define the logical "conjunction" between each filters of the next level.</p>
  
  <p>So it makes sense that :</p>
  
  <pre><code>{
      "$like": { "key": "name", "value": "foo" }
  }
  </code></pre>
  
  <p>Is valid and could result in this SQL statement : </p>
  
  <pre><code>SELECT * FROM products WHERE name LIKE "%foo%";
  </code></pre>
  
  <p>Also :</p>
  
  <pre><code>{
      "$and": [
          { 
              "$like": { "key": "name", "value": "foo" }
          },
          {
              "$gt": { "key": "price", "value": 15 }
          }
      ]
  }  
  </code></pre>
  
  <p>Is valid and could result as this sql statement : </p>
  
  <pre><code>SELECT * FROM products WHERE name LIKE "%foo%" AND price &gt; 15;
  </code></pre>
  
  <p>But :</p>
  
  <pre><code>{
      "$like": { "key": "name", "value": "foo" },
      "$eq": { "key": "price", "value": 5 },
  }
  </code></pre>
  
  <p>Is not valid because it will result a this sql statement : </p>
  
  <pre><code>SELECT * FROM products WHERE name LIKE "%foo%" price &gt; 15
  </code></pre>
  
  <p>Witch is not valid because of the missing AND or OR keyword.</p>
  
  <p>What I would like to do :
  In a beautiful world, I would like to be able to serialize this json in my web app, convert it to a valid URL param string, and send it to the API (c#) deserialize that JSON to get the same object structure that I had on the WebApp.
  For our example with the price and the name, I would like to have an instance of the AndNode class with a member "child" that contains 2 object: 1 instance of the LikeNode class and 1 instance of the GTNode class.</p>
  
  <p>This implementation is maybe debatable, but this is what we need for now.</p>
  
  <p>For now, I have : </p>
  
  <pre><code>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("$eq")]
      public EQNode eq { get; set; }
  }
  
  public class AndNode
  {
      [JsonProperty("$eq")]
      public EQNode eq { get; set; }
  
      [JsonProperty("$like")]
      public LikeNode like { get; set; }
  
      [JsonProperty("$gt")]
      public GTNode gt { get; set; }
  
      [JsonProperty("$lt")]
      public LTNode lt { get; set; }
  
      [JsonProperty("$or")]
      public List&lt;OrNode&gt; or { get; set; }
  
      [JsonProperty("$and")]
      public List&lt;OrNode&gt; and { get; set; }
  }
  
  public class RootObject
  {
      [JsonProperty("$and")]
      public List&lt;AndNode&gt; and { get; set; }
  
      [JsonProperty("$or")]
      public List&lt;OrNode&gt; or { get; set; }
  
      [JsonProperty("$eq")]
      public EQNode eq { get; set; }
  
      [JsonProperty("$like")]
      public LikeNode like { get; set; }
  
      [JsonProperty("$gt")]
      public GTNode gt { get; set; }
  
      [JsonProperty("$lt")]
      public LTNode lt { get; set; }
  }
  </code></pre>
  
  <p>This has been generated by the "past special" feature of visual studio (and I added all the JsonProperty decorator to match the names in my json).
  It works, but, this is not really what I expected.</p>
  
  <p>First, i would like to have the closest structure from my WebApp structure (with inheritance). 
  Second, i don't like the way this generated code handle the fact that i can have either $eq, $gt, $lt, $like, $or or $and node in my root object, i can't believe there is no way to have a clean structure like that :
  (note: this is for example purpose some decorator used doesn't exist or are badly used, this is just for demo)</p>
  
  <pre><code>public class RootObject {
      public FilterNode root;
  } 
  
  public class FilterNode {
  }
  
  public class ConjunctionNode: FilterNode {
      public FilterNode[] childs
  }
  
  [JsonObject("$and")]
  public class AndNode: ConjunctionNode {
  }
  
  [JsonObject("$or")]
  public class OrNode: ConjunctionNode {
  }
  
  [JsonObject("$like")]
  public class LikeNode: FilterNode {
      public string key;
      public string value;
  }
  
  [JsonObject("$eq")]
  public class EQNode: FilterNode {
      public string key;
      public string value;
  }
  
  [JsonObject("$gt")]
  public class GTNode: FilterNode {
      public string key;
      public string value;
  }
  
  [JsonObject("$lt")]
  public class LTNode: FilterNode {
      public string key;
      public string value;
  }
  </code></pre>
  
  <p>So, if we use the sample json : </p>
  
  <pre><code>{
      "$and": [
          { 
              "$like": { "key": "name", "value": "foo" }
          },
          {
              "$gt": { "key": "price", "value": 15 }
          }
      ]
  }  
  </code></pre>
  
  <p>I could use :</p>
  
  <pre><code>RootObject obj = JsonConvert.DeserializeObject&lt;RootObject&gt;(filters);
  </code></pre>
  
  <p>With the "root" member of the RootObject as an instance of the AndNode.
  This AndNode would contain two objects in this child's array, which are an instance of the LikeNode and an instance of the GTNode.</p>
  
  <p>Can this be done?</p>