X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwrap-json.c;h=9bb8d192795913ed7eeb1c6b8a4dc186039a48b6;hb=0891ef4826e347d5554c630b5c0ce73c68f76c9c;hp=a762c383c044093a075dec33ffe518bc95f0e136;hpb=ab50e75b91041f7c4cfad896370fc0029b072d44;p=src%2Fapp-framework-binder.git diff --git a/src/wrap-json.c b/src/wrap-json.c index a762c383..9bb8d192 100644 --- a/src/wrap-json.c +++ b/src/wrap-json.c @@ -525,6 +525,7 @@ static int vunpack(struct json_object *object, const char *desc, va_list args, i goto invalid_character; if (!ignore && top->index != top->count) goto incomplete; + /*@fallthrough@*/ case '*': acc = xacc; continue; @@ -643,6 +644,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 @@ -655,7 +718,7 @@ void p(const char *desc, ...) va_start(args, desc); rc = wrap_json_vpack(&result, desc, args); va_end(args); - if (!rc) + if (!rc) printf(" SUCCESS %s\n\n", json_object_to_json_string(result)); else printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); @@ -686,7 +749,7 @@ void u(const char *value, const char *desc, ...) va_start(args, desc); rc = wrap_json_vunpack(obj, desc, args); va_end(args); - if (rc) + if (rc) printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); else { value = NULL;