summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-07-31 16:31:47 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-07-31 16:31:47 +0200
commit129e549d44b5e8075ef87a260577344e912c92aa (patch)
tree8d2d3289a0596ce12bd7c857e262b8b79fc21754
parentfbe9349a747aba8d7c8c27e51cdd8c3c5ef8b39c (diff)
wrap-json: add "for_all" functions
The "for_all" functions are facilities for handling collections of json objects Change-Id: Icaecb39e9f0108e1fb1c42bef2a90935feb312ed Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/wrap-json.c62
-rw-r--r--src/wrap-json.h10
2 files changed, 72 insertions, 0 deletions
diff --git a/src/wrap-json.c b/src/wrap-json.c
index a762c383..271a825a 100644
--- a/src/wrap-json.c
+++ b/src/wrap-json.c
@@ -643,6 +643,68 @@ int wrap_json_unpack(struct json_object *object, const char *desc, ...)
return rc;
}
+static void object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure)
+{
+ struct json_object_iterator it = json_object_iter_begin(object);
+ struct json_object_iterator end = json_object_iter_end(object);
+ while (!json_object_iter_equal(&it, &end)) {
+ callback(closure, json_object_iter_peek_value(&it), json_object_iter_peek_name(&it));
+ json_object_iter_next(&it);
+ }
+}
+
+static void array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure)
+{
+ int n = json_object_array_length(object);
+ int i = 0;
+ while(i < n)
+ callback(closure, json_object_array_get_idx(object, i++));
+}
+
+void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure)
+{
+ if (json_object_is_type(object, json_type_array))
+ array_for_all(object, callback, closure);
+ else
+ callback(closure, object);
+}
+
+void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure)
+{
+ if (json_object_is_type(object, json_type_array))
+ array_for_all(object, callback, closure);
+}
+
+void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure)
+{
+ if (json_object_is_type(object, json_type_object))
+ object_for_all(object, callback, closure);
+}
+
+void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure)
+{
+ if (json_object_is_type(object, json_type_object))
+ object_for_all(object, callback, closure);
+ else
+ callback(closure, object, NULL);
+}
+
+void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure)
+{
+ if (!object)
+ /* do nothing */;
+ else if (json_object_is_type(object, json_type_object))
+ object_for_all(object, callback, closure);
+ else if (!json_object_is_type(object, json_type_array))
+ callback(closure, object, NULL);
+ else {
+ int n = json_object_array_length(object);
+ int i = 0;
+ while(i < n)
+ callback(closure, json_object_array_get_idx(object, i++), NULL);
+ }
+}
+
#if defined(WRAP_JSON_TEST)
#include <stdio.h>
diff --git a/src/wrap-json.h b/src/wrap-json.h
index 7fe90eee..cb2d0bfe 100644
--- a/src/wrap-json.h
+++ b/src/wrap-json.h
@@ -34,3 +34,13 @@ extern int wrap_json_vcheck(struct json_object *object, const char *desc, va_lis
extern int wrap_json_check(struct json_object *object, const char *desc, ...);
extern int wrap_json_vmatch(struct json_object *object, const char *desc, va_list args);
extern int wrap_json_match(struct json_object *object, const char *desc, ...);
+
+extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure);
+extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure);
+
+extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure);
+extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure);
+extern void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure);
+extern void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure);
+extern void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure);
+