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
|
/*
* Copyright (c) 2023 Balazs Scheidler <balazs.scheidler@axoflow.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "filterx/object-json-internal.h"
#include "filterx/object-null.h"
#include "filterx/object-primitive.h"
#include "filterx/object-string.h"
#include "filterx/object-message-value.h"
#include "filterx/filterx-weakrefs.h"
#include "filterx/filterx-eval.h"
#include "scanner/list-scanner/list-scanner.h"
#include "str-repr/encode.h"
static int
_deep_copy_filterx_object_ref(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
{
int result = json_c_shallow_copy_default(src, parent, key, index, dst);
if (*dst != NULL)
{
/* we need to copy the userdata for primitive types */
switch (json_object_get_type(src))
{
case json_type_null:
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
{
FilterXObject *fobj = json_object_get_userdata(src);
if (fobj)
filterx_json_associate_cached_object(*dst, fobj);
break;
}
default:
break;
}
return 2;
}
return result;
}
struct json_object *
filterx_json_deep_copy(struct json_object *jso)
{
struct json_object *clone = NULL;
if (json_object_deep_copy(jso, &clone, _deep_copy_filterx_object_ref) != 0)
return NULL;
return clone;
}
static FilterXObject *
_convert_json_to_object(FilterXObject *self, FilterXWeakRef *root_container, struct json_object *jso)
{
switch (json_object_get_type(jso))
{
case json_type_double:
return filterx_double_new(json_object_get_double(jso));
case json_type_boolean:
return filterx_boolean_new(json_object_get_boolean(jso));
case json_type_int:
return filterx_integer_new(json_object_get_int64(jso));
case json_type_string:
return filterx_string_new(json_object_get_string(jso), -1);
case json_type_array:
return filterx_json_array_new_sub(json_object_get(jso),
filterx_weakref_get(root_container) ? : filterx_object_ref(self));
case json_type_object:
return filterx_json_object_new_sub(json_object_get(jso),
filterx_weakref_get(root_container) ? : filterx_object_ref(self));
default:
g_assert_not_reached();
}
}
FilterXObject *
filterx_json_convert_json_to_object_cached(FilterXObject *self, FilterXWeakRef *root_container,
struct json_object *jso)
{
FilterXObject *filterx_obj;
if (!jso || json_object_get_type(jso) == json_type_null)
return filterx_null_new();
if (json_object_get_type(jso) == json_type_double)
{
/* this is a workaround to ditch builtin serializer for double objects
* that are set when parsing from a string representation.
* json_object_double_new_ds() will set userdata to the string
* representation of the number, as extracted from the JSON source.
*
* Changing the value of the double to the same value, ditches this,
* but only if necessary.
*/
json_object_set_double(jso, json_object_get_double(jso));
}
/* NOTE: userdata is a weak reference */
filterx_obj = json_object_get_userdata(jso);
if (filterx_obj)
return filterx_object_ref(filterx_obj);
filterx_obj = _convert_json_to_object(self, root_container, jso);
if (!filterx_object_is_frozen(self))
filterx_json_associate_cached_object(jso, filterx_obj);
return filterx_obj;
}
void
filterx_json_associate_cached_object(struct json_object *jso, FilterXObject *filterx_obj)
{
filterx_eval_store_weak_ref(filterx_obj);
/* we are not storing a reference in userdata to avoid circular
* references. That ref is retained by the filterx_scope_store_weak_ref()
* call above, which is automatically terminated when the scope ends */
json_object_set_userdata(jso, filterx_obj, NULL);
}
FilterXObject *
filterx_json_new_from_repr(const gchar *repr, gssize repr_len)
{
if (repr_len == 0 || repr[0] == '{')
return filterx_json_object_new_from_repr(repr, repr_len);
return filterx_json_array_new_from_repr(repr, repr_len);
}
FilterXObject *
filterx_json_new_from_args(GPtrArray *args)
{
if (!args || args->len == 0)
return filterx_json_object_new_empty();
if (args->len != 1)
{
msg_error("FilterX: Failed to create JSON object: invalid number of arguments. "
"Usage: json() or json($raw_json_string) or json($existing_json)");
return NULL;
}
FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);
if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(json_array)) ||
filterx_object_is_type(arg, &FILTERX_TYPE_NAME(json_object)))
return filterx_object_ref(arg);
if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(message_value)))
{
FilterXObject *unmarshalled = filterx_object_unmarshal(arg);
if (!filterx_object_is_type(unmarshalled, &FILTERX_TYPE_NAME(json_array)) &&
!filterx_object_is_type(unmarshalled, &FILTERX_TYPE_NAME(json_object)))
{
filterx_object_unref(unmarshalled);
goto error;
}
return unmarshalled;
}
gsize repr_len;
const gchar *repr = filterx_string_get_value(arg, &repr_len);
if (repr)
return filterx_json_new_from_repr(repr, repr_len);
error:
msg_error("FilterX: Failed to create JSON object: invalid argument type. "
"Usage: json() or json($raw_json_string) or json($syslog_ng_list) or json($existing_json)",
evt_tag_str("type", arg->type->name));
return NULL;
}
FilterXObject *
filterx_json_new_from_object(struct json_object *jso)
{
if (json_object_get_type(jso) == json_type_object)
return filterx_json_object_new_sub(jso, NULL);
if (json_object_get_type(jso) == json_type_array)
return filterx_json_array_new_sub(jso, NULL);
return NULL;
}
const gchar *
filterx_json_to_json_literal(FilterXObject *s)
{
if (filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_object)))
return filterx_json_object_to_json_literal(s);
if (filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_array)))
return filterx_json_array_to_json_literal(s);
return NULL;
}
|