From d126b166241e36a33884bc799190c708226ddb7e Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 29 Jan 2018 19:26:10 +0100 Subject: serialize amounts as string instead of an object --- src/json/json_helper.c | 47 +++++++++++++++++++++++++++++++---------------- src/json/test_json.c | 4 ++-- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/json/json_helper.c b/src/json/json_helper.c index 4f966c66f..c8eab065a 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c @@ -35,25 +35,15 @@ json_t * TALER_JSON_from_amount (const struct TALER_Amount *amount) { - json_t *j; + char *amount_str = TALER_amount_to_string (amount); + + GNUNET_assert (NULL != amount_str); - if ( (amount->value != (uint64_t) ((json_int_t) amount->value)) || - (0 > ((json_int_t) amount->value)) ) { - /* Theoretically, json_int_t can be a 32-bit "long", or we might - have a 64-bit value which converted to a 63-bit signed long - long causes problems here. So we check. Note that depending - on the platform, the compiler may be able to statically tell - that at least the first check is always false. */ - GNUNET_break (0); - return NULL; + json_t *j = json_string (amount_str); + GNUNET_free (amount_str); + return j; } - j = json_pack ("{s:s, s:I, s:I}", - "currency", amount->currency, - "value", (json_int_t) amount->value, - "fraction", (json_int_t) amount->fraction); - GNUNET_assert (NULL != j); - return j; } @@ -93,6 +83,20 @@ parse_amount (void *cls, json_int_t fraction; const char *currency; + 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)); @@ -179,6 +183,17 @@ parse_amount_nbo (void *cls, json_int_t fraction; const char *currency; + 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)); diff --git a/src/json/test_json.c b/src/json/test_json.c index 3a89746bd..d5b6d13e9 100644 --- a/src/json/test_json.c +++ b/src/json/test_json.c @@ -36,14 +36,14 @@ test_amount () struct TALER_Amount a1; struct TALER_Amount a2; struct GNUNET_JSON_Specification spec[] = { - TALER_JSON_spec_amount (NULL, &a2), + TALER_JSON_spec_amount ("amount", &a2), GNUNET_JSON_spec_end() }; GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("EUR:4.3", &a1)); - j = TALER_JSON_from_amount (&a1); + j = json_pack("{s:o}", "amount", TALER_JSON_from_amount (&a1)); GNUNET_assert (NULL != j); GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, -- cgit v1.2.3