diff --git a/api.c b/api.c index b41a6ce23dad2b794ac122d4e6e53be01b0da550..a77bd1e524c6f0c03bb9af39a0599a45cf8bc2e1 100644 --- a/api.c +++ b/api.c @@ -11,7 +11,7 @@ #include "api.h" /* will allocate memory! remember to free! */ -char *get_file(const char *path) +static char *get_file(const char *path) { FILE *f; size_t len, nread; @@ -49,7 +49,7 @@ out: return NULL; } -struct json_object *path_to_obj(const char *path) +struct json_object *json_object_file_to_obj(const char *path) { struct json_object *obj; char *file_str; @@ -66,6 +66,42 @@ out: return NULL; } +int json_object_obj_to_file(struct json_object *obj, const char *path) +{ + FILE *out; + int rv = -1; + char *data; + + if (!obj) { + fprintf(stderr, "%s error: No object provided!\n", __func__); + goto out; + } + + if (!path) { + fprintf(stderr, "%s error: No file path provided!\n", __func__); + goto out; + } + + data = json_object_get_string(obj); + if (!data) { + fprintf(stderr, "%s error: Converting object to string!\n", __func__); + goto out; + } + + out = fopen (path, "ab+"); + if (out != NULL) { + if (fputs (data, out) != EOF) + goto out_fclose; + + rv = 0; + } + +out_fclose: + fclose (out); +out: + return rv; +} + int get_idx(char *key, int *idx, char *out) { char *open_brace; @@ -215,10 +251,14 @@ int add_val(struct json_object *ptr, char *key, char *val, enum json_type type) return 0; } -int set_by_string(struct json_object **src, char *fmt, char *val, enum json_type type) +int json_object_set_by_string(struct json_object **src, char *fmt, char *val, enum json_type type) +{ + return json_object_set_by_string_delimiter(src, fmt, val, type, "."); +} + +int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, char *val, enum json_type type, const char *delimiter) { struct json_object *ptr, *outer_obj, *tmp; - const char *delimiter = "."; char fmt_cpy[1024] = {0}, parsed_key[32] = {0}; char *p, *key = fmt; int idx[32]; @@ -305,9 +345,13 @@ add_key: return 0; } -int set(struct json_object *src, char *fmt, struct json_object *val) +int json_object_set(struct json_object *src, char *fmt, struct json_object *val) +{ + return json_object_set_delimiter(src, fmt, val, "."); +} + +int json_object_set_delimiter(struct json_object *src, char *fmt, struct json_object *val, const char *delimiter) { - const char *delimiter = "."; char fmt_cpy[1024] = {0}; char *p; @@ -316,7 +360,7 @@ int set(struct json_object *src, char *fmt, struct json_object *val) return -1; } - src = get(src, fmt); + src = json_object_get_by_string(src, fmt); strcpy(fmt_cpy, fmt); @@ -338,10 +382,14 @@ int set(struct json_object *src, char *fmt, struct json_object *val) return 0; } -struct json_object *get(struct json_object *src, char *fmt) +struct json_object *json_object_get_by_string(struct json_object *src, char *fmt) +{ + return json_object_get_by_string_delimiter(src, fmt, "."); +} + +struct json_object *json_object_get_by_string_delimiter(struct json_object *src, char *fmt, const char *delimiter) { struct json_object *ptr, *tmp = src; - const char *delimiter = "."; char fmt_cpy[1024] = {0}; char parsed_key[32] = {0}; int idx[32]; diff --git a/api.h b/api.h index 97377381e1b86fb2446717f30c74c13e3d2b4caa..bc9f3af586e07a0539bfa64d17db6f96df90b14a 100644 --- a/api.h +++ b/api.h @@ -8,9 +8,11 @@ #include <libubox/blobmsg_json.h> #include <json-c/json.h> -char *get_file(const char *path); -struct json_object *path_to_obj(const char *path); -int set_by_string( struct json_object **src, char *fmt, char *val, enum json_type type); -int set(struct json_object *src, char *fmt, struct json_object *val); - -struct json_object *get(struct json_object *src, char *fmt); +struct json_object *json_object_file_to_obj(const char *path); +int json_object_obj_to_file(struct json_object *obj, const char *path); +int json_object_set_by_string( struct json_object **src, char *fmt, char *val, enum json_type type); +int json_object_set_by_string_delimiter( struct json_object **src, char *fmt, char *val, enum json_type type, const char *delimiter); +int json_object_set(struct json_object *src, char *fmt, struct json_object *val); +int json_object_set_delimiter(struct json_object *src, char *fmt, struct json_object *val, const char *delimiter); +struct json_object *json_object_get_by_string(struct json_object *src, char *fmt); +struct json_object *json_object_get_by_string_delimiter(struct json_object *src, char *fmt, const char *delimiter); diff --git a/test/api_test.c b/test/api_test.c index 0fb24ee9b876537713609a9f7e7c75e1d3b2c081..a2c9b13781a5be000bbc6b2e7c7dae0a74c6bfc6 100644 --- a/test/api_test.c +++ b/test/api_test.c @@ -13,7 +13,7 @@ static void test_cfg_parse_success(void **state) { (void) state; /* unused */ - struct json_object *file = path_to_obj("test.json"); + struct json_object *file = json_object_file_to_obj("test.json"); struct json_object *obj = json_object_new_object(); struct json_object *tot = json_object_new_object(); @@ -30,7 +30,7 @@ static void test_cfg_parse_fail(void **state) { (void) state; /* unused */ - struct json_object *obj = path_to_obj("NON_EXISTENT_FILE"); + struct json_object *obj = json_object_file_to_obj("NON_EXISTENT_FILE"); assert_null(obj); if (obj) json_object_put(obj); @@ -40,10 +40,10 @@ static void test_build_from_scratch(void **state) { (void) state; /* unused */ - struct json_object *file = path_to_obj("test.json"); + struct json_object *file = json_object_file_to_obj("test.json"); struct json_object *jobj = NULL; - set_by_string(&jobj, NULL, + json_object_set_by_string(&jobj, NULL, "{\ \"test\":\"success\",\ \"nested\": { \ @@ -82,7 +82,7 @@ static void test_json_set_object(void **state) json_object_object_add(file_obj, "string", obj); //json_object_object_add(file_obj, "string", json_object_new_string("1")); - set_by_string(&modify_obj, "string", "{\"test2\":\"success\"}", json_type_object); + json_object_set_by_string(&modify_obj, "string", "{\"test2\":\"success\"}", json_type_object); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -102,7 +102,7 @@ static void test_json_set_array(void **state) json_object_object_add(file_obj, "ints", arr); //json_object_object_add(file_obj, "string", json_object_new_string("1")); - set_by_string(&modify_obj, "ints", "[ 1, 2, 3 ]", json_type_array); + json_object_set_by_string(&modify_obj, "ints", "[ 1, 2, 3 ]", json_type_array); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -131,10 +131,10 @@ static void test_json_set_multi_types(void **state) json_object_object_add(file_obj, "string", json_object_new_string("1")); json_object_object_add(file_obj, "integer", json_object_new_int(1)); - set_by_string(&modify_obj, "nested0.nested1.integer", "1", json_type_int); - set_by_string(&modify_obj, "ints", "[ 1, 2, 3 ]", json_type_array); - set_by_string(&modify_obj, "string", "1", json_type_string); - set_by_string(&modify_obj, "integer", "1", json_type_int); + json_object_set_by_string(&modify_obj, "nested0.nested1.integer", "1", json_type_int); + json_object_set_by_string(&modify_obj, "ints", "[ 1, 2, 3 ]", json_type_array); + json_object_set_by_string(&modify_obj, "string", "1", json_type_string); + json_object_set_by_string(&modify_obj, "integer", "1", json_type_int); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -163,7 +163,7 @@ static void test_json_set_multi_obj(void **state) json_object_object_add(file_obj, "string", json_object_new_string("1")); json_object_object_add(file_obj, "integer", json_object_new_int(1)); - set_by_string(&modify_obj, NULL, "{ \"nested0\": {\"nested1\": {\"integer\": 1}}, \"ints\": [1, 2, 3], \"string\":\"1\", \"integer\": 1}", json_type_object); + json_object_set_by_string(&modify_obj, NULL, "{ \"nested0\": {\"nested1\": {\"integer\": 1}}, \"ints\": [1, 2, 3], \"string\":\"1\", \"integer\": 1}", json_type_object); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -179,7 +179,7 @@ static void test_json_set_string(void **state) json_object_object_add(file_obj, "string", json_object_new_string("1")); - set_by_string(&modify_obj, "string", "1", json_type_string); + json_object_set_by_string(&modify_obj, "string", "1", json_type_string); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -195,7 +195,7 @@ static void test_json_set_overwrite_string(void **state) //struct json_object *obj = json_object_new_object(); json_object_object_add(file_obj, "test", json_object_new_string("1")); - set_by_string(&modify_obj, "test", "1", json_type_string); + json_object_set_by_string(&modify_obj, "test", "1", json_type_string); printf("file_obj = %s\n", json_object_get_string(file_obj)); printf("modify_obj = %s\n", json_object_get_string(modify_obj)); @@ -208,7 +208,7 @@ static void test_json_set_overwrite_string(void **state) json_object_object_get_ex(nested, "api", &obj); json_object_set_string(obj, "2"); - set_by_string(&modify_obj, "nested.api", "2", json_type_string); + json_object_set_by_string(&modify_obj, "nested.api", "2", json_type_string); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -224,7 +224,7 @@ static void test_json_set_int(void **state) json_object_object_add(file_obj, "integer", json_object_new_int(1)); - set_by_string(&modify_obj, "integer", "1", json_type_int); + json_object_set_by_string(&modify_obj, "integer", "1", json_type_int); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -243,7 +243,7 @@ static void test_json_set_int_nested(void **state) json_object_object_add(obj, "nested1", nested); json_object_object_add(file_obj, "nested0", obj); - set_by_string(&modify_obj, "nested0.nested1.integer", "1", json_type_int); + json_object_set_by_string(&modify_obj, "nested0.nested1.integer", "1", json_type_int); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -263,8 +263,8 @@ static void test_json_set_array_int(void **state) json_object_object_add(obj, "array", json_object_get(arr)); json_object_object_add(file_obj, "inner", obj); - set_by_string(&modify_obj, "array1[-1]", "1", json_type_int); - set_by_string(&modify_obj, "inner.array[-1]", "1", json_type_int); + json_object_set_by_string(&modify_obj, "array1[-1]", "1", json_type_int); + json_object_set_by_string(&modify_obj, "inner.array[-1]", "1", json_type_int); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -287,9 +287,9 @@ static void test_json_set_array_object(void **state) json_object_array_add(arr, json_object_get(obj)); // array will now hold two references to the same object (will double free otherwise) json_object_object_add(file_obj, "array123", arr); - set_by_string(&modify_obj, "array123[-1].integer", "1", json_type_int); - set_by_string(&modify_obj, "array123[0].string", "test", json_type_string); - set_by_string(&modify_obj, "array123[-1]", "{\"integer\": 1, \"string\":\"test\"}", json_type_object); + json_object_set_by_string(&modify_obj, "array123[-1].integer", "1", json_type_int); + json_object_set_by_string(&modify_obj, "array123[0].string", "test", json_type_string); + json_object_set_by_string(&modify_obj, "array123[-1]", "{\"integer\": 1, \"string\":\"test\"}", json_type_object); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -316,11 +316,11 @@ static void test_json_set_array_nested_object(void **state) json_object_array_add(arr, json_object_get(obj)); json_object_object_add(file_obj, "array123", arr); - set_by_string(&modify_obj, NULL, "{\"array123\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object); - set_by_string(&modify_obj, "array123[5].nested.nested1.integer", "1", json_type_int); - set_by_string(&modify_obj, "array123[-1].nested.nested1", "{\"integer\": 1}", json_type_object); - set_by_string(&modify_obj, "array123[-1].nested", "{\"nested1\": {\"integer\": 1}}", json_type_object); - set_by_string(&modify_obj, "array123[-1]", "{\"nested\": {\"nested1\": {\"integer\": 1}}}", json_type_object); + json_object_set_by_string(&modify_obj, NULL, "{\"array123\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object); + json_object_set_by_string(&modify_obj, "array123[5].nested.nested1.integer", "1", json_type_int); + json_object_set_by_string(&modify_obj, "array123[-1].nested.nested1", "{\"integer\": 1}", json_type_object); + json_object_set_by_string(&modify_obj, "array123[-1].nested", "{\"nested1\": {\"integer\": 1}}", json_type_object); + json_object_set_by_string(&modify_obj, "array123[-1]", "{\"nested\": {\"nested1\": {\"integer\": 1}}}", json_type_object); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -348,14 +348,14 @@ static void test_json_set_array_nested_array(void **state) json_object_object_add(file_obj, "array123", arr); - //set_by_string("array[0][0].test", &modify_obj, "1", json_type_int); - set_by_string(&modify_obj, "array123[0][0]", "1", json_type_int); - set_by_string(&modify_obj, "array123[0][1]", "2", json_type_int); - set_by_string(&modify_obj, "array123[0][2]", "3", json_type_int); + //json_object_set_by_string("array[0][0].test", &modify_obj, "1", json_type_int); + json_object_set_by_string(&modify_obj, "array123[0][0]", "1", json_type_int); + json_object_set_by_string(&modify_obj, "array123[0][1]", "2", json_type_int); + json_object_set_by_string(&modify_obj, "array123[0][2]", "3", json_type_int); - set_by_string(&modify_obj, "array123[1][-1]", "5", json_type_int); - set_by_string(&modify_obj, "array123[1][-1]", "6", json_type_int); - set_by_string(&modify_obj, "array123[1][-1]", "7", json_type_int); + json_object_set_by_string(&modify_obj, "array123[1][-1]", "5", json_type_int); + json_object_set_by_string(&modify_obj, "array123[1][-1]", "6", json_type_int); + json_object_set_by_string(&modify_obj, "array123[1][-1]", "7", json_type_int); printf("file_obj=%s\n", json_object_get_string(file_obj)); printf("modify_obj=%s\n", json_object_get_string(modify_obj)); @@ -366,8 +366,8 @@ static void test_json_set_array_nested_array(void **state) } static int setup (void** state) { - file_obj = path_to_obj("test.json"); - modify_obj = path_to_obj("test.json"); + file_obj = json_object_file_to_obj("test.json"); + modify_obj = json_object_file_to_obj("test.json"); return 0; } @@ -384,7 +384,7 @@ static void test_json_get_int(void **state) json_object_object_get_ex(file_obj, "integer", &tmp); - obj = get(file_obj, "integer"); + obj = json_object_get_by_string(file_obj, "integer"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -399,7 +399,7 @@ static void test_json_get_string(void **state) json_object_object_get_ex(file_obj, "test", &tmp); - obj = get(file_obj, "test"); + obj = json_object_get_by_string(file_obj, "test"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -414,7 +414,7 @@ static void test_json_get_array(void **state) json_object_object_get_ex(file_obj, "simple_array", &tmp); - obj = get(file_obj, "simple_array"); + obj = json_object_get_by_string(file_obj, "simple_array"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -429,7 +429,7 @@ static void test_json_get_object(void **state) json_object_object_get_ex(file_obj, "nested", &tmp); - obj = get(file_obj, "nested"); + obj = json_object_get_by_string(file_obj, "nested"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -445,7 +445,7 @@ static void test_json_get_nested_object(void **state) json_object_object_get_ex(file_obj, "nested", &tmp); json_object_object_get_ex(tmp, "api", &tmp); - obj = get(file_obj, "nested.api"); + obj = json_object_get_by_string(file_obj, "nested.api"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -460,7 +460,7 @@ static void test_json_get_array_int(void **state) json_object_object_get_ex(file_obj, "simple_array", &tmp); tmp = json_object_array_get_idx(tmp, 1); - obj = get(file_obj, "simple_array[1]"); + obj = json_object_get_by_string(file_obj, "simple_array[1]"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -476,7 +476,7 @@ static void test_json_get_array_nested_int(void **state) json_object_object_get_ex(file_obj, "array", &tmp); tmp = json_object_array_get_idx(tmp, 1); tmp = json_object_array_get_idx(tmp, 2); - obj = get(file_obj, "array[1][-1]"); + obj = json_object_get_by_string(file_obj, "array[1][-1]"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -493,7 +493,7 @@ static void test_json_get_object_array_nested_int(void **state) json_object_object_get_ex(tmp, "array", &tmp); tmp = json_object_array_get_idx(tmp, 1); json_object_object_get_ex(tmp, "test2", &tmp); - obj = get(file_obj, "complex_nested.array[1].test2"); + obj = json_object_get_by_string(file_obj, "complex_nested.array[1].test2"); printf("obj=%s\n", json_object_get_string(obj)); printf("tmp=%s\n", json_object_get_string(tmp)); @@ -501,6 +501,18 @@ static void test_json_get_object_array_nested_int(void **state) assert_int_equal(1, json_object_equal(tmp, obj)); } +static void test_json_write_to_file(void **state) +{ + (void) state; + struct json_object *tmp, *obj; + + json_object_obj_to_file(file_obj, "cpy.json"); + + obj = json_object_file_to_obj("cpy.json"); + + assert_int_equal(1, json_object_equal(file_obj, obj)); +} + int main(void) { const struct CMUnitTest tests[] = { //cmocka_unit_test(test_cfg_parse_success), @@ -526,6 +538,7 @@ int main(void) { cmocka_unit_test_setup_teardown(test_json_get_array_int, setup, teardown), cmocka_unit_test_setup_teardown(test_json_get_array_nested_int, setup, teardown), cmocka_unit_test_setup_teardown(test_json_get_object_array_nested_int, setup, teardown), + cmocka_unit_test_setup_teardown(test_json_write_to_file, setup, teardown), }; return cmocka_run_group_tests(tests, NULL, NULL);