MCPcopy Index your code
hub / github.com/DaveGamble/cJSON / apply_patch

Function apply_patch

cJSON_Utils.c:807–1036  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

805}
806
807static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_sensitive)
808{
809 cJSON *path = NULL;
810 cJSON *value = NULL;
811 cJSON *parent = NULL;
812 enum patch_operation opcode = INVALID;
813 unsigned char *parent_pointer = NULL;
814 unsigned char *child_pointer = NULL;
815 int status = 0;
816
817 path = get_object_item(patch, "path", case_sensitive);
818 if (!cJSON_IsString(path))
819 {
820 /* malformed patch. */
821 status = 2;
822 goto cleanup;
823 }
824
825 opcode = decode_patch_operation(patch, case_sensitive);
826 if (opcode == INVALID)
827 {
828 status = 3;
829 goto cleanup;
830 }
831 else if (opcode == TEST)
832 {
833 /* compare value: {...} with the given path */
834 status = !compare_json(get_item_from_pointer(object, path->valuestring, case_sensitive), get_object_item(patch, "value", case_sensitive), case_sensitive);
835 goto cleanup;
836 }
837
838 /* special case for replacing the root */
839 if (path->valuestring[0] == '\0')
840 {
841 if (opcode == REMOVE)
842 {
843 static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL};
844
845 overwrite_item(object, invalid);
846
847 status = 0;
848 goto cleanup;
849 }
850
851 if ((opcode == REPLACE) || (opcode == ADD))
852 {
853 value = get_object_item(patch, "value", case_sensitive);
854 if (value == NULL)
855 {
856 /* missing "value" for add/replace. */
857 status = 7;
858 goto cleanup;
859 }
860
861 value = cJSON_Duplicate(value, 1);
862 if (value == NULL)
863 {
864 /* out of memory for add/replace. */

Callers 2

cJSONUtils_ApplyPatchesFunction · 0.85

Calls 15

cJSON_IsStringFunction · 0.85
decode_patch_operationFunction · 0.85
compare_jsonFunction · 0.85
get_item_from_pointerFunction · 0.85
overwrite_itemFunction · 0.85
cJSON_DuplicateFunction · 0.85
cJSON_freeFunction · 0.85
detach_pathFunction · 0.85
cJSON_DeleteFunction · 0.85
cJSONUtils_strdupFunction · 0.85
decode_pointer_inplaceFunction · 0.85
cJSON_IsArrayFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…