Sun, 26 Jan 2025 13:20:05 +0100
add test and implementation for malformed escape sequences
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" | |
1154
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
123 | "\t\"object\":\"{\\n\\t\\\"object\\\":null\\n}\",\n" |
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
124 | "\t\"ctrl-chars\":\"\\\\foo\\r\\nbar\\f*ring\\/ring*\\b\"\n" |
1015 | 125 | "}" |
126 | ); | |
127 | ||
128 | CxJson json; | |
129 | cxJsonInit(&json, NULL); | |
130 | CX_TEST_DO { | |
131 | cxJsonFill(&json, text); | |
132 | CxJsonValue *obj; | |
133 | CxJsonStatus result = cxJsonNext(&json, &obj); | |
134 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
135 | CX_TEST_ASSERT(cxJsonIsObject(obj)); | |
136 | CxJsonValue *object = cxJsonObjGet(obj, "object"); | |
137 | CX_TEST_ASSERT(cxJsonIsString(object)); | |
138 | CX_TEST_ASSERT(0 == cx_strcmp( | |
139 | cxJsonAsCxString(object), | |
140 | CX_STR("{\n\t\"object\":null\n}")) | |
141 | ); | |
1154
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
142 | CxJsonValue *ctrl = cxJsonObjGet(obj, "ctrl-chars"); |
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
143 | CX_TEST_ASSERT(cxJsonIsString(ctrl)); |
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
144 | CX_TEST_ASSERT(0 == cx_strcmp( |
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
145 | cxJsonAsCxString(ctrl), |
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
146 | CX_STR("\\foo\r\nbar\f*ring/ring*\b")) |
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
147 | ); |
afd12f31d28a
add missing test coverage for escape sequences
Mike Becker <universe@uap-core.de>
parents:
1152
diff
changeset
|
148 | cxJsonValueFree(ctrl); |
1015 | 149 | } |
150 | cxJsonDestroy(&json); | |
151 | } | |
152 | ||
1149
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
153 | CX_TEST(test_json_escaped_unicode_strings) { |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
154 | cxstring text = cx_str( |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
155 | "{\n" |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
156 | "\"ascii\":\"\\u0041\\u0053\\u0043\\u0049\\u0049\",\n" |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
157 | "\"unicode\":\"\\u00df\\u00DF\",\n" |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
158 | "\"mixed\":\"mixed ä ö \\u00e4 \\u00f6\",\n" |
1152
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
159 | "\"wide\":\"\\u03a3\\u29b0\",\n" |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
160 | "\"surrogatepair1\":\"\\ud83e\\udff5\",\n" |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
161 | "\"surrogatepair2\":\"test\\ud83e\\udff1AA\"\n," |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
162 | "\"mixed2\":\"123\\u03a3\\ud83e\\udfc5\\u00df\"" |
1149
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
163 | "}" |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
164 | ); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
165 | |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
166 | CxJson json; |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
167 | cxJsonInit(&json, NULL); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
168 | CX_TEST_DO { |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
169 | cxJsonFill(&json, text); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
170 | CxJsonValue *obj; |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
171 | CxJsonStatus result = cxJsonNext(&json, &obj); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
172 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
173 | CX_TEST_ASSERT(cxJsonIsObject(obj)); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
174 | |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
175 | CxJsonValue *ascii = cxJsonObjGet(obj, "ascii"); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
176 | CX_TEST_ASSERT(cxJsonIsString(ascii)); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
177 | CX_TEST_ASSERT(0 == cx_strcmp( |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
178 | cxJsonAsCxString(ascii), |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
179 | CX_STR("ASCII")) |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
180 | ); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
181 | |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
182 | CxJsonValue *unicode = cxJsonObjGet(obj, "unicode"); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
183 | CX_TEST_ASSERT(cxJsonIsString(unicode)); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
184 | CX_TEST_ASSERT(0 == cx_strcmp( |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
185 | cxJsonAsCxString(unicode), |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
186 | CX_STR("ßß")) |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
187 | ); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
188 | |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
189 | CxJsonValue *mixed = cxJsonObjGet(obj, "mixed"); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
190 | CX_TEST_ASSERT(cxJsonIsString(mixed)); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
191 | CX_TEST_ASSERT(0 == cx_strcmp( |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
192 | cxJsonAsCxString(mixed), |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
193 | CX_STR("mixed ä ö ä ö")) |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
194 | ); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
195 | |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
196 | CxJsonValue *wide = cxJsonObjGet(obj, "wide"); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
197 | CX_TEST_ASSERT(cxJsonIsString(wide)); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
198 | CX_TEST_ASSERT(0 == cx_strcmp( |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
199 | cxJsonAsCxString(wide), |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
200 | CX_STR("\u03a3\u29b0")) |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
201 | ); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
202 | |
1152
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
203 | CxJsonValue *surrogatepair1 = cxJsonObjGet(obj, "surrogatepair1"); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
204 | CX_TEST_ASSERT(cxJsonIsString(surrogatepair1)); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
205 | CX_TEST_ASSERT(0 == cx_strcmp( |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
206 | cxJsonAsCxString(surrogatepair1), |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
207 | CX_STR("\xf0\x9f\xaf\xb5")) |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
208 | ); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
209 | |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
210 | CxJsonValue *surrogatepair2 = cxJsonObjGet(obj, "surrogatepair2"); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
211 | CX_TEST_ASSERT(cxJsonIsString(surrogatepair2)); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
212 | CX_TEST_ASSERT(0 == cx_strcmp( |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
213 | cxJsonAsCxString(surrogatepair2), |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
214 | CX_STR("test\xf0\x9f\xaf\xb1" "AA")) |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
215 | ); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
216 | |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
217 | CxJsonValue *mixed2 = cxJsonObjGet(obj, "mixed2"); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
218 | CX_TEST_ASSERT(cxJsonIsString(mixed2)); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
219 | CX_TEST_ASSERT(0 == cx_strcmp( |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
220 | cxJsonAsCxString(mixed2), |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
221 | CX_STR("123\u03a3\xf0\x9f\xaf\x85ß")) |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
222 | ); |
e4af44b488bc
implement decoder for utf16 surrogate pairs in unescape_string
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1149
diff
changeset
|
223 | |
1149
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
224 | cxJsonValueFree(obj); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
225 | } |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
226 | cxJsonDestroy(&json); |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
227 | } |
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
228 | |
1156
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
229 | CX_TEST(test_json_escaped_unicode_malformed) { |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
230 | CxJson json; |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
231 | cxJsonInit(&json, NULL); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
232 | CxJsonValue *obj; |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
233 | CxJsonStatus result; |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
234 | CX_TEST_DO { |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
235 | cxJsonFill(&json, "\"too few \\u123 digits\""); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
236 | result = cxJsonNext(&json, &obj); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
237 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
238 | CX_TEST_ASSERT(cxJsonIsString(obj)); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
239 | CX_TEST_ASSERT(0 == cx_strcmp( |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
240 | cxJsonAsCxString(obj), |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
241 | CX_STR("too few \\u123 digits") |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
242 | )); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
243 | cxJsonFill(&json, "\"too many \\u00E456 digits\""); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
244 | result = cxJsonNext(&json, &obj); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
245 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
246 | CX_TEST_ASSERT(cxJsonIsString(obj)); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
247 | CX_TEST_ASSERT(0 == cx_strcmp( |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
248 | cxJsonAsCxString(obj), |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
249 | CX_STR("too many ä56 digits") |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
250 | )); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
251 | cxJsonFill(&json, "\"only high \\uD800 surrogate\""); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
252 | result = cxJsonNext(&json, &obj); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
253 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
254 | CX_TEST_ASSERT(cxJsonIsString(obj)); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
255 | CX_TEST_ASSERT(0 == cx_strcmp( |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
256 | cxJsonAsCxString(obj), |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
257 | CX_STR("only high \\uD800 surrogate") |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
258 | )); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
259 | cxJsonFill(&json, "\"only low \\uDC00 surrogate\""); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
260 | result = cxJsonNext(&json, &obj); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
261 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
262 | CX_TEST_ASSERT(cxJsonIsString(obj)); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
263 | CX_TEST_ASSERT(0 == cx_strcmp( |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
264 | cxJsonAsCxString(obj), |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
265 | CX_STR("only low \\uDC00 surrogate") |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
266 | )); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
267 | cxJsonFill(&json, "\"two high \\uD800\\uD800 surrogates\""); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
268 | result = cxJsonNext(&json, &obj); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
269 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
270 | CX_TEST_ASSERT(cxJsonIsString(obj)); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
271 | CX_TEST_ASSERT(0 == cx_strcmp( |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
272 | cxJsonAsCxString(obj), |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
273 | CX_STR("two high \\uD800\\uD800 surrogates") |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
274 | )); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
275 | cxJsonFill(&json, "\"high plus bullshit \\uD800\\u567 foo\""); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
276 | result = cxJsonNext(&json, &obj); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
277 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
278 | CX_TEST_ASSERT(cxJsonIsString(obj)); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
279 | CX_TEST_ASSERT(0 == cx_strcmp( |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
280 | cxJsonAsCxString(obj), |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
281 | CX_STR("high plus bullshit \\uD800\\u567 foo") |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
282 | )); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
283 | } |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
284 | cxJsonDestroy(&json); |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
285 | } |
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
286 | |
1139
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
287 | CX_TEST(test_json_escaped_end_of_string) { |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
288 | CxJson json; |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
289 | cxJsonInit(&json, NULL); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
290 | CX_TEST_DO { |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
291 | // first test, normal scenario |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
292 | cxJsonFill(&json, "\"a \\\"test\\\" string\""); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
293 | CxJsonValue *val; |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
294 | CxJsonStatus result = cxJsonNext(&json, &val); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
295 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
296 | CX_TEST_ASSERT(cxJsonIsString(val)); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
297 | CX_TEST_ASSERT(0 == cx_strcmp( |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
298 | cxJsonAsCxString(val), |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
299 | cx_str("a \"test\" string")) |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
300 | ); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
301 | cxJsonValueFree(val); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
302 | |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
303 | // second test - uncompleted token with hanging escape char |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
304 | cxJsonFill(&json, "\"a \\\"test\\"); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
305 | result = cxJsonNext(&json, &val); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
306 | CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
307 | cxJsonFill(&json, "\" string\""); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
308 | result = cxJsonNext(&json, &val); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
309 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
310 | CX_TEST_ASSERT(cxJsonIsString(val)); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
311 | CX_TEST_ASSERT(0 == cx_strcmp( |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
312 | cxJsonAsCxString(val), |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
313 | cx_str("a \"test\" string")) |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
314 | ); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
315 | cxJsonValueFree(val); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
316 | } |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
317 | cxJsonDestroy(&json); |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
318 | } |
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
319 | |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
320 | 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
|
321 | cxstring text = cx_str( |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
322 | "{\"message\":\"success\" , \"__timestamp\":1729348561}"); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
323 | cxstring parts[16]; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
324 | 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
|
325 | 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
|
326 | 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
|
327 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
328 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
329 | CX_TEST_DO { |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
330 | CxJsonStatus result; |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
331 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
332 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
333 | cxJsonInit(&json, NULL); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
334 | CxJsonValue *obj; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
335 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
336 | 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
|
337 | while(part < nparts - 1) { |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
338 | 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
|
339 | part++; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
340 | 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
|
341 | 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
|
342 | } |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
343 | 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
|
344 | result = cxJsonNext(&json, &obj); |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
345 | 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
|
346 | 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
|
347 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
348 | 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
|
349 | 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
|
350 | 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
|
351 | cxJsonAsCxString(message), |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
352 | cx_str("success")) |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
353 | ); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
354 | 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
|
355 | 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
|
356 | 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
|
357 | 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
|
358 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
359 | // 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
|
360 | cxJsonValueFree(obj); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
361 | |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
362 | // 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
|
363 | 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
|
364 | 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
|
365 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
366 | cxJsonDestroy(&json); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
367 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
368 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
369 | |
1015 | 370 | 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
|
371 | CxTestingAllocator talloc; |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
372 | cx_testing_allocator_init(&talloc); |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
373 | const CxAllocator *alloc = &talloc.base; |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
374 | |
1015 | 375 | cxstring text = cx_str("{\"number\": 47110815!}"); |
376 | cxstring part1 = cx_strsubsl(text, 0, 16); | |
377 | cxstring part2 = cx_strsubs(text, 16); | |
378 | ||
379 | CX_TEST_DO { | |
1075
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
380 | CxJson json; |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
381 | cxJsonInit(&json, alloc); |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
382 | |
1015 | 383 | CxJsonStatus result; |
384 | CxJsonValue *obj; | |
385 | ||
386 | cxJsonFill(&json, part1); | |
387 | result = cxJsonNext(&json, &obj); | |
388 | CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA); | |
389 | cxJsonFill(&json, part2); | |
390 | result = cxJsonNext(&json, &obj); | |
391 | CX_TEST_ASSERT(result == CX_JSON_FORMAT_ERROR_NUMBER); | |
392 | 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
|
393 | |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
394 | cxJsonDestroy(&json); |
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
395 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
1015 | 396 | } |
1075
0cc4b63a0ae0
fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents:
1072
diff
changeset
|
397 | cx_testing_allocator_destroy(&talloc); |
1015 | 398 | } |
399 | ||
1007
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
400 | 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
|
401 | 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
|
402 | "{\"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
|
403 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
404 | 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
|
405 | 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
|
406 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
407 | 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
|
408 | CxJson json; |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
409 | 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
|
410 | CxJsonValue *obj; |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
411 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
412 | 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
|
413 | 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
|
414 | 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
|
415 | 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
|
416 | 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
|
417 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
418 | 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
|
419 | 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
|
420 | 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
|
421 | cxJsonAsCxString(message), |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
422 | 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
|
423 | ); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
424 | 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
|
425 | 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
|
426 | 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
|
427 | 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
|
428 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
429 | cxJsonValueFree(obj); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
430 | 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
|
431 | 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
|
432 | |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
433 | cxJsonDestroy(&json); |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
434 | } |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
435 | } |
81b2986d2b04
fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents:
1002
diff
changeset
|
436 | |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
437 | 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
|
438 | cxstring text0 = cx_str( |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
439 | "{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
440 | "\t\"message\":\"success\",\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
441 | "\t\"data\":{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
442 | "\t\t\"obj\":{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
443 | "\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
|
444 | "\t\t\"}\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
445 | "\t},\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
446 | "\t\"timestamp\":1729348561,\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
447 | "}" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
448 | ); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
449 | 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
|
450 | 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
|
451 | 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
|
452 | 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
|
453 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
454 | 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
|
455 | CxJsonStatus errors[] = { |
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
456 | 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
|
457 | 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
|
458 | 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
|
459 | 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
|
460 | 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
|
461 | }; |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
462 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
463 | CX_TEST_DO { |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
464 | CxJsonStatus result; |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
465 | CxJson json; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
466 | CxJsonValue *obj = NULL; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
467 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
468 | 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
|
469 | cxJsonInit(&json, NULL); |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
470 | 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
|
471 | result = cxJsonNext(&json, &obj); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
472 | |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
473 | CX_TEST_ASSERT(result == errors[i]); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
474 | 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
|
475 | cxJsonDestroy(&json); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
476 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
477 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
478 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
479 | |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
480 | 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
|
481 | CxJson json; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
482 | CxJsonValue *d1; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
483 | 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
|
484 | CX_TEST_DO { |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
485 | cxJsonInit(&json, NULL); |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
486 | 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
|
487 | cxJsonNext(&json, &d1); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
488 | |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
489 | 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
|
490 | 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
|
491 | 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
|
492 | 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
|
493 | 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
|
494 | 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
|
495 | 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
|
496 | 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
|
497 | 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
|
498 | 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
|
499 | 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
|
500 | 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
|
501 | 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
|
502 | 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
|
503 | 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
|
504 | 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
|
505 | 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
|
506 | 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
|
507 | 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
|
508 | 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
|
509 | 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
|
510 | 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
|
511 | 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
|
512 | 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
|
513 | 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
|
514 | 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
|
515 | 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
|
516 | 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
|
517 | |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
518 | 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
|
519 | 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
|
520 | |
978
1da52af18ac3
fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
977
diff
changeset
|
521 | cxJsonValueFree(d1); |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
522 | cxJsonDestroy(&json); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
523 | } |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
524 | } |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
525 | |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
526 | CX_TEST(test_json_number) { |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
527 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
528 | cxJsonInit(&json, NULL); |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
529 | CX_TEST_DO { |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
530 | CxJsonValue *v; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
531 | CxJsonStatus result; |
1015 | 532 | |
975
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
533 | 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
|
534 | 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
|
535 | 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
|
536 | CX_TEST_ASSERT(cxJsonIsNumber(v)); |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
537 | 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
|
538 | cxJsonValueFree(v); |
1015 | 539 | |
975
96bf02371aef
adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
540 | 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
|
541 | 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
|
542 | 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
|
543 | CX_TEST_ASSERT(cxJsonIsNumber(v)); |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
544 | 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
|
545 | cxJsonValueFree(v); |
1015 | 546 | |
547 | cxJsonFill(&json, "0.815e-3 "); | |
548 | result = cxJsonNext(&json, &v); | |
549 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
550 | CX_TEST_ASSERT(cxJsonIsNumber(v)); | |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
551 | 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
|
552 | cxJsonValueFree(v); |
1015 | 553 | |
554 | cxJsonFill(&json, "1.23E4 "); | |
555 | result = cxJsonNext(&json, &v); | |
556 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
557 | CX_TEST_ASSERT(cxJsonIsNumber(v)); | |
558 | CX_TEST_ASSERT(cxJsonAsInteger(v) == 12300); | |
559 | CX_TEST_ASSERT(cxJsonAsDouble(v) == 12300.0); | |
560 | 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
|
561 | |
0a7c1bb2372d
avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents:
1057
diff
changeset
|
562 | 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
|
563 | 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
|
564 | 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
|
565 | 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
|
566 | // be as precise as possible |
1063
e453e717876e
implement floating point string to number conversions
Mike Becker <universe@uap-core.de>
parents:
1060
diff
changeset
|
567 | // 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
|
568 | 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
|
569 | cxJsonValueFree(v); |
1015 | 570 | } |
571 | cxJsonDestroy(&json); | |
572 | } | |
573 | ||
574 | CX_TEST(test_json_number_format_errors) { | |
575 | CxJson json; | |
576 | cxJsonInit(&json, NULL); | |
577 | CX_TEST_DO { | |
578 | CxJsonValue *v; | |
579 | CxJsonStatus result; | |
580 | ||
581 | cxJsonFill(&json, "+3.1415 "); | |
582 | result = cxJsonNext(&json, &v); | |
583 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
584 | "leading plus is not RFC-8259 compliant"); | |
585 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
586 | cxJsonReset(&json); | |
587 | ||
588 | cxJsonFill(&json, "0.815e-3.0 "); | |
589 | result = cxJsonNext(&json, &v); | |
590 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
591 | "exponent must be an integer"); | |
592 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
593 | cxJsonReset(&json); | |
594 | ||
595 | cxJsonFill(&json, "3.14e "); | |
596 | result = cxJsonNext(&json, &v); | |
597 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
598 | "exponent cannot be empty"); | |
599 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
600 | cxJsonReset(&json); | |
601 | ||
602 | cxJsonFill(&json, "3.14e~7 "); | |
603 | result = cxJsonNext(&json, &v); | |
604 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
605 | "exponent cannot start with bullshit"); | |
606 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
607 | cxJsonReset(&json); | |
608 | ||
609 | cxJsonFill(&json, "1.23e4f "); | |
610 | result = cxJsonNext(&json, &v); | |
611 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
612 | "non-digits in exponent"); | |
613 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
614 | cxJsonReset(&json); | |
615 | ||
616 | cxJsonFill(&json, "1.23f "); | |
617 | result = cxJsonNext(&json, &v); | |
618 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
619 | "non-digits in value"); | |
620 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
621 | cxJsonReset(&json); | |
622 | ||
623 | cxJsonFill(&json, "1.23.45 "); | |
624 | result = cxJsonNext(&json, &v); | |
625 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
626 | "multiple decimal separators"); | |
627 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
628 | cxJsonReset(&json); | |
629 | ||
630 | cxJsonFill(&json, "184467440737095516150123456789 "); | |
631 | result = cxJsonNext(&json, &v); | |
632 | CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, | |
633 | "30 digit int does not fit into 64-bit int"); | |
634 | CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); | |
635 | cxJsonReset(&json); | |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
636 | } |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
637 | cxJsonDestroy(&json); |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
638 | } |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
639 | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
640 | CX_TEST(test_json_multiple_values) { |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
641 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
642 | cxJsonInit(&json, NULL); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
643 | CX_TEST_DO { |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
644 | CxJsonValue *v; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
645 | CxJsonStatus result; |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
646 | |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
647 | // read number |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
648 | cxJsonFill(&json, "10\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
649 | 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
|
650 | 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
|
651 | CX_TEST_ASSERT(cxJsonIsNumber(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
652 | CX_TEST_ASSERT(cxJsonAsInteger(v) == 10); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
653 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
654 | // read remaining '\n' |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
655 | 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
|
656 | 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
|
657 | // read string |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
658 | cxJsonFill(&json, "\"hello world\"\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
659 | 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
|
660 | 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
|
661 | CX_TEST_ASSERT(cxJsonIsString(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
662 | 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
|
663 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
664 | // 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
|
665 | // read obj |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
666 | cxJsonFill(&json, "{ \"value\": \"test\" }\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
667 | 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
|
668 | 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
|
669 | CX_TEST_ASSERT(cxJsonIsObject(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
670 | CxJsonValue *value = cxJsonObjGet(v, "value"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
671 | CX_TEST_ASSERT(cxJsonAsString(value)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
672 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
673 | // read array |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
674 | 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
|
675 | 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
|
676 | 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
|
677 | CX_TEST_ASSERT(cxJsonIsArray(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
678 | CxJsonValue *a0 = cxJsonArrGet(v, 0); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
679 | CxJsonValue *a3 = cxJsonArrGet(v, 3); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
680 | CX_TEST_ASSERT(cxJsonIsNumber(a0)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
681 | CX_TEST_ASSERT(cxJsonAsInteger(a0) == 0); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
682 | CX_TEST_ASSERT(cxJsonIsNumber(a3)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
683 | CX_TEST_ASSERT(cxJsonAsInteger(a3) == 3); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
684 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
685 | // read literal |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
686 | cxJsonFill(&json, "true\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
687 | 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
|
688 | 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
|
689 | CX_TEST_ASSERT(cxJsonIsLiteral(v)); |
1015 | 690 | CX_TEST_ASSERT(cxJsonIsBool(v)); |
691 | CX_TEST_ASSERT(cxJsonIsTrue(v)); | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
692 | CX_TEST_ASSERT(cxJsonAsBool(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
693 | cxJsonValueFree(v); |
1015 | 694 | cxJsonFill(&json, "false\n"); |
695 | result = cxJsonNext(&json, &v); | |
696 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); | |
697 | CX_TEST_ASSERT(cxJsonIsLiteral(v)); | |
698 | CX_TEST_ASSERT(cxJsonIsBool(v)); | |
699 | CX_TEST_ASSERT(cxJsonIsFalse(v)); | |
700 | CX_TEST_ASSERT(!cxJsonAsBool(v)); | |
701 | cxJsonValueFree(v); | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
702 | cxJsonFill(&json, "null\n"); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
703 | 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
|
704 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
1015 | 705 | CX_TEST_ASSERT(cxJsonIsLiteral(v)); |
706 | CX_TEST_ASSERT(!cxJsonIsBool(v)); | |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
707 | CX_TEST_ASSERT(cxJsonIsNull(v)); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
708 | cxJsonValueFree(v); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
709 | } |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
710 | cxJsonDestroy(&json); |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
711 | } |
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
712 | |
1033
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
713 | CX_TEST(test_json_array_iterator) { |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
714 | CxJson json; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
715 | cxJsonInit(&json, NULL); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
716 | CX_TEST_DO { |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
717 | CxJsonValue *v; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
718 | CxJsonStatus result; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
719 | cxJsonFill(&json, "[ 0, 3, 6, 9, 12, 15 ]\n"); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
720 | result = cxJsonNext(&json, &v); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
721 | CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
722 | CX_TEST_ASSERT(cxJsonIsArray(v)); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
723 | CxIterator iter = cxJsonArrIter(v); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
724 | unsigned i = 0; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
725 | cx_foreach(CxJsonValue*, elem, iter) { |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
726 | CX_TEST_ASSERT(cxJsonIsNumber(elem)); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
727 | CX_TEST_ASSERT(i == cxJsonAsInteger(elem)); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
728 | i += 3; |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
729 | } |
1051
7d17bd1103d7
fix another missing free() in json tests
Mike Becker <universe@uap-core.de>
parents:
1033
diff
changeset
|
730 | cxJsonValueFree(v); |
1033
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
731 | } |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
732 | cxJsonDestroy(&json); |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
733 | } |
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
734 | |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
735 | CX_TEST(test_json_allocator) { |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
736 | CxTestingAllocator talloc; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
737 | cx_testing_allocator_init(&talloc); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
738 | CxAllocator *allocator = &talloc.base; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
739 | |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
740 | cxstring text = cx_str( |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
741 | "{\n" |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
742 | "\t\"message\":\"success\",\n" |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
743 | "\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
|
744 | "}" |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
745 | ); |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
746 | |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
747 | CX_TEST_DO { |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
748 | CxJson json; |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
749 | cxJsonInit(&json, allocator); |
996
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
750 | cxJsonFill(&json, text); |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
751 | |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
752 | CxJsonValue *obj; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
753 | 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
|
754 | 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
|
755 | 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
|
756 | |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
757 | // this recursively frees everything |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
758 | cxJsonValueFree(obj); |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
759 | cxJsonDestroy(&json); |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
760 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
761 | 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
|
762 | } |
1000
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
763 | cx_testing_allocator_destroy(&talloc); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
764 | } |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
765 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
766 | CX_TEST(test_json_allocator_parse_error) { |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
767 | CxTestingAllocator talloc; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
768 | cx_testing_allocator_init(&talloc); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
769 | CxAllocator *allocator = &talloc.base; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
770 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
771 | cxstring text = cx_str( |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
772 | "{\n" |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
773 | "\t\"message\":\"success\"\n" // <-- missing comma |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
774 | "\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
|
775 | "}" |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
776 | ); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
777 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
778 | CX_TEST_DO { |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
779 | CxJson json; |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
780 | cxJsonInit(&json, allocator); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
781 | cxJsonFill(&json, text); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
782 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
783 | CxJsonValue *obj = NULL; |
1002
1483c47063a8
add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents:
1000
diff
changeset
|
784 | 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
|
785 | 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
|
786 | 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
|
787 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
788 | // clean-up any left-over memory |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
789 | cxJsonDestroy(&json); |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
790 | |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
791 | 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
|
792 | } |
1aecddf7e209
simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents:
997
diff
changeset
|
793 | 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
|
794 | } |
333155f234c4
add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
978
diff
changeset
|
795 | |
1057
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
796 | 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
|
797 | 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
|
798 | 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
|
799 | 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
|
800 | 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
|
801 | /* |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
802 | * 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
|
803 | * |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
804 | * { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
805 | * "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
|
806 | * "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
|
807 | * "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
|
808 | * "nested": { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
809 | * "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
|
810 | * "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
|
811 | * "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
|
812 | * "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
|
813 | * } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
814 | * } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
815 | */ |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
816 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
817 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
818 | // 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
|
819 | 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
|
820 | 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
|
821 | 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
|
822 | 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
|
823 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
824 | // 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
|
825 | { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
826 | 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
|
827 | 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
|
828 | 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
|
829 | 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
|
830 | 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
|
831 | 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
|
832 | 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
|
833 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
834 | 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
|
835 | 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
|
836 | 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
|
837 | 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
|
838 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
839 | 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
|
840 | (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
|
841 | 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
|
842 | (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
|
843 | 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
|
844 | (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
|
845 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
846 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
847 | // 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
|
848 | { |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
849 | 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
|
850 | 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
|
851 | 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
|
852 | 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
|
853 | 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
|
854 | 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
|
855 | 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
|
856 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
857 | 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
|
858 | 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
|
859 | 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
|
860 | 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
|
861 | 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
|
862 | 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
|
863 | 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
|
864 | 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
|
865 | 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
|
866 | 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
|
867 | 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
|
868 | 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
|
869 | 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
|
870 | 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
|
871 | 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
|
872 | 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
|
873 | 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
|
874 | 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
|
875 | 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
|
876 | 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
|
877 | 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
|
878 | 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
|
879 | 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
|
880 | 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
|
881 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
882 | |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
883 | // 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
|
884 | 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
|
885 | 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
|
886 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
887 | 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
|
888 | } |
4e8436c3e806
add tests for JSON create functions and fix a bug in cxJsonCreateCxString()
Mike Becker <universe@uap-core.de>
parents:
1051
diff
changeset
|
889 | |
1072
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
890 | |
1081
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
891 | CX_TEST_SUBROUTINE(test_json_write_sub, |
1080
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
892 | const CxAllocator *allocator, |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
893 | cxstring expected, |
1081
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
894 | const CxJsonWriter *writer |
1080
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
895 | ) { |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
896 | // create the value |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
897 | CxJsonValue *obj = cxJsonCreateObj(allocator); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
898 | cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
899 | cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
900 | CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
901 | cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
902 | CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
903 | CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
904 | CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
905 | cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
906 | cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); |
1082
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
907 | cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); |
1080
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
908 | cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
909 | cxJsonArrAddValues(objects, obj_in_arr, 2); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
910 | cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
911 | (double[]){3.1415, 47.11, 8.15}, 3); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
912 | cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
913 | (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
914 | CxJsonValue *ints = cxJsonObjPutArr(nested, CX_STR("ints")); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
915 | cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
916 | CxJsonValue *nested_array = cxJsonCreateArr(allocator); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
917 | cxJsonArrAddValues(ints, &nested_array, 1); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
918 | cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
919 | cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
920 | |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
921 | // write it to a buffer |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
922 | CxBuffer buf; |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
923 | cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); |
1112
22dc2163fffd
add convenience macros to avoid a fptr cast for cxBufferRead and cxBufferWrite
Mike Becker <universe@uap-core.de>
parents:
1082
diff
changeset
|
924 | int result = cxJsonWrite(&buf, obj, cxBufferWriteFunc, writer); |
1080
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
925 | cxBufferTerminate(&buf); // makes debugging easier |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
926 | CX_TEST_ASSERT(result == 0); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
927 | |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
928 | // compare the string |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
929 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
930 | |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
931 | // destroy everything |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
932 | cxBufferDestroy(&buf); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
933 | cxJsonValueFree(obj); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
934 | } |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
935 | |
1081
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
936 | CX_TEST(test_json_write_default_format) { |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
937 | CxTestingAllocator talloc; |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
938 | cx_testing_allocator_init(&talloc); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
939 | CxAllocator *allocator = &talloc.base; |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
940 | CX_TEST_DO { |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
941 | // expected value |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
942 | cxstring expected = CX_STR( |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
943 | "{\"bool\":false," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
944 | "\"int\":47," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
945 | "\"nested\":{" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
946 | "\"floats\":[3.1415,47.11,8.15]," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
947 | "\"ints\":[4,8,15,[16,23],42]," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
948 | "\"literals\":[true,null,false]," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
949 | "\"objects\":[{" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
950 | "\"name1\":1," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
951 | "\"name2\":3" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
952 | "},{" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
953 | "\"name1\":3," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
954 | "\"name2\":7" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
955 | "}]" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
956 | "}," |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
957 | "\"strings\":[\"hello\",\"world\"]" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
958 | "}" |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
959 | ); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
960 | |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
961 | CxJsonWriter writer = cxJsonWriterCompact(); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
962 | CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
963 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
964 | } |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
965 | cx_testing_allocator_destroy(&talloc); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
966 | } |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
967 | |
1078
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
968 | CX_TEST(test_json_write_pretty_default_spaces) { |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
969 | CxTestingAllocator talloc; |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
970 | cx_testing_allocator_init(&talloc); |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
971 | CxAllocator *allocator = &talloc.base; |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
972 | CX_TEST_DO { |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
973 | cxstring expected = CX_STR( |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
974 | "{\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
975 | " \"bool\": false,\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
976 | " \"int\": 47,\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
977 | " \"nested\": {\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
978 | " \"floats\": [3.1415, 47.11, 8.15],\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
979 | " \"ints\": [4, 8, 15, [16, 23], 42],\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
980 | " \"literals\": [true, null, false],\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
981 | " \"objects\": [{\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
982 | " \"name1\": 1,\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
983 | " \"name2\": 3\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
984 | " }, {\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
985 | " \"name1\": 3,\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
986 | " \"name2\": 7\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
987 | " }]\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
988 | " },\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
989 | " \"strings\": [\"hello\", \"world\"]\n" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
990 | "}" |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
991 | ); |
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
992 | |
1081
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
993 | CxJsonWriter writer = cxJsonWriterPretty(true); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
994 | CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
1080
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
995 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
996 | } |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
997 | cx_testing_allocator_destroy(&talloc); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
998 | } |
1078
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
999 | |
1080
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1000 | CX_TEST(test_json_write_pretty_default_tabs) { |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1001 | CxTestingAllocator talloc; |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1002 | cx_testing_allocator_init(&talloc); |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1003 | CxAllocator *allocator = &talloc.base; |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1004 | CX_TEST_DO { |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1005 | cxstring expected = CX_STR( |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1006 | "{\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1007 | "\t\"bool\": false,\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1008 | "\t\"int\": 47,\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1009 | "\t\"nested\": {\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1010 | "\t\t\"floats\": [3.1415, 47.11, 8.15],\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1011 | "\t\t\"ints\": [4, 8, 15, [16, 23], 42],\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1012 | "\t\t\"literals\": [true, null, false],\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1013 | "\t\t\"objects\": [{\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1014 | "\t\t\t\"name1\": 1,\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1015 | "\t\t\t\"name2\": 3\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1016 | "\t\t}, {\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1017 | "\t\t\t\"name1\": 3,\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1018 | "\t\t\t\"name2\": 7\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1019 | "\t\t}]\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1020 | "\t},\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1021 | "\t\"strings\": [\"hello\", \"world\"]\n" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1022 | "}" |
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1023 | ); |
1081
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
1024 | CxJsonWriter writer = cxJsonWriterPretty(false); |
33c9d7e7d830
remove code duplication from writer tests - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1080
diff
changeset
|
1025 | CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
1072
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
1026 | 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
|
1027 | } |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
1028 | 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
|
1029 | } |
c89283cd559b
first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1063
diff
changeset
|
1030 | |
1082
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1031 | CX_TEST(test_json_write_pretty_preserve_order) { |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1032 | CxTestingAllocator talloc; |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1033 | cx_testing_allocator_init(&talloc); |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1034 | CxAllocator *allocator = &talloc.base; |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1035 | CX_TEST_DO { |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1036 | cxstring expected = CX_STR( |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1037 | "{\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1038 | " \"bool\": false,\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1039 | " \"int\": 47,\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1040 | " \"strings\": [\"hello\", \"world\"],\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1041 | " \"nested\": {\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1042 | " \"objects\": [{\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1043 | " \"name1\": 1,\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1044 | " \"name2\": 3\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1045 | " }, {\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1046 | " \"name2\": 7,\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1047 | " \"name1\": 3\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1048 | " }],\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1049 | " \"floats\": [3.1415, 47.11, 8.15],\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1050 | " \"literals\": [true, null, false],\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1051 | " \"ints\": [4, 8, 15, [16, 23], 42]\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1052 | " }\n" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1053 | "}" |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1054 | ); |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1055 | |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1056 | CxJsonWriter writer = cxJsonWriterPretty(true); |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1057 | writer.sort_members = false; |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1058 | CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1059 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1060 | } |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1061 | cx_testing_allocator_destroy(&talloc); |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1062 | } |
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1063 | |
1117
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1064 | CX_TEST(test_json_write_frac_max_digits) { |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1065 | CxJsonValue* num = cxJsonCreateNumber(NULL, 3.141592653589793); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1066 | CxJsonWriter writer = cxJsonWriterCompact(); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1067 | CxBuffer buf; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1068 | cxBufferInit(&buf, NULL, 32, NULL, 0); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1069 | CX_TEST_DO { |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1070 | // test default settings (6 digits) |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1071 | cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1117
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1072 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141592"))); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1073 | |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1074 | // test too many digits |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1075 | cxBufferReset(&buf); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1076 | writer.frac_max_digits = 200; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1077 | cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1078 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141592653589793"))); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1079 | |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1080 | // test 0 digits |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1081 | cxBufferReset(&buf); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1082 | writer.frac_max_digits = 0; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1083 | cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1084 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3"))); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1085 | |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1086 | // test 2 digits |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1087 | cxBufferReset(&buf); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1088 | writer.frac_max_digits = 2; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1089 | cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1090 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.14"))); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1091 | |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1092 | // test 3 digits |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1093 | cxBufferReset(&buf); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1094 | writer.frac_max_digits = 3; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1095 | cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1096 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141"))); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1097 | |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1098 | // test 6 digits, but two are left of the decimal point |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1099 | num->value.number = 47.110815; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1100 | cxBufferReset(&buf); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1101 | writer.frac_max_digits = 6; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1102 | cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1103 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("47.110815"))); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1104 | |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1105 | // test 4 digits with exponent |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1106 | num->value.number = 5.11223344e23; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1107 | cxBufferReset(&buf); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1108 | writer.frac_max_digits = 4; |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1109 | cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1110 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("5.1122e+23"))); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1111 | } |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1112 | cxBufferDestroy(&buf); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1113 | cxJsonValueFree(num); |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1114 | } |
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1115 | |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1116 | CX_TEST(test_json_write_string_escape) { |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1117 | /** |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1118 | * According to RFC-8259 we have to test the following characters: |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1119 | * " quotation mark |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1120 | * \ reverse solidus |
1123
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1121 | * / solidus ---> we make this optional, see test_json_write_solidus |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1122 | * b backspace |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1123 | * f form feed |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1124 | * n line feed |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1125 | * r carriage return |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1126 | * t tab |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1127 | * And all other control characters must be encoded uXXXX - in our example the bell character. |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1128 | * Also, all unicode characters are encoded that way - in our example the 'ö'. |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1129 | */ |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1130 | CxJsonValue* str = cxJsonCreateString(NULL, |
1123
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1131 | "hello\twörld\r\nthis is\\a \"string\"\b in \a string\f"); |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1132 | CxJsonWriter writer = cxJsonWriterCompact(); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1133 | CxBuffer buf; |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1134 | cxBufferInit(&buf, NULL, 128, NULL, 0); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1135 | CX_TEST_DO { |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1136 | cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1137 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
1130
5bcb725119b6
fix wrong accidental unicode escaping
Mike Becker <universe@uap-core.de>
parents:
1123
diff
changeset
|
1138 | CX_STR("\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\""))); |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1139 | } |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1140 | cxBufferDestroy(&buf); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1141 | cxJsonValueFree(str); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1142 | } |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1143 | |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1144 | CX_TEST(test_json_write_name_escape) { |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1145 | CxJsonValue* obj = cxJsonCreateObj(NULL); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1146 | cxJsonObjPutLiteral(obj, |
1123
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1147 | CX_STR("hello\twörld\r\nthis is\\a \"string\"\b in \a string\f"), CX_JSON_TRUE); |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1148 | CxJsonWriter writer = cxJsonWriterCompact(); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1149 | CxBuffer buf; |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1150 | cxBufferInit(&buf, NULL, 128, NULL, 0); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1151 | CX_TEST_DO { |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1152 | cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1153 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
1130
5bcb725119b6
fix wrong accidental unicode escaping
Mike Becker <universe@uap-core.de>
parents:
1123
diff
changeset
|
1154 | CX_STR("{\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}"))); |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1155 | } |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1156 | cxBufferDestroy(&buf); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1157 | cxJsonValueFree(obj); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1158 | } |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1159 | |
1123
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1160 | CX_TEST(test_json_write_solidus) { |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1161 | CxJsonValue* str = cxJsonCreateString(NULL,"test/solidus"); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1162 | CxJsonWriter writer = cxJsonWriterCompact(); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1163 | CxBuffer buf; |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1164 | cxBufferInit(&buf, NULL, 16, NULL, 0); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1165 | CX_TEST_DO { |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1166 | // default: do not escape |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1167 | cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1168 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("\"test/solidus\""))); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1169 | |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1170 | // enable escaping |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1171 | writer.escape_slash = true; |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1172 | cxBufferReset(&buf); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1173 | cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1174 | CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("\"test\\/solidus\""))); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1175 | } |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1176 | cxBufferDestroy(&buf); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1177 | cxJsonValueFree(str); |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1178 | } |
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1179 | |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1180 | 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
|
1181 | 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
|
1182 | |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
941
diff
changeset
|
1183 | 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
|
1184 | cx_test_register(suite, test_json_simple_object); |
1015 | 1185 | cx_test_register(suite, test_json_escaped_strings); |
1149
df5665de7344
implement unicode escape sequences in json unescape_string function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1139
diff
changeset
|
1186 | cx_test_register(suite, test_json_escaped_unicode_strings); |
1156
96f16b5a0029
add test and implementation for malformed escape sequences
Mike Becker <universe@uap-core.de>
parents:
1154
diff
changeset
|
1187 | cx_test_register(suite, test_json_escaped_unicode_malformed); |
1139
7dfa5bcf39ee
remove token_escape bool from CxJson struct
Mike Becker <universe@uap-core.de>
parents:
1130
diff
changeset
|
1188 | cx_test_register(suite, test_json_escaped_end_of_string); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
1189 | cx_test_register(suite, test_json_object_incomplete_token); |
1015 | 1190 | 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
|
1191 | 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
|
1192 | 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
|
1193 | 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
|
1194 | cx_test_register(suite, test_json_number); |
1015 | 1195 | 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
|
1196 | cx_test_register(suite, test_json_multiple_values); |
1033
e3009345984b
add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents:
1032
diff
changeset
|
1197 | 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
|
1198 | 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
|
1199 | 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
|
1200 | 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
|
1201 | cx_test_register(suite, test_json_write_default_format); |
1078
ffa8bb4e9288
implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1075
diff
changeset
|
1202 | cx_test_register(suite, test_json_write_pretty_default_spaces); |
1080
e16f4f336e3c
add test case for pretty printing with tabs - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1078
diff
changeset
|
1203 | cx_test_register(suite, test_json_write_pretty_default_tabs); |
1082
46cdc8689fc4
implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents:
1081
diff
changeset
|
1204 | cx_test_register(suite, test_json_write_pretty_preserve_order); |
1117
54df904472b0
add fractional number formatting - relates to #526
Mike Becker <universe@uap-core.de>
parents:
1112
diff
changeset
|
1205 | cx_test_register(suite, test_json_write_frac_max_digits); |
1119
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1206 | cx_test_register(suite, test_json_write_string_escape); |
ff4d7e76f85a
implement string escape - resolves #526
Mike Becker <universe@uap-core.de>
parents:
1117
diff
changeset
|
1207 | cx_test_register(suite, test_json_write_name_escape); |
1123
2b83302d595a
make escaping slashes optional - fixes #569
Mike Becker <universe@uap-core.de>
parents:
1119
diff
changeset
|
1208 | cx_test_register(suite, test_json_write_solidus); |
977
a941b01b684f
add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
975
diff
changeset
|
1209 | |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1210 | return suite; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1211 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1212 |