2 * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
19 static int my_writer(const char *buffer, size_t len, void *data) {
20 struct my_sink *s = data;
21 if (len > s->cap - s->off) {
24 memcpy(s->buf + s->off, buffer, len);
29 static void run_tests()
33 const char str[] = "[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
34 char *dumped_to_string;
36 json = json_loads(str, 0, NULL);
38 fail("json_loads failed");
41 dumped_to_string = json_dumps(json, 0);
42 if (!dumped_to_string) {
44 fail("json_dumps failed");
48 s.cap = strlen(dumped_to_string);
49 s.buf = malloc(s.cap);
52 free(dumped_to_string);
53 fail("malloc failed");
56 if (json_dump_callback(json, my_writer, &s, 0) == -1) {
58 free(dumped_to_string);
60 fail("json_dump_callback failed on an exact-length sink buffer");
63 if (strncmp(dumped_to_string, s.buf, s.off) != 0) {
65 free(dumped_to_string);
67 fail("json_dump_callback and json_dumps did not produce identical output");
71 if (json_dump_callback(json, my_writer, &s, 0) != -1) {
73 free(dumped_to_string);
75 fail("json_dump_callback succeeded on a short buffer when it should have failed");
79 free(dumped_to_string);