Welcome to mirror list, hosted at ThFree Co, Russian Federation.

jsonobj.c « zbxjson « libs « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2a7c365284cb5b7959b7659c35cbf919f1777f10 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "jsonobj.h"

#include "json_parser.h"
#include "json.h"
#include "zbxstr.h"

ZBX_PTR_VECTOR_IMPL(jsonobj_ptr, zbx_jsonobj_t *)
ZBX_VECTOR_IMPL(jsonobj_ref, zbx_jsonobj_ref_t)

/* jsonobject index hashset support */

static zbx_hash_t	jsonobj_index_el_hash(const void *v)
{
	const zbx_jsonobj_index_el_t	*el = (const zbx_jsonobj_index_el_t *)v;

	return ZBX_DEFAULT_STRING_HASH_FUNC(el->value);
}

static int	jsonobj_index_el_compare(const void *v1, const void *v2)
{
	const zbx_jsonobj_index_el_t	*el1 = (const zbx_jsonobj_index_el_t *)v1;
	const zbx_jsonobj_index_el_t	*el2 = (const zbx_jsonobj_index_el_t *)v2;

	return strcmp(el1->value, el2->value);
}

/* jsonobject values hashset support */

static zbx_hash_t	jsonobj_el_hash(const void *v)
{
	const zbx_jsonobj_el_t	*el = (const zbx_jsonobj_el_t *)v;

	return ZBX_DEFAULT_STRING_HASH_FUNC(el->name);
}

static int	jsonobj_el_compare(const void *v1, const void *v2)
{
	const zbx_jsonobj_el_t	*el1 = (const zbx_jsonobj_el_t *)v1;
	const zbx_jsonobj_el_t	*el2 = (const zbx_jsonobj_el_t *)v2;

	return strcmp(el1->name, el2->name);
}

/******************************************************************************
 *                                                                            *
 * Purpose: initialize json object structure                                  *
 *                                                                            *
 * Parameters: obj  - [IN/OUT] the json object to initialize                  *
 *             type - [IN] the json object type                               *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_init(zbx_jsonobj_t *obj, zbx_json_type_t type)
{
	obj->type = type;

	switch (type)
	{
		case ZBX_JSON_TYPE_ARRAY:
			zbx_vector_jsonobj_ptr_create(&obj->data.array);
			break;
		case ZBX_JSON_TYPE_OBJECT:
			zbx_hashset_create(&obj->data.object, 0, jsonobj_el_hash, jsonobj_el_compare);
			break;
		default:
			memset(&obj->data, 0, sizeof(obj->data));
			break;
	}

	obj->index = NULL;
}

/******************************************************************************
 *                                                                            *
 * Purpose: free resources allocated by json object index element             *
 *                                                                            *
 * Parameters: v  - [IN] the json index element                               *
 *                                                                            *
 ******************************************************************************/
static void	jsonobj_index_el_clear(void *v)
{
	zbx_jsonobj_index_el_t	*el = (zbx_jsonobj_index_el_t *)v;
	int			i;

	zbx_free(el->value);
	for (i = 0; i < el->objects.values_num; i++)
	{
		zbx_free(el->objects.values[i].name);

		if (0 != el->objects.values[i].external)
		{
			zbx_jsonobj_clear(el->objects.values[i].value);
			zbx_free(el->objects.values[i].value);
		}
	}

	zbx_vector_jsonobj_ref_destroy(&el->objects);
}

/******************************************************************************
 *                                                                            *
 * Purpose: initialize json object index                                      *
 *                                                                            *
 * Parameters: obj  - [IN/OUT] the json object                                *
 *             path - [IN] the indexed relative path                          *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_init_index(zbx_jsonobj_t *obj, const char *path)
{
	obj->index = (zbx_jsonobj_index_t *)zbx_malloc(NULL, sizeof(zbx_jsonobj_index_t));
	obj->index->path = zbx_strdup(NULL, path);
	zbx_hashset_create_ext(&obj->index->objects, 0, jsonobj_index_el_hash, jsonobj_index_el_compare,
			jsonobj_index_el_clear, ZBX_DEFAULT_MEM_MALLOC_FUNC, ZBX_DEFAULT_MEM_REALLOC_FUNC,
			ZBX_DEFAULT_MEM_FREE_FUNC);
}

/******************************************************************************
 *                                                                            *
 * Purpose: set string value to json object                                   *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_set_string(zbx_jsonobj_t *obj, char *str)
{
	obj->type = ZBX_JSON_TYPE_STRING;
	obj->data.string = str;
}

/******************************************************************************
 *                                                                            *
 * Purpose: set numeric value to json object                                  *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_set_number(zbx_jsonobj_t *obj, double number)
{
	obj->type = ZBX_JSON_TYPE_NUMBER;
	obj->data.number = number;
}

/******************************************************************************
 *                                                                            *
 * Purpose: set true value to json object                                     *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_set_true(zbx_jsonobj_t *obj)
{
	obj->type = ZBX_JSON_TYPE_TRUE;
}

/******************************************************************************
 *                                                                            *
 * Purpose: set false value to json object                                    *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_set_false(zbx_jsonobj_t *obj)
{
	obj->type = ZBX_JSON_TYPE_FALSE;
}

/******************************************************************************
 *                                                                            *
 * Purpose: set null value to json object                                     *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_set_null(zbx_jsonobj_t *obj)
{
	obj->type = ZBX_JSON_TYPE_NULL;
}

/******************************************************************************
 *                                                                            *
 * Purpose: initialize json object element                                    *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_el_init(zbx_jsonobj_el_t *el)
{
	el->name = NULL;
	jsonobj_init(&el->value, ZBX_JSON_TYPE_UNKNOWN);
}

/******************************************************************************
 *                                                                            *
 * Purpose: free resources allocated by json object element                   *
 *                                                                            *
 ******************************************************************************/
void	jsonobj_el_clear(zbx_jsonobj_el_t *el)
{
	zbx_free(el->name);
	zbx_jsonobj_clear(&el->value);
}

/******************************************************************************
 *                                                                            *
 * Purpose: free json object                                                  *
 *                                                                            *
 ******************************************************************************/
static void	jsonobj_free(zbx_jsonobj_t *obj)
{
	zbx_jsonobj_clear(obj);
	zbx_free(obj);
}

/******************************************************************************
 *                                                                            *
 * Purpose: free resources allocated by json object                           *
 *                                                                            *
 ******************************************************************************/
void	zbx_jsonobj_clear(zbx_jsonobj_t *obj)
{
	zbx_jsonobj_el_t	*el;
	zbx_hashset_iter_t	iter;

	switch (obj->type)
	{
		case ZBX_JSON_TYPE_STRING:
			zbx_free(obj->data.string);
			break;
		case ZBX_JSON_TYPE_ARRAY:
			zbx_vector_jsonobj_ptr_clear_ext(&obj->data.array, jsonobj_free);
			zbx_vector_jsonobj_ptr_destroy(&obj->data.array);
			break;
		case ZBX_JSON_TYPE_OBJECT:
			zbx_hashset_iter_reset(&obj->data.object, &iter);
			while (NULL != (el = (zbx_jsonobj_el_t *)zbx_hashset_iter_next(&iter)))
			{
				zbx_free(el->name);
				zbx_jsonobj_clear(&el->value);
			}
			zbx_hashset_destroy(&obj->data.object);
			break;
		default:
			break;
	}

	if (NULL != obj->index)
	{
		zbx_free(obj->index->path);
		zbx_hashset_destroy(&obj->index->objects);
		zbx_free(obj->index);
	}
}

/******************************************************************************
 *                                                                            *
 * Purpose: convert json object to text format                                *
 *                                                                            *
 ******************************************************************************/
int	zbx_jsonobj_to_string(char **str, size_t *str_alloc, size_t *str_offset, zbx_jsonobj_t *obj)
{
	char			*tmp, buf[32], delim;
	int			i;
	zbx_hashset_iter_t	iter;
	zbx_jsonobj_el_t	*el;

	switch (obj->type)
	{
		case ZBX_JSON_TYPE_TRUE:
			zbx_strcpy_alloc(str, str_alloc, str_offset, "true");
			break;
		case ZBX_JSON_TYPE_FALSE:
			zbx_strcpy_alloc(str, str_alloc, str_offset, "false");
			break;
		case ZBX_JSON_TYPE_NULL:
			zbx_strcpy_alloc(str, str_alloc, str_offset, "null");
			break;
		case ZBX_JSON_TYPE_STRING:
			tmp = zbx_strdup(NULL, obj->data.string);
			zbx_json_escape(&tmp);
			zbx_snprintf_alloc(str, str_alloc, str_offset, "\"%s\"", tmp);
			zbx_free(tmp);
			break;
		case ZBX_JSON_TYPE_NUMBER:
			zbx_print_double(buf, sizeof(buf), obj->data.number);
			zbx_strcpy_alloc(str, str_alloc, str_offset, buf);
			break;
		case ZBX_JSON_TYPE_ARRAY:
			delim = '[';
			for (i = 0; i < obj->data.array.values_num; i++)
			{
				zbx_chrcpy_alloc(str, str_alloc, str_offset, delim);
				delim = ',';
				zbx_jsonobj_to_string(str, str_alloc, str_offset, obj->data.array.values[i]);
			}
			zbx_chrcpy_alloc(str, str_alloc, str_offset, ']');
			break;
		case ZBX_JSON_TYPE_OBJECT:
			delim = '{';
			zbx_hashset_iter_reset(&obj->data.object, &iter);
			while (NULL != (el = (zbx_jsonobj_el_t *)zbx_hashset_iter_next(&iter)))
			{
				zbx_chrcpy_alloc(str, str_alloc, str_offset, delim);
				delim = ',';

				tmp = zbx_strdup(NULL, el->name);
				zbx_json_escape(&tmp);
				zbx_snprintf_alloc(str, str_alloc, str_offset, "\"%s\"", tmp);
				zbx_chrcpy_alloc(str, str_alloc, str_offset, ':');
				zbx_free(tmp);

				zbx_jsonobj_to_string(str, str_alloc, str_offset, &el->value);
			}
			zbx_chrcpy_alloc(str, str_alloc, str_offset, '}');
			break;
		default:
			zbx_set_json_strerror("unknown json object with type: %u", obj->type);
			return FAIL;
	}

	return SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * Purpose: parses json formatted data into json object structure             *
 *                                                                            *
 ******************************************************************************/
int	zbx_jsonobj_open(const char *data, zbx_jsonobj_t *obj)
{
	int	ret = FAIL;
	char	*error = NULL;

	switch (*data)
	{
		case '{':
			if (0 == json_parse_object(data, obj, &error))
				goto out;
			break;
		case '[':
			if (0 == json_parse_array(data, obj, &error))
				goto out;
			break;
		default:
			/* not json data, failing */
			jsonobj_init(obj, ZBX_JSON_TYPE_UNKNOWN);
			(void)json_error("invalid object format, expected opening character '{' or '['", data, &error);
			goto out;
	}

	ret = SUCCEED;
out:
	if (FAIL == ret)
	{
		zbx_jsonobj_clear(obj);
		zbx_set_json_strerror("%s", error);
		zbx_free(error);
	}

	return ret;
}