Wed, 01 Jan 2025 15:56:02 +0100
fix cxJsonDestroy() not freeing uncompleted object member names
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
29 | #include "util_allocator.h" |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #include "cx/test.h" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include "cx/json.h" |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
33 | #include "cx/compare.h" |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
35 | CX_TEST(test_json_init_default) { |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
36 | CxJson json; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
37 | CX_TEST_DO { |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
38 | cxJsonInit(&json, NULL); |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
39 | CX_TEST_ASSERT(json.states == json.states_internal); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
40 | CX_TEST_ASSERT(json.states_size == 1); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
41 | CX_TEST_ASSERT(json.states_capacity >= 8); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
42 | CX_TEST_ASSERT(json.vbuf == json.vbuf_internal); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
43 | CX_TEST_ASSERT(json.vbuf_size == 0); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
44 | CX_TEST_ASSERT(json.vbuf_capacity >= 8); |
1032
aaad28e23dac
fix memory leaks in json tests
Mike Becker <universe@uap-core.de>
parents:
1015
diff
changeset
|
45 | cxJsonDestroy(&json); |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
46 | } |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
47 | } |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
48 | |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | CX_TEST(test_json_simple_object) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | cxstring text = cx_str( |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | "{\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | "\t\"message\":\"success\",\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | "\t\"position\":{\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | "\t\t\"longitude\":-94.7099,\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | "\t\t\"latitude\":51.5539\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | "\t},\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | "\t\"timestamp\":1729348561,\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | "\t\"alive\":true\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | "}" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | ); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | CX_TEST_DO { |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
63 | CxJsonStatus result; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
66 | cxJsonInit(&json, NULL); |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
67 | cxJsonFill(&json, text); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | // parse the big fat object |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | CxJsonValue *obj; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | result = cxJsonNext(&json, &obj); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
72 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | // check the contents |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | CX_TEST_ASSERT(cxJsonIsObject(obj)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | CxJsonValue *message = cxJsonObjGet(obj, "message"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | CX_TEST_ASSERT(cxJsonIsString(message)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | CX_TEST_ASSERT(0 == cx_strcmp( |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
80 | cxJsonAsCxString(message), |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | cx_str("success")) |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | ); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | CxJsonValue *position = cxJsonObjGet(obj, "position"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | CX_TEST_ASSERT(cxJsonIsObject(position)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | CxJsonValue *longitude = cxJsonObjGet(position, "longitude"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | CX_TEST_ASSERT(cxJsonIsNumber(longitude)); |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
88 | CX_TEST_ASSERT(!cxJsonIsInteger(longitude)); |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
89 | CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(longitude), -94.7099)); |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
90 | CX_TEST_ASSERT(cxJsonAsInteger(longitude) == -94); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | CxJsonValue *latitude = cxJsonObjGet(position, "latitude"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | CX_TEST_ASSERT(cxJsonIsNumber(latitude)); |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
93 | CX_TEST_ASSERT(!cxJsonIsInteger(latitude)); |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
94 | CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(latitude), 51.5539)); |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
95 | CX_TEST_ASSERT(cxJsonAsInteger(latitude) == 51); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | CxJsonValue *timestamp = cxJsonObjGet(obj, "timestamp"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | CX_TEST_ASSERT(cxJsonIsInteger(timestamp)); |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
99 | CX_TEST_ASSERT(cxJsonIsNumber(timestamp)); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561); |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
101 | CX_TEST_ASSERT(cxJsonAsDouble(timestamp) == 1729348561.0); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | CxJsonValue *alive = cxJsonObjGet(obj, "alive"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | CX_TEST_ASSERT(cxJsonIsBool(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | CX_TEST_ASSERT(cxJsonIsTrue(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | CX_TEST_ASSERT(!cxJsonIsFalse(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | CX_TEST_ASSERT(cxJsonAsBool(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | // this recursively frees everything else |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | cxJsonValueFree(obj); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | // we only have one object that already contained all the data |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | result = cxJsonNext(&json, &obj); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
114 | CX_TEST_ASSERT(result == CX_JSON_NO_DATA); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | cxJsonDestroy(&json); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | |
1015 | 120 | CX_TEST(test_json_escaped_strings) { |
121 | cxstring text = cx_str( | |
122 | "{\n" | |
123 | "\t\"object\":\"{\\n\\t\\\"object\\\":null\\n}\"}\"\n" | |
124 | "}" | |
125 | ); | |
126 | ||
127 | CxJson json; | |
128 | cxJsonInit(&json, NULL); | |
129 | CX_TEST_DO { | |
130 | cxJsonFill(&json, text); | |
131 | CxJsonValue *obj; | |
132 | CxJsonStatus result = cxJsonNext(&json, &obj); | |
133 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
134 | CX_TEST_ASSERT(cxJsonIsObject(obj)); | |
135 | CxJsonValue *object = cxJsonObjGet(obj, "object"); | |
136 | CX_TEST_ASSERT(cxJsonIsString(object)); | |
137 | CX_TEST_ASSERT(0 == cx_strcmp( | |
138 | cxJsonAsCxString(object), | |
139 | CX_STR("{\n\t\"object\":null\n}")) | |
140 | ); | |
1032
aaad28e23dac
fix memory leaks in json tests
Mike Becker <universe@uap-core.de>
parents:
1015
diff
changeset
|
141 | cxJsonValueFree(obj); |
1015 | 142 | } |
143 | cxJsonDestroy(&json); | |
144 | } | |
145 | ||
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
146 | CX_TEST(test_json_object_incomplete_token) { |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
147 | cxstring text = cx_str( |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
148 | "{\"message\":\"success\" , \"__timestamp\":1729348561}"); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
149 | cxstring parts[16]; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
150 | size_t nparts = 0; // split the json text into mulple parts |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
151 | for(size_t i=0;i<text.length;i+=4) { |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
152 | parts[nparts++] = cx_strsubsl(text, i, 4); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
153 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
154 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
155 | CX_TEST_DO { |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
156 | CxJsonStatus result; |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
157 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
158 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
159 | cxJsonInit(&json, NULL); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
160 | CxJsonValue *obj; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
161 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
162 | size_t part = 0; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
163 | while(part < nparts - 1) { |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
164 | cxJsonFill(&json, parts[part]); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
165 | part++; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
166 | result = cxJsonNext(&json, &obj); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
167 | CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
168 | } |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
169 | cxJsonFill(&json, parts[nparts - 1]); |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
170 | result = cxJsonNext(&json, &obj); |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
171 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
1007
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
172 | CX_TEST_ASSERT(cxJsonIsObject(obj)); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
173 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
174 | CxJsonValue *message = cxJsonObjGet(obj, "message"); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
175 | CX_TEST_ASSERT(cxJsonIsString(message)); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
176 | CX_TEST_ASSERT(0 == cx_strcmp( |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
177 | cxJsonAsCxString(message), |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
178 | cx_str("success")) |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
179 | ); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
180 | CxJsonValue *timestamp = cxJsonObjGet(obj, "__timestamp"); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
181 | CX_TEST_ASSERT(message->type == CX_JSON_STRING); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
182 | CX_TEST_ASSERT(cxJsonIsInteger(timestamp)); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
183 | CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
184 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
185 | // this recursively frees everything else |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
186 | cxJsonValueFree(obj); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
187 | |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
188 | // now there is everything read |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
189 | result = cxJsonNext(&json, &obj); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
190 | CX_TEST_ASSERT(result == CX_JSON_NO_DATA); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
191 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
192 | cxJsonDestroy(&json); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
193 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
194 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
195 | |
1015 | 196 | CX_TEST(test_json_token_wrongly_completed) { |
1075
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
197 | CxTestingAllocator talloc; |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
198 | cx_testing_allocator_init(&talloc); |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
199 | const CxAllocator *alloc = &talloc.base; |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
200 | |
1015 | 201 | cxstring text = cx_str("{\"number\": 47110815!}"); |
202 | cxstring part1 = cx_strsubsl(text, 0, 16); | |
203 | cxstring part2 = cx_strsubs(text, 16); | |
204 | ||
205 | CX_TEST_DO { | |
1075
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
206 | CxJson json; |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
207 | cxJsonInit(&json, alloc); |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
208 | |
1015 | 209 | CxJsonStatus result; |
210 | CxJsonValue *obj; | |
211 | ||
212 | cxJsonFill(&json, part1); | |
213 | result = cxJsonNext(&json, &obj); | |
214 | CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA); | |
215 | cxJsonFill(&json, part2); | |
216 | result = cxJsonNext(&json, &obj); | |
217 | CX_TEST_ASSERT(result == CX_JSON_FORMAT_ERROR_NUMBER); | |
218 | CX_TEST_ASSERT(obj->type == CX_JSON_NOTHING); | |
1075
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
219 | |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
220 | cxJsonDestroy(&json); |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
221 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
1015 | 222 | } |
1075
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
223 | cx_testing_allocator_destroy(&talloc); |
1015 | 224 | } |
225 | ||
1007
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
226 | CX_TEST(test_json_subsequent_fill) { |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
227 | cxstring text = cx_str( |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
228 | "{\"message\":\"success\" , \"__timestamp\":1729348561}"); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
229 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
230 | cxstring part1 = cx_strsubsl(text, 0, 25); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
231 | cxstring part2 = cx_strsubs(text, 25); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
232 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
233 | CX_TEST_DO { |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
234 | CxJson json; |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
235 | cxJsonInit(&json, NULL); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
236 | CxJsonValue *obj; |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
237 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
238 | cxJsonFill(&json, part1); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
239 | cxJsonFill(&json, part2); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
240 | CxJsonStatus result = cxJsonNext(&json, &obj); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
241 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
242 | CX_TEST_ASSERT(cxJsonIsObject(obj)); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
243 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
244 | CxJsonValue *message = cxJsonObjGet(obj, "message"); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
245 | CX_TEST_ASSERT(cxJsonIsString(message)); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
246 | CX_TEST_ASSERT(0 == cx_strcmp( |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
247 | cxJsonAsCxString(message), |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
248 | cx_str("success")) |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
249 | ); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
250 | CxJsonValue *timestamp = cxJsonObjGet(obj, "__timestamp"); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
251 | CX_TEST_ASSERT(message->type == CX_JSON_STRING); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
252 | CX_TEST_ASSERT(cxJsonIsInteger(timestamp)); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
253 | CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
254 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
255 | cxJsonValueFree(obj); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
256 | result = cxJsonNext(&json, &obj); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
257 | CX_TEST_ASSERT(result == CX_JSON_NO_DATA); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
258 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
259 | cxJsonDestroy(&json); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
260 | } |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
261 | } |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
262 | |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
263 | CX_TEST(test_json_object_error) { |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
264 | cxstring text0 = cx_str( |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
265 | "{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
266 | "\t\"message\":\"success\",\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
267 | "\t\"data\":{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
268 | "\t\t\"obj\":{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
269 | "\t\t\t\"array\": [1, 2, 3, ?syntaxerror? ]\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
270 | "\t\t\"}\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
271 | "\t},\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
272 | "\t\"timestamp\":1729348561,\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
273 | "}" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
274 | ); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
275 | cxstring text1 = cx_str("{ \"string\" }"); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
276 | cxstring text2 = cx_str("{ \"a\" : }"); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
277 | cxstring text3 = cx_str("{ \"a\" : \"b\" ]"); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
278 | cxstring text4 = cx_str("{ \"name\": \"value\" ]"); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
279 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
280 | cxstring tests[] = { text0, text1, text2, text3, text4 }; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
281 | CxJsonStatus errors[] = { |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
282 | CX_JSON_FORMAT_ERROR_NUMBER, |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
283 | CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN, |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
284 | CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN, |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
285 | CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN, |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
286 | CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
287 | }; |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
288 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
289 | CX_TEST_DO { |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
290 | CxJsonStatus result; |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
291 | CxJson json; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
292 | CxJsonValue *obj = NULL; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
293 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
294 | for(int i=0;i<5;i++) { |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
295 | cxJsonInit(&json, NULL); |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
296 | cxJsonFill(&json, tests[i]); |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
297 | result = cxJsonNext(&json, &obj); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
298 | |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
299 | CX_TEST_ASSERT(result == errors[i]); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
300 | CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING); |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
301 | cxJsonDestroy(&json); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
302 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
303 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
304 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
305 | |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
306 | CX_TEST(test_json_large_nesting_depth) { |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
307 | CxJson json; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
308 | CxJsonValue *d1; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
309 | cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}"); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
310 | CX_TEST_DO { |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
311 | cxJsonInit(&json, NULL); |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
312 | cxJsonFill(&json, text); |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
313 | cxJsonNext(&json, &d1); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
314 | |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
315 | CX_TEST_ASSERT(d1 != NULL); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
316 | CX_TEST_ASSERT(cxJsonIsObject(d1)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
317 | CxJsonValue *d2 = cxJsonObjGet(d1, "test"); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
318 | CX_TEST_ASSERT(cxJsonIsArray(d2)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
319 | CX_TEST_ASSERT(cxJsonArrSize(d2) == 2); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
320 | CxJsonValue *d3 = cxJsonArrGet(d2, 1); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
321 | CX_TEST_ASSERT(cxJsonIsObject(d3)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
322 | CxJsonValue *d4 = cxJsonObjGet(d3, "foo"); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
323 | CX_TEST_ASSERT(cxJsonIsArray(d4)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
324 | CX_TEST_ASSERT(cxJsonArrSize(d4) == 1); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
325 | CxJsonValue *d5 = cxJsonArrGet(d4, 0); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
326 | CX_TEST_ASSERT(cxJsonIsArray(d5)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
327 | CX_TEST_ASSERT(cxJsonArrSize(d5) == 1); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
328 | CxJsonValue *d6 = cxJsonArrGet(d5, 0); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
329 | CX_TEST_ASSERT(cxJsonIsObject(d6)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
330 | CxJsonValue *d7 = cxJsonObjGet(d6, "bar"); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
331 | CX_TEST_ASSERT(cxJsonIsArray(d7)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
332 | CX_TEST_ASSERT(cxJsonArrSize(d7) == 3); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
333 | CxJsonValue *d8 = cxJsonArrGet(d7, 2); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
334 | CX_TEST_ASSERT(cxJsonIsArray(d8)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
335 | CX_TEST_ASSERT(cxJsonArrSize(d8) == 2); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
336 | CxJsonValue *d9a = cxJsonArrGet(d8, 0); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
337 | CX_TEST_ASSERT(cxJsonIsNull(d9a)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
338 | CxJsonValue *d9b = cxJsonArrGet(d8, 1); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
339 | CX_TEST_ASSERT(cxJsonIsObject(d9b)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
340 | CxJsonValue *d10 = cxJsonObjGet(d9b, "key"); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
341 | CX_TEST_ASSERT(cxJsonIsInteger(d10)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
342 | CX_TEST_ASSERT(cxJsonAsInteger(d10) == 47); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
343 | |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
344 | CX_TEST_ASSERT(json.states != json.states_internal); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
345 | CX_TEST_ASSERT(json.states_capacity > cx_nmemb(json.states_internal)); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
346 | |
978
1da52af18ac3
fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
977
diff
changeset
|
347 | cxJsonValueFree(d1); |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
348 | cxJsonDestroy(&json); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
349 | } |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
350 | } |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
351 | |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
352 | CX_TEST(test_json_number) { |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
353 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
354 | cxJsonInit(&json, NULL); |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
355 | CX_TEST_DO { |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
356 | CxJsonValue *v; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
357 | CxJsonStatus result; |
1015 | 358 | |
975
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
359 | cxJsonFill(&json, "3.1415 "); |
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
360 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
361 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
975
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
362 | CX_TEST_ASSERT(cxJsonIsNumber(v)); |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
363 | CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), 3.1415)); |
978
1da52af18ac3
fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
977
diff
changeset
|
364 | cxJsonValueFree(v); |
1015 | 365 | |
975
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
366 | cxJsonFill(&json, "-47.11e2 "); |
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
367 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
368 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
975
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
369 | CX_TEST_ASSERT(cxJsonIsNumber(v)); |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
370 | CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), -4711.0)); |
978
1da52af18ac3
fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
977
diff
changeset
|
371 | cxJsonValueFree(v); |
1015 | 372 | |
373 | cxJsonFill(&json, "0.815e-3 "); | |
374 | result = cxJsonNext(&json, &v); | |
375 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
376 | CX_TEST_ASSERT(cxJsonIsNumber(v)); | |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
377 | CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), 0.000815)); |
1032
aaad28e23dac
fix memory leaks in json tests
Mike Becker <universe@uap-core.de>
parents:
1015
diff
changeset
|
378 | cxJsonValueFree(v); |
1015 | 379 | |
380 | cxJsonFill(&json, "1.23E4 "); | |
381 | result = cxJsonNext(&json, &v); | |
382 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
383 | CX_TEST_ASSERT(cxJsonIsNumber(v)); | |
384 | CX_TEST_ASSERT(cxJsonAsInteger(v) == 12300); | |
385 | CX_TEST_ASSERT(cxJsonAsDouble(v) == 12300.0); | |
386 | cxJsonValueFree(v); | |
1060
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
387 | |
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
388 | cxJsonFill(&json, "18446744073709551615.0123456789 "); |
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
389 | result = cxJsonNext(&json, &v); |
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
390 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
391 | CX_TEST_ASSERT(cxJsonIsNumber(v)); |
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
392 | // be as precise as possible |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
393 | // TODO: this might produce format error / out of range in future implementations |
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
394 | CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), 1.8446744073709552e+19)); |
1060
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
395 | cxJsonValueFree(v); |
1015 | 396 | } |
397 | cxJsonDestroy(&json); | |
398 | } | |
399 | ||
400 | CX_TEST(test_json_number_format_errors) { | |
401 | CxJson json; | |
402 | cxJsonInit(&json, NULL); | |
403 | CX_TEST_DO { | |
404 | CxJsonValue *v; | |
405 | CxJsonStatus result; | |
406 | ||
407 | cxJsonFill(&json, "+3.1415 "); | |
408 | result = cxJsonNext(&json, &v); | |
409 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
410 | "leading plus is not RFC-8259 compliant"); | |
411 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
412 | cxJsonReset(&json); | |
413 | ||
414 | cxJsonFill(&json, "0.815e-3.0 "); | |
415 | result = cxJsonNext(&json, &v); | |
416 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
417 | "exponent must be an integer"); | |
418 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
419 | cxJsonReset(&json); | |
420 | ||
421 | cxJsonFill(&json, "3.14e "); | |
422 | result = cxJsonNext(&json, &v); | |
423 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
424 | "exponent cannot be empty"); | |
425 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
426 | cxJsonReset(&json); | |
427 | ||
428 | cxJsonFill(&json, "3.14e~7 "); | |
429 | result = cxJsonNext(&json, &v); | |
430 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
431 | "exponent cannot start with bullshit"); | |
432 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
433 | cxJsonReset(&json); | |
434 | ||
435 | cxJsonFill(&json, "1.23e4f "); | |
436 | result = cxJsonNext(&json, &v); | |
437 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
438 | "non-digits in exponent"); | |
439 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
440 | cxJsonReset(&json); | |
441 | ||
442 | cxJsonFill(&json, "1.23f "); | |
443 | result = cxJsonNext(&json, &v); | |
444 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
445 | "non-digits in value"); | |
446 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
447 | cxJsonReset(&json); | |
448 | ||
449 | cxJsonFill(&json, "1.23.45 "); | |
450 | result = cxJsonNext(&json, &v); | |
451 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
452 | "multiple decimal separators"); | |
453 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
454 | cxJsonReset(&json); | |
455 | ||
456 | cxJsonFill(&json, "184467440737095516150123456789 "); | |
457 | result = cxJsonNext(&json, &v); | |
458 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
459 | "30 digit int does not fit into 64-bit int"); | |
460 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
461 | cxJsonReset(&json); | |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
462 | } |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
463 | cxJsonDestroy(&json); |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
464 | } |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
465 | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
466 | CX_TEST(test_json_multiple_values) { |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
467 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
468 | cxJsonInit(&json, NULL); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
469 | CX_TEST_DO { |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
470 | CxJsonValue *v; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
471 | CxJsonStatus result; |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
472 | |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
473 | // read number |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
474 | cxJsonFill(&json, "10\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
475 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
476 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
477 | CX_TEST_ASSERT(cxJsonIsNumber(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
478 | CX_TEST_ASSERT(cxJsonAsInteger(v) == 10); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
479 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
480 | // read remaining '\n' |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
481 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
482 | CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
483 | // read string |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
484 | cxJsonFill(&json, "\"hello world\"\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
485 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
486 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
487 | CX_TEST_ASSERT(cxJsonIsString(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
488 | CX_TEST_ASSERT(!cx_strcmp(cxJsonAsCxString(v), CX_STR("hello world"))); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
489 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
490 | // don't process the remaining newline this time |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
491 | // read obj |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
492 | cxJsonFill(&json, "{ \"value\": \"test\" }\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
493 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
494 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
495 | CX_TEST_ASSERT(cxJsonIsObject(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
496 | CxJsonValue *value = cxJsonObjGet(v, "value"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
497 | CX_TEST_ASSERT(cxJsonAsString(value)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
498 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
499 | // read array |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
500 | cxJsonFill(&json, "[ 0, 1, 2, 3, 4, 5 ]\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
501 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
502 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
503 | CX_TEST_ASSERT(cxJsonIsArray(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
504 | CxJsonValue *a0 = cxJsonArrGet(v, 0); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
505 | CxJsonValue *a3 = cxJsonArrGet(v, 3); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
506 | CX_TEST_ASSERT(cxJsonIsNumber(a0)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
507 | CX_TEST_ASSERT(cxJsonAsInteger(a0) == 0); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
508 | CX_TEST_ASSERT(cxJsonIsNumber(a3)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
509 | CX_TEST_ASSERT(cxJsonAsInteger(a3) == 3); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
510 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
511 | // read literal |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
512 | cxJsonFill(&json, "true\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
513 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
514 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
515 | CX_TEST_ASSERT(cxJsonIsLiteral(v)); |
1015 | 516 | CX_TEST_ASSERT(cxJsonIsBool(v)); |
517 | CX_TEST_ASSERT(cxJsonIsTrue(v)); | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
518 | CX_TEST_ASSERT(cxJsonAsBool(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
519 | cxJsonValueFree(v); |
1015 | 520 | cxJsonFill(&json, "false\n"); |
521 | result = cxJsonNext(&json, &v); | |
522 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
523 | CX_TEST_ASSERT(cxJsonIsLiteral(v)); | |
524 | CX_TEST_ASSERT(cxJsonIsBool(v)); | |
525 | CX_TEST_ASSERT(cxJsonIsFalse(v)); | |
526 | CX_TEST_ASSERT(!cxJsonAsBool(v)); | |
527 | cxJsonValueFree(v); | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
528 | cxJsonFill(&json, "null\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
529 | result = cxJsonNext(&json, &v); |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
530 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
1015 | 531 | CX_TEST_ASSERT(cxJsonIsLiteral(v)); |
532 | CX_TEST_ASSERT(!cxJsonIsBool(v)); | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
533 | CX_TEST_ASSERT(cxJsonIsNull(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
534 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
535 | } |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
536 | cxJsonDestroy(&json); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
537 | } |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
538 | |
1033
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
539 | CX_TEST(test_json_array_iterator) { |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
540 | CxJson json; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
541 | cxJsonInit(&json, NULL); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
542 | CX_TEST_DO { |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
543 | CxJsonValue *v; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
544 | CxJsonStatus result; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
545 | cxJsonFill(&json, "[ 0, 3, 6, 9, 12, 15 ]\n"); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
546 | result = cxJsonNext(&json, &v); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
547 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
548 | CX_TEST_ASSERT(cxJsonIsArray(v)); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
549 | CxIterator iter = cxJsonArrIter(v); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
550 | unsigned i = 0; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
551 | cx_foreach(CxJsonValue*, elem, iter) { |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
552 | CX_TEST_ASSERT(cxJsonIsNumber(elem)); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
553 | CX_TEST_ASSERT(i == cxJsonAsInteger(elem)); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
554 | i += 3; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
555 | } |
1051
7d17bd1103d7
fix another missing free() in json tests
Mike Becker <universe@uap-core.de>
parents:
1033
diff
changeset
|
556 | cxJsonValueFree(v); |
1033
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
557 | } |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
558 | cxJsonDestroy(&json); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
559 | } |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
560 | |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
561 | CX_TEST(test_json_allocator) { |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
562 | CxTestingAllocator talloc; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
563 | cx_testing_allocator_init(&talloc); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
564 | CxAllocator *allocator = &talloc.base; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
565 | |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
566 | cxstring text = cx_str( |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
567 | "{\n" |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
568 | "\t\"message\":\"success\",\n" |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
569 | "\t\"data\":[\"value1\",{\"x\":123, \"y\":523 }]\n" |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
570 | "}" |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
571 | ); |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
572 | |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
573 | CX_TEST_DO { |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
574 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
575 | cxJsonInit(&json, allocator); |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
576 | cxJsonFill(&json, text); |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
577 | |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
578 | CxJsonValue *obj; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
579 | CxJsonStatus result = cxJsonNext(&json, &obj); |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
580 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
581 | CX_TEST_ASSERT(obj->allocator == allocator); |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
582 | |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
583 | // this recursively frees everything |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
584 | cxJsonValueFree(obj); |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
585 | cxJsonDestroy(&json); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
586 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
587 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
588 | } |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
589 | cx_testing_allocator_destroy(&talloc); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
590 | } |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
591 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
592 | CX_TEST(test_json_allocator_parse_error) { |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
593 | CxTestingAllocator talloc; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
594 | cx_testing_allocator_init(&talloc); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
595 | CxAllocator *allocator = &talloc.base; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
596 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
597 | cxstring text = cx_str( |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
598 | "{\n" |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
599 | "\t\"message\":\"success\"\n" // <-- missing comma |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
600 | "\t\"data\":[\"value1\",{\"x\":123, \"y\":523 }]\n" |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
601 | "}" |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
602 | ); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
603 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
604 | CX_TEST_DO { |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
605 | CxJson json; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
606 | cxJsonInit(&json, allocator); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
607 | cxJsonFill(&json, text); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
608 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
609 | CxJsonValue *obj = NULL; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
610 | CxJsonStatus result = cxJsonNext(&json, &obj); |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
611 | CX_TEST_ASSERT(result == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
612 | CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
613 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
614 | // clean-up any left-over memory |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
615 | cxJsonDestroy(&json); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
616 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
617 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
618 | } |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
619 | cx_testing_allocator_destroy(&talloc); |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
620 | } |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
621 | |
1057
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
622 | CX_TEST(test_json_create_value) { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
623 | CxTestingAllocator talloc; |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
624 | cx_testing_allocator_init(&talloc); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
625 | CxAllocator *allocator = &talloc.base; |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
626 | CX_TEST_DO { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
627 | /* |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
628 | * This is the value we want to create in this test: |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
629 | * |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
630 | * { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
631 | * "bool": false, |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
632 | * "int": 47, |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
633 | * "strings": [ "hello", "world" ], |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
634 | * "nested": { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
635 | * "string": "test", |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
636 | * "floats": [ 3.1415, 47.11, 8.15 ], |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
637 | * "ints": [ 4, 8, 15, 16, 23, 42 ], |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
638 | * "literals": [ true, null, false ] |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
639 | * } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
640 | * } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
641 | */ |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
642 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
643 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
644 | // create the object |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
645 | CxJsonValue *obj = cxJsonCreateObj(allocator); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
646 | CX_TEST_ASSERT(obj != NULL); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
647 | CX_TEST_ASSERT(cxJsonIsObject(obj)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
648 | CX_TEST_ASSERT(obj->allocator == allocator); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
649 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
650 | // add the members |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
651 | { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
652 | cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
653 | cxJsonObjPutInteger(obj, CX_STR("int"), 47); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
654 | CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
655 | CX_TEST_ASSERT(strings != NULL); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
656 | CX_TEST_ASSERT(cxJsonIsArray(strings)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
657 | const char* str[] = {"hello", "world"}; |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
658 | CX_TEST_ASSERT(0 == cxJsonArrAddStrings(strings, str, 2)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
659 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
660 | CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
661 | CX_TEST_ASSERT(nested != NULL); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
662 | CX_TEST_ASSERT(cxJsonIsObject(nested)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
663 | cxJsonObjPutCxString(nested, CX_STR("string"), CX_STR("test")); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
664 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
665 | cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
666 | (double[]){3.1415, 47.11, 8.15}, 3); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
667 | cxJsonArrAddIntegers(cxJsonObjPutArr(nested, CX_STR("ints")), |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
668 | (int64_t[]){4, 8, 15, 16, 23, 42}, 6); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
669 | cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
670 | (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
671 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
672 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
673 | // verify the contents |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
674 | { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
675 | CX_TEST_ASSERT(cxJsonIsFalse(cxJsonObjGet(obj, "bool"))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
676 | CX_TEST_ASSERT(47 == cxJsonAsInteger(cxJsonObjGet(obj, "int"))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
677 | CxJsonValue *strings = cxJsonObjGet(obj, "strings"); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
678 | CX_TEST_ASSERT(cxJsonIsArray(strings)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
679 | CX_TEST_ASSERT(2 == cxJsonArrSize(strings)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
680 | CX_TEST_ASSERT(0 == cx_strcmp(CX_STR("hello"), cxJsonAsCxString(cxJsonArrGet(strings, 0)))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
681 | CX_TEST_ASSERT(0 == cx_strcmp(CX_STR("world"), cxJsonAsCxString(cxJsonArrGet(strings, 1)))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
682 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
683 | CxJsonValue *nested = cxJsonObjGet(obj, "nested"); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
684 | CX_TEST_ASSERT(cxJsonIsObject(nested)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
685 | CX_TEST_ASSERT(0 == strcmp("test", cxJsonAsString(cxJsonObjGet(nested, "string")))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
686 | CxJsonValue *floats = cxJsonObjGet(nested, "floats"); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
687 | CX_TEST_ASSERT(cxJsonIsArray(floats)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
688 | CX_TEST_ASSERT(3 == cxJsonArrSize(floats)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
689 | CX_TEST_ASSERT(3.1415 == cxJsonAsDouble(cxJsonArrGet(floats, 0))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
690 | CX_TEST_ASSERT(47.11 == cxJsonAsDouble(cxJsonArrGet(floats, 1))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
691 | CX_TEST_ASSERT(8.15 == cxJsonAsDouble(cxJsonArrGet(floats, 2))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
692 | CxJsonValue *ints = cxJsonObjGet(nested, "ints"); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
693 | CX_TEST_ASSERT(cxJsonIsArray(ints)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
694 | CX_TEST_ASSERT(6 == cxJsonArrSize(ints)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
695 | CX_TEST_ASSERT(4 == cxJsonAsInteger(cxJsonArrGet(ints, 0))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
696 | CX_TEST_ASSERT(8 == cxJsonAsInteger(cxJsonArrGet(ints, 1))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
697 | CX_TEST_ASSERT(15 == cxJsonAsInteger(cxJsonArrGet(ints, 2))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
698 | CX_TEST_ASSERT(16 == cxJsonAsInteger(cxJsonArrGet(ints, 3))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
699 | CX_TEST_ASSERT(23 == cxJsonAsInteger(cxJsonArrGet(ints, 4))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
700 | CX_TEST_ASSERT(42 == cxJsonAsInteger(cxJsonArrGet(ints, 5))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
701 | CxJsonValue *literals = cxJsonObjGet(nested, "literals"); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
702 | CX_TEST_ASSERT(cxJsonIsArray(literals)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
703 | CX_TEST_ASSERT(3 == cxJsonArrSize(literals)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
704 | CX_TEST_ASSERT(cxJsonIsTrue(cxJsonArrGet(literals, 0))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
705 | CX_TEST_ASSERT(cxJsonIsNull(cxJsonArrGet(literals, 1))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
706 | CX_TEST_ASSERT(cxJsonIsFalse(cxJsonArrGet(literals, 2))); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
707 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
708 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
709 | // destroy the value and verify the allocations |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
710 | cxJsonValueFree(obj); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
711 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
712 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
713 | cx_testing_allocator_destroy(&talloc); |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
714 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
715 | |
1072
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
716 | CX_TEST(test_json_write_default_format) { |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
717 | CxTestingAllocator talloc; |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
718 | cx_testing_allocator_init(&talloc); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
719 | CxAllocator *allocator = &talloc.base; |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
720 | CX_TEST_DO { |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
721 | // expected value |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
722 | cxstring expected = CX_STR("{\"bool\":false,\"nested\":{\"floats\":[3.1415,47.11,8.15],\"ints\":[4,8,15,16,23,42],\"literals\":[true,null,false],\"string\":\"test\"},\"num\":47.11,\"strings\":[\"hello\",\"world\"]}"); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
723 | |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
724 | // create the value |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
725 | CxJsonValue *obj = cxJsonCreateObj(allocator); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
726 | cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
727 | cxJsonObjPutNumber(obj, CX_STR("num"), 47.11); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
728 | CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
729 | cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
730 | CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
731 | cxJsonObjPutString(nested, CX_STR("string"), "test"); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
732 | cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
733 | (double[]){3.1415, 47.11, 8.15}, 3); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
734 | cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
735 | (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
736 | cxJsonArrAddIntegers(cxJsonObjPutArr(nested, CX_STR("ints")), |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
737 | (int64_t[]){4, 8, 15, 16, 23, 42}, 6); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
738 | |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
739 | // write it to a buffer |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
740 | CxBuffer buf; |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
741 | cxBufferInit(&buf, NULL, 256, NULL, CX_BUFFER_DEFAULT); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
742 | int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, NULL); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
743 | CX_TEST_ASSERT(result == 0); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
744 | |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
745 | // compare the string |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
746 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
747 | |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
748 | // destroy everything |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
749 | cxBufferDestroy(&buf); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
750 | cxJsonValueFree(obj); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
751 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
752 | } |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
753 | cx_testing_allocator_destroy(&talloc); |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
754 | } |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
755 | |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
756 | CxTestSuite *cx_test_suite_json(void) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
757 | CxTestSuite *suite = cx_test_suite_new("json"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
758 | |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
759 | cx_test_register(suite, test_json_init_default); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
760 | cx_test_register(suite, test_json_simple_object); |
1015 | 761 | cx_test_register(suite, test_json_escaped_strings); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
762 | cx_test_register(suite, test_json_object_incomplete_token); |
1015 | 763 | cx_test_register(suite, test_json_token_wrongly_completed); |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
764 | cx_test_register(suite, test_json_object_error); |
1007
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
765 | cx_test_register(suite, test_json_subsequent_fill); |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
766 | cx_test_register(suite, test_json_large_nesting_depth); |
975
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
767 | cx_test_register(suite, test_json_number); |
1015 | 768 | cx_test_register(suite, test_json_number_format_errors); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
769 | cx_test_register(suite, test_json_multiple_values); |
1033
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
770 | cx_test_register(suite, test_json_array_iterator); |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
771 | cx_test_register(suite, test_json_allocator); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
772 | cx_test_register(suite, test_json_allocator_parse_error); |
1057
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
773 | cx_test_register(suite, test_json_create_value); |
1072
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
774 | cx_test_register(suite, test_json_write_default_format); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
775 | |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
776 | return suite; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
777 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
778 |