summaryrefslogtreecommitdiff
path: root/src/lib/json_util.c
blob: 448c97269141e32fa9f453a2695d9fb02b995e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * @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;
}