summaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json_helper.c62
-rw-r--r--src/json/test_json.c4
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 ()
};