Skip to content

Commit 5e7ef8b

Browse files
M1chacfriedt
authored andcommitted
json: fix parsing first array-array element
Previously, the first element inside the array-array didn't contain the decoded data. Signed-off-by: Michael Zimmermann <[email protected]>
1 parent ed76b91 commit 5e7ef8b

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

include/data/json.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,14 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
360360
.offset = offsetof(struct_, \
361361
len_field_), \
362362
{ \
363-
.object = { \
364-
.sub_descr = \
363+
.array = { \
364+
.element_descr = \
365365
elem_descr_, \
366-
.sub_descr_len = \
367-
elem_descr_len_, \
366+
.n_elements = \
367+
1 + \
368+
ZERO_OR_COMPILE_ERROR( \
369+
elem_descr_len_ == 1 \
370+
), \
368371
}, \
369372
}, \
370373
} }, \

lib/os/json.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,18 @@ static int arr_parse(struct json_obj *obj,
509509
{
510510
ptrdiff_t elem_size = get_elem_size(elem_descr);
511511
void *last_elem = (char *)field + elem_size * max_elements;
512-
size_t *elements = (size_t *)((char *)val + elem_descr->offset);
512+
size_t *elements = NULL;
513513
struct token value;
514514

515+
if (val) {
516+
elements = (size_t *)((char *)val + elem_descr->offset);
517+
}
518+
515519
__ASSERT_NO_MSG(elem_size > 0);
516520

517-
*elements = 0;
521+
if (elements) {
522+
*elements = 0;
523+
}
518524

519525
while (!arr_next(obj, &value)) {
520526
if (value.type == JSON_TOK_LIST_END) {
@@ -525,11 +531,13 @@ static int arr_parse(struct json_obj *obj,
525531
return -ENOSPC;
526532
}
527533

528-
if (decode_value(obj, elem_descr, &value, field, val) < 0) {
534+
if (decode_value(obj, elem_descr, &value, field, NULL) < 0) {
529535
return -EINVAL;
530536
}
531537

532-
(*elements)++;
538+
if (elements) {
539+
(*elements)++;
540+
}
533541
field = (char *)field + elem_size;
534542
}
535543

tests/lib/json/src/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,20 @@ static void test_json_decoding_array_array(void)
232232
&obj_array_array_ts);
233233

234234
zassert_equal(ret, 1, "Encoding array of object returned no errors");
235+
zassert_equal(obj_array_array_ts.objects_array_len, 3, "Array has correct number of items");
236+
237+
zassert_true(!strcmp(obj_array_array_ts.objects_array[0].objects.name,
238+
"Simón Bolívar"), "String decoded correctly");
239+
zassert_equal(obj_array_array_ts.objects_array[0].objects.height, 168,
240+
"Simón Bolívar height decoded correctly");
241+
235242
zassert_true(!strcmp(obj_array_array_ts.objects_array[1].objects.name,
236243
"Pelé"), "String decoded correctly");
244+
zassert_equal(obj_array_array_ts.objects_array[1].objects.height, 173,
245+
"Pelé height decoded correctly");
246+
247+
zassert_true(!strcmp(obj_array_array_ts.objects_array[2].objects.name,
248+
"Usain Bolt"), "String decoded correctly");
237249
zassert_equal(obj_array_array_ts.objects_array[2].objects.height, 195,
238250
"Usain Bolt height decoded correctly");
239251
}

0 commit comments

Comments
 (0)