@@ -348,6 +348,28 @@ static const struct json_obj_descr enums_descr[] = {
348
348
JSON_OBJ_DESCR_PRIM (struct test_enums , u32 , JSON_TOK_UINT ),
349
349
};
350
350
351
+ struct test_mixed_arr {
352
+ const char * msg_type ;
353
+ uint64_t dev_id ;
354
+ struct test_nested nested ;
355
+ int arr [3 ];
356
+ size_t arr_len ;
357
+ char status_buf [10 ];
358
+ size_t count ;
359
+ };
360
+
361
+ static const struct json_obj_descr test_mixed_arr_descr_arr [] = {
362
+ JSON_OBJ_DESCR_ARRAY (struct test_mixed_arr , arr , 3 , arr_len , JSON_TOK_NUMBER ),
363
+ };
364
+
365
+ static const struct json_mixed_arr_descr test_mixed_arr_descr [] = {
366
+ JSON_MIXED_ARR_DESCR_PRIM (struct test_mixed_arr , msg_type , JSON_TOK_STRING , count ),
367
+ JSON_MIXED_ARR_DESCR_PRIM (struct test_mixed_arr , dev_id , JSON_TOK_UINT64 , count ),
368
+ JSON_MIXED_ARR_DESCR_OBJECT (struct test_mixed_arr , nested , nested_descr , count ),
369
+ JSON_MIXED_ARR_DESCR_ARRAY (struct test_mixed_arr , arr , 3 , test_mixed_arr_descr_arr , count ),
370
+ JSON_MIXED_ARR_DESCR_PRIM (struct test_mixed_arr , status_buf , JSON_TOK_STRING_BUF , count ),
371
+ };
372
+
351
373
352
374
ZTEST (lib_json_test , test_json_encoding )
353
375
{
@@ -2198,4 +2220,110 @@ ZTEST(lib_json_test, test_json_string_nullptr)
2198
2220
zassert_str_equal (ts .some_string , "" , "String not decoded correctly" );
2199
2221
}
2200
2222
2223
+ ZTEST (lib_json_test , test_json_mixed_arr_parse )
2224
+ {
2225
+ struct test_mixed_arr arr = {0 };
2226
+ char json [] = "[\"msg\", 123456, {\"nested_int\":42,\"nested_bool\":true,"
2227
+ "\"nested_string\":\"abc\","
2228
+ "\"nested_string_buf\":\"buf\",\"nested_int8\":1,\"nested_uint8\":2,"
2229
+ "\"nested_int64\":3,\"nested_uint64\":4}, [10,20,30], \"ok\"]" ;
2230
+ int ret = json_mixed_arr_parse (json , strlen (json ),
2231
+ test_mixed_arr_descr ,
2232
+ ARRAY_SIZE (test_mixed_arr_descr ), & arr );
2233
+
2234
+ zassert_equal (ret , 5 , "Should parse 5 elements" );
2235
+ zassert_str_equal (arr .msg_type , "msg" , NULL );
2236
+ zassert_equal (arr .dev_id , 123456 , NULL );
2237
+ zassert_equal (arr .nested .nested_int , 42 , NULL );
2238
+ zassert_equal (arr .arr_len , 3 , NULL );
2239
+ zassert_equal (arr .arr [0 ], 10 , NULL );
2240
+ zassert_equal (arr .arr [1 ], 20 , NULL );
2241
+ zassert_equal (arr .arr [2 ], 30 , NULL );
2242
+ zassert_str_equal (arr .status_buf , "ok" , NULL );
2243
+ }
2244
+
2245
+ ZTEST (lib_json_test , test_json_mixed_arr_encode )
2246
+ {
2247
+ char buf [256 ];
2248
+ struct test_mixed_arr pkt = {0 };
2249
+ struct test_mixed_arr arr = {
2250
+ .msg_type = "msg" ,
2251
+ .dev_id = 123456 ,
2252
+ .nested = {
2253
+ .nested_int = 42 ,
2254
+ .nested_bool = true,
2255
+ .nested_string = "abc" ,
2256
+ .nested_string_buf = "buf" ,
2257
+ .nested_int8 = 1 ,
2258
+ .nested_uint8 = 2 ,
2259
+ .nested_int64 = 3 ,
2260
+ .nested_uint64 = 4 ,
2261
+ },
2262
+ .arr = {10 , 20 , 30 },
2263
+ .arr_len = 3 ,
2264
+ .status_buf = "ok" ,
2265
+ .count = 5
2266
+ };
2267
+ int ret = json_mixed_arr_encode_buf (test_mixed_arr_descr ,
2268
+ ARRAY_SIZE (test_mixed_arr_descr ),
2269
+ & arr , buf , sizeof (buf ));
2270
+
2271
+ zassert_equal (ret , 0 , NULL );
2272
+
2273
+ ret = json_mixed_arr_parse (buf , strlen (buf ),
2274
+ test_mixed_arr_descr ,
2275
+ ARRAY_SIZE (test_mixed_arr_descr ), & pkt );
2276
+
2277
+ zassert_equal (ret , 5 , NULL );
2278
+ zassert_str_equal (pkt .msg_type , "msg" , NULL );
2279
+ zassert_equal (pkt .dev_id , 123456 , NULL );
2280
+ zassert_equal (pkt .arr [0 ], 10 , NULL );
2281
+ zassert_str_equal (pkt .status_buf , "ok" , NULL );
2282
+ }
2283
+
2284
+ ZTEST (lib_json_test , test_json_mixed_arr_empty )
2285
+ {
2286
+ struct test_mixed_arr arr = {0 };
2287
+ char json [] = "[]" ;
2288
+ int ret = json_mixed_arr_parse (json , strlen (json ),
2289
+ test_mixed_arr_descr , 0 , & arr );
2290
+
2291
+ zassert_equal (ret , 0 , NULL );
2292
+ }
2293
+
2294
+ ZTEST (lib_json_test , test_json_mixed_arr_calc_len )
2295
+ {
2296
+ char buf [256 ];
2297
+ ssize_t calc_len ;
2298
+ int ret ;
2299
+ struct test_mixed_arr arr = {
2300
+ .msg_type = "msg" ,
2301
+ .dev_id = 123456 ,
2302
+ .nested = {
2303
+ .nested_int = 42 ,
2304
+ .nested_bool = true,
2305
+ .nested_string = "abc" ,
2306
+ .nested_string_buf = "buf" ,
2307
+ .nested_int8 = 1 ,
2308
+ .nested_uint8 = 2 ,
2309
+ .nested_int64 = 3 ,
2310
+ .nested_uint64 = 4 ,
2311
+ },
2312
+ .arr = {10 , 20 , 30 },
2313
+ .arr_len = 3 ,
2314
+ .status_buf = "ok" ,
2315
+ .count = 5
2316
+ };
2317
+
2318
+ calc_len = json_calc_mixed_arr_len (test_mixed_arr_descr ,
2319
+ ARRAY_SIZE (test_mixed_arr_descr ),
2320
+ & arr );
2321
+ ret = json_mixed_arr_encode_buf (test_mixed_arr_descr ,
2322
+ ARRAY_SIZE (test_mixed_arr_descr ),
2323
+ & arr , buf , sizeof (buf ));
2324
+ zassert_equal (ret , 0 , NULL );
2325
+
2326
+ zassert_equal (calc_len , (ssize_t )strlen (buf ), "Length mismatch" );
2327
+ }
2328
+
2201
2329
ZTEST_SUITE (lib_json_test , NULL , NULL , NULL , NULL , NULL );
0 commit comments