aboutsummaryrefslogtreecommitdiff
path: root/test/suites/api/test_dump.c
diff options
context:
space:
mode:
authorNathaniel McCallum <npmccallum@redhat.com>2017-01-26 09:46:48 +0100
committerNathaniel McCallum <npmccallum@redhat.com>2017-01-26 16:16:24 +0100
commitb900967f6fbfae098ced9dfeab7b2b51e1a22c0a (patch)
treec58c08d1d918756cfb57b5fd8d20345eeb410fa6 /test/suites/api/test_dump.c
parent746c2c3a996a7f6e80bd19fe3de438637e6363e1 (diff)
downloadjansson-b900967f6fbfae098ced9dfeab7b2b51e1a22c0a.zip
jansson-b900967f6fbfae098ced9dfeab7b2b51e1a22c0a.tar.gz
jansson-b900967f6fbfae098ced9dfeab7b2b51e1a22c0a.tar.bz2
Implement json_dumpb()
This function encodes the json_t object to a pre-allocated buffer. It compliments the already existing json_loadb() function and is useful for parsing JSON-RPC (among other protocols) when sent over datagram sockets. Signed-off-by: Nathaniel McCallum <npmccallum@redhat.com>
Diffstat (limited to 'test/suites/api/test_dump.c')
-rw-r--r--test/suites/api/test_dump.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/test/suites/api/test_dump.c b/test/suites/api/test_dump.c
index 3591fa5..e528031 100644
--- a/test/suites/api/test_dump.c
+++ b/test/suites/api/test_dump.c
@@ -22,6 +22,9 @@ static void encode_null()
if(json_dumps(NULL, JSON_ENCODE_ANY) != NULL)
fail("json_dumps didn't fail for NULL");
+ if(json_dumpb(NULL, NULL, 0, JSON_ENCODE_ANY) != 0)
+ fail("json_dumps didn't fail for NULL");
+
if(json_dumpf(NULL, stderr, JSON_ENCODE_ANY) != -1)
fail("json_dumpf didn't fail for NULL");
@@ -212,6 +215,28 @@ static void dump_file()
remove("json_dump_file.json");
}
+static void dumpb()
+{
+ char buf[2];
+ json_t *obj;
+ size_t size;
+
+ obj = json_object();
+
+ size = json_dumpb(obj, buf, sizeof(buf), 0);
+ if(size != 2 || strncmp(buf, "{}", 2))
+ fail("json_dumpb failed");
+
+ json_decref(obj);
+ obj = json_pack("{s:s}", "foo", "bar");
+
+ size = json_dumpb(obj, buf, sizeof(buf), JSON_COMPACT);
+ if(size != 13)
+ fail("json_dumpb size check failed");
+
+ json_decref(obj);
+}
+
static void run_tests()
{
encode_null();
@@ -221,4 +246,5 @@ static void run_tests()
escape_slashes();
encode_nul_byte();
dump_file();
+ dumpb();
}