summaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-01-16 08:49:26 +0100
committerChristian Grothoff <christian@grothoff.org>2020-01-16 08:49:26 +0100
commit7c6853d830336a377410fb15411a0350f184f092 (patch)
treec9530646e52008b688fd84d60bb16c68c1c46b89 /src/json
parent90e15720391b2943096352c023f64544a8d009a8 (diff)
downloadexchange-7c6853d830336a377410fb15411a0350f184f092.tar.gz
exchange-7c6853d830336a377410fb15411a0350f184f092.tar.bz2
exchange-7c6853d830336a377410fb15411a0350f184f092.zip
simplify amount logic
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json_helper.c107
1 files changed, 9 insertions, 98 deletions
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 0f67e9d8..536eece9 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -78,65 +78,20 @@ parse_amount (void *cls,
struct GNUNET_JSON_Specification *spec)
{
struct TALER_Amount *r_amount = spec->ptr;
- json_int_t value;
- json_int_t fraction;
- const char *currency;
(void) cls;
- if (json_is_string (root))
- {
- if (GNUNET_OK !=
- TALER_string_to_amount (json_string_value (root), r_amount))
- {
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- return GNUNET_OK;
- }
-
- /* Also allow the legacy { value, fraction, currency} format.
- This might be removed in the future. */
-
- memset (r_amount,
- 0,
- sizeof (struct TALER_Amount));
- if (0 != json_unpack (root,
- "{s:I, s:I, s:s}",
- "value", &value,
- "fraction", &fraction,
- "currency", &currency))
- {
- char *json_enc;
-
- if (NULL == (json_enc = json_dumps (root,
- JSON_COMPACT | JSON_ENCODE_ANY)))
- {
- GNUNET_break (0);
- return GNUNET_SYSERR;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- "Malformed JSON amount: %s\n",
- json_enc);
- free (json_enc);
- return GNUNET_SYSERR;
- }
- if ( (value < 0) ||
- (fraction < 0) ||
- (((uint64_t) value) > UINT64_MAX) ||
- (fraction > UINT32_MAX) )
+ if (! json_is_string (root))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
- if (strlen (currency) >= TALER_CURRENCY_LEN)
+ if (GNUNET_OK !=
+ TALER_string_to_amount (json_string_value (root),
+ r_amount))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
- r_amount->value = (uint64_t) value;
- r_amount->fraction = (uint32_t) fraction;
- strcpy (r_amount->currency, currency);
- (void) TALER_amount_normalize (r_amount);
return GNUNET_OK;
}
@@ -178,64 +133,20 @@ parse_amount_nbo (void *cls,
struct GNUNET_JSON_Specification *spec)
{
struct TALER_AmountNBO *r_amount = spec->ptr;
- struct TALER_Amount amount;
- json_int_t value;
- json_int_t fraction;
- const char *currency;
(void) cls;
- if (json_is_string (root))
+ if (! json_is_string (root))
{
- if (GNUNET_OK !=
- TALER_string_to_amount_nbo (json_string_value (root), r_amount))
- {
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- return GNUNET_OK;
- }
-
- memset (&amount,
- 0,
- sizeof (struct TALER_Amount));
- if (0 != json_unpack (root,
- "{s:I, s:I, s:s}",
- "value", &value,
- "fraction", &fraction,
- "currency", &currency))
- {
- char *json_enc;
-
- if (NULL == (json_enc = json_dumps (root,
- JSON_COMPACT | JSON_ENCODE_ANY)))
- {
- GNUNET_break (0);
- return GNUNET_SYSERR;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- "Malformed JSON amount: %s\n",
- json_enc);
- free (json_enc);
- return GNUNET_SYSERR;
- }
- if ( (value < 0) ||
- (fraction < 0) ||
- (fraction > (json_int_t) UINT32_MAX) )
- {
- GNUNET_break_op (0);
+ GNUNET_break (0);
return GNUNET_SYSERR;
}
- if (strlen (currency) >= TALER_CURRENCY_LEN)
+ if (GNUNET_OK !=
+ TALER_string_to_amount_nbo (json_string_value (root),
+ r_amount))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
- amount.value = (uint64_t) value;
- amount.fraction = (uint32_t) fraction;
- strcpy (amount.currency, currency);
- (void) TALER_amount_normalize (&amount);
- TALER_amount_hton (r_amount,
- &amount);
return GNUNET_OK;
}