GNUNET_JSON_spec_uint64 (const char *name,
uint64_t *u64);
+/**
+ * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
+ *
+ * @param name name of the JSON field
+ * @param[out] boolean where to store the boolean found under @a name
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_boolean (const char *name,
+ int *boolean);
+
/* ************ GNUnet-specific parser specifications ******************* */
}
+/**
+ * Parse given JSON object to an int as a boolean.
+ *
+ * @param cls closure, 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
+ */
+static int
+parse_boolean (void *cls,
+ json_t *root,
+ struct GNUNET_JSON_Specification *spec)
+{
+ int *bp = spec->ptr;
+
+ if (! json_is_boolean (root))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO;
+ return GNUNET_OK;
+}
+
+
+/**
+ * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
+ *
+ * @param name name of the JSON field
+ * @param[out] boolean where to store the boolean found under @a name
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_boolean (const char *name,
+ int *boolean)
+{
+ struct GNUNET_JSON_Specification ret = {
+ .parser = &parse_boolean,
+ .cleaner = NULL,
+ .cls = NULL,
+ .field = name,
+ .ptr = boolean,
+ .ptr_size = sizeof (int),
+ .size_ptr = NULL
+ };
+ return ret;
+}
+
+
/* end of json_helper.c */
}
+/**
+ * Test rsa conversions from/to JSON.
+ *
+ * @return 0 on success
+ */
+static int
+test_boolean ()
+{
+ int b1;
+ int b2;
+ json_t *json;
+ struct GNUNET_JSON_Specification pspec[] = {
+ GNUNET_JSON_spec_boolean ("b1", &b1),
+ GNUNET_JSON_spec_boolean ("b2", &b2),
+ GNUNET_JSON_spec_end()
+ };
+
+ json = json_object ();
+ json_object_set_new (json, "b1", json_true ());
+ json_object_set_new (json, "b2", json_false ());
+
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_JSON_parse (json, pspec,
+ NULL, NULL));
+
+ GNUNET_assert (GNUNET_YES == b1);
+ GNUNET_assert (GNUNET_NO == b2);
+
+ json_object_set_new (json, "b1", json_integer (42));
+
+ GNUNET_assert (GNUNET_OK !=
+ GNUNET_JSON_parse (json, pspec,
+ NULL, NULL));
+
+ return 0;
+}
+
+
int
main(int argc,
const char *const argv[])
return 1;
if (0 != test_rsa ())
return 1;
+ if (0 != test_boolean ())
+ return 1;
/* FIXME: test EdDSA signature conversion... */
return 0;
}