mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Utility function to generate the patch between two objects.
This commit is contained in:
@ -343,7 +343,8 @@ static cJSON *cJSONUtils_SortList(cJSON *list)
|
||||
|
||||
void cJSONUtils_SortObject(cJSON *object) {object->child=cJSONUtils_SortList(object->child);}
|
||||
|
||||
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch) {
|
||||
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
|
||||
{
|
||||
if (!patch || patch->type != cJSON_Object) {cJSON_Delete(target);return cJSON_Duplicate(patch,1);}
|
||||
if (!target || target->type != cJSON_Object) {cJSON_Delete(target);target=cJSON_CreateObject();}
|
||||
|
||||
@ -360,3 +361,35 @@ cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch) {
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
cJSON *cJSONUtils_GenerateMergePatch(cJSON *from,cJSON *to)
|
||||
{
|
||||
cJSON *patch=0;
|
||||
if (!to) return cJSON_CreateNull();
|
||||
if (to->type!=cJSON_Object || !from || from->type!=cJSON_Object) return cJSON_Duplicate(to,1);
|
||||
cJSONUtils_SortObject(from);
|
||||
cJSONUtils_SortObject(to);
|
||||
from=from->child;to=to->child;
|
||||
patch=cJSON_CreateObject();
|
||||
while (from || to)
|
||||
{
|
||||
int compare=from?(to?strcmp(from->string,to->string):-1):1;
|
||||
if (compare<0)
|
||||
{
|
||||
cJSON_AddItemToObject(patch,from->string,cJSON_CreateNull());
|
||||
from=from->next;
|
||||
}
|
||||
else if (compare>0)
|
||||
{
|
||||
cJSON_AddItemToObject(patch,to->string,cJSON_Duplicate(to,1));
|
||||
to=to->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cJSONUtils_Compare(from,to)) cJSON_AddItemToObject(patch,to->string,cJSONUtils_GenerateMergePatch(from,to));
|
||||
from=from->next;to=to->next;
|
||||
}
|
||||
}
|
||||
if (!patch->child) {cJSON_Delete(patch);return 0;}
|
||||
return patch;
|
||||
}
|
Reference in New Issue
Block a user