cJSON_Utils.c 18 KB

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