int main(int argc, char **argv) { MC_SET_DEBUG(1); /* * Check that replacing an existing object keeps the key valid, * and that it keeps the order the same. */ json_object *my_object = json_object_new_object(); json_object_object_add(my_object, "foo1", json_object_new_string("bar1")); json_object_object_add(my_object, "foo2", json_object_new_string("bar2")); json_object_object_add(my_object, "deleteme", json_object_new_string("bar2")); json_object_object_add(my_object, "foo3", json_object_new_string("bar3")); printf("==== delete-in-loop test starting ====\n"); int orig_count = 0; json_object_object_foreach(my_object, key0, val0) { printf("Key at index %d is [%s]", orig_count, key0); if (strcmp(key0, "deleteme") == 0) { json_object_object_del(my_object, key0); printf(" (deleted)\n"); } else printf(" (kept)\n"); orig_count++; }
/** ** Function use to read config from the config file (config.json) **/ Device getConfig(){ // init a device Device device; // init three json_object json_object *root, *username, *password; #ifdef DEBUG MC_SET_DEBUG(1); #endif // parse the file and get the root node root = json_object_from_file(CONFIG_FILE_PATH); // get the username field username = json_object_object_get(root,"username"); // get the password field password = json_object_object_get(root,"password"); // fill in the device fields device.username = strdup(json_object_get_string(username)); device.password = strdup(json_object_get_string(password)); return device; }
/** ** Function use to read response from the server **/ ServerResponse getResponse(char* resp){ // init a ServerResponse ServerResponse serverResponse ; // init four json_object json_object *root, *msg, *error, *status; #ifdef DEBUG MC_SET_DEBUG(1); #endif // parse the file and get the root node root = json_tokener_parse(resp); // get the msg field msg = json_object_object_get(root,"msg"); // get the error field error = json_object_object_get(root,"error"); // get the status field status = json_object_object_get(root,"status"); // fill in the serverResponse fields serverResponse.msg = strdup(json_object_get_string(msg)); serverResponse.error = strdup(json_object_get_string(error)); serverResponse.status = strdup(json_object_get_string(status)); return serverResponse; }
int test_jsonc() { json_tokener *tok; json_object *my_string, *my_int, *my_object, *my_array; json_object *new_obj; int i; MC_SET_DEBUG(1); my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("\\"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object_object_add(my_object, "baz", json_object_new_string("fark")); json_object_object_del(my_object, "baz"); /*json_object_object_add(my_object, "arr", my_array);*/ printf("my_object=\n"); json_object_object_foreach(my_object, key, val) { printf("\t%s: %s\n", key, json_object_to_json_string(val)); }
int main(int argc, char **argv) { MC_SET_DEBUG(1); test_basic_parse(); printf("==================================\n"); test_verbose_parse(); printf("==================================\n"); test_incremental_parse(); printf("==================================\n"); }
int main(int argc, char **argv) { json_object *new_obj; #ifdef HAVE_SETLOCALE setlocale(LC_NUMERIC, "de_DE"); #else printf("No locale\n"); #endif MC_SET_DEBUG(1); new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); printf("new_obj.to_string()=%s\n", json_object_to_json_string_ext(new_obj,JSON_C_TO_STRING_NOZERO)); json_object_put(new_obj); }
int main(int argc, char **argv) { json_object *my_object; MC_SET_DEBUG(1); printf("Test setting, then resetting a custom serializer:\n"); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object)); struct myinfo userdata = { .value = 123 }; json_object_set_serializer(my_object, custom_serializer, &userdata, freeit); printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object)); printf("Next line of output should be from the custom freeit function:\n"); freeit_was_called = 0; json_object_set_serializer(my_object, NULL, NULL, NULL); assert(freeit_was_called); printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object)); json_object_put(my_object); // ============================================ my_object = json_object_new_object(); printf("Check that the custom serializer isn't free'd until the last json_object_put:\n"); json_object_set_serializer(my_object, custom_serializer, &userdata, freeit); json_object_get(my_object); json_object_put(my_object); printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object)); printf("Next line of output should be from the custom freeit function:\n"); freeit_was_called = 0; json_object_put(my_object); assert(freeit_was_called); return 0; }
int main(int argc, char **argv) { json_object *new_obj; #ifdef TEST_FORMATTED int sflags = 0; #endif MC_SET_DEBUG(1); #ifdef TEST_FORMATTED sflags = parse_flags(argc, argv); #endif new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); return 0; }
int main(int argc, char **argv) { json_object *my_string, *my_int, *my_object, *my_array; int i; #ifdef TEST_FORMATTED int sflags = 0; #endif MC_SET_DEBUG(1); #ifdef TEST_FORMATTED sflags = parse_flags(argc, argv); #endif my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("\\"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); json_object_put(my_array); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_put_idx(my_array, 4, json_object_new_int(0)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); json_object_array_sort(my_array, sort_fn); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object *baz_obj = json_object_new_string("fark"); json_object_get(baz_obj); json_object_object_add(my_object, "baz", baz_obj); json_object_object_del(my_object, "baz"); /* baz_obj should still be valid */ printf("baz_obj.to_string()=%s\n", json_object_to_json_string(baz_obj)); json_object_put(baz_obj); /*json_object_object_add(my_object, "arr", my_array);*/ printf("my_object=\n"); json_object_object_foreach(my_object, key, val) { printf("\t%s: %s\n", key, json_object_to_json_string(val)); }
int main(int argc, char **argv) { json_tokener *tok; json_object *my_string, *my_int, *my_object, *my_array; json_object *new_obj; int i; MC_SET_DEBUG(1); my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("\\"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object_object_add(my_object, "baz", json_object_new_string("fark")); json_object_object_del(my_object, "baz"); /*json_object_object_add(my_object, "arr", my_array);*/ /*printf("my_object=\n"); json_object_object_foreach(my_object, key, val) { printf("\t%s: %s\n", key, json_object_to_json_string(val)); } printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object)); */ new_obj = json_tokener_parse("\"\003\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("/* hello */\"foo\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("// hello\n\"foo\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("null"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("True"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("12"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("12.3"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[\"\\n\"]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[\"\\nabc\\n\"]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[null]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[false]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{}"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\": \"bar\" }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); /* enum json_tokener_error error = json_tokener_success; new_obj = json_tokener_parse_verbose("{ foo }", &error); assert (error == json_tokener_error_parse_object_key_name); assert (new_obj == NULL); new_obj = json_tokener_parse("{ foo }"); assert (new_obj == NULL); // if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse("foo"); assert (new_obj == NULL); new_obj = json_tokener_parse_verbose("foo", &error); assert (new_obj == NULL); assert (error == json_tokener_error_parse_boolean); */ new_obj = json_tokener_parse("{ \"foo"); if(is_error(new_obj)) printf("got error as expected\n"); /* test incremental parsing */ tok = json_tokener_new(); new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse_ex(tok, "\":13}}", 6); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); json_tokener_free(tok); json_object_put(my_string); json_object_put(my_int); json_object_put(my_object); json_object_put(my_array); return 0; }