Skip to content

Instantly share code, notes, and snippets.

@vinniefalco
Last active August 27, 2020 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinniefalco/0a23d999ae06ea18e70d0bbd9cdbf1ae to your computer and use it in GitHub Desktop.
Save vinniefalco/0a23d999ae06ea18e70d0bbd9cdbf1ae to your computer and use it in GitHub Desktop.
// Construct the builder using a local buffer
char temp[4096];
value_builder vb( storage_ptr(), temp, sizeof(temp) );
// Create a monotonic resource with a local initial buffer
char buf[4096];
monotonic_resource mr( buf, sizeof(buf) );
// The builder will create a value using `mr`
vb.reset(&mr);
// Iteratively create the elements
vb.begin_object();
vb.insert_key("a");
vb.insert_int64(1);
vb.insert_key("b");
vb.insert_null();
vb.insert_key("c");
vb.insert_string("hello");
vb.end_object();
// Take ownership of the value
value jv = vb.release();
assert( to_string(jv) == "{\"a\":1,\"b\":null,\"c\":\"hello\"}" );
// At this point we could re-use the builder by calling reset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment