cJSON_Utils.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include "cJSON_Utils.h"
  6. static char* cJSONUtils_strdup(const char* str)
  7. {
  8. size_t len = 0;
  9. char *copy = NULL;
  10. len = strlen(str) + 1;
  11. if (!(copy = (char*)malloc(len)))
  12. {
  13. return NULL;
  14. }
  15. memcpy(copy, str, len);
  16. return copy;
  17. }
  18. static int cJSONUtils_strcasecmp(const char *s1, const char *s2)
  19. {
  20. if (!s1)
  21. {
  22. return (s1 == s2) ? 0 : 1; /* both NULL? */
  23. }
  24. if (!s2)
  25. {
  26. return 1;
  27. }
  28. for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
  29. {
  30. if(*s1 == 0)
  31. {
  32. return 0;
  33. }
  34. }
  35. return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
  36. }
  37. /* JSON Pointer implementation: */
  38. static int cJSONUtils_Pstrcasecmp(const char *a, const char *e)
  39. {
  40. if (!a || !e)
  41. {
  42. return (a == e) ? 0 : 1; /* both NULL? */
  43. }
  44. for (; *a && *e && (*e != '/'); a++, e++) /* compare until next '/' */
  45. {
  46. if (*e == '~')
  47. {
  48. /* check for escaped '~' (~0) and '/' (~1) */
  49. if (!((e[1] == '0') && (*a == '~')) && !((e[1] == '1') && (*a == '/')))
  50. {
  51. /* invalid escape sequence or wrong character in *a */
  52. return 1;
  53. }
  54. else
  55. {
  56. e++;
  57. }
  58. }
  59. else if (tolower(*a) != tolower(*e))
  60. {
  61. return 1;
  62. }
  63. }
  64. if (((*e != 0) && (*e != '/')) != (*a != 0))
  65. {
  66. /* one string has ended, the other not */
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. static int cJSONUtils_PointerEncodedstrlen(const char *s)
  72. {
  73. int l = 0;
  74. for (; *s; s++, l++)
  75. {
  76. if ((*s == '~') || (*s == '/'))
  77. {
  78. l++;
  79. }
  80. }
  81. return l;
  82. }
  83. static void cJSONUtils_PointerEncodedstrcpy(char *d, const char *s)
  84. {
  85. for (; *s; s++)
  86. {
  87. if (*s == '/')
  88. {
  89. *d++ = '~';
  90. *d++ = '1';
  91. }
  92. else if (*s == '~')
  93. {
  94. *d++ = '~';
  95. *d++ = '0';
  96. }
  97. else
  98. {
  99. *d++ = *s;
  100. }
  101. }
  102. *d = '\0';
  103. }
  104. char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
  105. {
  106. int type = object->type;
  107. int c = 0;
  108. cJSON *obj = 0;
  109. if (object == target)
  110. {
  111. /* found */
  112. return cJSONUtils_strdup("");
  113. }
  114. /* recursively search all children of the object */
  115. for (obj = object->child; obj; obj = obj->next, c++)
  116. {
  117. char *found = cJSONUtils_FindPointerFromObjectTo(obj, target);
  118. if (found)
  119. {
  120. if ((type & 0xFF) == cJSON_Array)
  121. {
  122. /* reserve enough memory for a 64 bit integer + '/' and '\0' */
  123. char *ret = (char*)malloc(strlen(found) + 23);
  124. sprintf(ret, "/%d%s", c, found); /* /<array_index><path> */
  125. free(found);
  126. return ret;
  127. }
  128. else if ((type & 0xFF) == cJSON_Object)
  129. {
  130. char *ret = (char*)malloc(strlen(found) + cJSONUtils_PointerEncodedstrlen(obj->string) + 2);
  131. *ret = '/';
  132. cJSONUtils_PointerEncodedstrcpy(ret + 1, obj->string);
  133. strcat(ret, found);
  134. free(found);
  135. return ret;
  136. }
  137. /* reached leaf of the tree, found nothing */
  138. free(found);
  139. return NULL;
  140. }
  141. }
  142. /* not found */
  143. return NULL;
  144. }
  145. cJSON *cJSONUtils_GetPointer(cJSON *object, const char *pointer)
  146. {
  147. /* follow path of the pointer */
  148. while ((*pointer++ == '/') && object)
  149. {
  150. if ((object->type & 0xFF) == cJSON_Array)
  151. {
  152. int which = 0;
  153. /* parse array index */
  154. while ((*pointer >= '0') && (*pointer <= '9'))
  155. {
  156. which = (10 * which) + (*pointer++ - '0');
  157. }
  158. if (*pointer && (*pointer != '/'))
  159. {
  160. /* not end of string or new path token */
  161. return NULL;
  162. }
  163. object = cJSON_GetArrayItem(object, which);
  164. }
  165. else if ((object->type & 0xFF) == cJSON_Object)
  166. {
  167. object = object->child;
  168. /* GetObjectItem. */
  169. while (object && cJSONUtils_Pstrcasecmp(object->string, pointer))
  170. {
  171. object = object->next;
  172. }
  173. /* skip to the next path token or end of string */
  174. while (*pointer && (*pointer != '/'))
  175. {
  176. pointer++;
  177. }
  178. }
  179. else
  180. {
  181. return NULL;
  182. }
  183. }
  184. return object;
  185. }
  186. /* JSON Patch implementation. */
  187. static void cJSONUtils_InplaceDecodePointerString(char *string)
  188. {
  189. char *s2 = string;
  190. if (string == NULL) {
  191. return;
  192. }
  193. for (; *string; s2++, string++)
  194. {
  195. *s2 = (*string != '~')
  196. ? (*string)
  197. : ((*(++string) == '0')
  198. ? '~'
  199. : '/');
  200. }
  201. *s2 = '\0';
  202. }
  203. static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path)
  204. {
  205. char *parentptr = NULL;
  206. char *childptr = NULL;
  207. cJSON *parent = NULL;
  208. cJSON *ret = NULL;
  209. /* copy path and split it in parent and child */
  210. parentptr = cJSONUtils_strdup(path);
  211. childptr = strrchr(parentptr, '/'); /* last '/' */
  212. if (childptr)
  213. {
  214. /* split strings */
  215. *childptr++ = '\0';
  216. }
  217. parent = cJSONUtils_GetPointer(object, parentptr);
  218. cJSONUtils_InplaceDecodePointerString(childptr);
  219. if (!parent)
  220. {
  221. /* Couldn't find object to remove child from. */
  222. ret = NULL;
  223. }
  224. else if ((parent->type & 0xFF) == cJSON_Array)
  225. {
  226. ret = cJSON_DetachItemFromArray(parent, atoi(childptr));
  227. }
  228. else if ((parent->type & 0xFF) == cJSON_Object)
  229. {
  230. ret = cJSON_DetachItemFromObject(parent, childptr);
  231. }
  232. free(parentptr);
  233. /* return the detachted item */
  234. return ret;
  235. }
  236. static int cJSONUtils_Compare(cJSON *a, cJSON *b)
  237. {
  238. if ((a->type & 0xFF) != (b->type & 0xFF))
  239. {
  240. /* mismatched type. */
  241. return -1;
  242. }
  243. switch (a->type & 0xFF)
  244. {
  245. case cJSON_Number:
  246. /* numeric mismatch. */
  247. return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
  248. case cJSON_String:
  249. /* string mismatch. */
  250. return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
  251. case cJSON_Array:
  252. for (a = a->child, b = b->child; a && b; a = a->next, b = b->next)
  253. {
  254. int err = cJSONUtils_Compare(a, b);
  255. if (err)
  256. {
  257. return err;
  258. }
  259. }
  260. /* array size mismatch? (one of both children is not NULL) */
  261. return (a || b) ? -4 : 0;
  262. case cJSON_Object:
  263. cJSONUtils_SortObject(a);
  264. cJSONUtils_SortObject(b);
  265. a = a->child;
  266. b = b->child;
  267. while (a && b)
  268. {
  269. int err = 0;
  270. /* compare object keys */
  271. if (cJSONUtils_strcasecmp(a->string, b->string))
  272. {
  273. /* missing member */
  274. return -6;
  275. }
  276. err = cJSONUtils_Compare(a, b);
  277. if (err)
  278. {
  279. return err;
  280. }
  281. a = a->next;
  282. b = b->next;
  283. }
  284. /* object length mismatch (one of both children is not null) */
  285. return (a || b) ? -5 : 0;
  286. default:
  287. break;
  288. }
  289. /* null, true or false */
  290. return 0;
  291. }
  292. static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
  293. {
  294. cJSON *op = NULL;
  295. cJSON *path = NULL;
  296. cJSON *value = NULL;
  297. cJSON *parent = NULL;
  298. int opcode = 0;
  299. char *parentptr = NULL;
  300. char *childptr = NULL;
  301. op = cJSON_GetObjectItem(patch, "op");
  302. path = cJSON_GetObjectItem(patch, "path");
  303. if (!op || !path)
  304. {
  305. /* malformed patch. */
  306. return 2;
  307. }
  308. /* decode operation */
  309. if (!strcmp(op->valuestring, "add"))
  310. {
  311. opcode = 0;
  312. }
  313. else if (!strcmp(op->valuestring, "remove"))
  314. {
  315. opcode = 1;
  316. }
  317. else if (!strcmp(op->valuestring, "replace"))
  318. {
  319. opcode = 2;
  320. }
  321. else if (!strcmp(op->valuestring, "move"))
  322. {
  323. opcode = 3;
  324. }
  325. else if (!strcmp(op->valuestring, "copy"))
  326. {
  327. opcode = 4;
  328. }
  329. else if (!strcmp(op->valuestring, "test"))
  330. {
  331. /* compare value: {...} with the given path */
  332. return cJSONUtils_Compare(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value"));
  333. }
  334. else
  335. {
  336. /* unknown opcode. */
  337. return 3;
  338. }
  339. /* Remove/Replace */
  340. if ((opcode == 1) || (opcode == 2))
  341. {
  342. /* Get rid of old. */
  343. cJSON_Delete(cJSONUtils_PatchDetach(object, path->valuestring));
  344. if (opcode == 1)
  345. {
  346. /* For Remove, this is job done. */
  347. return 0;
  348. }
  349. }
  350. /* Copy/Move uses "from". */
  351. if ((opcode == 3) || (opcode == 4))
  352. {
  353. cJSON *from = cJSON_GetObjectItem(patch, "from");
  354. if (!from)
  355. {
  356. /* missing "from" for copy/move. */
  357. return 4;
  358. }
  359. if (opcode == 3)
  360. {
  361. /* move */
  362. value = cJSONUtils_PatchDetach(object, from->valuestring);
  363. }
  364. if (opcode == 4)
  365. {
  366. /* copy */
  367. value = cJSONUtils_GetPointer(object, from->valuestring);
  368. }
  369. if (!value)
  370. {
  371. /* missing "from" for copy/move. */
  372. return 5;
  373. }
  374. if (opcode == 4)
  375. {
  376. value = cJSON_Duplicate(value, 1);
  377. }
  378. if (!value)
  379. {
  380. /* out of memory for copy/move. */
  381. return 6;
  382. }
  383. }
  384. else /* Add/Replace uses "value". */
  385. {
  386. value = cJSON_GetObjectItem(patch, "value");
  387. if (!value)
  388. {
  389. /* missing "value" for add/replace. */
  390. return 7;
  391. }
  392. value = cJSON_Duplicate(value, 1);
  393. if (!value)
  394. {
  395. /* out of memory for add/replace. */
  396. return 8;
  397. }
  398. }
  399. /* Now, just add "value" to "path". */
  400. /* split pointer in parent and child */
  401. parentptr = cJSONUtils_strdup(path->valuestring);
  402. childptr = strrchr(parentptr, '/');
  403. if (childptr)
  404. {
  405. *childptr++ = '\0';
  406. }
  407. parent = cJSONUtils_GetPointer(object, parentptr);
  408. cJSONUtils_InplaceDecodePointerString(childptr);
  409. /* add, remove, replace, move, copy, test. */
  410. if (!parent)
  411. {
  412. /* Couldn't find object to add to. */
  413. free(parentptr);
  414. cJSON_Delete(value);
  415. return 9;
  416. }
  417. else if ((parent->type & 0xFF) == cJSON_Array)
  418. {
  419. if (!strcmp(childptr, "-"))
  420. {
  421. cJSON_AddItemToArray(parent, value);
  422. }
  423. else
  424. {
  425. cJSON_InsertItemInArray(parent, atoi(childptr), value);
  426. }
  427. }
  428. else if ((parent->type & 0xFF) == cJSON_Object)
  429. {
  430. cJSON_DeleteItemFromObject(parent, childptr);
  431. cJSON_AddItemToObject(parent, childptr, value);
  432. }
  433. else
  434. {
  435. cJSON_Delete(value);
  436. }
  437. free(parentptr);
  438. return 0;
  439. }
  440. int cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
  441. {
  442. int err = 0;
  443. if ((patches->type & 0xFF) != cJSON_Array)
  444. {
  445. /* malformed patches. */
  446. return 1;
  447. }
  448. if (patches)
  449. {
  450. patches = patches->child;
  451. }
  452. while (patches)
  453. {
  454. if ((err = cJSONUtils_ApplyPatch(object, patches)))
  455. {
  456. return err;
  457. }
  458. patches = patches->next;
  459. }
  460. return 0;
  461. }
  462. static void cJSONUtils_GeneratePatch(cJSON *patches, const char *op, const char *path, const char *suffix, cJSON *val)
  463. {
  464. cJSON *patch = cJSON_CreateObject();
  465. cJSON_AddItemToObject(patch, "op", cJSON_CreateString(op));
  466. if (suffix)
  467. {
  468. char *newpath = (char*)malloc(strlen(path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2);
  469. cJSONUtils_PointerEncodedstrcpy(newpath + sprintf(newpath, "%s/", path), suffix);
  470. cJSON_AddItemToObject(patch, "path", cJSON_CreateString(newpath));
  471. free(newpath);
  472. }
  473. else
  474. {
  475. cJSON_AddItemToObject(patch, "path", cJSON_CreateString(path));
  476. }
  477. if (val)
  478. {
  479. cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1));
  480. }
  481. cJSON_AddItemToArray(patches, patch);
  482. }
  483. void cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val)
  484. {
  485. cJSONUtils_GeneratePatch(array, op, path, 0, val);
  486. }
  487. static void cJSONUtils_CompareToPatch(cJSON *patches, const char *path, cJSON *from, cJSON *to)
  488. {
  489. if ((from->type & 0xFF) != (to->type & 0xFF))
  490. {
  491. cJSONUtils_GeneratePatch(patches, "replace", path, 0, to);
  492. return;
  493. }
  494. switch ((from->type & 0xFF))
  495. {
  496. case cJSON_Number:
  497. if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
  498. {
  499. cJSONUtils_GeneratePatch(patches, "replace", path, 0, to);
  500. }
  501. return;
  502. case cJSON_String:
  503. if (strcmp(from->valuestring, to->valuestring) != 0)
  504. {
  505. cJSONUtils_GeneratePatch(patches, "replace", path, 0, to);
  506. }
  507. return;
  508. case cJSON_Array:
  509. {
  510. int c = 0;
  511. char *newpath = (char*)malloc(strlen(path) + 23); /* Allow space for 64bit int. */
  512. /* generate patches for all array elements that exist in "from" and "to" */
  513. for (c = 0, from = from->child, to = to->child; from && to; from = from->next, to = to->next, c++)
  514. {
  515. sprintf(newpath, "%s/%d", path, c); /* path of the current array element */
  516. cJSONUtils_CompareToPatch(patches, newpath, from, to);
  517. }
  518. /* remove leftover elements from 'from' that are not in 'to' */
  519. for (; from; from = from->next, c++)
  520. {
  521. sprintf(newpath, "%d", c);
  522. cJSONUtils_GeneratePatch(patches, "remove", path, newpath, 0);
  523. }
  524. /* add new elements in 'to' that were not in 'from' */
  525. for (; to; to = to->next, c++)
  526. {
  527. cJSONUtils_GeneratePatch(patches, "add", path, "-", to);
  528. }
  529. free(newpath);
  530. return;
  531. }
  532. case cJSON_Object:
  533. {
  534. cJSON *a = NULL;
  535. cJSON *b = NULL;
  536. cJSONUtils_SortObject(from);
  537. cJSONUtils_SortObject(to);
  538. a = from->child;
  539. b = to->child;
  540. /* for all object values in the object with more of them */
  541. while (a || b)
  542. {
  543. int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp(a->string, b->string));
  544. if (!diff)
  545. {
  546. /* both object keys are the same */
  547. char *newpath = (char*)malloc(strlen(path) + cJSONUtils_PointerEncodedstrlen(a->string) + 2);
  548. cJSONUtils_PointerEncodedstrcpy(newpath + sprintf(newpath, "%s/", path), a->string);
  549. /* create a patch for the element */
  550. cJSONUtils_CompareToPatch(patches, newpath, a, b);
  551. free(newpath);
  552. a = a->next;
  553. b = b->next;
  554. }
  555. else if (diff < 0)
  556. {
  557. /* object element doesn't exist in 'to' --> remove it */
  558. cJSONUtils_GeneratePatch(patches, "remove", path, a->string, 0);
  559. a = a->next;
  560. }
  561. else
  562. {
  563. /* object element doesn't exist in 'from' --> add it */
  564. cJSONUtils_GeneratePatch(patches, "add", path, b->string, b);
  565. b = b->next;
  566. }
  567. }
  568. return;
  569. }
  570. default:
  571. break;
  572. }
  573. }
  574. cJSON* cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
  575. {
  576. cJSON *patches = cJSON_CreateArray();
  577. cJSONUtils_CompareToPatch(patches, "", from, to);
  578. return patches;
  579. }
  580. /* sort lists using mergesort */
  581. static cJSON *cJSONUtils_SortList(cJSON *list)
  582. {
  583. cJSON *first = list;
  584. cJSON *second = list;
  585. cJSON *ptr = list;
  586. if (!list || !list->next)
  587. {
  588. /* One entry is sorted already. */
  589. return list;
  590. }
  591. while (ptr && ptr->next && (cJSONUtils_strcasecmp(ptr->string, ptr->next->string) < 0))
  592. {
  593. /* Test for list sorted. */
  594. ptr = ptr->next;
  595. }
  596. if (!ptr || !ptr->next)
  597. {
  598. /* Leave sorted lists unmodified. */
  599. return list;
  600. }
  601. /* reset ptr to the beginning */
  602. ptr = list;
  603. while (ptr)
  604. {
  605. /* Walk two pointers to find the middle. */
  606. second = second->next;
  607. ptr = ptr->next;
  608. /* advances ptr two steps at a time */
  609. if (ptr)
  610. {
  611. ptr = ptr->next;
  612. }
  613. }
  614. if (second && second->prev)
  615. {
  616. /* Split the lists */
  617. second->prev->next = NULL;
  618. }
  619. /* Recursively sort the sub-lists. */
  620. first = cJSONUtils_SortList(first);
  621. second = cJSONUtils_SortList(second);
  622. list = ptr = NULL;
  623. while (first && second) /* Merge the sub-lists */
  624. {
  625. if (cJSONUtils_strcasecmp(first->string, second->string) < 0)
  626. {
  627. if (!list)
  628. {
  629. /* start merged list with the first element of the first list */
  630. list = ptr = first;
  631. }
  632. else
  633. {
  634. /* add first element of first list to merged list */
  635. ptr->next = first;
  636. first->prev = ptr;
  637. ptr = first;
  638. }
  639. first = first->next;
  640. }
  641. else
  642. {
  643. if (!list)
  644. {
  645. /* start merged list with the first element of the second list */
  646. list = ptr = second;
  647. }
  648. else
  649. {
  650. /* add first element of second list to merged list */
  651. ptr->next = second;
  652. second->prev = ptr;
  653. ptr = second;
  654. }
  655. second = second->next;
  656. }
  657. }
  658. if (first)
  659. {
  660. /* Append rest of first list. */
  661. if (!list)
  662. {
  663. return first;
  664. }
  665. ptr->next = first;
  666. first->prev = ptr;
  667. }
  668. if (second)
  669. {
  670. /* Append rest of second list */
  671. if (!list)
  672. {
  673. return second;
  674. }
  675. ptr->next = second;
  676. second->prev = ptr;
  677. }
  678. return list;
  679. }
  680. void cJSONUtils_SortObject(cJSON *object)
  681. {
  682. object->child = cJSONUtils_SortList(object->child);
  683. }
  684. cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
  685. {
  686. if (!patch || ((patch->type & 0xFF) != cJSON_Object))
  687. {
  688. /* scalar value, array or NULL, just duplicate */
  689. cJSON_Delete(target);
  690. return cJSON_Duplicate(patch, 1);
  691. }
  692. if (!target || ((target->type & 0xFF) != cJSON_Object))
  693. {
  694. cJSON_Delete(target);
  695. target = cJSON_CreateObject();
  696. }
  697. patch = patch->child;
  698. while (patch)
  699. {
  700. if ((patch->type & 0xFF) == cJSON_NULL)
  701. {
  702. /* NULL is the indicator to remove a value, see RFC7396 */
  703. cJSON_DeleteItemFromObject(target, patch->string);
  704. }
  705. else
  706. {
  707. cJSON *replaceme = cJSON_DetachItemFromObject(target, patch->string);
  708. cJSON_AddItemToObject(target, patch->string, cJSONUtils_MergePatch(replaceme, patch));
  709. }
  710. patch = patch->next;
  711. }
  712. return target;
  713. }
  714. cJSON *cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
  715. {
  716. cJSON *patch = NULL;
  717. if (!to)
  718. {
  719. /* patch to delete everything */
  720. return cJSON_CreateNull();
  721. }
  722. if (((to->type & 0xFF) != cJSON_Object) || !from || ((from->type & 0xFF) != cJSON_Object))
  723. {
  724. return cJSON_Duplicate(to, 1);
  725. }
  726. cJSONUtils_SortObject(from);
  727. cJSONUtils_SortObject(to);
  728. from = from->child;
  729. to = to->child;
  730. patch = cJSON_CreateObject();
  731. while (from || to)
  732. {
  733. int compare = from ? (to ? strcmp(from->string, to->string) : -1) : 1;
  734. if (compare < 0)
  735. {
  736. /* from has a value that to doesn't have -> remove */
  737. cJSON_AddItemToObject(patch, from->string, cJSON_CreateNull());
  738. from = from->next;
  739. }
  740. else if (compare > 0)
  741. {
  742. /* to has a value that from doesn't have -> add to patch */
  743. cJSON_AddItemToObject(patch, to->string, cJSON_Duplicate(to, 1));
  744. to = to->next;
  745. }
  746. else
  747. {
  748. /* object key exists in both objects */
  749. if (cJSONUtils_Compare(from, to))
  750. {
  751. /* not identical --> generate a patch */
  752. cJSON_AddItemToObject(patch, to->string, cJSONUtils_GenerateMergePatch(from, to));
  753. }
  754. /* next key in the object */
  755. from = from->next;
  756. to = to->next;
  757. }
  758. }
  759. if (!patch->child)
  760. {
  761. cJSON_Delete(patch);
  762. return NULL;
  763. }
  764. return patch;
  765. }