#include <stdio.h> #include <string.h> #include <json.h> #define MAXDEPTH (128) /* compile with e.g.: gcc -Wall -O2 -g -I/usr/include/json-c -ljson-c bla.c */ char *teststr="{ \"root\": { \"status\": { \"version\": \"v1.234\" } } }"; void recurse(json_object *jo, char **path, int depth) { int type; int i; json_object_iter iter; if (depth >= MAXDEPTH) { fprintf(stderr,"MAXDEPTH reached\n"); exit(-1); } json_object_object_foreachC(jo, iter) { type = json_object_get_type(iter.val); if (type == json_type_object) { path[depth] = iter.key; recurse(iter.val, path, depth+1); } else { if (path[1]) { if ((strcmp(path[1],"status") == 0) || (strcmp(path[1],"config") == 0)) { printf("void get"); for (i = 1; i < depth; i++) { printf("_%s",path[i]); } printf("_%s(char * %s) {\n", iter.key, iter.key); printf(" strcpy(%s, \"%s\");\n", iter.key, json_object_get_string(iter.val)); printf("}\n"); } if ((strcmp(path[1],"config") == 0)) { printf("void set"); for (i = 1; i < depth; i++) { printf("_%s",path[i]); } printf("_%s(char * %s) {\n", iter.key, iter.key); printf(" //blafasel\n"); printf("}\n"); } } } } //foreach } int main () { char ** path; json_object *root; int root_type = 0; path = calloc(MAXDEPTH, sizeof(char *)); root = json_object_from_file("./simple.json"); // root = json_tokener_parse(teststr); root_type = json_object_get_type(root); if (root_type != json_type_object) { perror("no object"); free(path); json_object_put(root); exit(-1); } recurse(root, path, 0); free(path); json_object_put(root); }