From: Christian Grothoff Date: Sun, 19 Apr 2020 19:37:35 +0000 (+0200) Subject: add i64 deserializer X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=254a9f066b2fb96b347ddb07040608eacaf7b942;p=oweals%2Fgnunet.git add i64 deserializer --- diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 82b8502e0..f6cabd589 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h @@ -225,7 +225,8 @@ GNUNET_JSON_spec_json (const char *name, json_t **jsonp); * @param[out] u8 where to store the integer found under @a name */ struct GNUNET_JSON_Specification -GNUNET_JSON_spec_uint8 (const char *name, uint8_t *u8); +GNUNET_JSON_spec_uint8 (const char *name, + uint8_t *u8); /** @@ -235,7 +236,8 @@ GNUNET_JSON_spec_uint8 (const char *name, uint8_t *u8); * @param[out] u16 where to store the integer found under @a name */ struct GNUNET_JSON_Specification -GNUNET_JSON_spec_uint16 (const char *name, uint16_t *u16); +GNUNET_JSON_spec_uint16 (const char *name, + uint16_t *u16); /** @@ -245,7 +247,8 @@ GNUNET_JSON_spec_uint16 (const char *name, uint16_t *u16); * @param[out] u32 where to store the integer found under @a name */ struct GNUNET_JSON_Specification -GNUNET_JSON_spec_uint32 (const char *name, uint32_t *u32); +GNUNET_JSON_spec_uint32 (const char *name, + uint32_t *u32); /** @@ -255,7 +258,19 @@ GNUNET_JSON_spec_uint32 (const char *name, uint32_t *u32); * @param[out] u64 where to store the integer found under @a name */ struct GNUNET_JSON_Specification -GNUNET_JSON_spec_uint64 (const char *name, uint64_t *u64); +GNUNET_JSON_spec_uint64 (const char *name, + uint64_t *u64); + + +/** + * 64-bit signed integer. + * + * @param name name of the JSON field + * @param[out] i64 where to store the integer found under @a name + */ +struct GNUNET_JSON_Specification +GNUNET_JSON_spec_int64 (const char *name, + int64_t *i64); /** diff --git a/src/json/json_helper.c b/src/json/json_helper.c index a405b8c3b..ea8408762 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c @@ -495,7 +495,7 @@ GNUNET_JSON_spec_uint32 (const char *name, /** - * Parse given JSON object to a uint8_t. + * Parse given JSON object to a uint64_t. * * @param cls closure, NULL * @param root the json object representing data @@ -545,6 +545,57 @@ GNUNET_JSON_spec_uint64 (const char *name, } +/** + * Parse given JSON object to a int64_t. + * + * @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_u64 (void *cls, + json_t *root, + struct GNUNET_JSON_Specification *spec) +{ + json_int_t val; + int64_t *up = spec->ptr; + + if (! json_is_integer (root)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + val = json_integer_value (root); + *up = (int64_t) val; + return GNUNET_OK; +} + + +/** + * 64-bit signed integer. + * + * @param name name of the JSON field + * @param[out] i64 where to store the integer found under @a name + */ +struct GNUNET_JSON_Specification +GNUNET_JSON_spec_uint64 (const char *name, + int64_t *i64) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_i64, + .cleaner = NULL, + .cls = NULL, + .field = name, + .ptr = i64, + .ptr_size = sizeof(int64_t), + .size_ptr = NULL + }; + + return ret; +} + + /* ************ GNUnet-specific parser specifications ******************* */ /**