I created a JSONBufferWriter object {“a”:123,“b”:123} for example. I want to pass it to a function and nest it into another JSON object like {“result”:{“a”:123,“b”:123}}.
I can’t pass this as a string because it will escape the double quotes. So I want something like this:
@rickkas7 If I understand your response you suggest I have to create the outer JSON first and then create the inner JSON object. But in my case I don’t think this will work.
I create the inner JSON object as my data. It has 10-12 key/value pairs which can come from a few different functions. Then I need to add a wrapper around the data as a second operation because it is more general.
I want the inner JSON to be an object, not a string. The string has escaped double quotes which adds length and also reduces ease of parsing.
It’s usually better to create the outside object first, because then you only need one buffer, and fewer copies, but if that structure won’t work, I added a new method to insert pre-formatted JSON into an existing JsonWriter object:
/**
* @brief Inserts a new key and an existing valid JSON object or array in a c-string
*
* @param key the key name to insert
*
* @param json The object or array to insert, a c-string. Must already be valid JSON.
*/
void insertKeyJson(const char *key, const char *json);
There’s also insertJson if you want to insert pre-formatted JSON in an array.
/**
* @brief Used to insert a string of existing JSON (typically a preformatted object or array) into a writer
*
* @param json The object or array to insert, a c-string. Must already be valid JSON.
*/
void insertJson(const char *json);
0.1.5 (2021-08-18)
Added JsonWriter::insertKeyJson so you can insert a pre-formatted JSON object into an existing JsonWriter.