json.c
Go to the documentation of this file.
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "json.h"
gchar *
gvm_json_string_escape (const char *string, gboolean single_quote)
{
gchar *point;
if (string == NULL)
return NULL;
GString *escaped = g_string_sized_new (strlen (string));
for (point = (char *) string; *point != 0; point++)
{
unsigned char character = *point;
if ((character > 31) && (character != '\\')
&& (single_quote ? (character != '\'') : (character != '\"')))
{
g_string_append_c (escaped, character);
}
else
{
g_string_append_c (escaped, '\\');
switch (*point)
{
case '\\':
case '\'':
case '\"':
g_string_append_c (escaped, *point);
break;
case '\b':
g_string_append_c (escaped, 'b');
break;
case '\f':
g_string_append_c (escaped, 'f');
break;
case '\n':
g_string_append_c (escaped, 'n');
break;
case '\r':
g_string_append_c (escaped, 'r');
break;
case '\t':
g_string_append_c (escaped, 't');
break;
default:
g_string_append_printf (escaped, "u%04x", character);
}
}
}
return g_string_free (escaped, FALSE);
}
double
gvm_json_obj_double (cJSON *obj, const gchar *key)
{
cJSON *item;
item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsNumber (item))
return item->valuedouble;
return 0;
}
int
gvm_json_obj_check_int (cJSON *obj, const gchar *key, int *val)
{
cJSON *item;
item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsNumber (item))
{
if (val)
*val = item->valueint;
return 0;
}
return 1;
}
int
gvm_json_obj_int (cJSON *obj, const gchar *key)
{
cJSON *item;
item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsNumber (item))
return item->valueint;
return 0;
}
int
gvm_json_obj_check_str (cJSON *obj, const gchar *key, gchar **val)
{
cJSON *item;
item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsString (item))
{
if (val)
*val = item->valuestring;
return 0;
}
return 1;
}
gchar *
gvm_json_obj_str (cJSON *obj, const gchar *key)
{
cJSON *item;
item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsString (item))
return item->valuestring;
return 0;
}
gvm_json_obj_double
double gvm_json_obj_double(cJSON *obj, const gchar *key)
Get a double field from a JSON object.
Definition json.c:75
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.
Definition json.c:97
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.
Definition json.c:17
gvm_json_obj_int
int gvm_json_obj_int(cJSON *obj, const gchar *key)
Get an int field from a JSON object.
Definition json.c:120
gvm_json_obj_str
gchar * gvm_json_obj_str(cJSON *obj, const gchar *key)
Get a string field from a JSON object.
Definition json.c:165
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.
Definition json.c:142
json.h