Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <jansson.h>
- struct foo {
- int a;
- double b;
- const char *c;
- };
- static const char format[] = "{s:i, s:f, s:s}";
- static json_t *struct_to_json(struct foo *f) {
- return json_pack(format, "a", f->a, "b", f->b, "c", f->c);
- }
- static struct foo *json_to_struct(json_t *j) {
- struct foo *ret = malloc(sizeof *ret);
- json_unpack(j, format, "a", &ret->a, "b", &ret->b, "c", &ret->c);
- return ret;
- }
- int main(void) {
- struct foo f = {43, 2.2, "orange"};
- json_t *j = struct_to_json(&f);
- struct foo *g = json_to_struct(j);
- printf("%d %f %s\n", g->a, g->b, g->c);
- json_decref(j);
- free(g);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement