reformatting: cJSONUtils_Compare

This commit is contained in:
Max Bruckner 2016-10-17 01:23:37 +07:00
parent 1235c62235
commit 284a8017b7
1 changed files with 54 additions and 23 deletions

View File

@ -242,31 +242,62 @@ static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path)
return ret;
}
static int cJSONUtils_Compare(cJSON *a,cJSON *b)
static int cJSONUtils_Compare(cJSON *a, cJSON *b)
{
if (a->type!=b->type) return -1; /* mismatched type. */
switch (a->type)
{
case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; /* numeric mismatch. */
case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; /* string mismatch. */
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;}
return (a || b)?-4:0; /* array size mismatch. */
case cJSON_Object:
cJSONUtils_SortObject(a);
cJSONUtils_SortObject(b);
a=a->child,b=b->child;
while (a && b)
{
int err;
if (cJSONUtils_strcasecmp(a->string,b->string)) return -6; /* missing member */
err=cJSONUtils_Compare(a,b);if (err) return err;
a=a->next,b=b->next;
}
return (a || b)?-5:0; /* object length mismatch */
if (a->type != b->type)
{
/* mismatched type. */
return -1;
}
switch (a->type)
{
case cJSON_Number:
/* numeric mismatch. */
return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
case cJSON_String:
/* string mismatch. */
return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
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;
}
}
/* array size mismatch? (one of both children is not NULL) */
return (a || b) ? -4 : 0;
case cJSON_Object:
cJSONUtils_SortObject(a);
cJSONUtils_SortObject(b);
a = a->child;
b = b->child;
while (a && b)
{
int err;
/* compare object keys */
if (cJSONUtils_strcasecmp(a->string, b->string))
{
/* missing member */
return -6;
}
err = cJSONUtils_Compare(a, b);
if (err)
{
return err;
}
a = a->next;
b = b->next;
}
/* object length mismatch (one of both children is not null) */
return (a || b) ? -5 : 0;
default: break;
}
return 0;
default:
break;
}
/* null, true or false */
return 0;
}
static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)