aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorNathaniel McCallum <npmccallum@redhat.com>2017-02-27 14:56:55 -0500
committerNathaniel McCallum <npmccallum@redhat.com>2017-02-27 15:09:03 -0500
commitb8bb078cc291a387ecce03a7a62ded593f76b85e (patch)
tree84d1771ca998b226970b1ed180316579b065a107 /test
parent3c51112063a09151390e93916a4637a7e0427b32 (diff)
downloadjansson-b8bb078cc291a387ecce03a7a62ded593f76b85e.zip
jansson-b8bb078cc291a387ecce03a7a62ded593f76b85e.tar.gz
jansson-b8bb078cc291a387ecce03a7a62ded593f76b85e.tar.bz2
Add JSON_EMBED encoding flag
The JSON_EMBED encoding flag causes the opening and closing characters of the top-level array ('[', ']') or object ('{', '}') to be omitted during encoding. This feature makes it possible to concatenate multiple arrays or objects in the stream output. It also makes it possible to perform outputs of partial composes. One such example of a partial compose is when outputting a JWE object. The output is a JSON object. But it has one top-level attribute ("ciphertext") that can grow out of proportion with the rest of the metadata. With the JSON_EMBED flag, the other metadata can be composed ahead of time and dumped during the beginning of output, where the "ciphertext" and "tag" attributes can be streamed out in chunks. Thus, the header material can be composed with Jansson and the ciphertext itself can be composed manually.
Diffstat (limited to 'test')
-rw-r--r--test/suites/api/test_dump.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/suites/api/test_dump.c b/test/suites/api/test_dump.c
index 8d59d40..17c495a 100644
--- a/test/suites/api/test_dump.c
+++ b/test/suites/api/test_dump.c
@@ -278,6 +278,39 @@ static void dumpfd()
#endif
}
+static void embed()
+{
+ static const char *plains[] = {
+ "{\"bar\":[],\"foo\":{}}",
+ "[[],{}]",
+ "{}",
+ "[]",
+ NULL
+ };
+
+ size_t i;
+
+ for(i = 0; plains[i]; i++) {
+ const char *plain = plains[i];
+ json_t *parse = NULL;
+ char *embed = NULL;
+ size_t psize = 0;
+ size_t esize = 0;
+
+ psize = strlen(plain) - 2;
+ embed = calloc(1, psize);
+ parse = json_loads(plain, 0, NULL);
+ esize = json_dumpb(parse, embed, psize,
+ JSON_COMPACT | JSON_SORT_KEYS | JSON_EMBED);
+ json_decref(parse);
+ if(esize != psize)
+ fail("json_dumpb(JSON_EMBED) returned an invalid size");
+ if(strncmp(plain + 1, embed, esize) != 0)
+ fail("json_dumps(JSON_EMBED) returned an invalid value");
+ free(embed);
+ }
+}
+
static void run_tests()
{
encode_null();
@@ -289,4 +322,5 @@ static void run_tests()
dump_file();
dumpb();
dumpfd();
+ embed();
}