@@ -106,6 +106,17 @@ static const struct json_obj_descr array_array_descr[] = {
106
106
ARRAY_SIZE (array_descr )),
107
107
};
108
108
109
+ struct obj_array_2dim {
110
+ struct obj_array objects_array_array [3 ];
111
+ size_t objects_array_array_len ;
112
+ };
113
+
114
+ static const struct json_obj_descr array_2dim_descr [] = {
115
+ JSON_OBJ_DESCR_ARRAY_ARRAY (struct obj_array_2dim , objects_array_array , 3 ,
116
+ objects_array_array_len , obj_array_descr ,
117
+ ARRAY_SIZE (obj_array_descr )),
118
+ };
119
+
109
120
ZTEST (lib_json_test , test_json_encoding )
110
121
{
111
122
struct test_struct ts = {
@@ -332,7 +343,7 @@ ZTEST(lib_json_test, test_json_obj_arr_encoding)
332
343
},
333
344
.num_elements = 10 ,
334
345
};
335
- const char encoded [] = "{\"elements\":["
346
+ char encoded [] = "{\"elements\":["
336
347
"{\"name\":\"Sim\303\263n Bol\303\255var\",\"height\":168},"
337
348
"{\"name\":\"Muggsy Bogues\",\"height\":160},"
338
349
"{\"name\":\"Pel\303\251\",\"height\":173},"
@@ -479,6 +490,184 @@ ZTEST(lib_json_test, test_json_obj_arr_decoding)
479
490
}
480
491
}
481
492
493
+ ZTEST (lib_json_test , test_json_2dim_arr_obj_encoding )
494
+ {
495
+ struct obj_array_2dim obj_array_array_ts = {
496
+ .objects_array_array = {
497
+ [0 ] = {
498
+ .elements = {
499
+ [0 ] = {
500
+ .name = "Sim\303\263n Bol\303\255var" ,
501
+ .height = 168
502
+ },
503
+ [1 ] = {
504
+ .name = "Pel\303\251" ,
505
+ .height = 173
506
+ },
507
+ [2 ] = {
508
+ .name = "Usain Bolt" ,
509
+ .height = 195
510
+ },
511
+ },
512
+ .num_elements = 3
513
+ },
514
+ [1 ] = {
515
+ .elements = {
516
+ [0 ] = {
517
+ .name = "Muggsy Bogues" ,
518
+ .height = 160
519
+ },
520
+ [1 ] = {
521
+ .name = "Hakeem Olajuwon" ,
522
+ .height = 213
523
+ },
524
+ },
525
+ .num_elements = 2
526
+ },
527
+ [2 ] = {
528
+ .elements = {
529
+ [0 ] = {
530
+ .name = "Alex Honnold" ,
531
+ .height = 180
532
+ },
533
+ [1 ] = {
534
+ .name = "Hazel Findlay" ,
535
+ .height = 157
536
+ },
537
+ [2 ] = {
538
+ .name = "Daila Ojeda" ,
539
+ .height = 158
540
+ },
541
+ [3 ] = {
542
+ .name = "Albert Einstein" ,
543
+ .height = 172
544
+ },
545
+ },
546
+ .num_elements = 4
547
+ },
548
+ },
549
+ .objects_array_array_len = 3 ,
550
+ };
551
+ char encoded [] = "{\"objects_array_array\":["
552
+ "[{\"name\":\"Sim\303\263n Bol\303\255var\",\"height\":168},"
553
+ "{\"name\":\"Pel\303\251\",\"height\":173},"
554
+ "{\"name\":\"Usain Bolt\",\"height\":195}],"
555
+ "[{\"name\":\"Muggsy Bogues\",\"height\":160},"
556
+ "{\"name\":\"Hakeem Olajuwon\",\"height\":213}],"
557
+ "[{\"name\":\"Alex Honnold\",\"height\":180},"
558
+ "{\"name\":\"Hazel Findlay\",\"height\":157},"
559
+ "{\"name\":\"Daila Ojeda\",\"height\":158},"
560
+ "{\"name\":\"Albert Einstein\",\"height\":172}]"
561
+ "]}" ;
562
+ char buffer [sizeof (encoded )];
563
+ int ret ;
564
+
565
+ ret = json_obj_encode_buf (array_2dim_descr , ARRAY_SIZE (array_2dim_descr ),
566
+ & obj_array_array_ts , buffer , sizeof (buffer ));
567
+ zassert_equal (ret , 0 , "Encoding two-dimensional array returned error" );
568
+ zassert_true (!strcmp (buffer , encoded ),
569
+ "Encoded two-dimensional array is not consistent" );
570
+ }
571
+
572
+ ZTEST (lib_json_test , test_json_2dim_obj_arr_decoding )
573
+ {
574
+ struct obj_array_2dim oaa ;
575
+ char encoded [] = "{\"objects_array_array\":["
576
+ "[{\"name\":\"Sim\303\263n Bol\303\255var\",\"height\":168},"
577
+ "{\"name\":\"Pel\303\251\",\"height\":173},"
578
+ "{\"name\":\"Usain Bolt\",\"height\":195}],"
579
+ "[{\"name\":\"Muggsy Bogues\",\"height\":160},"
580
+ "{\"name\":\"Hakeem Olajuwon\",\"height\":213}],"
581
+ "[{\"name\":\"Alex Honnold\",\"height\":180},"
582
+ "{\"name\":\"Hazel Findlay\",\"height\":157},"
583
+ "{\"name\":\"Daila Ojeda\",\"height\":158},"
584
+ "{\"name\":\"Albert Einstein\",\"height\":172}]"
585
+ "]}" ;
586
+ const struct obj_array_2dim expected = {
587
+ .objects_array_array = {
588
+ [0 ] = {
589
+ .elements = {
590
+ [0 ] = {
591
+ .name = "Sim\303\263n Bol\303\255var" ,
592
+ .height = 168
593
+ },
594
+ [1 ] = {
595
+ .name = "Pel\303\251" ,
596
+ .height = 173
597
+ },
598
+ [2 ] = {
599
+ .name = "Usain Bolt" ,
600
+ .height = 195
601
+ },
602
+ },
603
+ .num_elements = 3
604
+ },
605
+ [1 ] = {
606
+ .elements = {
607
+ [0 ] = {
608
+ .name = "Muggsy Bogues" ,
609
+ .height = 160
610
+ },
611
+ [1 ] = {
612
+ .name = "Hakeem Olajuwon" ,
613
+ .height = 213
614
+ },
615
+ },
616
+ .num_elements = 2
617
+ },
618
+ [2 ] = {
619
+ .elements = {
620
+ [0 ] = {
621
+ .name = "Alex Honnold" ,
622
+ .height = 180
623
+ },
624
+ [1 ] = {
625
+ .name = "Hazel Findlay" ,
626
+ .height = 157
627
+ },
628
+ [2 ] = {
629
+ .name = "Daila Ojeda" ,
630
+ .height = 158
631
+ },
632
+ [3 ] = {
633
+ .name = "Albert Einstein" ,
634
+ .height = 172
635
+ },
636
+ },
637
+ .num_elements = 4
638
+ },
639
+ },
640
+ .objects_array_array_len = 3 ,
641
+ };
642
+ int ret ;
643
+
644
+ ret = json_obj_parse (encoded , sizeof (encoded ),
645
+ array_2dim_descr ,
646
+ ARRAY_SIZE (array_2dim_descr ),
647
+ & oaa );
648
+
649
+ zassert_equal (ret , 1 , "Array of arrays fields not decoded correctly" );
650
+ zassert_equal (oaa .objects_array_array_len , 3 ,
651
+ "Number of subarrays not decoded correctly" );
652
+ zassert_equal (oaa .objects_array_array [0 ].num_elements , 3 ,
653
+ "Number of object fields not decoded correctly" );
654
+ zassert_equal (oaa .objects_array_array [1 ].num_elements , 2 ,
655
+ "Number of object fields not decoded correctly" );
656
+ zassert_equal (oaa .objects_array_array [2 ].num_elements , 4 ,
657
+ "Number of object fields not decoded correctly" );
658
+
659
+ for (int i = 0 ; i < expected .objects_array_array_len ; i ++ ) {
660
+ for (int j = 0 ; j < expected .objects_array_array [i ].num_elements ; j ++ ) {
661
+ zassert_true (!strcmp (oaa .objects_array_array [i ].elements [j ].name ,
662
+ expected .objects_array_array [i ].elements [j ].name ),
663
+ "Element [%d][%d] name not decoded correctly" , i , j );
664
+ zassert_equal (oaa .objects_array_array [i ].elements [j ].height ,
665
+ expected .objects_array_array [i ].elements [j ].height ,
666
+ "Element [%d][%d] height not decoded correctly" , i , j );
667
+ }
668
+ }
669
+ }
670
+
482
671
struct encoding_test {
483
672
char * str ;
484
673
int result ;
0 commit comments