Advertisement
Guest User

Untitled

a guest
May 3rd, 2011
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <jansson.h>
  4.  
  5. struct foo {
  6.     int a;
  7.     double b;
  8.     const char *c;
  9. };
  10.  
  11. static const char format[] = "{s:i, s:f, s:s}";
  12.  
  13. static json_t *struct_to_json(struct foo *f) {
  14.     return json_pack(format, "a", f->a, "b", f->b, "c", f->c);
  15. }
  16.  
  17. static struct foo *json_to_struct(json_t *j) {
  18.     struct foo *ret = malloc(sizeof *ret);
  19.     json_unpack(j, format, "a", &ret->a, "b", &ret->b, "c", &ret->c);
  20.     return ret;
  21. }
  22.  
  23. int main(void) {
  24.  
  25.     struct foo f = {43, 2.2, "orange"};
  26.     json_t *j = struct_to_json(&f);
  27.     struct foo *g = json_to_struct(j);
  28.  
  29.     printf("%d %f %s\n", g->a, g->b, g->c);
  30.     json_decref(j);
  31.     free(g);
  32.  
  33.     return 1;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement