cJSON_Utils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include "cJSON_Utils.h"
  6. /* JSON Pointer implementation: */
  7. static int cJSONUtils_Pstrcasecmp(const char *a,const char *e)
  8. {
  9. if (!a || !e) return (a==e)?0:1;
  10. for (;*a && *e && *e!='/';a++,e++) {
  11. if (*e=='~') {if (!(e[1]=='0' && *a=='~') && !(e[1]=='1' && *a=='/')) return 1; else e++;}
  12. else if (tolower(*a)!=tolower(*e)) return 1;
  13. }
  14. if ((*e!=0 && *e!='/') != (*a!=0)) return 1;
  15. return 0;
  16. }
  17. static int cJSONUtils_PointerEncodedstrlen(const char *s) {int l=0;for (;*s;s++,l++) if (*s=='~' || *s=='/') l++;return l;}
  18. static void cJSONUtils_PointerEncodedstrcpy(char *d,const char *s)
  19. {
  20. for (;*s;s++)
  21. {
  22. if (*s=='/') {*d++='~';*d++='1';}
  23. else if (*s=='~') {*d++='~';*d++='0';}
  24. else *d++=*s;
  25. }
  26. *d=0;
  27. }
  28. char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target)
  29. {
  30. int type=object->type,c=0;cJSON *obj=0;
  31. if (object==target) return strdup("");
  32. for (obj=object->child;obj;obj=obj->next,c++)
  33. {
  34. char *found=cJSONUtils_FindPointerFromObjectTo(obj,target);
  35. if (found)
  36. {
  37. if (type==cJSON_Array)
  38. {
  39. char *ret=(char*)malloc(strlen(found)+23);
  40. sprintf(ret,"/%d%s",c,found);
  41. free(found);
  42. return ret;
  43. }
  44. else if (type==cJSON_Object)
  45. {
  46. char *ret=(char*)malloc(strlen(found)+cJSONUtils_PointerEncodedstrlen(obj->string)+2);
  47. *ret='/';cJSONUtils_PointerEncodedstrcpy(ret+1,obj->string);
  48. strcat(ret,found);
  49. free(found);
  50. return ret;
  51. }
  52. free(found);
  53. return 0;
  54. }
  55. }
  56. return 0;
  57. }
  58. cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer)
  59. {
  60. while (*pointer++=='/' && object)
  61. {
  62. if (object->type==cJSON_Array)
  63. {
  64. int which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0';
  65. if (*pointer && *pointer!='/') return 0;
  66. object=cJSON_GetArrayItem(object,which);
  67. }
  68. else if (object->type==cJSON_Object)
  69. {
  70. object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,pointer)) object=object->next; /* GetObjectItem. */
  71. while (*pointer && *pointer!='/') pointer++;
  72. }
  73. else return 0;
  74. }
  75. return object;
  76. }
  77. /* JSON Patch implementation. */
  78. static void cJSONUtils_InplaceDecodePointerString(char *string)
  79. {
  80. char *s2=string;
  81. for (;*string;s2++,string++) *s2=(*string!='~')?(*string):((*(++string)=='0')?'~':'/');
  82. *s2=0;
  83. }
  84. static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path)
  85. {
  86. char *parentptr=0,*childptr=0;cJSON *parent=0,*ret=0;
  87. parentptr=strdup(path); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  88. parent=cJSONUtils_GetPointer(object,parentptr);
  89. cJSONUtils_InplaceDecodePointerString(childptr);
  90. if (!parent) ret=0; /* Couldn't find object to remove child from. */
  91. else if (parent->type==cJSON_Array) ret=cJSON_DetachItemFromArray(parent,atoi(childptr));
  92. else if (parent->type==cJSON_Object) ret=cJSON_DetachItemFromObject(parent,childptr);
  93. free(parentptr);
  94. return ret;
  95. }
  96. static int cJSONUtils_Compare(cJSON *a,cJSON *b)
  97. {
  98. if (a->type!=b->type) return -1; /* mismatched type. */
  99. switch (a->type)
  100. {
  101. case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; /* numeric mismatch. */
  102. case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; /* string mismatch. */
  103. 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;}
  104. return (a || b)?-4:0; /* array size mismatch. */
  105. case cJSON_Object:
  106. if (cJSON_GetArraySize(a)!=cJSON_GetArraySize(b)) return -5; /* object length mismatch. */
  107. for (a=a->child;a;a=a->next)
  108. {
  109. int err=0;cJSON *s=cJSON_GetObjectItem(b,a->string); if (!s) return -6; /* missing object member. */
  110. err=cJSONUtils_Compare(a,s);if (err) return err;
  111. }
  112. return 0;
  113. default: break;
  114. }
  115. return 0;
  116. }
  117. static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)
  118. {
  119. cJSON *op=0,*path=0,*value=0,*parent=0;int opcode=0;char *parentptr=0,*childptr=0;
  120. op=cJSON_GetObjectItem(patch,"op");
  121. path=cJSON_GetObjectItem(patch,"path");
  122. if (!op || !path) return 2; /* malformed patch. */
  123. if (!strcmp(op->valuestring,"add")) opcode=0;
  124. else if (!strcmp(op->valuestring,"remove")) opcode=1;
  125. else if (!strcmp(op->valuestring,"replace"))opcode=2;
  126. else if (!strcmp(op->valuestring,"move")) opcode=3;
  127. else if (!strcmp(op->valuestring,"copy")) opcode=4;
  128. else if (!strcmp(op->valuestring,"test")) return cJSONUtils_Compare(cJSONUtils_GetPointer(object,path->valuestring),cJSON_GetObjectItem(patch,"value"));
  129. else return 3; /* unknown opcode. */
  130. if (opcode==1 || opcode==2) /* Remove/Replace */
  131. {
  132. cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); /* Get rid of old. */
  133. if (opcode==1) return 0; /* For Remove, this is job done. */
  134. }
  135. if (opcode==3 || opcode==4) /* Copy/Move uses "from". */
  136. {
  137. cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; /* missing "from" for copy/move. */
  138. if (opcode==3) value=cJSONUtils_PatchDetach(object,from->valuestring);
  139. if (opcode==4) value=cJSONUtils_GetPointer(object,from->valuestring);
  140. if (!value) return 5; /* missing "from" for copy/move. */
  141. if (opcode==4) value=cJSON_Duplicate(value,1);
  142. if (!value) return 6; /* out of memory for copy/move. */
  143. }
  144. else /* Add/Replace uses "value". */
  145. {
  146. value=cJSON_GetObjectItem(patch,"value");
  147. if (!value) return 7; /* missing "value" for add/replace. */
  148. value=cJSON_Duplicate(value,1);
  149. if (!value) return 8; /* out of memory for add/replace. */
  150. }
  151. /* Now, just add "value" to "path". */
  152. parentptr=strdup(path->valuestring); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  153. parent=cJSONUtils_GetPointer(object,parentptr);
  154. cJSONUtils_InplaceDecodePointerString(childptr);
  155. /* add, remove, replace, move, copy, test. */
  156. if (!parent) {free(parentptr); return 9;} /* Couldn't find object to add to. */
  157. else if (parent->type==cJSON_Array)
  158. {
  159. if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value);
  160. else cJSON_InsertItemInArray(parent,atoi(childptr),value);
  161. }
  162. else if (parent->type==cJSON_Object)
  163. {
  164. cJSON_DeleteItemFromObject(parent,childptr);
  165. cJSON_AddItemToObject(parent,childptr,value);
  166. }
  167. free(parentptr);
  168. return 0;
  169. }
  170. int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches)
  171. {
  172. int err;
  173. if (!patches->type==cJSON_Array) return 1; /* malformed patches. */
  174. if (patches) patches=patches->child;
  175. while (patches)
  176. {
  177. if ((err=cJSONUtils_ApplyPatch(object,patches))) return err;
  178. patches=patches->next;
  179. }
  180. return 0;
  181. }
  182. static void cJSONUtils_GeneratePatch(cJSON *patches,const char *op,const char *path,const char *suffix,cJSON *val)
  183. {
  184. cJSON *patch=cJSON_CreateObject();
  185. cJSON_AddItemToObject(patch,"op",cJSON_CreateString(op));
  186. if (suffix)
  187. {
  188. char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(suffix)+2);
  189. cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),suffix);
  190. cJSON_AddItemToObject(patch,"path",cJSON_CreateString(newpath));
  191. free(newpath);
  192. }
  193. else cJSON_AddItemToObject(patch,"path",cJSON_CreateString(path));
  194. if (val) cJSON_AddItemToObject(patch,"value",cJSON_Duplicate(val,1));
  195. cJSON_AddItemToArray(patches,patch);
  196. }
  197. void cJSONUtils_AddPatchToArray(cJSON *array,const char *op,const char *path,cJSON *val) {cJSONUtils_GeneratePatch(array,op,path,0,val);}
  198. static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *from,cJSON *to)
  199. {
  200. if (from->type!=to->type) {cJSONUtils_GeneratePatch(patches,"replace",path,0,to); return; }
  201. switch (from->type)
  202. {
  203. case cJSON_Number:
  204. if (from->valueint!=to->valueint || from->valuedouble!=to->valuedouble)
  205. cJSONUtils_GeneratePatch(patches,"replace",path,0,to);
  206. return;
  207. case cJSON_String:
  208. if (strcmp(from->valuestring,to->valuestring)!=0)
  209. cJSONUtils_GeneratePatch(patches,"replace",path,0,to);
  210. return;
  211. case cJSON_Array:
  212. {
  213. int c;char *newpath=(char*)malloc(strlen(path)+23); /* Allow space for 64bit int. */
  214. for (c=0,from=from->child,to=to->child;from && to;from=from->next,to=to->next,c++){
  215. sprintf(newpath,"%s/%d",path,c); cJSONUtils_CompareToPatch(patches,newpath,from,to);
  216. }
  217. for (;from;from=from->next,c++) {sprintf(newpath,"%d",c); cJSONUtils_GeneratePatch(patches,"remove",path,newpath,0); }
  218. for (;to;to=to->next,c++) cJSONUtils_GeneratePatch(patches,"add",path,"-",to);
  219. free(newpath);
  220. return;
  221. }
  222. case cJSON_Object:
  223. {
  224. cJSON *a;
  225. for (a=from->child;a;a=a->next)
  226. {
  227. if (!cJSON_GetObjectItem(to,a->string)) cJSONUtils_GeneratePatch(patches,"remove",path,a->string,0);
  228. }
  229. for (a=to->child;a;a=a->next)
  230. {
  231. cJSON *other=cJSON_GetObjectItem(from,a->string);
  232. if (!other) cJSONUtils_GeneratePatch(patches,"add",path,a->string,a);
  233. else
  234. {
  235. char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(a->string)+2);
  236. cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),a->string);
  237. cJSONUtils_CompareToPatch(patches,newpath,other,a);
  238. free(newpath);
  239. }
  240. }
  241. return;
  242. }
  243. default: break;
  244. }
  245. }
  246. cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to)
  247. {
  248. cJSON *patches=cJSON_CreateArray();
  249. cJSONUtils_CompareToPatch(patches,"",from,to);
  250. return patches;
  251. }
  252. static int cJSONUtils_strcasecmp(const char *s1,const char *s2)
  253. {
  254. if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
  255. for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
  256. return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
  257. }
  258. static cJSON *cJSONUtils_SortList(cJSON *list)
  259. {
  260. cJSON *first=list,*second=list,*ptr=list;
  261. if (!list || !list->next) return list; /* One entry is sorted already. */
  262. while (ptr) {second=second->next;ptr=ptr->next;if (ptr) ptr=ptr->next;} /* Walk two pointers to find the middle. */
  263. if (second && second->prev) second->prev->next=0; /* Split the lists */
  264. first=cJSONUtils_SortList(first); /* Recursively sort the sub-lists. */
  265. second=cJSONUtils_SortList(second);
  266. list=ptr=0;
  267. while (first && second) /* Merge the sub-lists */
  268. {
  269. if (cJSONUtils_strcasecmp(first->string,second->string)<0)
  270. {
  271. if (!list) list=ptr=first;
  272. else {ptr->next=first;first->prev=ptr;ptr=first;}
  273. first=first->next;
  274. }
  275. else
  276. {
  277. if (!list) list=ptr=second;
  278. else {ptr->next=second;second->prev=ptr;ptr=second;}
  279. second=second->next;
  280. }
  281. }
  282. if (first) { if (!list) return first; ptr->next=first; first->prev=ptr; } /* Append any tails. */
  283. if (second) { if (!list) return second; ptr->next=second; second->prev=ptr; }
  284. return list;
  285. }
  286. void cJSONUtils_SortObject(cJSON *object) {object->child=cJSONUtils_SortList(object->child);}