Skip to content

Commit 56234ae

Browse files
mrfuchscarlescufi
authored andcommitted
tests: lib: json: Fix and enhance test for array of arrays
Fix encoded JSON string in test_json_decoding_array_array() test so it matches the described array object and add a test case for encoding arrays of arrays. Signed-off-by: Markus Fuchs <[email protected]>
1 parent 2304331 commit 56234ae

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

tests/lib/json/src/main.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,39 @@ ZTEST(lib_json_test, test_json_limits)
255255
"Integer limits not decoded correctly");
256256
}
257257

258+
ZTEST(lib_json_test, test_json_encoding_array_array)
259+
{
260+
struct obj_array_array obj_array_array_ts = {
261+
.objects_array = {
262+
[0] = { { .name = "Sim\303\263n Bol\303\255var", .height = 168 } },
263+
[1] = { { .name = "Pel\303\251", .height = 173 } },
264+
[2] = { { .name = "Usain Bolt", .height = 195 } },
265+
},
266+
.objects_array_len = 3,
267+
};
268+
char encoded[] = "{\"objects_array\":["
269+
"{\"name\":\"Sim\303\263n Bol\303\255var\",\"height\":168},"
270+
"{\"name\":\"Pel\303\251\",\"height\":173},"
271+
"{\"name\":\"Usain Bolt\",\"height\":195}"
272+
"]}";
273+
char buffer[sizeof(encoded)];
274+
int ret;
275+
276+
ret = json_obj_encode_buf(array_array_descr, ARRAY_SIZE(array_array_descr),
277+
&obj_array_array_ts, buffer, sizeof(buffer));
278+
zassert_equal(ret, 0, "Encoding array returned error");
279+
zassert_true(!strcmp(buffer, encoded),
280+
"Encoded array of objects is not consistent");
281+
}
282+
258283
ZTEST(lib_json_test, test_json_decoding_array_array)
259284
{
260285
int ret;
261286
struct obj_array_array obj_array_array_ts;
262287
char encoded[] = "{\"objects_array\":["
263-
"[{\"height\":168,\"name\":\"Sim\303\263n Bol\303\255var\"}],"
264-
"[{\"height\":173,\"name\":\"Pel\303\251\"}],"
265-
"[{\"height\":195,\"name\":\"Usain Bolt\"}]]"
288+
"{\"height\":168,\"name\":\"Sim\303\263n Bol\303\255var\"},"
289+
"{\"height\":173,\"name\":\"Pel\303\251\"},"
290+
"{\"height\":195,\"name\":\"Usain Bolt\"}]"
266291
"}";
267292

268293
ret = json_obj_parse(encoded, sizeof(encoded),

0 commit comments

Comments
 (0)