Created
April 21, 2020 20:16
-
-
Save pabigot/f33f471cc9498f6e98c157deb5d227b2 to your computer and use it in GitHub Desktop.
zephyr issue 24372
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <zephyr.h> | |
#include <sys/printk.h> | |
#include <data/json.h> | |
typedef struct { | |
char *id; | |
uint32_t d; | |
uint8_t t; | |
} data_first; | |
typedef struct { | |
char *id; | |
uint32_t d; | |
} data_second; | |
typedef struct { | |
data_first t_0[4]; | |
size_t data_first_size; | |
data_second t_1[2]; | |
size_t data_second_size; | |
} message; | |
const struct json_obj_descr data_first_json[] = { | |
JSON_OBJ_DESCR_PRIM(data_first, id, JSON_TOK_STRING), | |
JSON_OBJ_DESCR_PRIM(data_first, d, JSON_TOK_NUMBER), | |
JSON_OBJ_DESCR_PRIM(data_first, t, JSON_TOK_NUMBER) | |
}; | |
const struct json_obj_descr data_second_json[] = { | |
JSON_OBJ_DESCR_PRIM(data_first, id, JSON_TOK_STRING), | |
JSON_OBJ_DESCR_PRIM(data_first, d, JSON_TOK_NUMBER) | |
}; | |
const struct json_obj_descr message_json[] = { | |
JSON_OBJ_DESCR_OBJ_ARRAY(message, t_0, 4, | |
data_first_size, data_first_json, | |
ARRAY_SIZE(data_first_json)), | |
JSON_OBJ_DESCR_OBJ_ARRAY(message, t_1, 2, | |
data_second_size, data_second_json, | |
ARRAY_SIZE(data_second_json)), | |
}; | |
u8_t buf[1024]; | |
void main(void) | |
{ | |
message m= { | |
.t_0[0] = { | |
.id = "t01", | |
.d = 20, | |
.t= 1 | |
}, | |
.t_0[1] = { | |
.id = "t02", | |
.d= 10, | |
.t= 1 | |
}, | |
.t_0[2] = { | |
.id = "t03", | |
.d= 15, | |
.t= 0 | |
}, | |
.t_0[3] = { | |
.id = "t04", | |
.d = 50, | |
.t = 1 | |
}, | |
.data_first_size= 4, | |
.t_1[0] = { | |
.id = "t11", | |
.d = 10 | |
}, | |
.t_1[1] = { | |
.id = "t12", | |
.d= 20 | |
}, | |
.data_second_size = 2 | |
}; | |
int rc = json_obj_encode_buf(message_json, ARRAY_SIZE(message_json), | |
&m, | |
buf, sizeof(buf)); | |
if (rc >= 0) { | |
printk("enc:\n%s\n", buf); | |
} else { | |
printk("failed: %d\n", rc); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment