Make real number encoding and decoding work under all locales
[jansson.git] / test / suites / api / test_dump.c
1 /*
2  * Copyright (c) 2009-2011 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 static void encode_twice()
13 {
14     /* Encode an empty object/array, add an item, encode again */
15
16     json_t *json;
17     char *result;
18
19     json = json_object();
20     result = json_dumps(json, 0);
21     if(!result || strcmp(result, "{}"))
22       fail("json_dumps failed");
23     free(result);
24
25     json_object_set_new(json, "foo", json_integer(5));
26     result = json_dumps(json, 0);
27     if(!result || strcmp(result, "{\"foo\": 5}"))
28       fail("json_dumps failed");
29     free(result);
30
31     json_decref(json);
32
33     json = json_array();
34     result = json_dumps(json, 0);
35     if(!result || strcmp(result, "[]"))
36       fail("json_dumps failed");
37     free(result);
38
39     json_array_append_new(json, json_integer(5));
40     result = json_dumps(json, 0);
41     if(!result || strcmp(result, "[5]"))
42       fail("json_dumps failed");
43     free(result);
44
45     json_decref(json);
46 }
47
48 static void circular_references()
49 {
50     /* Construct a JSON object/array with a circular reference:
51
52        object: {"a": {"b": {"c": <circular reference to $.a>}}}
53        array: [[[<circular reference to the $[0] array>]]]
54
55        Encode it, remove the circular reference and encode again.
56     */
57
58     json_t *json;
59     char *result;
60
61     json = json_object();
62     json_object_set_new(json, "a", json_object());
63     json_object_set_new(json_object_get(json, "a"), "b", json_object());
64     json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c",
65                     json_object_get(json, "a"));
66
67     if(json_dumps(json, 0))
68         fail("json_dumps encoded a circular reference!");
69
70     json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c");
71
72     result = json_dumps(json, 0);
73     if(!result || strcmp(result, "{\"a\": {\"b\": {}}}"))
74         fail("json_dumps failed!");
75     free(result);
76
77     json_decref(json);
78
79     json = json_array();
80     json_array_append_new(json, json_array());
81     json_array_append_new(json_array_get(json, 0), json_array());
82     json_array_append(json_array_get(json_array_get(json, 0), 0),
83                       json_array_get(json, 0));
84
85     if(json_dumps(json, 0))
86         fail("json_dumps encoded a circular reference!");
87
88     json_array_remove(json_array_get(json_array_get(json, 0), 0), 0);
89
90     result = json_dumps(json, 0);
91     if(!result || strcmp(result, "[[[]]]"))
92         fail("json_dumps failed!");
93     free(result);
94
95     json_decref(json);
96 }
97
98 static void encode_other_than_array_or_object()
99 {
100     /* Encoding anything other than array or object should only
101      * succeed if the JSON_ENCODE_ANY flag is used */
102
103     json_t *json;
104     FILE *fp = NULL;
105     char *result;
106
107     json = json_string("foo");
108     if(json_dumps(json, 0) != NULL)
109         fail("json_dumps encoded a string!");
110     if(json_dumpf(json, fp, 0) == 0)
111         fail("json_dumpf encoded a string!");
112
113     result = json_dumps(json, JSON_ENCODE_ANY);
114     if(!result || strcmp(result, "\"foo\"") != 0)
115         fail("json_dumps failed to encode a string with JSON_ENCODE_ANY");
116
117     free(result);
118     json_decref(json);
119
120     json = json_integer(42);
121     if(json_dumps(json, 0) != NULL)
122         fail("json_dumps encoded an integer!");
123     if(json_dumpf(json, fp, 0) == 0)
124         fail("json_dumpf encoded an integer!");
125
126     result = json_dumps(json, JSON_ENCODE_ANY);
127     if(!result || strcmp(result, "42") != 0)
128         fail("json_dumps failed to encode an integer with JSON_ENCODE_ANY");
129
130     free(result);
131     json_decref(json);
132
133
134 }
135
136 static void run_tests()
137 {
138     encode_twice();
139     circular_references();
140     encode_other_than_array_or_object();
141 }