diff options
Diffstat (limited to 'src/lib/json_util.c')
-rwxr-xr-x | src/lib/json_util.c | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/src/lib/json_util.c b/src/lib/json_util.c new file mode 100755 index 0000000..15194aa --- /dev/null +++ b/src/lib/json_util.c @@ -0,0 +1,222 @@ +/* + * @file json_util.c + * @brief Utility for json data, it extracts value from json + + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * This software is the confidential and proprietary information + * of Samsung Electronics, Inc. ("Confidential Information"). You + * shall not disclose such Confidential Information and shall use + * it only in accordance with the terms of the license agreement + * you entered into with Samsung. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "json_util.h" + +char *json_getString(char *data, char *key) +{ + struct json_object *inputObj; + struct json_object *bodyObj; + enum json_type type; + char *ret_buf = NULL; + const char *buf = NULL; + int i, j = 0; + + inputObj = json_tokener_parse(data); + type = json_object_get_type(inputObj); + + if (type == json_type_object) { + if (json_object_object_get_ex(inputObj, key, &bodyObj)) { + buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + ret_buf = malloc(strlen(buf) - 1); + if (ret_buf != NULL) { + memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude + for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude + if (buf[i] == '\\') + continue; + ret_buf[j++] = buf[i]; + } + } + } else + printf("ERROR : no data\n"); + } + json_object_put(inputObj); + printf("JSON>> ret_buf = %s\n", ret_buf); + + return ret_buf; +} + + +int json_getNumber(char *data, char *key) +{ + struct json_object *inputObj; + struct json_object *bodyObj; + enum json_type type; + int ret_num = 0; + + inputObj = json_tokener_parse(data); + type = json_object_get_type(inputObj); + + if (type == json_type_object) { + if (json_object_object_get_ex(inputObj, key, &bodyObj)) + ret_num = json_object_get_int(bodyObj); + else + printf("ERROR : no data\n"); + } + json_object_put(inputObj); + + return ret_num; +} + + +char *json_getObject(char *data, char *key) +{ + struct json_object *inputObj = NULL; + struct json_object *bodyObj = NULL; + const char *buf = NULL; + char *ret_buf = NULL; + + inputObj = json_tokener_parse(data); + if (inputObj != NULL) { + if (json_object_object_get_ex(inputObj, key, &bodyObj)) { + if (json_object_get_type(bodyObj) == json_type_object) { + buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + ret_buf = (char *)malloc(strlen(buf) + 1); + if (ret_buf != NULL) { + memset(ret_buf, 0x00, strlen(buf) + 1); + memcpy(ret_buf, buf, strlen(buf) + 1); + } + } + } + json_object_put(inputObj); + } + return ret_buf; +} + + +char *json_getArray(char *data, char *key) +{ + struct json_object *inputObj, *bodyObj; + enum json_type type; + const char *buf = NULL; + char *ret_buf = NULL; + + inputObj = json_tokener_parse(data); + type = json_object_get_type(inputObj); + + if (type == json_type_object) { + if (json_object_object_get_ex(inputObj, key, &bodyObj)) { + type = json_object_get_type(bodyObj); + if (type == json_type_array) { + buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + ret_buf = malloc(strlen(buf) + 1); + if (ret_buf != NULL) { + memset(ret_buf, 0x00, strlen(buf) + 1); + memcpy(ret_buf, buf, strlen(buf) + 1); + } + } + } + } + json_object_put(inputObj); + + return ret_buf; +} + +int json_getArrayLength(char *data) +{ + struct json_object *inputObj; + enum json_type type; + int cnt = 0; + + inputObj = json_tokener_parse(data); + type = json_object_get_type(inputObj); + + if (type == json_type_array) + cnt = json_object_array_length(inputObj); + + return cnt; +} + +char *json_getStringFromArray(char *data, char *arrayKey, int arrayIndex, char *key) +{ + struct json_object *inputObj; + struct json_object *bodyObj; + struct json_object *arrayObj; + struct json_object *elementObj; + char *ret_buf = NULL; + const char *buf = NULL; + int i, j = 0; + + inputObj = json_tokener_parse(data); + if (json_object_object_get_ex(inputObj, arrayKey, &arrayObj)) { + elementObj = json_object_array_get_idx(arrayObj, arrayIndex); + if (json_object_object_get_ex(elementObj, key, &bodyObj)) { + buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + ret_buf = malloc(strlen(buf) - 1); + if (ret_buf != NULL) { + memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude + for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude + if (buf[i] == '\\') + continue; + ret_buf[j++] = buf[i]; + } + } + } else + printf("ERROR : no data\n"); + + json_object_put(inputObj); + } else + printf("ERROR : not found(%s)\n", arrayKey); + + return ret_buf; +} + +double json_getDoubleFromArray(char *data, char *arrayKey, int arrayIndex, char *key) +{ + struct json_object *inputObj; + struct json_object *bodyObj; + struct json_object *arrayObj; + struct json_object *elementObj; + double ret_num = 0; + + inputObj = json_tokener_parse(data); + + if (json_object_object_get_ex(inputObj, arrayKey, &arrayObj)) { + elementObj = json_object_array_get_idx(arrayObj, arrayIndex); + if (json_object_object_get_ex(elementObj, key, &bodyObj)) + ret_num = json_object_get_double(bodyObj); + else + printf("ERROR : no data\n"); + + json_object_put(inputObj); + } else + printf("ERROR : not found(%s)\n", arrayKey); + + return ret_num; +} + +int json_getIntFromArray(char *data, char *arrayKey, int arrayIndex, char *key) +{ + struct json_object *inputObj; + struct json_object *bodyObj; + struct json_object *arrayObj; + struct json_object *elementObj; + int ret_num = 0; + + inputObj = json_tokener_parse(data); + + if (json_object_object_get_ex(inputObj, arrayKey, &arrayObj)) { + elementObj = json_object_array_get_idx(arrayObj, arrayIndex); + if (json_object_object_get_ex(elementObj, key, &bodyObj)) + ret_num = json_object_get_int(bodyObj); + else + printf("ERROR : no data\n"); + + json_object_put(inputObj); + } else + printf("ERROR : not found(%s)\n", arrayKey); + + return ret_num; +} |