Skip to content

Commit

Permalink
Fixed JsonCompare() function and switched to using it for json_test
Browse files Browse the repository at this point in the history
Also added DEBUG logging for details on why JsonCompare() found two json elements to not be equal.

Ticket: ENT-11450
Changelog: none
  • Loading branch information
craigcomstock committed Apr 18, 2024
1 parent 412f6b3 commit 994be5a
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 125 deletions.
39 changes: 20 additions & 19 deletions libutils/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,12 @@ static int JsonArrayCompare(
int ret = JsonLength(a) - JsonLength(b);
if (ret != 0)
{
Log(LOG_LEVEL_DEBUG, "JsonArrayCompare() fails, length differs by %d", ret);
return ret;
}

JsonIterator iter_a = JsonIteratorInit(a);
JsonIterator iter_b = JsonIteratorInit(a);
JsonIterator iter_b = JsonIteratorInit(b);

for (size_t i = 0; i < JsonLength(a); i++)
{
Expand All @@ -276,6 +277,7 @@ static int JsonArrayCompare(
ret = JsonCompare(child_a, child_b);
if (ret != 0)
{
Log(LOG_LEVEL_DEBUG, "JsonArrayCompare() fails for index %ld, children not equal", i);
return ret;
}
}
Expand All @@ -296,31 +298,24 @@ static int JsonObjectCompare(
int ret = JsonLength(a) - JsonLength(b);
if (ret != 0)
{
Log(LOG_LEVEL_DEBUG, "JsonObjectCompare() fails, length differs by %d", ret);
return ret;
}

JsonIterator iter_a = JsonIteratorInit(a);
JsonIterator iter_b = JsonIteratorInit(a);
JsonIterator iter = JsonIteratorInit(a);

for (size_t i = 0; i < JsonLength(a); i++)
while (JsonIteratorHasMore(&iter))
{
const JsonElement *child_a = JsonIteratorNextValue(&iter_a);
const JsonElement *child_b = JsonIteratorNextValue(&iter_b);

const char *const key_a = JsonIteratorCurrentKey(&iter_a);
const char *const key_b = JsonIteratorCurrentKey(&iter_b);

ret = strcmp(key_a, key_b);
if (ret != 0)
{
return ret;
}
const char *const key = JsonIteratorNextKey(&iter);
const JsonElement *const child_a = JsonObjectGet(a, key);
const JsonElement *const child_b = JsonObjectGet(b, key);

ret = JsonCompare(child_a, child_b);
if (ret != 0)
if (child_b == NULL || JsonCompare(child_a, child_b))
{
return ret;
Log(LOG_LEVEL_DEBUG, "JsonObjectCompare() fails for key '%s', children not equal", key);
return 1;
}
return 0;
}

return ret;
Expand All @@ -340,6 +335,7 @@ static int JsonContainerCompare(

if (type_a != type_b)
{
Log(LOG_LEVEL_DEBUG, "JsonContainerCompare() fails, type %d not equal to type %d", type_a, type_b);
return type_a - type_b;
}

Expand All @@ -359,6 +355,7 @@ static int JsonContainerCompare(

int JsonCompare(const JsonElement *const a, const JsonElement *const b)
{
int ret;
assert(a != NULL);
assert(b != NULL);

Expand All @@ -367,6 +364,7 @@ int JsonCompare(const JsonElement *const a, const JsonElement *const b)

if (type_a != type_b)
{
Log(LOG_LEVEL_DEBUG, "JsonCompare() fails, type %d not equal to type %d", type_a, type_b);
return type_a - type_b;
}

Expand All @@ -376,7 +374,10 @@ int JsonCompare(const JsonElement *const a, const JsonElement *const b)
return JsonContainerCompare(a, b);

case JSON_ELEMENT_TYPE_PRIMITIVE:
return strcmp(a->primitive.value, b->primitive.value);
ret = strcmp(a->primitive.value, b->primitive.value);
if (ret)
Log(LOG_LEVEL_DEBUG, "JsonCompare() fails, primitive '%s' not equal to '%s'", a->primitive.value, b->primitive.value);
return ret;

default:
UnexpectedError("Unknown JSON element type: %d", type_a);
Expand Down
Loading

0 comments on commit 994be5a

Please sign in to comment.