c4711593daebc047695a4b82d4a53e80bf47a7e1
[jansson.git] / test / suites / api / test_dump.c
1 /*
2  * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
3  *
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.
6  */
7
8 #include <jansson.h>
9 #include <string.h>
10 #include "util.h"
11
12 int main()
13 {
14     json_t *json;
15     char *result;
16
17     /* Encode an empty object/array, add an item, encode again */
18
19     json = json_object();
20     result = json_dumps(json, 0);
21     if(!result || strcmp(result, "{}"))
22       fail("json_dumps failed");
23
24     json_object_set_new(json, "foo", json_integer(5));
25     result = json_dumps(json, 0);
26     if(!result || strcmp(result, "{\"foo\": 5}"))
27       fail("json_dumps failed");
28
29     json_decref(json);
30
31     json = json_array();
32     result = json_dumps(json, 0);
33     if(!result || strcmp(result, "[]"))
34       fail("json_dumps failed");
35     free(result);
36
37     json_array_append_new(json, json_integer(5));
38     result = json_dumps(json, 0);
39     if(!result || strcmp(result, "[5]"))
40       fail("json_dumps failed");
41     free(result);
42
43     json_decref(json);
44
45     return 0;
46 }