cJSON_Utils.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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++) *s2=(*string!='~')?(*string):((*(++string)=='0')?'~':'/');
  179. *s2=0;
  180. }
  181. static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path)
  182. {
  183. char *parentptr=0,*childptr=0;cJSON *parent=0,*ret=0;
  184. parentptr=strdup(path); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  185. parent=cJSONUtils_GetPointer(object,parentptr);
  186. cJSONUtils_InplaceDecodePointerString(childptr);
  187. if (!parent) ret=0; /* Couldn't find object to remove child from. */
  188. else if (parent->type==cJSON_Array) ret=cJSON_DetachItemFromArray(parent,atoi(childptr));
  189. else if (parent->type==cJSON_Object) ret=cJSON_DetachItemFromObject(parent,childptr);
  190. free(parentptr);
  191. return ret;
  192. }
  193. static int cJSONUtils_Compare(cJSON *a,cJSON *b)
  194. {
  195. if (a->type!=b->type) return -1; /* mismatched type. */
  196. switch (a->type)
  197. {
  198. case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; /* numeric mismatch. */
  199. case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; /* string mismatch. */
  200. 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;}
  201. return (a || b)?-4:0; /* array size mismatch. */
  202. case cJSON_Object:
  203. cJSONUtils_SortObject(a);
  204. cJSONUtils_SortObject(b);
  205. a=a->child,b=b->child;
  206. while (a && b)
  207. {
  208. int err;
  209. if (cJSONUtils_strcasecmp(a->string,b->string)) return -6; /* missing member */
  210. err=cJSONUtils_Compare(a,b);if (err) return err;
  211. a=a->next,b=b->next;
  212. }
  213. return (a || b)?-5:0; /* object length mismatch */
  214. default: break;
  215. }
  216. return 0;
  217. }
  218. static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)
  219. {
  220. cJSON *op=0,*path=0,*value=0,*parent=0;int opcode=0;char *parentptr=0,*childptr=0;
  221. op=cJSON_GetObjectItem(patch,"op");
  222. path=cJSON_GetObjectItem(patch,"path");
  223. if (!op || !path) return 2; /* malformed patch. */
  224. if (!strcmp(op->valuestring,"add")) opcode=0;
  225. else if (!strcmp(op->valuestring,"remove")) opcode=1;
  226. else if (!strcmp(op->valuestring,"replace"))opcode=2;
  227. else if (!strcmp(op->valuestring,"move")) opcode=3;
  228. else if (!strcmp(op->valuestring,"copy")) opcode=4;
  229. else if (!strcmp(op->valuestring,"test")) return cJSONUtils_Compare(cJSONUtils_GetPointer(object,path->valuestring),cJSON_GetObjectItem(patch,"value"));
  230. else return 3; /* unknown opcode. */
  231. if (opcode==1 || opcode==2) /* Remove/Replace */
  232. {
  233. cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); /* Get rid of old. */
  234. if (opcode==1) return 0; /* For Remove, this is job done. */
  235. }
  236. if (opcode==3 || opcode==4) /* Copy/Move uses "from". */
  237. {
  238. cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; /* missing "from" for copy/move. */
  239. if (opcode==3) value=cJSONUtils_PatchDetach(object,from->valuestring);
  240. if (opcode==4) value=cJSONUtils_GetPointer(object,from->valuestring);
  241. if (!value) return 5; /* missing "from" for copy/move. */
  242. if (opcode==4) value=cJSON_Duplicate(value,1);
  243. if (!value) return 6; /* out of memory for copy/move. */
  244. }
  245. else /* Add/Replace uses "value". */
  246. {
  247. value=cJSON_GetObjectItem(patch,"value");
  248. if (!value) return 7; /* missing "value" for add/replace. */
  249. value=cJSON_Duplicate(value,1);
  250. if (!value) return 8; /* out of memory for add/replace. */
  251. }
  252. /* Now, just add "value" to "path". */
  253. parentptr=strdup(path->valuestring); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  254. parent=cJSONUtils_GetPointer(object,parentptr);
  255. cJSONUtils_InplaceDecodePointerString(childptr);
  256. /* add, remove, replace, move, copy, test. */
  257. if (!parent) {free(parentptr); cJSON_Delete(value); return 9;} /* Couldn't find object to add to. */
  258. else if (parent->type==cJSON_Array)
  259. {
  260. if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value);
  261. else cJSON_InsertItemInArray(parent,atoi(childptr),value);
  262. }
  263. else if (parent->type==cJSON_Object)
  264. {
  265. cJSON_DeleteItemFromObject(parent,childptr);
  266. cJSON_AddItemToObject(parent,childptr,value);
  267. }
  268. else
  269. {
  270. cJSON_Delete(value);
  271. }
  272. free(parentptr);
  273. return 0;
  274. }
  275. int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches)
  276. {
  277. int err;
  278. if (patches->type!=cJSON_Array) return 1; /* malformed patches. */
  279. if (patches) patches=patches->child;
  280. while (patches)
  281. {
  282. if ((err=cJSONUtils_ApplyPatch(object,patches))) return err;
  283. patches=patches->next;
  284. }
  285. return 0;
  286. }
  287. static void cJSONUtils_GeneratePatch(cJSON *patches,const char *op,const char *path,const char *suffix,cJSON *val)
  288. {
  289. cJSON *patch=cJSON_CreateObject();
  290. cJSON_AddItemToObject(patch,"op",cJSON_CreateString(op));
  291. if (suffix)
  292. {
  293. char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(suffix)+2);
  294. cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),suffix);
  295. cJSON_AddItemToObject(patch,"path",cJSON_CreateString(newpath));
  296. free(newpath);
  297. }
  298. else cJSON_AddItemToObject(patch,"path",cJSON_CreateString(path));
  299. if (val) cJSON_AddItemToObject(patch,"value",cJSON_Duplicate(val,1));
  300. cJSON_AddItemToArray(patches,patch);
  301. }
  302. void cJSONUtils_AddPatchToArray(cJSON *array,const char *op,const char *path,cJSON *val) {cJSONUtils_GeneratePatch(array,op,path,0,val);}
  303. static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *from,cJSON *to)
  304. {
  305. if (from->type!=to->type) {cJSONUtils_GeneratePatch(patches,"replace",path,0,to); return; }
  306. switch (from->type)
  307. {
  308. case cJSON_Number:
  309. if (from->valueint!=to->valueint || from->valuedouble!=to->valuedouble)
  310. cJSONUtils_GeneratePatch(patches,"replace",path,0,to);
  311. return;
  312. case cJSON_String:
  313. if (strcmp(from->valuestring,to->valuestring)!=0)
  314. cJSONUtils_GeneratePatch(patches,"replace",path,0,to);
  315. return;
  316. case cJSON_Array:
  317. {
  318. int c;char *newpath=(char*)malloc(strlen(path)+23); /* Allow space for 64bit int. */
  319. for (c=0,from=from->child,to=to->child;from && to;from=from->next,to=to->next,c++){
  320. sprintf(newpath,"%s/%d",path,c); cJSONUtils_CompareToPatch(patches,newpath,from,to);
  321. }
  322. for (;from;from=from->next,c++) {sprintf(newpath,"%d",c); cJSONUtils_GeneratePatch(patches,"remove",path,newpath,0); }
  323. for (;to;to=to->next,c++) cJSONUtils_GeneratePatch(patches,"add",path,"-",to);
  324. free(newpath);
  325. return;
  326. }
  327. case cJSON_Object:
  328. {
  329. cJSON *a,*b;
  330. cJSONUtils_SortObject(from);
  331. cJSONUtils_SortObject(to);
  332. a=from->child,b=to->child;
  333. while (a || b)
  334. {
  335. int diff=(!a)?1:(!b)?-1:cJSONUtils_strcasecmp(a->string,b->string);
  336. if (!diff)
  337. {
  338. char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(a->string)+2);
  339. cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),a->string);
  340. cJSONUtils_CompareToPatch(patches,newpath,a,b);
  341. free(newpath);
  342. a=a->next;
  343. b=b->next;
  344. }
  345. else if (diff<0) {cJSONUtils_GeneratePatch(patches,"remove",path,a->string,0); a=a->next;}
  346. else {cJSONUtils_GeneratePatch(patches,"add",path,b->string,b); b=b->next;}
  347. }
  348. return;
  349. }
  350. default: break;
  351. }
  352. }
  353. cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to)
  354. {
  355. cJSON *patches=cJSON_CreateArray();
  356. cJSONUtils_CompareToPatch(patches,"",from,to);
  357. return patches;
  358. }
  359. static cJSON *cJSONUtils_SortList(cJSON *list)
  360. {
  361. cJSON *first=list,*second=list,*ptr=list;
  362. if (!list || !list->next) return list; /* One entry is sorted already. */
  363. while (ptr && ptr->next && cJSONUtils_strcasecmp(ptr->string,ptr->next->string)<0) ptr=ptr->next; /* Test for list sorted. */
  364. if (!ptr || !ptr->next) return list; /* Leave sorted lists unmodified. */
  365. ptr=list;
  366. while (ptr) {second=second->next;ptr=ptr->next;if (ptr) ptr=ptr->next;} /* Walk two pointers to find the middle. */
  367. if (second && second->prev) second->prev->next=0; /* Split the lists */
  368. first=cJSONUtils_SortList(first); /* Recursively sort the sub-lists. */
  369. second=cJSONUtils_SortList(second);
  370. list=ptr=0;
  371. while (first && second) /* Merge the sub-lists */
  372. {
  373. if (cJSONUtils_strcasecmp(first->string,second->string)<0)
  374. {
  375. if (!list) list=ptr=first;
  376. else {ptr->next=first;first->prev=ptr;ptr=first;}
  377. first=first->next;
  378. }
  379. else
  380. {
  381. if (!list) list=ptr=second;
  382. else {ptr->next=second;second->prev=ptr;ptr=second;}
  383. second=second->next;
  384. }
  385. }
  386. if (first) { if (!list) return first; ptr->next=first; first->prev=ptr; } /* Append any tails. */
  387. if (second) { if (!list) return second; ptr->next=second; second->prev=ptr; }
  388. return list;
  389. }
  390. void cJSONUtils_SortObject(cJSON *object) {object->child=cJSONUtils_SortList(object->child);}
  391. cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
  392. {
  393. if (!patch || patch->type != cJSON_Object) {cJSON_Delete(target);return cJSON_Duplicate(patch,1);}
  394. if (!target || target->type != cJSON_Object) {cJSON_Delete(target);target=cJSON_CreateObject();}
  395. patch=patch->child;
  396. while (patch)
  397. {
  398. if (patch->type == cJSON_NULL) cJSON_DeleteItemFromObject(target,patch->string);
  399. else
  400. {
  401. cJSON *replaceme=cJSON_DetachItemFromObject(target,patch->string);
  402. cJSON_AddItemToObject(target,patch->string,cJSONUtils_MergePatch(replaceme,patch));
  403. }
  404. patch=patch->next;
  405. }
  406. return target;
  407. }
  408. cJSON *cJSONUtils_GenerateMergePatch(cJSON *from,cJSON *to)
  409. {
  410. cJSON *patch=0;
  411. if (!to) return cJSON_CreateNull();
  412. if (to->type!=cJSON_Object || !from || from->type!=cJSON_Object) return cJSON_Duplicate(to,1);
  413. cJSONUtils_SortObject(from);
  414. cJSONUtils_SortObject(to);
  415. from=from->child;to=to->child;
  416. patch=cJSON_CreateObject();
  417. while (from || to)
  418. {
  419. int compare=from?(to?strcmp(from->string,to->string):-1):1;
  420. if (compare<0)
  421. {
  422. cJSON_AddItemToObject(patch,from->string,cJSON_CreateNull());
  423. from=from->next;
  424. }
  425. else if (compare>0)
  426. {
  427. cJSON_AddItemToObject(patch,to->string,cJSON_Duplicate(to,1));
  428. to=to->next;
  429. }
  430. else
  431. {
  432. if (cJSONUtils_Compare(from,to)) cJSON_AddItemToObject(patch,to->string,cJSONUtils_GenerateMergePatch(from,to));
  433. from=from->next;to=to->next;
  434. }
  435. }
  436. if (!patch->child) {cJSON_Delete(patch);return 0;}
  437. return patch;
  438. }