diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-07-24 09:00:35 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-07-24 09:00:35 +0200 |
commit | 42decef957861689c41d16a0dcfa8af3d9052816 (patch) | |
tree | ed55c4e5a2e2a4b8d2880fe10c8f82173cf0a11e /src/json | |
parent | 325b60989faadb54213af578bb9b1b705d022726 (diff) | |
download | exchange-42decef957861689c41d16a0dcfa8af3d9052816.tar.xz |
fix #6939 in exchange
Diffstat (limited to 'src/json')
-rw-r--r-- | src/json/json_helper.c | 62 | ||||
-rw-r--r-- | src/json/test_json.c | 4 |
2 files changed, 64 insertions, 2 deletions
diff --git a/src/json/json_helper.c b/src/json/json_helper.c index 866c094d0..1f8d320bf 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c @@ -55,7 +55,7 @@ TALER_JSON_from_amount_nbo (const struct TALER_AmountNBO *amount) /** * Parse given JSON object to Amount * - * @param cls closure, NULL + * @param cls closure, expected currency, or 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 @@ -65,6 +65,7 @@ parse_amount (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) { + const char *currency = cls; struct TALER_Amount *r_amount = spec->ptr; (void) cls; @@ -80,23 +81,52 @@ parse_amount (void *cls, GNUNET_break_op (0); return GNUNET_SYSERR; } + if ( (NULL != currency) && + (0 != + strcasecmp (currency, + r_amount->currency)) ) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } struct GNUNET_JSON_Specification TALER_JSON_spec_amount (const char *name, + const char *currency, struct TALER_Amount *r_amount) { struct GNUNET_JSON_Specification ret = { .parser = &parse_amount, .cleaner = NULL, + .cls = (void *) currency, + .field = name, + .ptr = r_amount, + .ptr_size = 0, + .size_ptr = NULL + }; + + GNUNET_assert (NULL != currency); + return ret; +} + + +struct GNUNET_JSON_Specification +TALER_JSON_spec_amount_any (const char *name, + struct TALER_Amount *r_amount) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_amount, + .cleaner = NULL, .cls = NULL, .field = name, .ptr = r_amount, .ptr_size = 0, .size_ptr = NULL }; + return ret; } @@ -114,6 +144,7 @@ parse_amount_nbo (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) { + const char *currency = cls; struct TALER_AmountNBO *r_amount = spec->ptr; const char *sv; @@ -134,23 +165,52 @@ parse_amount_nbo (void *cls, GNUNET_break_op (0); return GNUNET_SYSERR; } + if ( (NULL != currency) && + (0 != + strcasecmp (currency, + r_amount->currency)) ) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } struct GNUNET_JSON_Specification TALER_JSON_spec_amount_nbo (const char *name, + const char *currency, struct TALER_AmountNBO *r_amount) { struct GNUNET_JSON_Specification ret = { .parser = &parse_amount_nbo, .cleaner = NULL, + .cls = (void *) currency, + .field = name, + .ptr = r_amount, + .ptr_size = 0, + .size_ptr = NULL + }; + + GNUNET_assert (NULL != currency); + return ret; +} + + +struct GNUNET_JSON_Specification +TALER_JSON_spec_amount_any_nbo (const char *name, + struct TALER_AmountNBO *r_amount) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_amount_nbo, + .cleaner = NULL, .cls = NULL, .field = name, .ptr = r_amount, .ptr_size = 0, .size_ptr = NULL }; + return ret; } diff --git a/src/json/test_json.c b/src/json/test_json.c index bedea76ac..e312a4ed3 100644 --- a/src/json/test_json.c +++ b/src/json/test_json.c @@ -36,7 +36,9 @@ test_amount (void) struct TALER_Amount a1; struct TALER_Amount a2; struct GNUNET_JSON_Specification spec[] = { - TALER_JSON_spec_amount ("amount", &a2), + TALER_JSON_spec_amount ("amount", + "EUR", + &a2), GNUNET_JSON_spec_end () }; |