I am using an API that frustratingly requires you to omit optional parameters rather than pass them as null
.
Also, data is slightly nested in the format:
{ data: { prop1: 5, prop2: 6, prop3: 7 } }
Previously I was converting it using a replace:
"{ data: { prop1: {prop1}, prop2: {prop2}, prop3: {prop3} } }"
.Replace("{prop1}", prop1)....
But it turns out that if a value is not provided the API I am sending this to will only accept it if it's not included in the JSON.
Rather than mess around with complex string concatenation (as I am not using model binding), I thought about simply creating a dictionary and serialising it to JSON.
If I do this:
Dictionary<string, int> props = new Dictionary<string, int>
{
{ "prop1", 6 },
{ "prop3", 7 },
};
string json = JsonConvert.SerializeObject(props, Formatting.Indented);
I can serialise whichever props I need nicely. Unfortunately you can see the way I need to send the data is contained within the data
property of the json. I would need to put a dictionary inside my dictionary, but the dictionary is defined as string, int so that isn't possible. If I changed the type, then I'd not be able to put my props in.
To solve this I can see 2 possible clean ways:
- Dynamically compose a JSON object somehow sorta like XML
eg. new JsonObject().AddNode("data").AddProperty("Prop1", 3).AddProperty("Prop3", 5)....
etc.
- Serialise from a dictionary object in a way that will let me include the nested property. Or, find a way to assign the json from a non-nested dictionary into the data property of a new json object.
I've not found a way to do this cleanly yet - alternatively another suggestion on solving this issue would be appreciated.