Reputation: 57
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"}}]}}
Upvotes: 1
Views: 781
Reputation: 759
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);
Upvotes: 2