Greenbone Vulnerability Management Libraries 22.32.0
json.c File Reference
#include "json.h"
Include dependency graph for json.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

gchar * gvm_json_string_escape (const char *string, gboolean single_quote)
 Escapes a string according to the JSON or JSONPath standard.
double gvm_json_obj_double (cJSON *obj, const gchar *key)
 Get a double field from a JSON object.
int gvm_json_obj_check_int (cJSON *obj, const gchar *key, int *val)
 Get an int field from a JSON object.
int gvm_json_obj_int (cJSON *obj, const gchar *key)
 Get an int field from a JSON object.
int gvm_json_obj_check_str (cJSON *obj, const gchar *key, gchar **val)
 Get a string field from a JSON object.
gchar * gvm_json_obj_str (cJSON *obj, const gchar *key)
 Get a string field from a JSON object.

Function Documentation

◆ gvm_json_obj_check_int()

int gvm_json_obj_check_int ( cJSON * obj,
const gchar * key,
int * val )

Get an int field from a JSON object.

Parameters
[in]objObject
[in]keyField name.
[out]valEither NULL or a return location for the int (only set if int field exists).
Returns
0 if such an int field exists, else 1.

Definition at line 97 of file json.c.

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}

Referenced by add_preferences_to_nvt(), Ensure(), Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ gvm_json_obj_check_str()

int gvm_json_obj_check_str ( cJSON * obj,
const gchar * key,
gchar ** val )

Get a string field from a JSON object.

Parameters
[in]objObject
[in]keyField name.
[out]valEither NULL or a return location for the string (only set if string field exists). Freed by cJSON_Delete.
Returns
0 if such a field exists, else 1.

Definition at line 142 of file json.c.

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}

Referenced by add_preferences_to_nvt(), add_tags_to_nvt(), Ensure(), Ensure(), Ensure(), Ensure(), parse_references(), and parse_vt_json().

Here is the caller graph for this function:

◆ gvm_json_obj_double()

double gvm_json_obj_double ( cJSON * obj,
const gchar * key )

Get a double field from a JSON object.

Parameters
[in]objObject
[in]keyField name.
Returns
A double.

Definition at line 75 of file json.c.

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}

Referenced by add_tags_to_nvt(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ gvm_json_obj_int()

int gvm_json_obj_int ( cJSON * obj,
const gchar * key )

Get an int field from a JSON object.

Parameters
[in]objObject
[in]keyField name.
Returns
An int.

Definition at line 120 of file json.c.

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}

Referenced by Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ gvm_json_obj_str()

gchar * gvm_json_obj_str ( cJSON * obj,
const gchar * key )

Get a string field from a JSON object.

Parameters
[in]objObject
[in]keyField name.
Returns
A string. Will be freed by cJSON_Delete.

Definition at line 165 of file json.c.

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}

Referenced by add_tags_to_nvt(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ gvm_json_string_escape()

gchar * gvm_json_string_escape ( const char * string,
gboolean single_quote )

Escapes a string according to the JSON or JSONPath standard.

Parameters
[in]stringThe string to escape
[in]single_quoteWhether to escape single quotes
Returns
The escaped string

Definition at line 17 of file json.c.

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}

Referenced by Ensure(), and gvm_json_path_string_add_elem().

Here is the caller graph for this function: