cJSON_Utils.c 21.2 KB
Newer Older
1
#include <ctype.h>
2 3
#include <string.h>
#include <stdlib.h>
4
#include <stdio.h>
5 6
#include "cJSON_Utils.h"

7 8
static char* cJSONUtils_strdup(const char* str)
{
M
Max Bruckner 已提交
9 10
    size_t len = 0;
    char *copy = NULL;
11 12 13 14

    len = strlen(str) + 1;
    if (!(copy = (char*)malloc(len)))
    {
15
        return NULL;
16 17 18 19 20 21
    }
    memcpy(copy, str, len);

    return copy;
}

M
Max Bruckner 已提交
22
static int cJSONUtils_strcasecmp(const char *s1, const char *s2)
23
{
M
Max Bruckner 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    if (!s1)
    {
        return (s1 == s2) ? 0 : 1; /* both NULL? */
    }
    if (!s2)
    {
        return 1;
    }
    for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
    {
        if(*s1 == 0)
        {
            return 0;
        }
    }

    return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
41 42
}

43
/* JSON Pointer implementation: */
44
static int cJSONUtils_Pstrcasecmp(const char *a, const char *e)
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
    if (!a || !e)
    {
        return (a == e) ? 0 : 1; /* both NULL? */
    }
    for (; *a && *e && (*e != '/'); a++, e++) /* compare until next '/' */
    {
        if (*e == '~')
        {
            /* check for escaped '~' (~0) and '/' (~1) */
            if (!((e[1] == '0') && (*a == '~')) && !((e[1] == '1') && (*a == '/')))
            {
                /* invalid escape sequence or wrong character in *a */
                return 1;
            }
            else
            {
                e++;
            }
        }
        else if (tolower(*a) != tolower(*e))
        {
            return 1;
        }
    }
    if (((*e != 0) && (*e != '/')) != (*a != 0))
    {
        /* one string has ended, the other not */
        return 1;
    }

    return 0;
77 78
}

79 80 81 82 83 84 85 86 87 88 89 90 91
static int cJSONUtils_PointerEncodedstrlen(const char *s)
{
    int l = 0;
    for (; *s; s++, l++)
    {
        if ((*s == '~') || (*s == '/'))
        {
            l++;
        }
    }

    return l;
}
92

93
static void cJSONUtils_PointerEncodedstrcpy(char *d, const char *s)
94
{
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    for (; *s; s++)
    {
        if (*s == '/')
        {
            *d++ = '~';
            *d++ = '1';
        }
        else if (*s == '~')
        {
            *d++ = '~';
            *d++ = '0';
        }
        else
        {
            *d++ = *s;
        }
    }

    *d = '\0';
114 115
}

116
char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
117
{
118 119 120
    int type = object->type;
    int c = 0;
    cJSON *obj = 0;
121

122 123 124
    if (object == target)
    {
        /* found */
125
        return cJSONUtils_strdup("");
126
    }
127

128 129 130 131 132 133
    /* recursively search all children of the object */
    for (obj = object->child; obj; obj = obj->next, c++)
    {
        char *found = cJSONUtils_FindPointerFromObjectTo(obj, target);
        if (found)
        {
134
            if ((type & 0xFF) == cJSON_Array)
135 136 137 138 139 140 141 142
            {
                /* reserve enough memory for a 64 bit integer + '/' and '\0' */
                char *ret = (char*)malloc(strlen(found) + 23);
                sprintf(ret, "/%d%s", c, found); /* /<array_index><path> */
                free(found);

                return ret;
            }
143
            else if ((type & 0xFF) == cJSON_Object)
144 145 146 147 148 149 150 151 152 153 154 155
            {
                char *ret = (char*)malloc(strlen(found) + cJSONUtils_PointerEncodedstrlen(obj->string) + 2);
                *ret = '/';
                cJSONUtils_PointerEncodedstrcpy(ret + 1, obj->string);
                strcat(ret, found);
                free(found);

                return ret;
            }

            /* reached leaf of the tree, found nothing */
            free(found);
156
            return NULL;
157 158 159 160
        }
    }

    /* not found */
161
    return NULL;
162 163
}

164
cJSON *cJSONUtils_GetPointer(cJSON *object, const char *pointer)
165
{
166 167 168
    /* follow path of the pointer */
    while ((*pointer++ == '/') && object)
    {
169
        if ((object->type & 0xFF) == cJSON_Array)
170 171 172 173 174 175 176 177 178 179
        {
            int which = 0;
            /* parse array index */
            while ((*pointer >= '0') && (*pointer <= '9'))
            {
                which = (10 * which) + (*pointer++ - '0');
            }
            if (*pointer && (*pointer != '/'))
            {
                /* not end of string or new path token */
180
                return NULL;
181 182 183
            }
            object = cJSON_GetArrayItem(object, which);
        }
184
        else if ((object->type & 0xFF) == cJSON_Object)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        {
            object = object->child;
            /* GetObjectItem. */
            while (object && cJSONUtils_Pstrcasecmp(object->string, pointer))
            {
                object = object->next;
            }
            /* skip to the next path token or end of string */
            while (*pointer && (*pointer != '/'))
            {
                pointer++;
            }
        }
        else
        {
200
            return NULL;
201 202 203 204
        }
    }

    return object;
205 206
}

207
/* JSON Patch implementation. */
208 209
static void cJSONUtils_InplaceDecodePointerString(char *string)
{
210
    char *s2 = string;
211 212 213 214 215

    if (string == NULL) {
        return;
    }

216 217 218 219 220 221 222 223 224 225
    for (; *string; s2++, string++)
    {
        *s2 = (*string != '~')
            ? (*string)
            : ((*(++string) == '0')
                    ? '~'
                    : '/');
    }

    *s2 = '\0';
226 227
}

228
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path)
229
{
230 231 232 233
    char *parentptr = NULL;
    char *childptr = NULL;
    cJSON *parent = NULL;
    cJSON *ret = NULL;
234 235

    /* copy path and split it in parent and child */
236
    parentptr = cJSONUtils_strdup(path);
237 238 239 240
    if (parentptr == NULL) {
        return NULL;
    }

241
    childptr = strrchr(parentptr, '/'); /* last '/' */
242
    if (childptr == NULL)
243
    {
244 245
        free(parentptr);
        return NULL;
246
    }
247 248 249
    /* split strings */
    *childptr++ = '\0';

250 251
    parent = cJSONUtils_GetPointer(object, parentptr);
    cJSONUtils_InplaceDecodePointerString(childptr);
252

253 254 255
    if (!parent)
    {
        /* Couldn't find object to remove child from. */
256
        ret = NULL;
257
    }
258
    else if ((parent->type & 0xFF) == cJSON_Array)
259 260 261
    {
        ret = cJSON_DetachItemFromArray(parent, atoi(childptr));
    }
262
    else if ((parent->type & 0xFF) == cJSON_Object)
263 264 265 266
    {
        ret = cJSON_DetachItemFromObject(parent, childptr);
    }
    free(parentptr);
267

268 269
    /* return the detachted item */
    return ret;
270 271
}

M
Max Bruckner 已提交
272
static int cJSONUtils_Compare(cJSON *a, cJSON *b)
273
{
274
    if ((a->type & 0xFF) != (b->type & 0xFF))
M
Max Bruckner 已提交
275 276 277 278
    {
        /* mismatched type. */
        return -1;
    }
279
    switch (a->type & 0xFF)
M
Max Bruckner 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    {
        case cJSON_Number:
            /* numeric mismatch. */
            return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
        case cJSON_String:
            /* string mismatch. */
            return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
        case cJSON_Array:
            for (a = a->child, b = b->child; a && b; a = a->next, b = b->next)
            {
                int err = cJSONUtils_Compare(a, b);
                if (err)
                {
                    return err;
                }
            }
            /* array size mismatch? (one of both children is not NULL) */
            return (a || b) ? -4 : 0;
        case cJSON_Object:
            cJSONUtils_SortObject(a);
            cJSONUtils_SortObject(b);
            a = a->child;
            b = b->child;
            while (a && b)
            {
M
Max Bruckner 已提交
305
                int err = 0;
M
Max Bruckner 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
                /* compare object keys */
                if (cJSONUtils_strcasecmp(a->string, b->string))
                {
                    /* missing member */
                    return -6;
                }
                err = cJSONUtils_Compare(a, b);
                if (err)
                {
                    return err;
                }
                a = a->next;
                b = b->next;
            }
            /* object length mismatch (one of both children is not null) */
            return (a || b) ? -5 : 0;
322

M
Max Bruckner 已提交
323 324 325 326 327
        default:
            break;
    }
    /* null, true or false */
    return 0;
328 329
}

M
Max Bruckner 已提交
330
static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
331
{
332 333 334 335
    cJSON *op = NULL;
    cJSON *path = NULL;
    cJSON *value = NULL;
    cJSON *parent = NULL;
M
Max Bruckner 已提交
336
    int opcode = 0;
337 338
    char *parentptr = NULL;
    char *childptr = NULL;
339

M
Max Bruckner 已提交
340 341 342 343 344 345 346
    op = cJSON_GetObjectItem(patch, "op");
    path = cJSON_GetObjectItem(patch, "path");
    if (!op || !path)
    {
        /* malformed patch. */
        return 2;
    }
347

M
Max Bruckner 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    /* decode operation */
    if (!strcmp(op->valuestring, "add"))
    {
        opcode = 0;
    }
    else if (!strcmp(op->valuestring, "remove"))
    {
        opcode = 1;
    }
    else if (!strcmp(op->valuestring, "replace"))
    {
        opcode = 2;
    }
    else if (!strcmp(op->valuestring, "move"))
    {
        opcode = 3;
    }
    else if (!strcmp(op->valuestring, "copy"))
    {
        opcode = 4;
    }
    else if (!strcmp(op->valuestring, "test"))
    {
        /* compare value: {...} with the given path */
        return cJSONUtils_Compare(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value"));
    }
    else
    {
        /* unknown opcode. */
        return 3;
    }
379

M
Max Bruckner 已提交
380 381 382 383 384 385 386 387 388 389 390
    /* Remove/Replace */
    if ((opcode == 1) || (opcode == 2))
    {
        /* Get rid of old. */
        cJSON_Delete(cJSONUtils_PatchDetach(object, path->valuestring));
        if (opcode == 1)
        {
            /* For Remove, this is job done. */
            return 0;
        }
    }
391

M
Max Bruckner 已提交
392 393 394 395 396 397 398 399 400
    /* Copy/Move uses "from". */
    if ((opcode == 3) || (opcode == 4))
    {
        cJSON *from = cJSON_GetObjectItem(patch, "from");
        if (!from)
        {
            /* missing "from" for copy/move. */
            return 4;
        }
401

M
Max Bruckner 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        if (opcode == 3)
        {
            /* move */
            value = cJSONUtils_PatchDetach(object, from->valuestring);
        }
        if (opcode == 4)
        {
            /* copy */
            value = cJSONUtils_GetPointer(object, from->valuestring);
        }
        if (!value)
        {
            /* missing "from" for copy/move. */
            return 5;
        }
        if (opcode == 4)
        {
            value = cJSON_Duplicate(value, 1);
        }
        if (!value)
        {
            /* out of memory for copy/move. */
            return 6;
        }
    }
    else /* Add/Replace uses "value". */
    {
        value = cJSON_GetObjectItem(patch, "value");
        if (!value)
        {
            /* missing "value" for add/replace. */
            return 7;
        }
        value = cJSON_Duplicate(value, 1);
        if (!value)
        {
            /* out of memory for add/replace. */
            return 8;
        }
    }
442

M
Max Bruckner 已提交
443
    /* Now, just add "value" to "path". */
444

M
Max Bruckner 已提交
445
    /* split pointer in parent and child */
446
    parentptr = cJSONUtils_strdup(path->valuestring);
M
Max Bruckner 已提交
447 448 449 450 451 452 453
    childptr = strrchr(parentptr, '/');
    if (childptr)
    {
        *childptr++ = '\0';
    }
    parent = cJSONUtils_GetPointer(object, parentptr);
    cJSONUtils_InplaceDecodePointerString(childptr);
454

M
Max Bruckner 已提交
455 456 457 458 459 460 461 462
    /* add, remove, replace, move, copy, test. */
    if (!parent)
    {
        /* Couldn't find object to add to. */
        free(parentptr);
        cJSON_Delete(value);
        return 9;
    }
463
    else if ((parent->type & 0xFF) == cJSON_Array)
M
Max Bruckner 已提交
464 465 466 467 468 469 470 471 472 473
    {
        if (!strcmp(childptr, "-"))
        {
            cJSON_AddItemToArray(parent, value);
        }
        else
        {
            cJSON_InsertItemInArray(parent, atoi(childptr), value);
        }
    }
474
    else if ((parent->type & 0xFF) == cJSON_Object)
M
Max Bruckner 已提交
475 476 477 478 479 480 481 482 483 484 485 486
    {
        cJSON_DeleteItemFromObject(parent, childptr);
        cJSON_AddItemToObject(parent, childptr, value);
    }
    else
    {
        cJSON_Delete(value);
    }
    free(parentptr);

    return 0;
}
487

488
int cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
489
{
M
Max Bruckner 已提交
490
    int err = 0;
491
    if ((patches->type & 0xFF) != cJSON_Array)
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    {
        /* malformed patches. */
        return 1;
    }
    if (patches)
    {
        patches = patches->child;
    }
    while (patches)
    {
        if ((err = cJSONUtils_ApplyPatch(object, patches)))
        {
            return err;
        }
        patches = patches->next;
    }

    return 0;
510
}
511

512
static void cJSONUtils_GeneratePatch(cJSON *patches, const char *op, const char *path, const char *suffix, cJSON *val)
513
{
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    cJSON *patch = cJSON_CreateObject();
    cJSON_AddItemToObject(patch, "op", cJSON_CreateString(op));
    if (suffix)
    {
        char *newpath = (char*)malloc(strlen(path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2);
        cJSONUtils_PointerEncodedstrcpy(newpath + sprintf(newpath, "%s/", path), suffix);
        cJSON_AddItemToObject(patch, "path", cJSON_CreateString(newpath));
        free(newpath);
    }
    else
    {
        cJSON_AddItemToObject(patch, "path", cJSON_CreateString(path));
    }
    if (val)
    {
        cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1));
    }
    cJSON_AddItemToArray(patches, patch);
532 533
}

M
Max Bruckner 已提交
534 535 536 537
void cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val)
{
    cJSONUtils_GeneratePatch(array, op, path, 0, val);
}
538

539
static void cJSONUtils_CompareToPatch(cJSON *patches, const char *path, cJSON *from, cJSON *to)
540
{
541
    if ((from->type & 0xFF) != (to->type & 0xFF))
542 543 544 545 546
    {
        cJSONUtils_GeneratePatch(patches, "replace", path, 0, to);
        return;
    }

547
    switch ((from->type & 0xFF))
548 549 550 551 552 553 554 555 556 557 558 559 560 561
    {
        case cJSON_Number:
            if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
            {
                cJSONUtils_GeneratePatch(patches, "replace", path, 0, to);
            }
            return;

        case cJSON_String:
            if (strcmp(from->valuestring, to->valuestring) != 0)
            {
                cJSONUtils_GeneratePatch(patches, "replace", path, 0, to);
            }
            return;
562

563 564
        case cJSON_Array:
        {
M
Max Bruckner 已提交
565
            int c = 0;
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
            char *newpath = (char*)malloc(strlen(path) + 23); /* Allow space for 64bit int. */
            /* generate patches for all array elements that exist in "from" and "to" */
            for (c = 0, from = from->child, to = to->child; from && to; from = from->next, to = to->next, c++)
            {
                sprintf(newpath, "%s/%d", path, c); /* path of the current array element */
                cJSONUtils_CompareToPatch(patches, newpath, from, to);
            }
            /* remove leftover elements from 'from' that are not in 'to' */
            for (; from; from = from->next, c++)
            {
                sprintf(newpath, "%d", c);
                cJSONUtils_GeneratePatch(patches, "remove", path, newpath, 0);
            }
            /* add new elements in 'to' that were not in 'from' */
            for (; to; to = to->next, c++)
            {
                cJSONUtils_GeneratePatch(patches, "add", path, "-", to);
            }
            free(newpath);
            return;
        }

        case cJSON_Object:
        {
M
Max Bruckner 已提交
590 591
            cJSON *a = NULL;
            cJSON *b = NULL;
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            cJSONUtils_SortObject(from);
            cJSONUtils_SortObject(to);

            a = from->child;
            b = to->child;
            /* for all object values in the object with more of them */
            while (a || b)
            {
                int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp(a->string, b->string));
                if (!diff)
                {
                    /* both object keys are the same */
                    char *newpath = (char*)malloc(strlen(path) + cJSONUtils_PointerEncodedstrlen(a->string) + 2);
                    cJSONUtils_PointerEncodedstrcpy(newpath + sprintf(newpath, "%s/", path), a->string);
                    /* create a patch for the element */
                    cJSONUtils_CompareToPatch(patches, newpath, a, b);
                    free(newpath);
                    a = a->next;
                    b = b->next;
                }
                else if (diff < 0)
                {
                    /* object element doesn't exist in 'to' --> remove it */
                    cJSONUtils_GeneratePatch(patches, "remove", path, a->string, 0);
                    a = a->next;
                }
                else
                {
                    /* object element doesn't exist in 'from' --> add it */
                    cJSONUtils_GeneratePatch(patches, "add", path, b->string, b);
                    b = b->next;
                }
            }
            return;
        }

        default:
            break;
    }
}
632

633
cJSON* cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
634
{
635 636
    cJSON *patches = cJSON_CreateArray();
    cJSONUtils_CompareToPatch(patches, "", from, to);
637

638 639
    return patches;
}
640

M
Max Bruckner 已提交
641
/* sort lists using mergesort */
642 643
static cJSON *cJSONUtils_SortList(cJSON *list)
{
M
Max Bruckner 已提交
644 645 646
    cJSON *first = list;
    cJSON *second = list;
    cJSON *ptr = list;
647

M
Max Bruckner 已提交
648 649 650 651 652
    if (!list || !list->next)
    {
        /* One entry is sorted already. */
        return list;
    }
653

M
Max Bruckner 已提交
654 655 656 657 658 659 660 661 662 663
    while (ptr && ptr->next && (cJSONUtils_strcasecmp(ptr->string, ptr->next->string) < 0))
    {
        /* Test for list sorted. */
        ptr = ptr->next;
    }
    if (!ptr || !ptr->next)
    {
        /* Leave sorted lists unmodified. */
        return list;
    }
664

M
Max Bruckner 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    /* reset ptr to the beginning */
    ptr = list;
    while (ptr)
    {
        /* Walk two pointers to find the middle. */
        second = second->next;
        ptr = ptr->next;
        /* advances ptr two steps at a time */
        if (ptr)
        {
            ptr = ptr->next;
        }
    }
    if (second && second->prev)
    {
        /* Split the lists */
681
        second->prev->next = NULL;
M
Max Bruckner 已提交
682
    }
683

M
Max Bruckner 已提交
684 685 686
    /* Recursively sort the sub-lists. */
    first = cJSONUtils_SortList(first);
    second = cJSONUtils_SortList(second);
687
    list = ptr = NULL;
M
Max Bruckner 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

    while (first && second) /* Merge the sub-lists */
    {
        if (cJSONUtils_strcasecmp(first->string, second->string) < 0)
        {
            if (!list)
            {
                /* start merged list with the first element of the first list */
                list = ptr = first;
            }
            else
            {
                /* add first element of first list to merged list */
                ptr->next = first;
                first->prev = ptr;
                ptr = first;
            }
            first = first->next;
        }
        else
        {
            if (!list)
            {
                /* start merged list with the first element of the second list */
                list = ptr = second;
            }
            else
            {
                /* add first element of second list to merged list */
                ptr->next = second;
                second->prev = ptr;
                ptr = second;
            }
            second = second->next;
        }
    }
    if (first)
    {
        /* Append rest of first list. */
        if (!list)
        {
            return first;
        }
        ptr->next = first;
        first->prev = ptr;
    }
    if (second)
    {
        /* Append rest of second list */
        if (!list)
        {
            return second;
        }
        ptr->next = second;
        second->prev = ptr;
    }

    return list;
746 747
}

M
Max Bruckner 已提交
748 749 750 751
void cJSONUtils_SortObject(cJSON *object)
{
    object->child = cJSONUtils_SortList(object->child);
}
752

753 754
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
{
755
    if (!patch || ((patch->type & 0xFF) != cJSON_Object))
M
Max Bruckner 已提交
756 757 758 759 760
    {
        /* scalar value, array or NULL, just duplicate */
        cJSON_Delete(target);
        return cJSON_Duplicate(patch, 1);
    }
761

762
    if (!target || ((target->type & 0xFF) != cJSON_Object))
M
Max Bruckner 已提交
763 764 765 766 767 768 769 770
    {
        cJSON_Delete(target);
        target = cJSON_CreateObject();
    }

    patch = patch->child;
    while (patch)
    {
771
        if ((patch->type & 0xFF) == cJSON_NULL)
M
Max Bruckner 已提交
772 773 774 775 776 777 778 779 780 781 782 783
        {
            /* NULL is the indicator to remove a value, see RFC7396 */
            cJSON_DeleteItemFromObject(target, patch->string);
        }
        else
        {
            cJSON *replaceme = cJSON_DetachItemFromObject(target, patch->string);
            cJSON_AddItemToObject(target, patch->string, cJSONUtils_MergePatch(replaceme, patch));
        }
        patch = patch->next;
    }
    return target;
784
}
785

786
cJSON *cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
787
{
788
    cJSON *patch = NULL;
789 790 791 792 793
    if (!to)
    {
        /* patch to delete everything */
        return cJSON_CreateNull();
    }
794
    if (((to->type & 0xFF) != cJSON_Object) || !from || ((from->type & 0xFF) != cJSON_Object))
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
    {
        return cJSON_Duplicate(to, 1);
    }

    cJSONUtils_SortObject(from);
    cJSONUtils_SortObject(to);

    from = from->child;
    to = to->child;
    patch = cJSON_CreateObject();
    while (from || to)
    {
        int compare = from ? (to ? strcmp(from->string, to->string) : -1) : 1;
        if (compare < 0)
        {
            /* from has a value that to doesn't have -> remove */
            cJSON_AddItemToObject(patch, from->string, cJSON_CreateNull());
            from = from->next;
        }
        else if (compare > 0)
        {
            /* to has a value that from doesn't have -> add to patch */
            cJSON_AddItemToObject(patch, to->string, cJSON_Duplicate(to, 1));
            to = to->next;
        }
        else
        {
            /* object key exists in both objects */
            if (cJSONUtils_Compare(from, to))
            {
                /* not identical --> generate a patch */
                cJSON_AddItemToObject(patch, to->string, cJSONUtils_GenerateMergePatch(from, to));
            }
            /* next key in the object */
            from = from->next;
            to = to->next;
        }
    }
    if (!patch->child)
    {
        cJSON_Delete(patch);
836
        return NULL;
837 838 839
    }

    return patch;
M
Max Bruckner 已提交
840
}