mirror of
https://github.com/DaveGamble/cJSON
synced 2025-01-27 09:43:42 +00:00
reformatting: cJSONUtils_Compare
This commit is contained in:
parent
1235c62235
commit
284a8017b7
@ -244,28 +244,59 @@ static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path)
|
||||
|
||||
static int cJSONUtils_Compare(cJSON *a, cJSON *b)
|
||||
{
|
||||
if (a->type!=b->type) return -1; /* mismatched type. */
|
||||
if (a->type != b->type)
|
||||
{
|
||||
/* mismatched type. */
|
||||
return -1;
|
||||
}
|
||||
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_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;
|
||||
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;
|
||||
/* compare object keys */
|
||||
if (cJSONUtils_strcasecmp(a->string, b->string))
|
||||
{
|
||||
/* missing member */
|
||||
return -6;
|
||||
}
|
||||
return (a || b)?-5:0; /* object length mismatch */
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
/* null, true or false */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user