1

Im trying to parse my class to Json, but i have some problemas to do it like i want.

{     
      "bool": {
         "must": [
            {
               "Term": {
                  "number": 5
               }
            },
            {
               "Match": {
                  "name": "xxx"
               }
            }
         ]
     }         
}

my class is

    public class BaseLeafQuery
        {
            public BaseFilterType Bool { get; set; }
        }

    public class BaseFilterType 
        {
            [JsonProperty(PropertyName = "must", NullValueHandling = NullValueHandling.Ignore)]
            public List<BaseTypeQuery> Must { get; set; }
        }
    public class BaseTypeQuery {
           [JsonProperty(PropertyName = "term", NullValueHandling = NullValueHandling.Ignore)]
            public Dictionary<string, object> Term { get; set; } 
            [JsonProperty(PropertyName = "match", NullValueHandling = NullValueHandling.Ignore)]
            public Dictionary<string, object> Match { get; set; }
    }

But when i convert the json becomes it

{     
      "bool": {
         "must": [
            {
               "Term": {
                  "number": 5
               },
               "Match": {
                  "name": "xxx"
               }
            }
         ]
     }         
}

Every class inside the "MUST" class must be beetween {}

Example:

BaseTypeQuery baseTypeQuery = new BaseTypeQuery();
            baseTypeQuery.Term = new Dictionary<string, object>() { { "Id", 5 } };
            baseTypeQuery.Match = new Dictionary<string, object>() { { "Email", "xxx" } };

            BaseLeafQuery leafQuery = new BaseLeafQuery();
            leafQuery.Bool = new BaseFilterType();
            leafQuery.Bool.Must = new List<BaseTypeQuery>();
            leafQuery.Bool.Must.Add(baseTypeQuery);
            var a = JsonConvert.SerializeObject(leafQuery);

The result of A is {"bool":{"must":[{"term":{"Id":5},"match":{"Email":"xxx"}}]}} but should bu {"bool":{"must":[{"term":{"Id":5}},{"match":{"Email":"xxx"}}]}}

6
  • 2
    The second JSON you posted isnt even valid JSON. Commented Sep 19, 2017 at 15:24
  • now the second JSON is correct. Commented Sep 19, 2017 at 15:26
  • 1
    What properties are "term" and "match" supposed to match with in your JSON? Commented Sep 19, 2017 at 15:28
  • 1
    I dont' userstand. Do you serialize or deserialize? Commented Sep 19, 2017 at 15:28
  • i have to serialize. And i fix the JSON again Commented Sep 19, 2017 at 15:35

1 Answer 1

2

This seems to have worked for me, can you confirm this is what you wanted?

    void Main()
{
    var a = Newtonsoft.Json.JsonConvert.DeserializeObject( "{     \"bool\": {\"must\": [{\"Term\": {\"number\": 5}},{\"Match\": {\"name\": \"xxx\"}}]}}",typeof(TestClass)).Dump();
    JsonConvert.SerializeObject(a).Dump();
}

public class TestClass
{
    [JsonProperty(PropertyName = "bool", NullValueHandling = NullValueHandling.Ignore)]
    public BaseFilterType Bool { get; set; }
}

public class BaseFilterType
{
    [JsonProperty(PropertyName = "must", NullValueHandling = NullValueHandling.Ignore)]
    public List<BaseTypeQuery> Must { get; set; }
}
public class BaseTypeQuery
{
    [JsonProperty(PropertyName = "term", NullValueHandling = NullValueHandling.Ignore)]
    public Dictionary<string, object> Term { get; set; }
    [JsonProperty(PropertyName = "match", NullValueHandling = NullValueHandling.Ignore)]
    public Dictionary<string, object> Match { get; set; }
}

Please note that I had to @bool the class because you cannot declare a class with a keyword name

The output for the serialize is

{"bool":{"must":[{"term":{"number":5}},{"match":{"name":"xxx"}}]}}

This is the change you've been looking for I hope

BaseTypeQuery baseTypeQuery1 = new BaseTypeQuery();
BaseTypeQuery baseTypeQuery2 = new BaseTypeQuery();
baseTypeQuery1.Term = new Dictionary<string, object>() { { "Id", 5 } };
baseTypeQuery2.Match = new Dictionary<string, object>() { { "Email", "xxx" } };

BaseLeafQuery leafQuery = new BaseLeafQuery();
leafQuery.Bool = new BaseFilterType();
leafQuery.Bool.Must = new List<BaseTypeQuery>();
leafQuery.Bool.Must.Add(baseTypeQuery1);
leafQuery.Bool.Must.Add(baseTypeQuery2);
var a = JsonConvert.SerializeObject(leafQuery, Newtonsoft.Json.Formatting.Indented);
Sign up to request clarification or add additional context in comments.

7 Comments

Dont use the @ sign, just use [JsonProperty(PropertyName = "bool"] on top of the public BaseFilterType Bool { get; set; }. Also, OP should consider not naming properties after reserved keywords...
True, was just staying as close as I can to the initial setting :), made the suggested change, thanks
I don't get it, when I plug your code in (assuming that BaseLeafQuery" is actuall the **bool class from the example), I don't have a definition for baseQuery, without that line I get {"bool":{"must":[{"term":{"Id":5},"match":{"Email":"xxx"}}]}}. So either something is missing or we're not using the same objects as you presented initially.
sorry, the baseQuery i have puted it wrong. And you are right the BaseLeafQuery is the bool from example
no worries but are we using the same classes as at the top?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.