@@ -27,6 +27,8 @@ struct test_struct {
27
27
int another_array [10 ]; /* JSON: "another-array" */
28
28
size_t another_array_len ;
29
29
struct test_nested xnother_nexx ; /* JSON: "4nother_ne$+" */
30
+ struct test_nested nested_obj_array [2 ];
31
+ size_t obj_array_len ;
30
32
};
31
33
32
34
struct elt {
@@ -69,6 +71,8 @@ static const struct json_obj_descr test_descr[] = {
69
71
JSON_TOK_NUMBER ),
70
72
JSON_OBJ_DESCR_OBJECT_NAMED (struct test_struct , "4nother_ne$+" ,
71
73
xnother_nexx , nested_descr ),
74
+ JSON_OBJ_DESCR_OBJ_ARRAY (struct test_struct , nested_obj_array , 2 ,
75
+ obj_array_len , nested_descr , ARRAY_SIZE (nested_descr )),
72
76
};
73
77
74
78
static const struct json_obj_descr elt_descr [] = {
@@ -146,6 +150,11 @@ ZTEST(lib_json_test, test_json_encoding)
146
150
.nested_bool = true,
147
151
.nested_string = "no escape necessary" ,
148
152
},
153
+ .nested_obj_array = {
154
+ {1 , true, "true" },
155
+ {0 , false, "false" }
156
+ },
157
+ .obj_array_len = 2
149
158
};
150
159
char encoded [] = "{\"some_string\":\"zephyr 123\uABCD\","
151
160
"\"some_int\":42,\"some_bool\":true,"
@@ -158,7 +167,10 @@ ZTEST(lib_json_test, test_json_encoding)
158
167
"\"another-array\":[2,3,5,7],"
159
168
"\"4nother_ne$+\":{\"nested_int\":1234,"
160
169
"\"nested_bool\":true,"
161
- "\"nested_string\":\"no escape necessary\"}"
170
+ "\"nested_string\":\"no escape necessary\"},"
171
+ "\"nested_obj_array\":["
172
+ "{\"nested_int\":1,\"nested_bool\":true,\"nested_string\":\"true\"},"
173
+ "{\"nested_int\":0,\"nested_bool\":false,\"nested_string\":\"false\"}]"
162
174
"}" ;
163
175
char buffer [sizeof (encoded )];
164
176
int ret ;
@@ -193,7 +205,10 @@ ZTEST(lib_json_test, test_json_decoding)
193
205
"\"another-array\":[2,3,5,7],"
194
206
"\"4nother_ne$+\":{\"nested_int\":1234,"
195
207
"\"nested_bool\":true,"
196
- "\"nested_string\":\"no escape necessary\"}"
208
+ "\"nested_string\":\"no escape necessary\"},"
209
+ "\"nested_obj_array\":["
210
+ "{\"nested_int\":1,\"nested_bool\":true,\"nested_string\":\"true\"},"
211
+ "{\"nested_int\":0,\"nested_bool\":false,\"nested_string\":\"false\"}]"
197
212
"}\n" ;
198
213
const int expected_array [] = { 11 , 22 , 33 , 45 , 299 };
199
214
const int expected_other_array [] = { 2 , 3 , 5 , 7 };
@@ -237,6 +252,20 @@ ZTEST(lib_json_test, test_json_decoding)
237
252
zassert_true (!strcmp (ts .xnother_nexx .nested_string ,
238
253
"no escape necessary" ),
239
254
"Named nested string not decoded correctly" );
255
+ zassert_equal (ts .obj_array_len , 2 ,
256
+ "Array of objects does not have correct number of items" );
257
+ zassert_equal (ts .nested_obj_array [0 ].nested_int , 1 ,
258
+ "Integer in first object array element not decoded correctly" );
259
+ zassert_equal (ts .nested_obj_array [0 ].nested_bool , true,
260
+ "Boolean value in first object array element not decoded correctly" );
261
+ zassert_true (!strcmp (ts .nested_obj_array [0 ].nested_string , "true" ),
262
+ "String in first object array element not decoded correctly" );
263
+ zassert_equal (ts .nested_obj_array [1 ].nested_int , 0 ,
264
+ "Integer in second object array element not decoded correctly" );
265
+ zassert_equal (ts .nested_obj_array [1 ].nested_bool , false,
266
+ "Boolean value in second object array element not decoded correctly" );
267
+ zassert_true (!strcmp (ts .nested_obj_array [1 ].nested_string , "false" ),
268
+ "String in second object array element not decoded correctly" );
240
269
}
241
270
242
271
ZTEST (lib_json_test , test_json_limits )
0 commit comments