@@ -22,6 +22,11 @@ struct test_struct {
22
22
struct test_nested some_nested_struct ;
23
23
int some_array [16 ];
24
24
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$+" */
25
30
};
26
31
27
32
static const struct json_obj_descr nested_descr [] = {
@@ -39,6 +44,15 @@ static const struct json_obj_descr test_descr[] = {
39
44
nested_descr ),
40
45
JSON_OBJ_DESCR_ARRAY (struct test_struct , some_array ,
41
46
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 ),
42
56
};
43
57
44
58
static void test_json_encoding (void )
@@ -57,14 +71,33 @@ static void test_json_encoding(void)
57
71
.some_array [2 ] = 8 ,
58
72
.some_array [3 ] = 16 ,
59
73
.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
+ },
61
87
};
62
88
char encoded [] = "{\"some_string\":\"zephyr 123\","
63
89
"\"some_int\":42,\"some_bool\":true,"
64
90
"\"some_nested_struct\":{\"nested_int\":-1234,"
65
91
"\"nested_bool\":false,\"nested_string\":"
66
92
"\"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
+ "}" ;
68
101
char buffer [sizeof (encoded )];
69
102
int ret ;
70
103
@@ -89,8 +122,15 @@ static void test_json_decoding(void)
89
122
"\"nested_bool\":false,\t"
90
123
"\"nested_string\":\"this should be escaped: \\t\"},"
91
124
"\"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\"}"
92
131
"}" ;
93
132
const int expected_array [] = { 11 , 22 , 33 , 45 , 299 };
133
+ const int expected_other_array [] = { 2 , 3 , 5 , 7 };
94
134
int ret ;
95
135
96
136
ret = json_obj_parse (encoded , sizeof (encoded ) - 1 , test_descr ,
@@ -114,6 +154,22 @@ static void test_json_decoding(void)
114
154
zassert_true (!memcmp (ts .some_array , expected_array ,
115
155
sizeof (expected_array )),
116
156
"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" );
117
173
}
118
174
119
175
static void test_json_invalid_unicode (void )
0 commit comments