Skip to content

Commit c5901e0

Browse files
committed
JSON stream: support streaming a key-value pair to an object
Arrays already allow the equivalent operation, json_array_streamt::push_back(const jsont &)
1 parent 0d5e588 commit c5901e0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/util/json_stream.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ class json_stream_objectt : public json_streamt
169169
return it->second;
170170
}
171171

172+
/// Push back a JSON element into the current object stream.
173+
/// Note the pushed key won't be available via operator[], as it has been
174+
/// output already.
175+
/// Provided for compatibility with `jsont`.
176+
/// \param key: new key to create in the streamed object
177+
/// \param json: a non-streaming JSON element
178+
void push_back(const std::string &key, const jsont &json)
179+
{
180+
PRECONDITION(open);
181+
// Check the key is not already due to be output later:
182+
PRECONDITION(!object.count(key));
183+
// To ensure consistency of the output, we flush and
184+
// close the current child stream before printing the given element.
185+
output_child_stream();
186+
output_delimiter();
187+
jsont::output_key(out, key);
188+
json.output_rec(out, indent + 1);
189+
}
190+
172191
json_stream_objectt &push_back_stream_object(const std::string &key);
173192
json_stream_arrayt &push_back_stream_array(const std::string &key);
174193

0 commit comments

Comments
 (0)