cJSON_Utils.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. if (object==target) return strdup("");
  31. int type=object->type,c=0;
  32. for (cJSON *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;
  87. parentptr=strdup(path); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  88. parent=cJSONUtils_GetPointer(object,parentptr);
  89. cJSONUtils_InplaceDecodePointerString(childptr);
  90. cJSON *ret=0;
  91. if (!parent) ret=0; // Couldn't find object to remove child from.
  92. else if (parent->type==cJSON_Array) ret=cJSON_DetachItemFromArray(parent,atoi(childptr));
  93. else if (parent->type==cJSON_Object) ret=cJSON_DetachItemFromObject(parent,childptr);
  94. free(parentptr);
  95. return ret;
  96. }
  97. static int cJSONUtils_Compare(cJSON *a,cJSON *b)
  98. {
  99. if (a->type!=b->type) return -1; // mismatched type.
  100. switch (a->type)
  101. {
  102. case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; // numeric mismatch.
  103. case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; // string mismatch.
  104. 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;}
  105. return (a || b)?-4:0; // array size mismatch.
  106. case cJSON_Object:
  107. if (cJSON_GetArraySize(a)!=cJSON_GetArraySize(b)) return -5; // object length mismatch.
  108. for (a=a->child;a;a=a->next)
  109. {
  110. int err=0;cJSON *s=cJSON_GetObjectItem(b,a->string); if (!s) return -6; // missing object member.
  111. err=cJSONUtils_Compare(a,s);if (err) return err;
  112. }
  113. return 0;
  114. default: break;
  115. }
  116. return 0;
  117. }
  118. static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)
  119. {
  120. cJSON *op=0,*path=0,*value=0;int opcode=0;
  121. op=cJSON_GetObjectItem(patch,"op");
  122. path=cJSON_GetObjectItem(patch,"path");
  123. if (!op || !path) return 2; // malformed patch.
  124. if (!strcmp(op->valuestring,"add")) opcode=0;
  125. else if (!strcmp(op->valuestring,"remove")) opcode=1;
  126. else if (!strcmp(op->valuestring,"replace"))opcode=2;
  127. else if (!strcmp(op->valuestring,"move")) opcode=3;
  128. else if (!strcmp(op->valuestring,"copy")) opcode=4;
  129. else if (!strcmp(op->valuestring,"test")) return cJSONUtils_Compare(cJSONUtils_GetPointer(object,path->valuestring),cJSON_GetObjectItem(patch,"value"));
  130. else return 3; // unknown opcode.
  131. if (opcode==1 || opcode==2) // Remove/Replace
  132. {
  133. cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); // Get rid of old.
  134. if (opcode==1) return 0; // For Remove, this is job done.
  135. }
  136. if (opcode==3 || opcode==4) // Copy/Move uses "from".
  137. {
  138. cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; // missing "from" for copy/move.
  139. if (opcode==3) value=cJSONUtils_PatchDetach(object,from->valuestring);
  140. if (opcode==4) value=cJSONUtils_GetPointer(object,from->valuestring);
  141. if (!value) return 5; // missing "from" for copy/move.
  142. if (opcode==4) value=cJSON_Duplicate(value,1);
  143. if (!value) return 6; // out of memory for copy/move.
  144. }
  145. else // Add/Replace uses "value".
  146. {
  147. value=cJSON_GetObjectItem(patch,"value");
  148. if (!value) return 7; // missing "value" for add/replace.
  149. value=cJSON_Duplicate(value,1);
  150. if (!value) return 8; // out of memory for add/replace.
  151. }
  152. // Now, just add "value" to "path".
  153. char *parentptr=0,*childptr=0;cJSON *parent=0;
  154. parentptr=strdup(path->valuestring); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  155. parent=cJSONUtils_GetPointer(object,parentptr);
  156. cJSONUtils_InplaceDecodePointerString(childptr);
  157. // add, remove, replace, move, copy, test.
  158. if (!parent) {free(parentptr); return 9;} // Couldn't find object to add to.
  159. else if (parent->type==cJSON_Array)
  160. {
  161. if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value);
  162. else cJSON_InsertItemInArray(parent,atoi(childptr),value);
  163. }
  164. else if (parent->type==cJSON_Object)
  165. {
  166. cJSON_DeleteItemFromObject(parent,childptr);
  167. cJSON_AddItemToObject(parent,childptr,value);
  168. }
  169. free(parentptr);
  170. return 0;
  171. }
  172. int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches)
  173. {
  174. int err;
  175. if (!patches->type==cJSON_Array) return 1; // malformed patches.
  176. if (patches) patches=patches->child;
  177. while (patches)
  178. {
  179. if ((err=cJSONUtils_ApplyPatch(object,patches))) return err;
  180. patches=patches->next;
  181. }
  182. return 0;
  183. }
  184. static void cJSONUtils_GeneratePatch(cJSON *patches,const char *op,const char *path,const char *suffix,cJSON *val)
  185. {
  186. cJSON *patch=cJSON_CreateObject();
  187. cJSON_AddItemToObject(patch,"op",cJSON_CreateString(op));
  188. if (suffix)
  189. {
  190. char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(suffix)+2);
  191. cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),suffix);
  192. cJSON_AddItemToObject(patch,"path",cJSON_CreateString(newpath));
  193. free(newpath);
  194. }
  195. else cJSON_AddItemToObject(patch,"path",cJSON_CreateString(path));
  196. if (val) cJSON_AddItemToObject(patch,"value",cJSON_Duplicate(val,1));
  197. cJSON_AddItemToArray(patches,patch);
  198. }
  199. void cJSONUtils_AddPatchToArray(cJSON *array,const char *op,const char *path,cJSON *val) {cJSONUtils_GeneratePatch(array,op,path,0,val);}
  200. static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *from,cJSON *to)
  201. {
  202. if (from->type!=to->type) {cJSONUtils_GeneratePatch(patches,"replace",path,0,to); return; }
  203. switch (from->type)
  204. {
  205. case cJSON_Number:
  206. if (from->valueint!=to->valueint || from->valuedouble!=to->valuedouble)
  207. cJSONUtils_GeneratePatch(patches,"replace",path,0,to);
  208. return;
  209. case cJSON_String:
  210. if (strcmp(from->valuestring,to->valuestring)!=0)
  211. cJSONUtils_GeneratePatch(patches,"replace",path,0,to);
  212. return;
  213. case cJSON_Array:
  214. {
  215. int c;char *newpath=(char*)malloc(strlen(path)+23); // Allow space for 64bit int.
  216. for (c=0,from=from->child,to=to->child;from && to;from=from->next,to=to->next,c++){
  217. sprintf(newpath,"%s/%d",path,c); cJSONUtils_CompareToPatch(patches,newpath,from,to);
  218. }
  219. for (;from;from=from->next,c++) {sprintf(newpath,"%d",c); cJSONUtils_GeneratePatch(patches,"remove",path,newpath,0); }
  220. for (;to;to=to->next,c++) cJSONUtils_GeneratePatch(patches,"add",path,"-",to);
  221. free(newpath);
  222. return;
  223. }
  224. case cJSON_Object:
  225. for (cJSON *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 (cJSON *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. default: break;
  243. }
  244. }
  245. cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to)
  246. {
  247. cJSON *patches=cJSON_CreateArray();
  248. cJSONUtils_CompareToPatch(patches,"",from,to);
  249. return patches;
  250. }