2017-03-22 14:46:42 +03:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-03-03 02:45:50 +03:00
|
|
|
#pragma GCC visibility push(default)
|
2015-02-10 15:14:59 +03:00
|
|
|
#include <ctype.h>
|
2015-02-10 17:17:59 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2015-02-11 04:29:40 +03:00
|
|
|
#include <stdio.h>
|
2017-02-03 20:34:37 +03:00
|
|
|
#include <limits.h>
|
2017-03-03 02:45:50 +03:00
|
|
|
#pragma GCC visibility pop
|
2017-02-03 20:34:37 +03:00
|
|
|
|
2015-02-09 21:29:05 +03:00
|
|
|
#include "cJSON_Utils.h"
|
|
|
|
|
2017-04-30 12:06:43 +03:00
|
|
|
static unsigned char* cJSONUtils_strdup(const unsigned char* const string)
|
2016-11-04 20:15:57 +03:00
|
|
|
{
|
2017-04-30 12:06:43 +03:00
|
|
|
size_t length = 0;
|
2017-02-03 17:59:00 +03:00
|
|
|
unsigned char *copy = NULL;
|
2016-11-04 20:15:57 +03:00
|
|
|
|
2017-04-30 12:06:43 +03:00
|
|
|
length = strlen((const char*)string) + sizeof("");
|
|
|
|
copy = (unsigned char*) cJSON_malloc(length);
|
|
|
|
if (copy == NULL)
|
2016-11-04 20:15:57 +03:00
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-11-04 20:15:57 +03:00
|
|
|
}
|
2017-04-30 12:06:43 +03:00
|
|
|
memcpy(copy, string, length);
|
2016-11-04 20:15:57 +03:00
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:52:29 +03:00
|
|
|
/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
|
|
|
|
static int cJSONUtils_strcasecmp(const unsigned char *string1, const unsigned char *string2)
|
2015-02-13 22:53:27 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
if ((string1 == NULL) || (string2 == NULL))
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
return 1;
|
2016-10-13 19:26:53 +03:00
|
|
|
}
|
2017-04-30 13:52:29 +03:00
|
|
|
|
|
|
|
if (string1 == string2)
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
return 0;
|
2016-10-13 19:26:53 +03:00
|
|
|
}
|
2017-04-30 13:52:29 +03:00
|
|
|
|
|
|
|
for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
if (*string1 == '\0')
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:52:29 +03:00
|
|
|
return tolower(*string1) - tolower(*string2);
|
2015-02-13 22:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 13:53:00 +03:00
|
|
|
/* Compare the next path element of two JSON pointers, two NULL pointers are considered unequal: */
|
|
|
|
static int cJSONUtils_Pstrcasecmp(const unsigned char *name, const unsigned char *pointer)
|
2015-02-10 15:14:59 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
if ((name == NULL) || (pointer == NULL))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
return 1;
|
2016-10-13 19:40:02 +03:00
|
|
|
}
|
2017-04-30 13:53:00 +03:00
|
|
|
|
|
|
|
for (; (*name != '\0') && (*pointer != '\0') && (*pointer != '/'); (void)name++, pointer++) /* compare until next '/' */
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
if (*pointer == '~')
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
|
|
|
/* check for escaped '~' (~0) and '/' (~1) */
|
2017-04-30 13:53:00 +03:00
|
|
|
if (((pointer[1] != '0') || (*name != '~')) && ((pointer[1] != '1') || (*name != '/')))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
/* invalid escape sequence or wrong character in *name */
|
2016-10-13 19:40:02 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
pointer++;
|
2016-10-13 19:40:02 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 13:53:00 +03:00
|
|
|
else if (tolower(*name) != tolower(*pointer))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-04-30 13:53:00 +03:00
|
|
|
if (((*pointer != 0) && (*pointer != '/')) != (*name != 0))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
|
|
|
/* one string has ended, the other not */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-02-10 15:14:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 13:54:00 +03:00
|
|
|
/* calculate the length of a string if encoded as JSON pointer with ~0 and ~1 escape sequences */
|
|
|
|
static size_t cJSONUtils_PointerEncodedstrlen(const unsigned char *string)
|
2016-10-13 19:42:34 +03:00
|
|
|
{
|
2017-04-30 13:54:00 +03:00
|
|
|
size_t length;
|
|
|
|
for (length = 0; *string != '\0'; (void)string++, length++)
|
2016-10-13 19:42:34 +03:00
|
|
|
{
|
2017-04-30 13:54:00 +03:00
|
|
|
/* character needs to be escaped? */
|
|
|
|
if ((*string == '~') || (*string == '/'))
|
2016-10-13 19:42:34 +03:00
|
|
|
{
|
2017-04-30 13:54:00 +03:00
|
|
|
length++;
|
2016-10-13 19:42:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:54:00 +03:00
|
|
|
return length;
|
2016-10-13 19:42:34 +03:00
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-04-30 13:54:44 +03:00
|
|
|
/* copy a string while escaping '~' and '/' with ~0 and ~1 JSON pointer escape codes */
|
|
|
|
static void cJSONUtils_PointerEncodedstrcpy(unsigned char *destination, const unsigned char *source)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
for (; source[0] != '\0'; (void)source++, destination++)
|
2016-10-13 19:47:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
if (source[0] == '/')
|
2016-10-13 19:47:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[1] = '1';
|
|
|
|
destination++;
|
2016-10-13 19:47:40 +03:00
|
|
|
}
|
2017-04-30 13:54:44 +03:00
|
|
|
else if (source[0] == '~')
|
2016-10-13 19:47:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[0] = '~';
|
|
|
|
destination[1] = '1';
|
|
|
|
destination++;
|
2016-10-13 19:47:40 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[0] = source[0];
|
2016-10-13 19:47:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[0] = '\0';
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
size_t child_index = 0;
|
|
|
|
cJSON *current_child = 0;
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-13 20:05:58 +03:00
|
|
|
if (object == target)
|
|
|
|
{
|
|
|
|
/* found */
|
2017-02-03 17:59:00 +03:00
|
|
|
return (char*)cJSONUtils_strdup((const unsigned char*)"");
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
/* recursively search all children of the object or array */
|
|
|
|
for (current_child = object->child; current_child != NULL; (void)(current_child = current_child->next), child_index++)
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
unsigned char *target_pointer = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(current_child, target);
|
|
|
|
/* found the target? */
|
|
|
|
if (target_pointer != NULL)
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
2017-02-26 23:26:34 +03:00
|
|
|
if (cJSON_IsArray(object))
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
|
|
|
/* reserve enough memory for a 64 bit integer + '/' and '\0' */
|
2017-04-30 13:56:02 +03:00
|
|
|
unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + 20 + sizeof("/"));
|
2017-02-15 15:09:32 +03:00
|
|
|
/* check if conversion to unsigned long is valid
|
|
|
|
* This should be eliminated at compile time by dead code elimination
|
|
|
|
* if size_t is an alias of unsigned long, or if it is bigger */
|
2017-04-30 13:56:02 +03:00
|
|
|
if (child_index > ULONG_MAX)
|
2017-02-15 15:09:32 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
cJSON_free(target_pointer);
|
2017-02-15 15:09:32 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-30 13:56:02 +03:00
|
|
|
sprintf((char*)full_pointer, "/%lu%s", (unsigned long)child_index, target_pointer); /* /<array_index><path> */
|
|
|
|
cJSON_free(target_pointer);
|
2016-10-13 20:05:58 +03:00
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
return (char*)full_pointer;
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
2017-04-30 13:56:02 +03:00
|
|
|
|
|
|
|
if (cJSON_IsObject(object))
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + cJSONUtils_PointerEncodedstrlen((unsigned char*)current_child->string) + 2);
|
|
|
|
full_pointer[0] = '/';
|
|
|
|
cJSONUtils_PointerEncodedstrcpy(full_pointer + 1, (unsigned char*)current_child->string);
|
|
|
|
strcat((char*)full_pointer, (char*)target_pointer);
|
|
|
|
cJSON_free(target_pointer);
|
2016-10-13 20:05:58 +03:00
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
return (char*)full_pointer;
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* reached leaf of the tree, found nothing */
|
2017-04-30 13:56:02 +03:00
|
|
|
cJSON_free(target_pointer);
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:21:48 +03:00
|
|
|
/* non broken version of cJSON_GetArrayItem */
|
|
|
|
static cJSON *get_array_item(const cJSON *array, size_t item)
|
|
|
|
{
|
|
|
|
cJSON *child = array ? array->child : NULL;
|
|
|
|
while ((child != NULL) && (item > 0))
|
|
|
|
{
|
|
|
|
item--;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
2017-04-12 13:06:27 +03:00
|
|
|
static cJSON_bool decode_array_index_from_pointer(const unsigned char * const pointer, size_t * const index)
|
|
|
|
{
|
|
|
|
size_t parsed_index = 0;
|
|
|
|
size_t position = 0;
|
|
|
|
|
|
|
|
if ((pointer[0] == '0') && ((pointer[1] != '\0') && (pointer[1] != '/')))
|
|
|
|
{
|
|
|
|
/* leading zeroes are not permitted */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:56:45 +03:00
|
|
|
for (position = 0; (pointer[position] >= '0') && (pointer[0] <= '9'); position++)
|
2017-04-12 13:06:27 +03:00
|
|
|
{
|
|
|
|
parsed_index = (10 * parsed_index) + (size_t)(pointer[position] - '0');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pointer[position] != '\0') && (pointer[position] != '/'))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*index = parsed_index;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:57:49 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer)
|
2015-02-09 21:29:05 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
cJSON *current_element = object;
|
2016-10-13 20:18:15 +03:00
|
|
|
/* follow path of the pointer */
|
2017-04-30 13:57:49 +03:00
|
|
|
while ((pointer[0] == '/') && (current_element != NULL))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
pointer++;
|
|
|
|
if (cJSON_IsArray(current_element))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-12 12:21:48 +03:00
|
|
|
size_t index = 0;
|
2017-04-12 13:06:27 +03:00
|
|
|
if (!decode_array_index_from_pointer((const unsigned char*)pointer, &index))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
2017-04-12 13:06:27 +03:00
|
|
|
|
2017-04-30 13:57:49 +03:00
|
|
|
current_element = get_array_item(current_element, index);
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
2017-04-30 13:57:49 +03:00
|
|
|
else if (cJSON_IsObject(current_element))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
current_element = current_element->child;
|
2016-10-13 20:18:15 +03:00
|
|
|
/* GetObjectItem. */
|
2017-04-30 13:57:49 +03:00
|
|
|
while ((current_element != NULL) && cJSONUtils_Pstrcasecmp((unsigned char*)current_element->string, (const unsigned char*)pointer))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
current_element = current_element->next;
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
|
|
|
/* skip to the next path token or end of string */
|
2017-04-30 13:57:49 +03:00
|
|
|
while ((pointer[0] != '\0') && (pointer[0] != '/'))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
|
|
|
pointer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:57:49 +03:00
|
|
|
return current_element;
|
2015-02-09 21:29:05 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:19:26 +03:00
|
|
|
/* JSON Patch implementation. */
|
2017-02-03 17:59:00 +03:00
|
|
|
static void cJSONUtils_InplaceDecodePointerString(unsigned char *string)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2017-04-30 14:00:16 +03:00
|
|
|
unsigned char *decoded_string = string;
|
2017-01-30 21:29:52 +03:00
|
|
|
|
|
|
|
if (string == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:00:16 +03:00
|
|
|
for (; *string; (void)decoded_string++, string++)
|
2016-10-15 08:20:47 +03:00
|
|
|
{
|
2017-04-30 14:00:16 +03:00
|
|
|
if (string[0] == '~')
|
|
|
|
{
|
|
|
|
if (string[1] == '0')
|
|
|
|
{
|
|
|
|
decoded_string[0] = '~';
|
|
|
|
}
|
|
|
|
else if (string[1] == '1')
|
|
|
|
{
|
|
|
|
decoded_string[1] = '/';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* invalid escape sequence */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string++;
|
|
|
|
}
|
2016-10-15 08:20:47 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:00:16 +03:00
|
|
|
decoded_string[0] = '\0';
|
2015-02-10 17:17:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:21:21 +03:00
|
|
|
/* non-broken cJSON_DetachItemFromArray */
|
|
|
|
static cJSON *detach_item_from_array(cJSON *array, size_t which)
|
|
|
|
{
|
|
|
|
cJSON *c = array->child;
|
|
|
|
while (c && (which > 0))
|
|
|
|
{
|
|
|
|
c = c->next;
|
|
|
|
which--;
|
|
|
|
}
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
/* item doesn't exist */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (c->prev)
|
|
|
|
{
|
|
|
|
/* not the first element */
|
|
|
|
c->prev->next = c->next;
|
|
|
|
}
|
|
|
|
if (c->next)
|
|
|
|
{
|
|
|
|
c->next->prev = c->prev;
|
|
|
|
}
|
|
|
|
if (c==array->child)
|
|
|
|
{
|
|
|
|
array->child = c->next;
|
|
|
|
}
|
|
|
|
/* make sure the detached item doesn't point anywhere anymore */
|
|
|
|
c->prev = c->next = NULL;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2017-02-03 17:59:00 +03:00
|
|
|
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2017-04-30 14:01:04 +03:00
|
|
|
unsigned char *parent_pointer = NULL;
|
|
|
|
unsigned char *child_pointer = NULL;
|
2016-11-13 13:45:40 +03:00
|
|
|
cJSON *parent = NULL;
|
2017-04-30 14:01:04 +03:00
|
|
|
cJSON *detached_item = NULL;
|
2016-10-16 21:17:31 +03:00
|
|
|
|
|
|
|
/* copy path and split it in parent and child */
|
2017-04-30 14:01:04 +03:00
|
|
|
parent_pointer = cJSONUtils_strdup(path);
|
|
|
|
if (parent_pointer == NULL) {
|
|
|
|
goto cleanup;
|
2017-01-30 21:30:16 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:01:04 +03:00
|
|
|
child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); /* last '/' */
|
|
|
|
if (child_pointer == NULL)
|
2016-10-16 21:17:31 +03:00
|
|
|
{
|
2017-04-30 14:01:04 +03:00
|
|
|
goto cleanup;
|
2016-10-16 21:17:31 +03:00
|
|
|
}
|
2017-01-30 21:30:16 +03:00
|
|
|
/* split strings */
|
2017-04-30 14:01:04 +03:00
|
|
|
child_pointer[0] = '\0';
|
|
|
|
child_pointer++;
|
2017-01-30 21:30:16 +03:00
|
|
|
|
2017-04-30 14:01:04 +03:00
|
|
|
parent = cJSONUtils_GetPointer(object, (char*)parent_pointer);
|
|
|
|
cJSONUtils_InplaceDecodePointerString(child_pointer);
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-30 14:01:04 +03:00
|
|
|
if (cJSON_IsArray(parent))
|
2016-10-16 21:17:31 +03:00
|
|
|
{
|
2017-04-12 12:21:21 +03:00
|
|
|
size_t index = 0;
|
2017-04-30 14:01:04 +03:00
|
|
|
if (!decode_array_index_from_pointer(child_pointer, &index))
|
2017-04-12 12:21:21 +03:00
|
|
|
{
|
2017-04-30 14:01:04 +03:00
|
|
|
goto cleanup;
|
2017-04-12 12:21:21 +03:00
|
|
|
}
|
2017-04-30 14:01:04 +03:00
|
|
|
detached_item = detach_item_from_array(parent, index);
|
2016-10-16 21:17:31 +03:00
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
else if (cJSON_IsObject(parent))
|
2016-10-16 21:17:31 +03:00
|
|
|
{
|
2017-04-30 14:01:04 +03:00
|
|
|
detached_item = cJSON_DetachItemFromObject(parent, (char*)child_pointer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Couldn't find object to remove child from. */
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (parent_pointer != NULL)
|
|
|
|
{
|
|
|
|
cJSON_free(parent_pointer);
|
2016-10-16 21:17:31 +03:00
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-30 14:01:04 +03:00
|
|
|
return detached_item;
|
2015-02-10 17:17:59 +03:00
|
|
|
}
|
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
static int cJSONUtils_Compare(cJSON *a, cJSON *b)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-02-08 05:00:44 +03:00
|
|
|
if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
|
|
|
/* mismatched type. */
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-18 18:48:40 +03:00
|
|
|
switch (a->type & 0xFF)
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
|
|
|
case cJSON_Number:
|
|
|
|
/* numeric mismatch. */
|
2017-04-30 14:06:09 +03:00
|
|
|
if ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble))
|
|
|
|
{
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
case cJSON_String:
|
|
|
|
/* string mismatch. */
|
2017-04-30 14:06:09 +03:00
|
|
|
if (strcmp(a->valuestring, b->valuestring) != 0)
|
|
|
|
{
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
case cJSON_Array:
|
2017-04-30 14:06:09 +03:00
|
|
|
for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
2017-04-30 14:06:09 +03:00
|
|
|
int status = cJSONUtils_Compare(a, b);
|
|
|
|
if (status != 0)
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
2017-04-30 14:06:09 +03:00
|
|
|
return status;
|
2016-10-16 21:23:37 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 14:06:09 +03:00
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
/* array size mismatch? (one of both children is not NULL) */
|
2017-04-30 14:06:09 +03:00
|
|
|
if ((a != NULL) || (b != NULL))
|
|
|
|
{
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
case cJSON_Object:
|
|
|
|
cJSONUtils_SortObject(a);
|
|
|
|
cJSONUtils_SortObject(b);
|
2017-04-30 14:06:09 +03:00
|
|
|
for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
2017-04-30 14:06:09 +03:00
|
|
|
int status = 0;
|
2016-10-16 21:23:37 +03:00
|
|
|
/* compare object keys */
|
2017-02-03 17:59:00 +03:00
|
|
|
if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
|
|
|
/* missing member */
|
|
|
|
return -6;
|
|
|
|
}
|
2017-04-30 14:06:09 +03:00
|
|
|
status = cJSONUtils_Compare(a, b);
|
|
|
|
if (status != 0)
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
2017-04-30 14:06:09 +03:00
|
|
|
return status;
|
2016-10-16 21:23:37 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 14:06:09 +03:00
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
/* object length mismatch (one of both children is not null) */
|
2017-04-30 14:06:09 +03:00
|
|
|
if ((a != NULL) || (b != NULL))
|
|
|
|
{
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-13 22:53:27 +03:00
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-04-30 14:06:09 +03:00
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
/* null, true or false */
|
|
|
|
return 0;
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:40:43 +03:00
|
|
|
/* non broken version of cJSON_InsertItemInArray */
|
|
|
|
static cJSON_bool insert_item_in_array(cJSON *array, size_t which, cJSON *newitem)
|
|
|
|
{
|
|
|
|
cJSON *child = array->child;
|
|
|
|
while (child && (which > 0))
|
|
|
|
{
|
|
|
|
child = child->next;
|
|
|
|
which--;
|
|
|
|
}
|
|
|
|
if (which > 0)
|
|
|
|
{
|
|
|
|
/* item is after the end of the array */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (child == NULL)
|
|
|
|
{
|
|
|
|
cJSON_AddItemToArray(array, newitem);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert into the linked list */
|
|
|
|
newitem->next = child;
|
|
|
|
newitem->prev = child->prev;
|
|
|
|
child->prev = newitem;
|
|
|
|
|
|
|
|
/* was it at the beginning */
|
|
|
|
if (child == array->child)
|
|
|
|
{
|
|
|
|
array->child = newitem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newitem->prev->next = newitem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:36:14 +03:00
|
|
|
enum patch_operation { INVALID, ADD, REMOVE, REPLACE, MOVE, COPY, TEST };
|
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
static enum patch_operation decode_patch_operation(const cJSON * const patch)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
cJSON *operation = cJSON_GetObjectItem(patch, "op");
|
|
|
|
if (!cJSON_IsString(operation))
|
|
|
|
{
|
|
|
|
return INVALID;
|
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
if (strcmp(operation->valuestring, "add") == 0)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
return ADD;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
if (strcmp(operation->valuestring, "remove") == 0)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
return REMOVE;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
|
|
|
|
if (strcmp(operation->valuestring, "replace") == 0)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
return REPLACE;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
|
|
|
|
if (strcmp(operation->valuestring, "move") == 0)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
return MOVE;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
|
|
|
|
if (strcmp(operation->valuestring, "copy") == 0)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
return COPY;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
|
|
|
|
if (strcmp(operation->valuestring, "test") == 0)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
return TEST;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
|
|
|
|
return INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* overwrite and existing item with another one and free resources on the way */
|
|
|
|
static void overwrite_item(cJSON * const root, const cJSON replacement)
|
|
|
|
{
|
|
|
|
if (root == NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
return;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
|
|
|
|
if (root->string != NULL)
|
|
|
|
{
|
|
|
|
cJSON_free(root->string);
|
|
|
|
}
|
|
|
|
if (root->valuestring != NULL)
|
|
|
|
{
|
|
|
|
cJSON_free(root->valuestring);
|
|
|
|
}
|
|
|
|
if (root->child != NULL)
|
|
|
|
{
|
|
|
|
cJSON_Delete(root->child);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(root, &replacement, sizeof(cJSON));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cJSONUtils_ApplyPatch(cJSON *object, const cJSON *patch)
|
|
|
|
{
|
|
|
|
cJSON *path = NULL;
|
|
|
|
cJSON *value = NULL;
|
|
|
|
cJSON *parent = NULL;
|
|
|
|
enum patch_operation opcode = INVALID;
|
|
|
|
unsigned char *parent_pointer = NULL;
|
|
|
|
unsigned char *child_pointer = NULL;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
path = cJSON_GetObjectItem(patch, "path");
|
|
|
|
if (!cJSON_IsString(path))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
/* malformed patch. */
|
|
|
|
status = 2;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
opcode = decode_patch_operation(patch);
|
|
|
|
if (opcode == INVALID)
|
|
|
|
{
|
|
|
|
status = 3;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
else if (opcode == TEST)
|
|
|
|
{
|
|
|
|
/* compare value: {...} with the given path */
|
|
|
|
status = cJSONUtils_Compare(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value"));
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-12 20:14:07 +03:00
|
|
|
/* special case for replacing the root */
|
|
|
|
if (path->valuestring[0] == '\0')
|
|
|
|
{
|
|
|
|
if (opcode == REMOVE)
|
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL};
|
2017-04-12 20:14:07 +03:00
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
overwrite_item(object, invalid);
|
2017-04-12 20:14:07 +03:00
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 0;
|
|
|
|
goto cleanup;
|
2017-04-12 20:14:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((opcode == REPLACE) || (opcode == ADD))
|
|
|
|
{
|
|
|
|
value = cJSON_GetObjectItem(patch, "value");
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
/* missing "value" for add/replace. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 7;
|
|
|
|
goto cleanup;
|
2017-04-12 20:14:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
value = cJSON_Duplicate(value, 1);
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
/* out of memory for add/replace. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 8;
|
|
|
|
goto cleanup;
|
2017-04-12 20:14:07 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
overwrite_item(object, *value);
|
2017-04-12 20:14:07 +03:00
|
|
|
|
|
|
|
/* delete the duplicated value */
|
|
|
|
cJSON_free(value);
|
2017-04-30 14:11:14 +03:00
|
|
|
value = NULL;
|
2017-04-12 20:14:07 +03:00
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
/* the string "value" isn't needed */
|
|
|
|
if (object->string != NULL)
|
|
|
|
{
|
|
|
|
cJSON_free(object->string);
|
|
|
|
object->string = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
goto cleanup;
|
2017-04-12 20:14:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:36:14 +03:00
|
|
|
if ((opcode == REMOVE) || (opcode == REPLACE))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* Get rid of old. */
|
2017-04-12 11:44:29 +03:00
|
|
|
cJSON *old_item = cJSONUtils_PatchDetach(object, (unsigned char*)path->valuestring);
|
|
|
|
if (old_item == NULL)
|
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 13;
|
|
|
|
goto cleanup;
|
2017-04-12 11:44:29 +03:00
|
|
|
}
|
|
|
|
cJSON_Delete(old_item);
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == REMOVE)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-12 11:44:29 +03:00
|
|
|
/* For Remove, this job is done. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 0;
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* Copy/Move uses "from". */
|
2017-04-12 12:36:14 +03:00
|
|
|
if ((opcode == MOVE) || (opcode == COPY))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
cJSON *from = cJSON_GetObjectItem(patch, "from");
|
2017-04-30 14:11:14 +03:00
|
|
|
if (from == NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* missing "from" for copy/move. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 4;
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == MOVE)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
value = cJSONUtils_PatchDetach(object, (unsigned char*)from->valuestring);
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == COPY)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
value = cJSONUtils_GetPointer(object, from->valuestring);
|
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
if (value == NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* missing "from" for copy/move. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 5;
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == COPY)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
value = cJSON_Duplicate(value, 1);
|
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
if (value == NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* out of memory for copy/move. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 6;
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* Add/Replace uses "value". */
|
|
|
|
{
|
|
|
|
value = cJSON_GetObjectItem(patch, "value");
|
2017-04-30 14:11:14 +03:00
|
|
|
if (value == NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* missing "value" for add/replace. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 7;
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
value = cJSON_Duplicate(value, 1);
|
2017-04-30 14:11:14 +03:00
|
|
|
if (value == NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* out of memory for add/replace. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 8;
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* Now, just add "value" to "path". */
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* split pointer in parent and child */
|
2017-04-30 14:11:14 +03:00
|
|
|
parent_pointer = cJSONUtils_strdup((unsigned char*)path->valuestring);
|
|
|
|
child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/');
|
|
|
|
if (child_pointer != NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
child_pointer[0] = '\0';
|
|
|
|
child_pointer++;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
parent = cJSONUtils_GetPointer(object, (char*)parent_pointer);
|
|
|
|
cJSONUtils_InplaceDecodePointerString(child_pointer);
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* add, remove, replace, move, copy, test. */
|
2017-04-30 14:11:14 +03:00
|
|
|
if ((parent == NULL) || (child_pointer == NULL))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* Couldn't find object to add to. */
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 9;
|
|
|
|
goto cleanup;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
else if (cJSON_IsArray(parent))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
if (strcmp((char*)child_pointer, "-") == 0)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
cJSON_AddItemToArray(parent, value);
|
2017-04-30 14:11:14 +03:00
|
|
|
value = NULL;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-12 12:07:21 +03:00
|
|
|
size_t index = 0;
|
2017-04-30 14:11:14 +03:00
|
|
|
if (!decode_array_index_from_pointer(child_pointer, &index))
|
2017-04-11 19:07:19 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 11;
|
|
|
|
goto cleanup;
|
2017-04-11 19:07:19 +03:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:21 +03:00
|
|
|
if (!insert_item_in_array(parent, index, value))
|
2017-04-11 18:40:43 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
status = 10;
|
|
|
|
goto cleanup;
|
2017-04-11 18:40:43 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
value = NULL;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
else if (cJSON_IsObject(parent))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-30 14:11:14 +03:00
|
|
|
cJSON_DeleteItemFromObject(parent, (char*)child_pointer);
|
|
|
|
cJSON_AddItemToObject(parent, (char*)child_pointer, value);
|
|
|
|
value = NULL;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (value != NULL)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
cJSON_Delete(value);
|
|
|
|
}
|
2017-04-30 14:11:14 +03:00
|
|
|
if (parent_pointer != NULL)
|
|
|
|
{
|
|
|
|
cJSON_free(parent_pointer);
|
|
|
|
}
|
2016-10-17 07:32:47 +03:00
|
|
|
|
2017-04-30 14:11:14 +03:00
|
|
|
return status;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-30 14:12:32 +03:00
|
|
|
CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2017-04-30 14:12:32 +03:00
|
|
|
const cJSON *current_patch = NULL;
|
|
|
|
int status = 0;
|
2017-02-07 21:07:18 +03:00
|
|
|
|
2017-04-11 17:38:51 +03:00
|
|
|
if (!cJSON_IsArray(patches))
|
2016-10-17 07:34:33 +03:00
|
|
|
{
|
|
|
|
/* malformed patches. */
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-30 14:12:32 +03:00
|
|
|
|
|
|
|
if (patches != NULL)
|
2016-10-17 07:34:33 +03:00
|
|
|
{
|
2017-04-30 14:12:32 +03:00
|
|
|
current_patch = patches->child;
|
2016-10-17 07:34:33 +03:00
|
|
|
}
|
2017-04-30 14:12:32 +03:00
|
|
|
|
|
|
|
while (current_patch != NULL)
|
2016-10-17 07:34:33 +03:00
|
|
|
{
|
2017-04-30 14:12:32 +03:00
|
|
|
status = cJSONUtils_ApplyPatch(object, current_patch);
|
|
|
|
if (status != 0)
|
2016-10-17 07:34:33 +03:00
|
|
|
{
|
2017-04-30 14:12:32 +03:00
|
|
|
return status;
|
2016-10-17 07:34:33 +03:00
|
|
|
}
|
2017-04-30 14:12:32 +03:00
|
|
|
current_patch = current_patch->next;
|
2016-10-17 07:34:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-02-10 17:17:59 +03:00
|
|
|
}
|
2015-02-09 21:29:05 +03:00
|
|
|
|
2017-04-30 14:12:57 +03:00
|
|
|
static void cJSONUtils_GeneratePatch(cJSON * const patches, const unsigned char * const operation, const unsigned char * const path, const unsigned char *suffix, const cJSON * const value)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2016-10-17 13:00:46 +03:00
|
|
|
cJSON *patch = cJSON_CreateObject();
|
2017-04-30 14:12:57 +03:00
|
|
|
if (patch == NULL)
|
2016-10-17 13:00:46 +03:00
|
|
|
{
|
2017-04-30 14:12:57 +03:00
|
|
|
return;
|
2016-10-17 13:00:46 +03:00
|
|
|
}
|
2017-04-30 14:12:57 +03:00
|
|
|
cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)operation));
|
|
|
|
|
|
|
|
if (suffix == NULL)
|
2016-10-17 13:00:46 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path));
|
2016-10-17 13:00:46 +03:00
|
|
|
}
|
2017-04-30 14:12:57 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t suffix_length = cJSONUtils_PointerEncodedstrlen(suffix);
|
|
|
|
size_t path_length = strlen((const char*)path);
|
|
|
|
unsigned char *full_path = (unsigned char*)cJSON_malloc(path_length + suffix_length + sizeof("/"));
|
|
|
|
|
|
|
|
sprintf((char*)full_path, "%s/", (const char*)path);
|
|
|
|
cJSONUtils_PointerEncodedstrcpy(full_path + path_length + 1, suffix);
|
|
|
|
|
|
|
|
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)full_path));
|
|
|
|
free(full_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != NULL)
|
2016-10-17 13:00:46 +03:00
|
|
|
{
|
2017-04-30 14:12:57 +03:00
|
|
|
cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(value, 1));
|
2016-10-17 13:00:46 +03:00
|
|
|
}
|
|
|
|
cJSON_AddItemToArray(patches, patch);
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:13:17 +03:00
|
|
|
CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value)
|
2016-10-17 13:01:52 +03:00
|
|
|
{
|
2017-04-30 14:13:17 +03:00
|
|
|
cJSONUtils_GeneratePatch(array, (const unsigned char*)operation, (const unsigned char*)path, NULL, value);
|
2016-10-17 13:01:52 +03:00
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-04-30 14:14:29 +03:00
|
|
|
static void cJSONUtils_CompareToPatch(cJSON * const patches, const unsigned char * const path, cJSON * const from, cJSON * const to)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-02-07 21:07:18 +03:00
|
|
|
if ((from == NULL) || (to == NULL))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-18 18:48:40 +03:00
|
|
|
if ((from->type & 0xFF) != (to->type & 0xFF))
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
|
2016-10-17 13:35:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:14:29 +03:00
|
|
|
switch (from->type & 0xFF)
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
|
|
|
case cJSON_Number:
|
|
|
|
if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
|
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, NULL, to);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case cJSON_String:
|
|
|
|
if (strcmp(from->valuestring, to->valuestring) != 0)
|
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, NULL, to);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
return;
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2016-10-17 13:35:49 +03:00
|
|
|
case cJSON_Array:
|
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
size_t index = 0;
|
|
|
|
cJSON *from_child = from->child;
|
|
|
|
cJSON *to_child = to->child;
|
|
|
|
unsigned char *new_path = (unsigned char*)cJSON_malloc(strlen((const char*)path) + 20 + sizeof("/")); /* Allow space for 64bit int. log10(2^64) = 20 */
|
|
|
|
|
|
|
|
/* generate patches for all array elements that exist in both "from" and "to" */
|
|
|
|
for (index = 0; (from_child != NULL) && (to_child != NULL); (void)(from_child = from_child->next), (void)(to_child = to_child->next), index++)
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-02-15 15:09:32 +03:00
|
|
|
/* check if conversion to unsigned long is valid
|
|
|
|
* This should be eliminated at compile time by dead code elimination
|
|
|
|
* if size_t is an alias of unsigned long, or if it is bigger */
|
2017-04-30 14:14:29 +03:00
|
|
|
if (index > ULONG_MAX)
|
2017-02-15 15:09:32 +03:00
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
free(new_path);
|
2017-02-15 15:09:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-04-30 14:14:29 +03:00
|
|
|
sprintf((char*)new_path, "%s/%lu", path, (unsigned long)index); /* path of the current array element */
|
|
|
|
cJSONUtils_CompareToPatch(patches, new_path, from_child, to_child);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
2017-04-30 14:14:29 +03:00
|
|
|
|
2016-10-17 13:35:49 +03:00
|
|
|
/* remove leftover elements from 'from' that are not in 'to' */
|
2017-04-30 14:14:29 +03:00
|
|
|
for (; (from_child != NULL); (void)(from_child = from_child->next))
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-02-15 15:09:32 +03:00
|
|
|
/* check if conversion to unsigned long is valid
|
|
|
|
* This should be eliminated at compile time by dead code elimination
|
|
|
|
* if size_t is an alias of unsigned long, or if it is bigger */
|
2017-04-30 14:14:29 +03:00
|
|
|
if (index > ULONG_MAX)
|
2017-02-15 15:09:32 +03:00
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
free(new_path);
|
2017-02-15 15:09:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-04-30 14:14:29 +03:00
|
|
|
sprintf((char*)new_path, "%lu", (unsigned long)index);
|
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, new_path, NULL);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
/* add new elements in 'to' that were not in 'from' */
|
2017-04-30 14:14:29 +03:00
|
|
|
for (; (to_child != NULL); (void)(to_child = to_child->next), index++)
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to_child);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
2017-04-30 14:14:29 +03:00
|
|
|
free(new_path);
|
2016-10-17 13:35:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case cJSON_Object:
|
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
cJSON *from_child = NULL;
|
|
|
|
cJSON *to_child = NULL;
|
2016-10-17 13:35:49 +03:00
|
|
|
cJSONUtils_SortObject(from);
|
|
|
|
cJSONUtils_SortObject(to);
|
|
|
|
|
2017-04-30 14:14:29 +03:00
|
|
|
from_child = from->child;
|
|
|
|
to_child = to->child;
|
2016-10-17 13:35:49 +03:00
|
|
|
/* for all object values in the object with more of them */
|
2017-04-30 14:14:29 +03:00
|
|
|
while ((from_child != NULL) || (to_child != NULL))
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-04-30 14:14:29 +03:00
|
|
|
int diff;
|
|
|
|
if (from_child == NULL)
|
|
|
|
{
|
|
|
|
diff = 1;
|
|
|
|
}
|
|
|
|
else if (to_child == NULL)
|
|
|
|
{
|
|
|
|
diff = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = cJSONUtils_strcasecmp((unsigned char*)from_child->string, (unsigned char*)to_child->string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diff == 0)
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
|
|
|
/* both object keys are the same */
|
2017-04-30 14:14:29 +03:00
|
|
|
size_t path_length = strlen((const char*)path);
|
|
|
|
size_t from_child_name_length = cJSONUtils_PointerEncodedstrlen((unsigned char*)from_child->string);
|
|
|
|
unsigned char *new_path = (unsigned char*)cJSON_malloc(path_length + from_child_name_length + sizeof("/"));
|
|
|
|
|
|
|
|
sprintf((char*)new_path, "%s/", path);
|
|
|
|
cJSONUtils_PointerEncodedstrcpy(new_path + path_length + 1, (unsigned char*)from_child->string);
|
|
|
|
|
2016-10-17 13:35:49 +03:00
|
|
|
/* create a patch for the element */
|
2017-04-30 14:14:29 +03:00
|
|
|
cJSONUtils_CompareToPatch(patches, new_path, from_child, to_child);
|
|
|
|
free(new_path);
|
|
|
|
|
|
|
|
from_child = from_child->next;
|
|
|
|
to_child = to_child->next;
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
else if (diff < 0)
|
|
|
|
{
|
|
|
|
/* object element doesn't exist in 'to' --> remove it */
|
2017-04-30 14:14:29 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)from_child->string, NULL);
|
|
|
|
|
|
|
|
from_child = from_child->next;
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* object element doesn't exist in 'from' --> add it */
|
2017-04-30 14:14:29 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)to_child->string, to_child);
|
|
|
|
|
|
|
|
to_child = to_child->next;
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-04-30 14:15:18 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2016-10-17 13:41:50 +03:00
|
|
|
cJSON *patches = cJSON_CreateArray();
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_CompareToPatch(patches, (const unsigned char*)"", from, to);
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2016-10-17 13:41:50 +03:00
|
|
|
return patches;
|
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-28 19:27:15 +03:00
|
|
|
/* sort lists using mergesort */
|
2015-02-13 22:19:26 +03:00
|
|
|
static cJSON *cJSONUtils_SortList(cJSON *list)
|
|
|
|
{
|
2016-10-28 19:27:15 +03:00
|
|
|
cJSON *first = list;
|
|
|
|
cJSON *second = list;
|
2017-04-30 14:16:30 +03:00
|
|
|
cJSON *current_item = list;
|
|
|
|
cJSON *result = list;
|
|
|
|
cJSON *result_tail = NULL;
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2017-04-30 14:16:30 +03:00
|
|
|
if ((list == NULL) || (list->next == NULL))
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* One entry is sorted already. */
|
2017-04-30 14:16:30 +03:00
|
|
|
return result;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2017-04-30 14:16:30 +03:00
|
|
|
while ((current_item != NULL) && (current_item->next != NULL) && (cJSONUtils_strcasecmp((unsigned char*)current_item->string, (unsigned char*)current_item->next->string) < 0))
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* Test for list sorted. */
|
2017-04-30 14:16:30 +03:00
|
|
|
current_item = current_item->next;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
2017-04-30 14:16:30 +03:00
|
|
|
if ((current_item == NULL) || (current_item->next == NULL))
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* Leave sorted lists unmodified. */
|
2017-04-30 14:16:30 +03:00
|
|
|
return result;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2017-04-30 14:16:30 +03:00
|
|
|
/* reset pointer to the beginning */
|
|
|
|
current_item = list;
|
|
|
|
while (current_item != NULL)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* Walk two pointers to find the middle. */
|
|
|
|
second = second->next;
|
2017-04-30 14:16:30 +03:00
|
|
|
current_item = current_item->next;
|
|
|
|
/* advances current_item two steps at a time */
|
|
|
|
if (current_item != NULL)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
2017-04-30 14:16:30 +03:00
|
|
|
current_item = current_item->next;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 14:16:30 +03:00
|
|
|
if ((second != NULL) && (second->prev != NULL))
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* Split the lists */
|
2016-11-13 13:45:40 +03:00
|
|
|
second->prev->next = NULL;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-28 19:27:15 +03:00
|
|
|
/* Recursively sort the sub-lists. */
|
|
|
|
first = cJSONUtils_SortList(first);
|
|
|
|
second = cJSONUtils_SortList(second);
|
2017-04-30 14:16:30 +03:00
|
|
|
result = NULL;
|
2016-10-28 19:27:15 +03:00
|
|
|
|
2017-04-30 14:16:30 +03:00
|
|
|
/* Merge the sub-lists */
|
|
|
|
while ((first != NULL) && (second != NULL))
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
2017-04-30 14:16:30 +03:00
|
|
|
cJSON *smaller = NULL;
|
2017-02-03 17:59:00 +03:00
|
|
|
if (cJSONUtils_strcasecmp((unsigned char*)first->string, (unsigned char*)second->string) < 0)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
2017-04-30 14:16:30 +03:00
|
|
|
smaller = first;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
smaller = second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
/* start merged list with the smaller element */
|
|
|
|
result_tail = smaller;
|
|
|
|
result = smaller;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* add smaller element to the list */
|
|
|
|
result_tail->next = smaller;
|
|
|
|
smaller->prev = result_tail;
|
|
|
|
result_tail = smaller;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first == smaller)
|
|
|
|
{
|
2016-10-28 19:27:15 +03:00
|
|
|
first = first->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
second = second->next;
|
|
|
|
}
|
|
|
|
}
|
2017-04-30 14:16:30 +03:00
|
|
|
|
|
|
|
if (first != NULL)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* Append rest of first list. */
|
2017-04-30 14:16:30 +03:00
|
|
|
if (result == NULL)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
return first;
|
|
|
|
}
|
2017-04-30 14:16:30 +03:00
|
|
|
result_tail->next = first;
|
|
|
|
first->prev = result_tail;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
2017-04-30 14:16:30 +03:00
|
|
|
if (second != NULL)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* Append rest of second list */
|
2017-04-30 14:16:30 +03:00
|
|
|
if (result == NULL)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
return second;
|
|
|
|
}
|
2017-04-30 14:16:30 +03:00
|
|
|
result_tail->next = second;
|
|
|
|
second->prev = result_tail;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:16:30 +03:00
|
|
|
return result;
|
2015-02-13 22:19:26 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:16:58 +03:00
|
|
|
CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object)
|
2016-10-28 19:37:21 +03:00
|
|
|
{
|
|
|
|
object->child = cJSONUtils_SortList(object->child);
|
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-04-30 14:17:39 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch)
|
2016-05-29 20:16:19 +03:00
|
|
|
{
|
2017-04-30 14:17:39 +03:00
|
|
|
cJSON *patch_child = NULL;
|
|
|
|
|
2017-02-26 23:26:34 +03:00
|
|
|
if (!cJSON_IsObject(patch))
|
2016-10-29 08:40:41 +03:00
|
|
|
{
|
|
|
|
/* scalar value, array or NULL, just duplicate */
|
|
|
|
cJSON_Delete(target);
|
|
|
|
return cJSON_Duplicate(patch, 1);
|
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-02-26 23:26:34 +03:00
|
|
|
if (!cJSON_IsObject(target))
|
2016-10-29 08:40:41 +03:00
|
|
|
{
|
|
|
|
cJSON_Delete(target);
|
|
|
|
target = cJSON_CreateObject();
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:17:39 +03:00
|
|
|
patch_child = patch->child;
|
|
|
|
while (patch_child != NULL)
|
2016-10-29 08:40:41 +03:00
|
|
|
{
|
2017-04-30 14:17:39 +03:00
|
|
|
if (cJSON_IsNull(patch_child))
|
2016-10-29 08:40:41 +03:00
|
|
|
{
|
|
|
|
/* NULL is the indicator to remove a value, see RFC7396 */
|
2017-04-30 14:17:39 +03:00
|
|
|
cJSON_DeleteItemFromObject(target, patch_child->string);
|
2016-10-29 08:40:41 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-30 14:17:39 +03:00
|
|
|
cJSON *replace_me = cJSON_DetachItemFromObject(target, patch_child->string);
|
|
|
|
cJSON_AddItemToObject(target, patch_child->string, cJSONUtils_MergePatch(replace_me, patch_child));
|
2016-10-29 08:40:41 +03:00
|
|
|
}
|
2017-04-30 14:17:39 +03:00
|
|
|
patch_child = patch_child->next;
|
2016-10-29 08:40:41 +03:00
|
|
|
}
|
|
|
|
return target;
|
2016-05-29 19:39:54 +03:00
|
|
|
}
|
2016-05-29 20:16:19 +03:00
|
|
|
|
2017-04-30 14:18:25 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to)
|
2016-05-29 20:16:19 +03:00
|
|
|
{
|
2017-04-30 14:18:25 +03:00
|
|
|
cJSON *from_child = NULL;
|
|
|
|
cJSON *to_child = NULL;
|
2016-11-13 13:45:40 +03:00
|
|
|
cJSON *patch = NULL;
|
2017-04-30 14:18:25 +03:00
|
|
|
if (to == NULL)
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
|
|
|
/* patch to delete everything */
|
|
|
|
return cJSON_CreateNull();
|
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
if (!cJSON_IsObject(to) || !cJSON_IsObject(from))
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
|
|
|
return cJSON_Duplicate(to, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSONUtils_SortObject(from);
|
|
|
|
cJSONUtils_SortObject(to);
|
|
|
|
|
2017-04-30 14:18:25 +03:00
|
|
|
from_child = from->child;
|
|
|
|
to_child = to->child;
|
2016-10-29 18:15:49 +03:00
|
|
|
patch = cJSON_CreateObject();
|
2017-04-30 14:18:25 +03:00
|
|
|
while (from_child || to_child)
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
2017-04-30 14:18:25 +03:00
|
|
|
int diff;
|
|
|
|
if (from_child != NULL)
|
|
|
|
{
|
|
|
|
if (to_child != NULL)
|
|
|
|
{
|
|
|
|
diff = strcmp(from_child->string, to_child->string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diff < 0)
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
|
|
|
/* from has a value that to doesn't have -> remove */
|
2017-04-30 14:18:25 +03:00
|
|
|
cJSON_AddItemToObject(patch, from_child->string, cJSON_CreateNull());
|
|
|
|
|
|
|
|
from_child = from_child->next;
|
2016-10-29 18:15:49 +03:00
|
|
|
}
|
2017-04-30 14:18:25 +03:00
|
|
|
else if (diff > 0)
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
|
|
|
/* to has a value that from doesn't have -> add to patch */
|
2017-04-30 14:18:25 +03:00
|
|
|
cJSON_AddItemToObject(patch, to_child->string, cJSON_Duplicate(to_child, 1));
|
|
|
|
|
|
|
|
to_child = to_child->next;
|
2016-10-29 18:15:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* object key exists in both objects */
|
2017-04-30 14:18:25 +03:00
|
|
|
if (cJSONUtils_Compare(from_child, to_child))
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
|
|
|
/* not identical --> generate a patch */
|
2017-04-30 14:18:25 +03:00
|
|
|
cJSON_AddItemToObject(patch, to_child->string, cJSONUtils_GenerateMergePatch(from_child, to_child));
|
2016-10-29 18:15:49 +03:00
|
|
|
}
|
2017-04-30 14:18:25 +03:00
|
|
|
|
2016-10-29 18:15:49 +03:00
|
|
|
/* next key in the object */
|
2017-04-30 14:18:25 +03:00
|
|
|
from_child = from_child->next;
|
|
|
|
to_child = to_child->next;
|
2016-10-29 18:15:49 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 14:18:25 +03:00
|
|
|
if (patch->child == NULL)
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
2017-04-30 14:18:25 +03:00
|
|
|
/* no patch generated */
|
2016-10-29 18:15:49 +03:00
|
|
|
cJSON_Delete(patch);
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-29 18:15:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return patch;
|
2016-10-13 19:26:53 +03:00
|
|
|
}
|