From 1a8041c503e5cd75e9fc45bb3d085f65d7ea76cb Mon Sep 17 00:00:00 2001 From: Helmut Schaa Date: Tue, 8 Sep 2015 12:41:58 +0200 Subject: [PATCH] Add utility function for comparing json_objects --- json_object.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ json_object.h | 11 +++++++ 2 files changed, 93 insertions(+) diff --git a/json_object.c b/json_object.c index e6bd870..5a665c3 100644 --- a/json_object.c +++ b/json_object.c @@ -933,3 +933,85 @@ struct json_object* json_object_array_get_idx(struct json_object *jso, return (struct json_object*)array_list_get_idx(jso->o.c_array, idx); } +static json_bool json_array_equal(struct json_object* jso1, + struct json_object* jso2) +{ + int len, i; + + len = json_object_array_length(jso1); + if (len != json_object_array_length(jso2)) + return FALSE; + + for (i = 0; i < len; i++) { + if (!json_object_equal(json_object_array_get_idx(jso1, i), + json_object_array_get_idx(jso2, i))) + return FALSE; + } + return TRUE; +} + +static json_bool json_hash_equal(struct json_object* jso1, + struct json_object* jso2) +{ + struct json_object_iter iter; + struct json_object *sub; + + json_object_object_foreachC(jso1, iter) { + if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key, + (void**)&sub)) + return FALSE; + if (!json_object_equal(iter.val, sub)) + return FALSE; + } + + /* FIXME: Not very efficient, only check remaining keys */ + json_object_object_foreachC(jso2, iter) { + if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key, + (void**)&sub)) + return FALSE; + if (!json_object_equal(iter.val, sub)) + return FALSE; + } + + return TRUE; +} + +json_bool json_object_equal(struct json_object* jso1, struct json_object* jso2) +{ + if (jso1 == jso2) + return TRUE; + + if (!jso1 || !jso2) + return FALSE; + + if (jso1->o_type != jso2->o_type) + return FALSE; + + switch(jso1->o_type) { + case json_type_boolean: + return (jso1->o.c_boolean == jso2->o.c_boolean); + + case json_type_double: + return (jso1->o.c_double == jso2->o.c_double); + + case json_type_int: + return (jso1->o.c_int64 == jso2->o.c_int64); + + case json_type_string: + return (jso1->o.c_string.len == jso2->o.c_string.len && + memcmp(jso1->o.c_string.str, + jso2->o.c_string.str, + jso1->o.c_string.len)); + + case json_type_object: + return json_hash_equal(jso1, jso2); + + case json_type_array: + return json_array_equal(jso1, jso2); + + case json_type_null: + return TRUE; + }; + + return FALSE; +} diff --git a/json_object.h b/json_object.h index 9e81ebe..f3c3f44 100644 --- a/json_object.h +++ b/json_object.h @@ -637,6 +637,17 @@ extern const char* json_object_get_string(struct json_object *obj); */ extern int json_object_get_string_len(struct json_object *obj); +/** Check if two json_object's are equal + * + * If the passed objects are equal 1 will be returned. + * + * @param obj1 the first json_object instance + * @param obj2 the second json_object instance + * @returns whether both objects are equal or not + */ +extern json_bool json_object_equal(struct json_object *obj1, + struct json_object *obj2); + #ifdef __cplusplus } #endif