From 407e79aca5cc2caadb9c9a916fcb91c3e9f3c244 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 22 Jan 2018 17:05:38 +0100 Subject: [PATCH] implement boolean json spec --- src/include/gnunet_json_lib.h | 10 ++++++++ src/json/json_helper.c | 48 +++++++++++++++++++++++++++++++++++ src/json/test_json.c | 40 +++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index c12badcd9..49120982a 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h @@ -248,6 +248,16 @@ struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64 (const char *name, uint64_t *u64); +/** + * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO). + * + * @param name name of the JSON field + * @param[out] boolean where to store the boolean found under @a name + */ +struct GNUNET_JSON_Specification +GNUNET_JSON_spec_boolean (const char *name, + int *boolean); + /* ************ GNUnet-specific parser specifications ******************* */ diff --git a/src/json/json_helper.c b/src/json/json_helper.c index 194ec5c76..c7eca97ec 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c @@ -943,4 +943,52 @@ GNUNET_JSON_spec_rsa_signature (const char *name, } +/** + * Parse given JSON object to an int as a boolean. + * + * @param cls closure, NULL + * @param root the json object representing data + * @param[out] spec where to write the data + * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error + */ +static int +parse_boolean (void *cls, + json_t *root, + struct GNUNET_JSON_Specification *spec) +{ + int *bp = spec->ptr; + + if (! json_is_boolean (root)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO; + return GNUNET_OK; +} + + +/** + * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO). + * + * @param name name of the JSON field + * @param[out] boolean where to store the boolean found under @a name + */ +struct GNUNET_JSON_Specification +GNUNET_JSON_spec_boolean (const char *name, + int *boolean) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_boolean, + .cleaner = NULL, + .cls = NULL, + .field = name, + .ptr = boolean, + .ptr_size = sizeof (int), + .size_ptr = NULL + }; + return ret; +} + + /* end of json_helper.c */ diff --git a/src/json/test_json.c b/src/json/test_json.c index 09a154678..adab817b9 100644 --- a/src/json/test_json.c +++ b/src/json/test_json.c @@ -195,6 +195,44 @@ test_rsa () } +/** + * Test rsa conversions from/to JSON. + * + * @return 0 on success + */ +static int +test_boolean () +{ + int b1; + int b2; + json_t *json; + struct GNUNET_JSON_Specification pspec[] = { + GNUNET_JSON_spec_boolean ("b1", &b1), + GNUNET_JSON_spec_boolean ("b2", &b2), + GNUNET_JSON_spec_end() + }; + + json = json_object (); + json_object_set_new (json, "b1", json_true ()); + json_object_set_new (json, "b2", json_false ()); + + GNUNET_assert (GNUNET_OK == + GNUNET_JSON_parse (json, pspec, + NULL, NULL)); + + GNUNET_assert (GNUNET_YES == b1); + GNUNET_assert (GNUNET_NO == b2); + + json_object_set_new (json, "b1", json_integer (42)); + + GNUNET_assert (GNUNET_OK != + GNUNET_JSON_parse (json, pspec, + NULL, NULL)); + + return 0; +} + + int main(int argc, const char *const argv[]) @@ -210,6 +248,8 @@ main(int argc, return 1; if (0 != test_rsa ()) return 1; + if (0 != test_boolean ()) + return 1; /* FIXME: test EdDSA signature conversion... */ return 0; } -- 2.25.1