summaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-01-29 19:26:10 +0100
committerFlorian Dold <florian.dold@gmail.com>2018-01-29 19:26:10 +0100
commitd126b166241e36a33884bc799190c708226ddb7e (patch)
treef953a28008961817f19e7bd88b63aa7f755f6bfb /src/json
parentafe945167ad8f794a6b9b1dbb23d7a1148c1c2eb (diff)
downloadexchange-d126b166241e36a33884bc799190c708226ddb7e.tar.gz
exchange-d126b166241e36a33884bc799190c708226ddb7e.tar.bz2
exchange-d126b166241e36a33884bc799190c708226ddb7e.zip
serialize amounts as string instead of an object
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json_helper.c47
-rw-r--r--src/json/test_json.c4
2 files changed, 33 insertions, 18 deletions
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 4f966c66..c8eab065 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 3a89746b..d5b6d13e 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,