Greenbone Vulnerability Management Libraries 22.18.1
json.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "json.h"
7
16gchar *
17gvm_json_string_escape (const char *string, gboolean single_quote)
18{
19 gchar *point;
20 if (string == NULL)
21 return NULL;
22
23 GString *escaped = g_string_sized_new (strlen (string));
24 for (point = (char *) string; *point != 0; point++)
25 {
26 unsigned char character = *point;
27
28 if ((character > 31) && (character != '\\')
29 && (single_quote ? (character != '\'') : (character != '\"')))
30 {
31 g_string_append_c (escaped, character);
32 }
33 else
34 {
35 g_string_append_c (escaped, '\\');
36 switch (*point)
37 {
38 case '\\':
39 case '\'':
40 case '\"':
41 g_string_append_c (escaped, *point);
42 break;
43 case '\b':
44 g_string_append_c (escaped, 'b');
45 break;
46 case '\f':
47 g_string_append_c (escaped, 'f');
48 break;
49 case '\n':
50 g_string_append_c (escaped, 'n');
51 break;
52 case '\r':
53 g_string_append_c (escaped, 'r');
54 break;
55 case '\t':
56 g_string_append_c (escaped, 't');
57 break;
58 default:
59 g_string_append_printf (escaped, "u%04x", character);
60 }
61 }
62 }
63 return g_string_free (escaped, FALSE);
64}
65
74double
75gvm_json_obj_double (cJSON *obj, const gchar *key)
76{
77 cJSON *item;
78
79 item = cJSON_GetObjectItem (obj, key);
80 if (item && cJSON_IsNumber (item))
81 return item->valuedouble;
82
83 return 0;
84}
85
96int
97gvm_json_obj_check_int (cJSON *obj, const gchar *key, int *val)
98{
99 cJSON *item;
100
101 item = cJSON_GetObjectItem (obj, key);
102 if (item && cJSON_IsNumber (item))
103 {
104 if (val)
105 *val = item->valueint;
106 return 0;
107 }
108 return 1;
109}
110
119int
120gvm_json_obj_int (cJSON *obj, const gchar *key)
121{
122 cJSON *item;
123
124 item = cJSON_GetObjectItem (obj, key);
125 if (item && cJSON_IsNumber (item))
126 return item->valueint;
127
128 return 0;
129}
130
141int
142gvm_json_obj_check_str (cJSON *obj, const gchar *key, gchar **val)
143{
144 cJSON *item;
145
146 item = cJSON_GetObjectItem (obj, key);
147 if (item && cJSON_IsString (item))
148 {
149 if (val)
150 *val = item->valuestring;
151 return 0;
152 }
153 return 1;
154}
155
164gchar *
165gvm_json_obj_str (cJSON *obj, const gchar *key)
166{
167 cJSON *item;
168
169 item = cJSON_GetObjectItem (obj, key);
170 if (item && cJSON_IsString (item))
171 return item->valuestring;
172
173 return 0;
174}
double gvm_json_obj_double(cJSON *obj, const gchar *key)
Get a double field from a JSON object.
Definition json.c:75
int gvm_json_obj_check_int(cJSON *obj, const gchar *key, int *val)
Get an int field from a JSON object.
Definition json.c:97
gchar * gvm_json_string_escape(const char *string, gboolean single_quote)
Escapes a string according to the JSON or JSONPath standard.
Definition json.c:17
int gvm_json_obj_int(cJSON *obj, const gchar *key)
Get an int field from a JSON object.
Definition json.c:120
gchar * gvm_json_obj_str(cJSON *obj, const gchar *key)
Get a string field from a JSON object.
Definition json.c:165
int gvm_json_obj_check_str(cJSON *obj, const gchar *key, gchar **val)
Get a string field from a JSON object.
Definition json.c:142