mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
refactor cJSONUtils_Compare
This commit is contained in:
parent
2040ce9004
commit
63db67bfeb
@ -416,49 +416,79 @@ static int cJSONUtils_Compare(cJSON *a, cJSON *b)
|
|||||||
{
|
{
|
||||||
case cJSON_Number:
|
case cJSON_Number:
|
||||||
/* numeric mismatch. */
|
/* numeric mismatch. */
|
||||||
return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
|
if ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble))
|
||||||
|
{
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
case cJSON_String:
|
case cJSON_String:
|
||||||
/* string mismatch. */
|
/* string mismatch. */
|
||||||
return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
|
if (strcmp(a->valuestring, b->valuestring) != 0)
|
||||||
|
{
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
case cJSON_Array:
|
case cJSON_Array:
|
||||||
for ((void)(a = a->child), b = b->child; a && b; (void)(a = a->next), b = b->next)
|
for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
|
||||||
{
|
{
|
||||||
int err = cJSONUtils_Compare(a, b);
|
int status = cJSONUtils_Compare(a, b);
|
||||||
if (err)
|
if (status != 0)
|
||||||
{
|
{
|
||||||
return err;
|
return status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* array size mismatch? (one of both children is not NULL) */
|
/* array size mismatch? (one of both children is not NULL) */
|
||||||
return (a || b) ? -4 : 0;
|
if ((a != NULL) || (b != NULL))
|
||||||
|
{
|
||||||
|
return -4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
case cJSON_Object:
|
case cJSON_Object:
|
||||||
cJSONUtils_SortObject(a);
|
cJSONUtils_SortObject(a);
|
||||||
cJSONUtils_SortObject(b);
|
cJSONUtils_SortObject(b);
|
||||||
a = a->child;
|
for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
|
||||||
b = b->child;
|
|
||||||
while (a && b)
|
|
||||||
{
|
{
|
||||||
int err = 0;
|
int status = 0;
|
||||||
/* compare object keys */
|
/* compare object keys */
|
||||||
if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
|
if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
|
||||||
{
|
{
|
||||||
/* missing member */
|
/* missing member */
|
||||||
return -6;
|
return -6;
|
||||||
}
|
}
|
||||||
err = cJSONUtils_Compare(a, b);
|
status = cJSONUtils_Compare(a, b);
|
||||||
if (err)
|
if (status != 0)
|
||||||
{
|
{
|
||||||
return err;
|
return status;
|
||||||
}
|
}
|
||||||
a = a->next;
|
|
||||||
b = b->next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* object length mismatch (one of both children is not null) */
|
/* object length mismatch (one of both children is not null) */
|
||||||
return (a || b) ? -5 : 0;
|
if ((a != NULL) || (b != NULL))
|
||||||
|
{
|
||||||
|
return -5;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* null, true or false */
|
/* null, true or false */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user