Tue, 22 Oct 2024 12:16:16 +0200
fix incomplete json object not resulting in syntax error
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #include "cx/test.h" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #include "cx/json.h" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | 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
|
34 | cxstring text = cx_str( |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | "{\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | "\t\"message\":\"success\",\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | "\t\"position\":{\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | "\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
|
39 | "\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
|
40 | "\t},\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | "\t\"timestamp\":1729348561,\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | "\t\"alive\":true\n" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | "}" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | ); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | CX_TEST_DO { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | int result; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | CxJson json; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | cxJsonInit(&json); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | cxJsonFill(&json, text.ptr, text.length); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | // parse the big fat object |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | CxJsonValue *obj; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | result = cxJsonNext(&json, &obj); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | CX_TEST_ASSERT(result == 1); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | // check the contents |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | CX_TEST_ASSERT(cxJsonIsObject(obj)); |
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 | CxJsonValue *message = cxJsonObjGet(obj, "message"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | CX_TEST_ASSERT(cxJsonIsString(message)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | CX_TEST_ASSERT(0 == cx_strcmp( |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | cx_strcast(cxJsonAsString(message)), |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | cx_str("success")) |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | ); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | CxJsonValue *position = cxJsonObjGet(obj, "position"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | CX_TEST_ASSERT(cxJsonIsObject(position)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | CxJsonValue *longitude = cxJsonObjGet(position, "longitude"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | CX_TEST_ASSERT(cxJsonIsNumber(longitude)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | CX_TEST_ASSERT(cxJsonAsDouble(longitude) == -94.7099); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | CxJsonValue *latitude = cxJsonObjGet(position, "latitude"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | CX_TEST_ASSERT(cxJsonIsNumber(latitude)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | CX_TEST_ASSERT(cxJsonAsDouble(latitude) == 51.5539); |
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 *timestamp = cxJsonObjGet(obj, "timestamp"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | CX_TEST_ASSERT(cxJsonIsInteger(timestamp)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | CxJsonValue *alive = cxJsonObjGet(obj, "alive"); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | CX_TEST_ASSERT(cxJsonIsBool(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | CX_TEST_ASSERT(cxJsonIsTrue(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | CX_TEST_ASSERT(!cxJsonIsFalse(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | CX_TEST_ASSERT(cxJsonAsBool(alive)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | // this recursively frees everything else |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | cxJsonValueFree(obj); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | // 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
|
91 | result = cxJsonNext(&json, &obj); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | CX_TEST_ASSERT(result == 0); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | cxJsonDestroy(&json); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | } |
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 | |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
98 | 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
|
99 | cxstring text = cx_str( |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
100 | "{\"message\":\"success\" , \"__timestamp\":1729348561}"); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
101 | cxstring parts[16]; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
102 | 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
|
103 | 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
|
104 | 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
|
105 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
106 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
107 | CX_TEST_DO { |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
108 | int result; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
109 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
110 | CxJson json; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
111 | cxJsonInit(&json); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
112 | CxJsonValue *obj; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
113 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
114 | size_t part = 0; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
115 | while(part < nparts) { |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
116 | cxJsonFill(&json, parts[part].ptr, parts[part].length); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
117 | part++; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
118 | result = cxJsonNext(&json, &obj); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
119 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
120 | if(result != 0) { |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
121 | break; |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
122 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
123 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
124 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
125 | CX_TEST_ASSERT(result == 1); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
126 | CX_TEST_ASSERT(part == nparts); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
127 | CX_TEST_ASSERT(obj); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
128 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
129 | 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
|
130 | 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
|
131 | CX_TEST_ASSERT(0 == cx_strcmp( |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
132 | cx_strcast(cxJsonAsString(message)), |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
133 | cx_str("success")) |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
134 | ); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
135 | 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
|
136 | 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
|
137 | 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
|
138 | 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
|
139 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
140 | // 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
|
141 | cxJsonValueFree(obj); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
142 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
143 | // we only have one object that already contained all the data |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
144 | result = cxJsonNext(&json, &obj); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
145 | CX_TEST_ASSERT(result == 0); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
146 | |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
147 | cxJsonDestroy(&json); |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
148 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
149 | } |
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
150 | |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
151 | 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
|
152 | cxstring text0 = cx_str( |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
153 | "{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
154 | "\t\"message\":\"success\",\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
155 | "\t\"data\":{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
156 | "\t\t\"obj\":{\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
157 | "\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
|
158 | "\t\t\"}\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
159 | "\t},\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
160 | "\t\"timestamp\":1729348561,\n" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
161 | "}" |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
162 | ); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
163 | 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
|
164 | 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
|
165 | 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
|
166 | 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
|
167 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
168 | cxstring tests[] = { text0, text1, text2, text3, text4 }; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
169 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
170 | CX_TEST_DO { |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
171 | int result; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
172 | CxJson json; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
173 | CxJsonValue *obj = NULL; |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
174 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
175 | for(int i=0;i<5;i++) { |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
176 | cxJsonInit(&json); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
177 | cxJsonFill(&json, tests[i].ptr, tests[i].length); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
178 | result = cxJsonNext(&json, &obj); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
179 | |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
180 | CX_TEST_ASSERT(result == -1); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
181 | CX_TEST_ASSERT(obj == NULL); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
182 | cxJsonDestroy(&json); |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
183 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
184 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
185 | } |
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
186 | |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | 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
|
188 | 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
|
189 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
190 | cx_test_register(suite, test_json_simple_object); |
939
0bb7258366a0
add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
937
diff
changeset
|
191 | cx_test_register(suite, test_json_object_incomplete_token); |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
939
diff
changeset
|
192 | cx_test_register(suite, test_json_object_error); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
193 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
194 | return suite; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 |