Browse Source

Add new functions so any old things still work

pull/715/head
HexTheDragon 4 years ago
parent
commit
816b00c1c6
4 changed files with 241 additions and 14 deletions
  1. +2
    -0
      json-c.sym
  2. +135
    -4
      json_object.c
  3. +99
    -6
      json_object.h
  4. +5
    -4
      tests/test_deep_copy.c

+ 2
- 0
json-c.sym View File

@@ -176,4 +176,6 @@ JSONC_0.16 {
json_object_object_get_len;
json_object_object_get_ex_len;
json_object_object_get_key;
json_c_shallow_copy_default_len;
json_object_deep_copy_len;
} JSONC_0.15;

+ 135
- 4
json_object.c View File

@@ -1728,8 +1728,30 @@ static int json_object_copy_serializer_data(struct json_object *src, struct json
*
* This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
*/
int json_c_shallow_copy_default(json_object *src, json_object *parent, const struct json_key *key,
int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key,
size_t index, json_object **dst)
{
if (key == NULL)
{
return json_c_shallow_copy_default_len(src, parent, NULL, index, dst);
}
else
{
const struct json_key nkey = {strlen(key), {key}};
return json_c_shallow_copy_default_len(src, parent, &nkey, index, dst);
}
}

/**
* The default shallow copy implementation. Simply creates a new object of the same
* type but does *not* copy over _userdata nor retain any custom serializer.
* If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
* implementation that is aware of how to copy them.
*
* This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
*/
int json_c_shallow_copy_default_len(json_object *src, json_object *parent,
const struct json_key *key, size_t index, json_object **dst)
{
switch (src->o_type)
{
@@ -1776,8 +1798,8 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const str
* Note: caller is responsible for freeing *dst if this fails and returns -1.
*/
static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent,
const struct json_key *key_in_parent,
size_t index_in_parent, struct json_object **dst,
const char *key_in_parent, size_t index_in_parent,
struct json_object **dst,
json_c_shallow_copy_fn *shallow_copy)
{
struct json_object_iter iter;
@@ -1802,7 +1824,8 @@ static int json_object_deep_copy_recursive(struct json_object *src, struct json_
/* This handles the `json_type_null` case */
if (!iter.val)
jso = NULL;
else if (json_object_deep_copy_recursive(iter.val, src, iter.key, UINT_MAX,
else if (json_object_deep_copy_recursive(iter.val, src,
json_key_data(iter.key), UINT_MAX,
&jso, shallow_copy) < 0)
{
json_object_put(jso);
@@ -1852,6 +1875,89 @@ static int json_object_deep_copy_recursive(struct json_object *src, struct json_
return 0;
}

/*
* The actual guts of json_object_deep_copy(), with a few additional args
* needed so we can keep track of where we are within the object tree.
*
* Note: caller is responsible for freeing *dst if this fails and returns -1.
*/
static int json_object_deep_copy_recursive_len(struct json_object *src, struct json_object *parent,
const struct json_key *key_in_parent,
size_t index_in_parent, struct json_object **dst,
json_c_shallow_copy_fn_len *shallow_copy)
{
struct json_object_iter iter;
size_t src_array_len, ii;

int shallow_copy_rc = 0;
shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
/* -1=error, 1=object created ok, 2=userdata set */
if (shallow_copy_rc < 1)
{
errno = EINVAL;
return -1;
}
assert(*dst != NULL);

switch (src->o_type)
{
case json_type_object:
json_object_object_foreachC(src, iter)
{
struct json_object *jso = NULL;
/* This handles the `json_type_null` case */
if (!iter.val)
jso = NULL;
else if (json_object_deep_copy_recursive_len(
iter.val, src, iter.key, UINT_MAX, &jso, shallow_copy) < 0)
{
json_object_put(jso);
return -1;
}

if (json_object_object_add_key(*dst, iter.key, jso, 0) < 0)
{
json_object_put(jso);
return -1;
}
}
break;

case json_type_array:
src_array_len = json_object_array_length(src);
for (ii = 0; ii < src_array_len; ii++)
{
struct json_object *jso = NULL;
struct json_object *jso1 = json_object_array_get_idx(src, ii);
/* This handles the `json_type_null` case */
if (!jso1)
jso = NULL;
else if (json_object_deep_copy_recursive_len(jso1, src, NULL, ii, &jso,
shallow_copy) < 0)
{
json_object_put(jso);
return -1;
}

if (json_object_array_add(*dst, jso) < 0)
{
json_object_put(jso);
return -1;
}
}
break;

default:
break;
/* else, nothing to do, shallow_copy already did. */
}

if (shallow_copy_rc != 2)
return json_object_copy_serializer_data(src, *dst);

return 0;
}

int json_object_deep_copy(struct json_object *src, struct json_object **dst,
json_c_shallow_copy_fn *shallow_copy)
{
@@ -1877,6 +1983,31 @@ int json_object_deep_copy(struct json_object *src, struct json_object **dst,
return rc;
}

int json_object_deep_copy_len(struct json_object *src, struct json_object **dst,
json_c_shallow_copy_fn_len *shallow_copy)
{
int rc;

/* Check if arguments are sane ; *dst must not point to a non-NULL object */
if (!src || !dst || *dst)
{
errno = EINVAL;
return -1;
}

if (shallow_copy == NULL)
shallow_copy = json_c_shallow_copy_default_len;

rc = json_object_deep_copy_recursive_len(src, NULL, NULL, UINT_MAX, dst, shallow_copy);
if (rc < 0)
{
json_object_put(*dst);
*dst = NULL;
}

return rc;
}

static void json_abort(const char *message)
{
if (message != NULL)


+ 99
- 6
json_object.h View File

@@ -1226,7 +1226,11 @@ JSON_EXPORT struct json_object *json_object_new_null(void);
JSON_EXPORT int json_object_equal(struct json_object *obj1, struct json_object *obj2);

/**
* Perform a shallow copy of src into *dst as part of an overall json_object_deep_copy().
* @deprecated This type is provided for backward-compatability only.
* It is reccomended to use `json_c_shallow_copy_fn_len` instead, and
* properly handle copying of keys with embedded NULL (<c>'\0'</c>) characters.
*
* @brief Perform a shallow copy of src into *dst as part of an overall json_object_deep_copy().
*
* If src is part of a containing object or array, parent will be non-NULL,
* and key or index will be provided.
@@ -1237,13 +1241,51 @@ JSON_EXPORT int json_object_equal(struct json_object *obj1, struct json_object *
* json_object_deep_copy that it should not attempt to use the standard userdata
* copy function.
*
* @param src The source object to be copied
* @param parent The the parent of the current object
* @param key The key that <p>src</p> has in <p>parent</p>, if <p>parent</p>
* is an object
* @param index The index that <p>src</p> can be found at in <p>parent</p>,
* if <p>parent</p> is an array
* @param dst The destination object
*
* @return On success 1 or 2, -1 on errors
*
* @since 0.13
*
* @see json_c_shallow_copy_fn_len
*
*/
typedef int(json_c_shallow_copy_fn)(json_object *src, json_object *parent,
const struct json_key *key, size_t index, json_object **dst);
typedef int(json_c_shallow_copy_fn)(json_object *src, json_object *parent, const char *key,
size_t index, json_object **dst);

/**
* The default shallow copy implementation for use with json_object_deep_copy().
* @brief Perform a shallow copy of src into *dst as part of an overall json_object_deep_copy().
*
* If src is part of a containing object or array, parent will be non-NULL,
* and key or index will be provided.
* When shallow_copy is called *dst will be NULL, and must be non-NULL when it returns.
* src will never be NULL.
*
* If shallow_copy sets the serializer on an object, return 2 to indicate to
* json_object_deep_copy that it should not attempt to use the standard userdata
* copy function.
*
* @since 0.16
*
* @return On success 1 or 2, -1 on errors
*/
typedef int(json_c_shallow_copy_fn_len)(json_object *src, json_object *parent,
const struct json_key *key, size_t index,
json_object **dst);

/**
* @deprecated This type is provided for backward-compatability only.
* It is reccomended to use `json_c_shallow_copy_fn_len` instead, and
* properly handle copying of keys with embedded NULL (<c>'\0'</c>) characters.
*
* @brief The default shallow copy implementation for use with json_object_deep_copy().
*
* This simply calls the appropriate json_object_new_<type>() function and
* copies over the serializer function (_to_json_string internal field of
* the json_object structure) but not any _userdata or _user_delete values.
@@ -1252,12 +1294,38 @@ typedef int(json_c_shallow_copy_fn)(json_object *src, json_object *parent,
* your own custom serializer, you can call this first to create the new object
* before customizing it with json_object_set_serializer().
*
* @since 0.13
*
* @return 1 on success, -1 on errors, but never 2.
*
* @see json_c_shallow_copy_fn_len
*/
JSON_EXPORT json_c_shallow_copy_fn json_c_shallow_copy_default;

/**
* Copy the contents of the JSON object.
* @brief The default shallow copy implementation for use with json_object_deep_copy().
*
* This simply calls the appropriate json_object_new_<type>() function and
* copies over the serializer function (_to_json_string internal field of
* the json_object structure) but not any _userdata or _user_delete values.
*
* If you're writing a custom shallow_copy function, perhaps because you're using
* your own custom serializer, you can call this first to create the new object
* before customizing it with json_object_set_serializer().
*
* @since 0.16
*
* @return 1 on success, -1 on errors, but never 2.
*/
JSON_EXPORT json_c_shallow_copy_fn_len json_c_shallow_copy_default_len;

/**
* @deprecated This function is provided for backward-compatability only.
* It is reccomended to use `json_object_deep_copy_len` instead, as it will
* properly handle copying of keys with embedded NULL (<c>'\0'</c>) characters.
*
* @brief Copy the contents of the JSON object.
*
* The destination object must be initialized to NULL,
* to make sure this function won't overwrite an existing JSON object.
*
@@ -1271,13 +1339,38 @@ JSON_EXPORT json_c_shallow_copy_fn json_c_shallow_copy_default;
* when custom serializers are in use. See also
* json_object set_serializer.
*
* @since 0.13
*
* @returns 0 if the copy went well, -1 if an error occured during copy
* or if the destination pointer is non-NULL
*/

JSON_EXPORT int json_object_deep_copy(struct json_object *src, struct json_object **dst,
json_c_shallow_copy_fn *shallow_copy);

/**
* @brief Copy the contents of the JSON object.
*
* The destination object must be initialized to NULL,
* to make sure this function won't overwrite an existing JSON object.
*
* This does roughly the same thing as
* `json_tokener_parse(json_object_get_string(src))`.
*
* @param src source JSON object whose contents will be copied
* @param dst pointer to the destination object where the contents of `src`;
* make sure this pointer is initialized to NULL
* @param shallow_copy an optional function to copy individual objects, needed
* when custom serializers are in use. See also
* json_object set_serializer.
*
* @since 0.16
*
* @returns 0 if the copy went well, -1 if an error occured during copy
* or if the destination pointer is non-NULL
*/
JSON_EXPORT int json_object_deep_copy_len(struct json_object *src, struct json_object **dst,
json_c_shallow_copy_fn_len *shallow_copy);

/* Json Object Keys */

/**


+ 5
- 4
tests/test_deep_copy.c View File

@@ -92,15 +92,16 @@ int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level
return 0;
}

json_c_shallow_copy_fn my_shallow_copy;
json_c_shallow_copy_fn_len my_shallow_copy;
int my_shallow_copy(json_object *src, json_object *parent, const struct json_key *key, size_t index,
json_object **dst)
{
int rc;
rc = json_c_shallow_copy_default(src, parent, key, index, dst);
rc = json_c_shallow_copy_default_len(src, parent, key, index, dst);
if (rc < 0)
return rc;
if (key != NULL && strcmp(json_key_data(key), "with_serializer") == 0)
if (key != NULL && json_key_size(key) == 15 &&
strcmp(json_key_data(key), "with_serializer") == 0)
{
printf("CALLED: my_shallow_copy on with_serializer object\n");
void *userdata = json_object_get_userdata(src);
@@ -199,7 +200,7 @@ int main(int argc, char **argv)
dst1 = NULL;
/* With a custom serializer in use, a custom shallow_copy function must also be used */
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
assert(0 == json_object_deep_copy(src1, &dst1, my_shallow_copy));
assert(0 == json_object_deep_copy_len(src1, &dst1, my_shallow_copy));

json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer");
assert(dest_with_serializer != NULL);


Loading…
Cancel
Save