@@ -22,6 +22,11 @@ struct test_struct {
2222 struct test_nested some_nested_struct ;
2323 int some_array [16 ];
2424 size_t some_array_len ;
25+ bool another_bxxl ; /* JSON field: "another_b!@l" */
26+ bool if_ ; /* JSON: "if" */
27+ int another_array [10 ]; /* JSON: "another-array" */
28+ size_t another_array_len ;
29+ struct test_nested xnother_nexx ; /* JSON: "4nother_ne$+" */
2530};
2631
2732static const struct json_obj_descr nested_descr [] = {
@@ -39,6 +44,15 @@ static const struct json_obj_descr test_descr[] = {
3944 nested_descr ),
4045 JSON_OBJ_DESCR_ARRAY (struct test_struct , some_array ,
4146 16 , some_array_len , JSON_TOK_NUMBER ),
47+ JSON_OBJ_DESCR_PRIM_NAMED (struct test_struct , "another_b!@l" ,
48+ another_bxxl , JSON_TOK_TRUE ),
49+ JSON_OBJ_DESCR_PRIM_NAMED (struct test_struct , "if" ,
50+ if_ , JSON_TOK_TRUE ),
51+ JSON_OBJ_DESCR_ARRAY_NAMED (struct test_struct , "another-array" ,
52+ another_array , 10 , another_array_len ,
53+ JSON_TOK_NUMBER ),
54+ JSON_OBJ_DESCR_OBJECT_NAMED (struct test_struct , "4nother_ne$+" ,
55+ xnother_nexx , nested_descr ),
4256};
4357
4458static void test_json_encoding (void )
@@ -57,14 +71,33 @@ static void test_json_encoding(void)
5771 .some_array [2 ] = 8 ,
5872 .some_array [3 ] = 16 ,
5973 .some_array [4 ] = 32 ,
60- .some_array_len = 5
74+ .some_array_len = 5 ,
75+ .another_bxxl = true,
76+ .if_ = false,
77+ .another_array [0 ] = 2 ,
78+ .another_array [1 ] = 3 ,
79+ .another_array [2 ] = 5 ,
80+ .another_array [3 ] = 7 ,
81+ .another_array_len = 4 ,
82+ .xnother_nexx = {
83+ .nested_int = 1234 ,
84+ .nested_bool = true,
85+ .nested_string = "no escape necessary" ,
86+ },
6187 };
6288 char encoded [] = "{\"some_string\":\"zephyr 123\","
6389 "\"some_int\":42,\"some_bool\":true,"
6490 "\"some_nested_struct\":{\"nested_int\":-1234,"
6591 "\"nested_bool\":false,\"nested_string\":"
6692 "\"this should be escaped: \\t\"},"
67- "\"some_array\":[1,4,8,16,32]}" ;
93+ "\"some_array\":[1,4,8,16,32],"
94+ "\"another_b!@l\":true,"
95+ "\"if\":false,"
96+ "\"another-array\":[2,3,5,7],"
97+ "\"4nother_ne$+\":{\"nested_int\":1234,"
98+ "\"nested_bool\":true,"
99+ "\"nested_string\":\"no escape necessary\"}"
100+ "}" ;
68101 char buffer [sizeof (encoded )];
69102 int ret ;
70103
@@ -89,8 +122,15 @@ static void test_json_decoding(void)
89122 "\"nested_bool\":false,\t"
90123 "\"nested_string\":\"this should be escaped: \\t\"},"
91124 "\"some_array\":[11,22, 33,\t45,\n299]"
125+ "\"another_b!@l\":true,"
126+ "\"if\":false,"
127+ "\"another-array\":[2,3,5,7],"
128+ "\"4nother_ne$+\":{\"nested_int\":1234,"
129+ "\"nested_bool\":true,"
130+ "\"nested_string\":\"no escape necessary\"}"
92131 "}" ;
93132 const int expected_array [] = { 11 , 22 , 33 , 45 , 299 };
133+ const int expected_other_array [] = { 2 , 3 , 5 , 7 };
94134 int ret ;
95135
96136 ret = json_obj_parse (encoded , sizeof (encoded ) - 1 , test_descr ,
@@ -114,6 +154,22 @@ static void test_json_decoding(void)
114154 zassert_true (!memcmp (ts .some_array , expected_array ,
115155 sizeof (expected_array )),
116156 "Array decoded with expected values" );
157+ zassert_true (ts .another_bxxl ,
158+ "Named boolean (special chars) decoded correctly" );
159+ zassert_false (ts .if_ ,
160+ "Named boolean (reserved word) decoded correctly" );
161+ zassert_equal (ts .another_array_len , 4 ,
162+ "Named array has correct number of items" );
163+ zassert_true (!memcmp (ts .another_array , expected_other_array ,
164+ sizeof (expected_other_array )),
165+ "Decoded named array with expected values" );
166+ zassert_equal (ts .xnother_nexx .nested_int , 1234 ,
167+ "Named nested integer decoded correctly" );
168+ zassert_equal (ts .xnother_nexx .nested_bool , true,
169+ "Named nested boolean decoded correctly" );
170+ zassert_true (!strcmp (ts .xnother_nexx .nested_string ,
171+ "no escape necessary" ),
172+ "Named nested string decoded correctly" );
117173}
118174
119175static void test_json_invalid_unicode (void )
0 commit comments