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
|
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "json.h"
/**
* @brief Escapes a string according to the JSON or JSONPath standard
*
* @param[in] string The string to escape
* @param[in] single_quote Whether to escape single quotes
*
* @return The escaped string
*/
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);
}
/**
* @brief Get a double field from a JSON object.
*
* @param[in] obj Object
* @param[in] key Field name.
*
* @return A double.
*/
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;
}
/**
* @brief Get an int field from a JSON object.
*
* @param[in] obj Object
* @param[in] key Field name.
* @param[out] val Either NULL or a return location for the int (only set if
* int field exists).
*
* @return 0 if such an int field exists, else 1.
*/
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;
}
/**
* @brief Get an int field from a JSON object.
*
* @param[in] obj Object
* @param[in] key Field name.
*
* @return An int.
*/
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;
}
/**
* @brief Get a string field from a JSON object.
*
* @param[in] obj Object
* @param[in] key Field name.
* @param[out] val Either NULL or a return location for the string (only set
* if string field exists). Freed by cJSON_Delete.
*
* @return 0 if such a field exists, else 1.
*/
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;
}
/**
* @brief Get a string field from a JSON object.
*
* @param[in] obj Object
* @param[in] key Field name.
*
* @return A string. Will be freed by cJSON_Delete.
*/
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;
}
|