cJSON_Utils.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "cJSON_Utils.h"
  5. // JSON Pointer implementation:
  6. static int cJSONUtils_Pstrcasecmp(const char *a,const char *e)
  7. {
  8. if (!a || !e) return (a==e)?0:1;
  9. for (;*a && *e && *e!='/';a++,e++) {
  10. if (*e=='~') {if (!(e[1]=='0' && *a=='~') && !(e[1]=='1' && *a=='/')) return 1; else e++;}
  11. else if (tolower(*a)!=tolower(*e)) return 1;
  12. }
  13. if ((*e!=0 && *e!='/') != (*a!=0)) return 1;
  14. return 0;
  15. }
  16. cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer)
  17. {
  18. cJSON *target=object;int which=0;const char *element=0;
  19. while (*pointer=='/' && object)
  20. {
  21. pointer++;
  22. if (object->type==cJSON_Array)
  23. {
  24. which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0';
  25. if (*pointer && *pointer!='/') return 0;
  26. object=cJSON_GetArrayItem(object,which);
  27. }
  28. else if (object->type==cJSON_Object)
  29. {
  30. element=pointer; while (*pointer && *pointer!='/') pointer++;
  31. object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,element)) object=object->next; // GetObjectItem.
  32. }
  33. else return 0;
  34. }
  35. return object;
  36. }
  37. // JSON Patch implementation.
  38. static void cJSONUtils_InplaceDecodePointerString(char *string)
  39. {
  40. char *s2=string;
  41. for (;*string;s2++,string++)
  42. *s2=(*string!='~')?(*string):((*(++string)=='0')?'~':'/');
  43. *s2=0;
  44. }
  45. static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path)
  46. {
  47. char *parentptr=0,*childptr=0;cJSON *parent=0;
  48. parentptr=strdup(path); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  49. parent=cJSONUtils_GetPointer(object,parentptr);
  50. cJSONUtils_InplaceDecodePointerString(childptr);
  51. cJSON *ret=0;
  52. if (!parent) ret=0; // Couldn't find object to remove child from.
  53. else if (parent->type==cJSON_Array) ret=cJSON_DetachItemFromArray(parent,atoi(childptr));
  54. else if (parent->type==cJSON_Object) ret=cJSON_DetachItemFromObject(parent,childptr);
  55. free(parentptr);
  56. return ret;
  57. }
  58. static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)
  59. {
  60. cJSON *op=0,*path=0,*value=0;int opcode=0;
  61. op=cJSON_GetObjectItem(patch,"op");
  62. path=cJSON_GetObjectItem(patch,"path");
  63. if (!op || !path) return 2; // malformed patch.
  64. if (!strcmp(op->valuestring,"add")) opcode=0;
  65. else if (!strcmp(op->valuestring,"remove")) opcode=1;
  66. else if (!strcmp(op->valuestring,"replace"))opcode=2;
  67. else if (!strcmp(op->valuestring,"move")) opcode=3;
  68. else if (!strcmp(op->valuestring,"copy")) opcode=4;
  69. else if (!strcmp(op->valuestring,"test")) opcode=5;
  70. else return 3; // unknown opcode.
  71. if (opcode==5) return 10; // TEST IS CURRENTLY UNIMPLEMENTED.
  72. if (opcode==1 || opcode==2) // Remove/Replace
  73. {
  74. cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); // Get rid of old.
  75. if (opcode==1) return 0; // For Remove, this is job done.
  76. }
  77. if (opcode==3 || opcode==4) // Copy/Move uses "from".
  78. {
  79. cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; // missing "from" for copy/move.
  80. if (opcode==3) value=cJSONUtils_PatchDetach(object,from->valuestring);
  81. if (opcode==4) value=cJSONUtils_GetPointer(object,from->valuestring);
  82. if (!value) return 5; // missing "from" for copy/move.
  83. if (opcode==4) value=cJSON_Duplicate(value,1);
  84. if (!value) return 6; // out of memory for copy/move.
  85. }
  86. else // Add/Replace uses "value".
  87. {
  88. value=cJSON_GetObjectItem(patch,"value");
  89. if (!value) return 7; // missing "value" for add/replace.
  90. value=cJSON_Duplicate(value,1);
  91. if (!value) return 8; // out of memory for add/replace.
  92. }
  93. // Now, just add "value" to "path".
  94. char *parentptr=0,*childptr=0;cJSON *parent=0;
  95. parentptr=strdup(path->valuestring); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
  96. parent=cJSONUtils_GetPointer(object,parentptr);
  97. cJSONUtils_InplaceDecodePointerString(childptr);
  98. // add, remove, replace, move, copy, test.
  99. if (!parent) {free(parentptr); return 9;} // Couldn't find object to add to.
  100. else if (parent->type==cJSON_Array)
  101. {
  102. if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value);
  103. else cJSON_InsertItemInArray(parent,atoi(childptr),value);
  104. }
  105. else if (parent->type==cJSON_Object)
  106. {
  107. cJSON_DeleteItemFromObject(parent,childptr);
  108. cJSON_AddItemToObject(parent,childptr,value);
  109. }
  110. free(parentptr);
  111. return 0;
  112. }
  113. int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches)
  114. {
  115. int err;
  116. if (!patches->type==cJSON_Array) return 1; // malformed patches.
  117. if (patches) patches=patches->child;
  118. while (patches)
  119. {
  120. if ((err=cJSONUtils_ApplyPatch(object,patches))) return err;
  121. patches=patches->next;
  122. }
  123. return 0;
  124. }