提交 af9c76e3 编写于 作者: M Max Bruckner

Add get_item_from_pointer: Configurable case sensitivity

上级 2f33e8ec
...@@ -254,7 +254,7 @@ static cJSON_bool decode_array_index_from_pointer(const unsigned char * const po ...@@ -254,7 +254,7 @@ static cJSON_bool decode_array_index_from_pointer(const unsigned char * const po
return 1; return 1;
} }
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer) static cJSON *get_item_from_pointer(cJSON * const object, const char * pointer, const cJSON_bool case_sensitive)
{ {
cJSON *current_element = object; cJSON *current_element = object;
/* follow path of the pointer */ /* follow path of the pointer */
...@@ -275,7 +275,7 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *po ...@@ -275,7 +275,7 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *po
{ {
current_element = current_element->child; current_element = current_element->child;
/* GetObjectItem. */ /* GetObjectItem. */
while ((current_element != NULL) && !compare_pointers((unsigned char*)current_element->string, (const unsigned char*)pointer, false)) while ((current_element != NULL) && !compare_pointers((unsigned char*)current_element->string, (const unsigned char*)pointer, case_sensitive))
{ {
current_element = current_element->next; current_element = current_element->next;
} }
...@@ -294,6 +294,11 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *po ...@@ -294,6 +294,11 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *po
return current_element; return current_element;
} }
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer)
{
return get_item_from_pointer(object, pointer, false);
}
/* JSON Patch implementation. */ /* JSON Patch implementation. */
static void decode_pointer_inplace(unsigned char *string) static void decode_pointer_inplace(unsigned char *string)
{ {
...@@ -364,6 +369,7 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which) ...@@ -364,6 +369,7 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which)
/* detach an item at the given path */ /* detach an item at the given path */
static cJSON *detach_path(cJSON *object, const unsigned char *path) static cJSON *detach_path(cJSON *object, const unsigned char *path)
{ {
cJSON_bool case_sensitive = false;
unsigned char *parent_pointer = NULL; unsigned char *parent_pointer = NULL;
unsigned char *child_pointer = NULL; unsigned char *child_pointer = NULL;
cJSON *parent = NULL; cJSON *parent = NULL;
...@@ -384,7 +390,7 @@ static cJSON *detach_path(cJSON *object, const unsigned char *path) ...@@ -384,7 +390,7 @@ static cJSON *detach_path(cJSON *object, const unsigned char *path)
child_pointer[0] = '\0'; child_pointer[0] = '\0';
child_pointer++; child_pointer++;
parent = cJSONUtils_GetPointer(object, (char*)parent_pointer); parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
decode_pointer_inplace(child_pointer); decode_pointer_inplace(child_pointer);
if (cJSON_IsArray(parent)) if (cJSON_IsArray(parent))
...@@ -610,6 +616,7 @@ static void overwrite_item(cJSON * const root, const cJSON replacement) ...@@ -610,6 +616,7 @@ static void overwrite_item(cJSON * const root, const cJSON replacement)
static int apply_patch(cJSON *object, const cJSON *patch) static int apply_patch(cJSON *object, const cJSON *patch)
{ {
cJSON_bool case_sensitive = false;
cJSON *path = NULL; cJSON *path = NULL;
cJSON *value = NULL; cJSON *value = NULL;
cJSON *parent = NULL; cJSON *parent = NULL;
...@@ -635,7 +642,7 @@ static int apply_patch(cJSON *object, const cJSON *patch) ...@@ -635,7 +642,7 @@ static int apply_patch(cJSON *object, const cJSON *patch)
else if (opcode == TEST) else if (opcode == TEST)
{ {
/* compare value: {...} with the given path */ /* compare value: {...} with the given path */
status = !compare_json(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value")); status = !compare_json(get_item_from_pointer(object, path->valuestring, case_sensitive), cJSON_GetObjectItem(patch, "value"));
goto cleanup; goto cleanup;
} }
...@@ -723,7 +730,7 @@ static int apply_patch(cJSON *object, const cJSON *patch) ...@@ -723,7 +730,7 @@ static int apply_patch(cJSON *object, const cJSON *patch)
} }
if (opcode == COPY) if (opcode == COPY)
{ {
value = cJSONUtils_GetPointer(object, from->valuestring); value = get_item_from_pointer(object, from->valuestring, case_sensitive);
} }
if (value == NULL) if (value == NULL)
{ {
...@@ -770,7 +777,7 @@ static int apply_patch(cJSON *object, const cJSON *patch) ...@@ -770,7 +777,7 @@ static int apply_patch(cJSON *object, const cJSON *patch)
child_pointer[0] = '\0'; child_pointer[0] = '\0';
child_pointer++; child_pointer++;
} }
parent = cJSONUtils_GetPointer(object, (char*)parent_pointer); parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
decode_pointer_inplace(child_pointer); decode_pointer_inplace(child_pointer);
/* add, remove, replace, move, copy, test. */ /* add, remove, replace, move, copy, test. */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册