summaryrefslogtreecommitdiffstats
path: root/src/wrap-json.c
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 /src/wrap-json.c
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>
Diffstat (limited to 'src/wrap-json.c')
-rw-r--r--src/wrap-json.c62
1 files changed, 62 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>