Skip to content

Commit

Permalink
Merge pull request #217 from craigcomstock/ENT-11450/master
Browse files Browse the repository at this point in the history
Fixed JsonCompare() function and switched to using it for json_test
  • Loading branch information
olehermanse authored Apr 23, 2024
2 parents 412f6b3 + dc30808 commit 28df3c4
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 123 deletions.
55 changes: 39 additions & 16 deletions libutils/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ struct JsonElement_
// JsonElement Functions
// *******************************************************************************************

const char *JsonContainerTypeToString(const JsonContainerType type)
{
switch (type)
{
case JSON_CONTAINER_TYPE_ARRAY:
return "array";
case JSON_CONTAINER_TYPE_OBJECT:
return "object";
default:
UnexpectedError("Unknown JSON container type: %d", type);
return "(null)";
}
}

const char *JsonPrimitiveTypeToString(const JsonPrimitiveType type)
{
switch (type)
Expand Down Expand Up @@ -262,11 +276,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 +291,7 @@ static int JsonArrayCompare(
ret = JsonCompare(child_a, child_b);
if (ret != 0)
{
Log(LOG_LEVEL_DEBUG, "JsonArrayCompare() fails for index %zu, children not equal", i);
return ret;
}
}
Expand All @@ -296,34 +312,32 @@ 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);
const char *const key = JsonIteratorNextKey(&iter);
const JsonElement *const child_a = JsonIteratorCurrentValue(&iter);
const JsonElement *const child_b = JsonObjectGet(b, key);

ret = strcmp(key_a, key_b);
if (ret != 0)
if (child_b == NULL)
{
return ret;
Log(LOG_LEVEL_DEBUG, "JsonObjectCompare() fails for key '%s', not present in object b", key);
return 1;
}

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

return ret;
return ret; // here ret will be 0 in the case that both objects are empty or all children are equal
}


Expand All @@ -340,6 +354,7 @@ static int JsonContainerCompare(

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

Expand Down Expand Up @@ -367,6 +382,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 +392,14 @@ 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);
{
if (!StringEqual(a->primitive.value, b->primitive.value))
{
Log(LOG_LEVEL_DEBUG, "JsonCompare() fails, primitive '%s' not equal to '%s'", a->primitive.value, b->primitive.value);
return 1;
}
return 0;
}

default:
UnexpectedError("Unknown JSON element type: %d", type_a);
Expand Down Expand Up @@ -1198,7 +1221,7 @@ static int JsonElementHasProperty(

assert(element->propertyName != NULL);

if (strcmp(propertyName, element->propertyName) == 0)
if (StringEqual(propertyName, element->propertyName))
{
return 0;
}
Expand Down
10 changes: 10 additions & 0 deletions libutils/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ char *Json5EscapeData(Slice unescaped_data);
//////////////////////////////////////////////////////////////////////////////

JsonElement *JsonCopy(const JsonElement *json);

/**
* @brief compare two JsonElement instances
*
* @param a JsonElement
* @param b JsonElement
* @return int like strcmp(), 0 is equal, non-zero is not equal
*/
int JsonCompare(const JsonElement *a, const JsonElement *b);

JsonElement *JsonMerge(const JsonElement *a, const JsonElement *b);

/**
Expand Down Expand Up @@ -228,6 +237,7 @@ const char *JsonGetPropertyAsString(const JsonElement *element);
// JSON Primitives
//////////////////////////////////////////////////////////////////////////////

const char *JsonContainerTypeToString(JsonContainerType type);
const char *JsonPrimitiveTypeToString(JsonPrimitiveType type);
JsonPrimitiveType JsonGetPrimitiveType(const JsonElement *primitive);
const char *JsonPrimitiveGetAsString(const JsonElement *primitive);
Expand Down
Loading

0 comments on commit 28df3c4

Please sign in to comment.