gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-exchange] branch master updated: serialize amounts a


From: gnunet
Subject: [GNUnet-SVN] [taler-exchange] branch master updated: serialize amounts as string instead of an object
Date: Mon, 29 Jan 2018 19:26:32 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository exchange.

The following commit(s) were added to refs/heads/master by this push:
     new d126b16  serialize amounts as string instead of an object
d126b16 is described below

commit d126b166241e36a33884bc799190c708226ddb7e
Author: Florian Dold <address@hidden>
AuthorDate: Mon Jan 29 19:26:10 2018 +0100

    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 4f966c6..c8eab06 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 3a89746..d5b6d13 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,

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]