diff options
author | José Bollo <jose.bollo@iot.bzh> | 2018-12-13 19:39:24 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-12-17 15:03:22 +0100 |
commit | 264b7abf8da063a1942da8db248c2529e6ab3ffc (patch) | |
tree | 001e329443b3761a3bf1a2cab570a46ef51f411a | |
parent | 00c0381b981fd8ba9c2db650cf0575799d6e7249 (diff) |
wrap-json: Improvements
* Most call "d = skip(++d);" are replaced by
"d = skip(d + 1);" that is equivalent but that
virtually doesn't assign transiently d with d+1.
* make use of JSON_C_TO_STRING_NOSLASHESCAPE in
test to improve output. Use a macro to reduce the
code.
* enforce 'wrap_json_check' to use 'wrap_json_vcheck'
and 'wrap_json_match' to use 'wrap_json_vmatch'.
Change-Id: I6e823da40415db4b54ec809455c93b39a8c5e9a5
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | wrap-json.c | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/wrap-json.c b/wrap-json.c index 0633991..27940be 100644 --- a/wrap-json.c +++ b/wrap-json.c @@ -288,7 +288,7 @@ int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) goto truncated; if (!strchr(top->acc, c)) goto invalid_character; - d = skip(++d); + d = skip(d + 1); switch(c) { case 's': nullable = 0; @@ -300,24 +300,24 @@ int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) if (strs[nstr].str) notnull = 1; if (*d == '?') { - d = skip(++d); + d = skip(d + 1); nullable = 1; } switch(*d) { - case '%': strs[nstr].sz = va_arg(args, size_t); d = skip(++d); break; - case '#': strs[nstr].sz = (size_t)va_arg(args, int); d = skip(++d); break; + case '%': strs[nstr].sz = va_arg(args, size_t); d = skip(d + 1); break; + case '#': strs[nstr].sz = (size_t)va_arg(args, int); d = skip(d + 1); break; default: strs[nstr].sz = strs[nstr].str ? strlen(strs[nstr].str) : 0; break; } sz += strs[nstr++].sz; if (*d == '?') { - d = skip(++d); + d = skip(d + 1); nullable = 1; } if (*d != '+') break; if (nstr >= STRCOUNT) goto too_long; - d = skip(++d); + d = skip(d + 1); } if (*d == '*') nullable = 1; @@ -369,7 +369,7 @@ int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) case 'O': obj = va_arg(args, struct json_object*); if (*d == '?') - d = skip(++d); + d = skip(d + 1); else if (*d != '*' && !obj) goto null_object; if (c == 'O') @@ -392,7 +392,7 @@ int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) goto out_of_memory; } if (*d == '?') - d = skip(++d); + d = skip(d + 1); else if (*d != '*' && !obj) { obj = json_object_new_string_len(d, 0); if (!obj) @@ -441,7 +441,7 @@ int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) if (obj || *d != '*') json_object_array_add(top->cont, obj); if (*d == '*') - d = skip(++d); + d = skip(d + 1); break; case '}': if (!obj) @@ -454,7 +454,7 @@ int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) if (obj || *d != '*') json_object_object_add(top->cont, json_object_get_string(top->key), obj); if (*d == '*') - d = skip(++d); + d = skip(d + 1); json_object_put(top->key); top->key = NULL; top->acc = pack_accept_key; @@ -549,7 +549,7 @@ static int vunpack(struct json_object *object, const char *desc, va_list args, i goto truncated; if (!strchr(acc, c)) goto invalid_character; - d = skip(++d); + d = skip(d + 1); switch(c) { case 's': if (xacc[0] == '}') { @@ -561,7 +561,7 @@ static int vunpack(struct json_object *object, const char *desc, va_list args, i optionnal = 0; else { optionnal = 1; - d = skip(++d); + d = skip(d + 1); } if (ignore) ignore++; @@ -591,7 +591,7 @@ static int vunpack(struct json_object *object, const char *desc, va_list args, i *ps = json_object_get_string(obj); } if (*d == '%') { - d = skip(++d); + d = skip(d + 1); if (store) { pz = va_arg(args, size_t *); if (!ignore && pz) @@ -815,7 +815,7 @@ int wrap_json_check(struct json_object *object, const char *desc, ...) va_list args; va_start(args, desc); - rc = vunpack(object, desc, args, 0); + rc = wrap_json_vcheck(object, desc, args); va_end(args); return rc; } @@ -831,9 +831,9 @@ int wrap_json_match(struct json_object *object, const char *desc, ...) va_list args; va_start(args, desc); - rc = vunpack(object, desc, args, 0); + rc = wrap_json_vmatch(object, desc, args); va_end(args); - return !rc; + return rc; } int wrap_json_vunpack(struct json_object *object, const char *desc, va_list args) @@ -1251,6 +1251,10 @@ int wrap_json_contains(struct json_object *x, struct json_object *y) #if defined(WRAP_JSON_TEST) #include <stdio.h> +#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE) +#define JSON_C_TO_STRING_NOSLASHESCAPE 0 +#endif +#define j2t(o) json_object_to_json_string_ext((o), JSON_C_TO_STRING_NOSLASHESCAPE) void tclone(struct json_object *object) { @@ -1258,12 +1262,12 @@ void tclone(struct json_object *object) o = wrap_json_clone(object); if (!wrap_json_equal(object, o)) - printf("ERROR in clone or equal: %s VERSUS %s\n", json_object_to_json_string(object), json_object_to_json_string(o)); + printf("ERROR in clone or equal: %s VERSUS %s\n", j2t(object), j2t(o)); json_object_put(o); o = wrap_json_clone_deep(object); if (!wrap_json_equal(object, o)) - printf("ERROR in clone_deep or equal: %s VERSUS %s\n", json_object_to_json_string(object), json_object_to_json_string(o)); + printf("ERROR in clone_deep or equal: %s VERSUS %s\n", j2t(object), j2t(o)); json_object_put(o); } @@ -1277,7 +1281,7 @@ void p(const char *desc, ...) rc = wrap_json_vpack(&result, desc, args); va_end(args); if (!rc) - printf(" SUCCESS %s\n\n", json_object_to_json_string(result)); + printf(" SUCCESS %s\n\n", j2t(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)); tclone(result); @@ -1331,8 +1335,8 @@ void u(const char *value, const char *desc, ...) case 'I': printf(" I:%lld", *va_arg(args, int64_t*)); k = m&1; break; case 'f': printf(" f:%f", *va_arg(args, double*)); k = m&1; break; case 'F': printf(" F:%f", *va_arg(args, double*)); k = m&1; break; - case 'o': printf(" o:%s", json_object_to_json_string(*va_arg(args, struct json_object**))); k = m&1; break; - case 'O': o = *va_arg(args, struct json_object**); printf(" O:%s", json_object_to_json_string(o)); json_object_put(o); k = m&1; break; + case 'o': printf(" o:%s", j2t(*va_arg(args, struct json_object**))); k = m&1; break; + case 'O': o = *va_arg(args, struct json_object**); printf(" O:%s", j2t(o)); json_object_put(o); k = m&1; break; case 'y': case 'Y': { uint8_t *p = *va_arg(args, uint8_t**); |