implement boolean json spec
authorFlorian Dold <florian.dold@gmail.com>
Mon, 22 Jan 2018 16:05:38 +0000 (17:05 +0100)
committerFlorian Dold <florian.dold@gmail.com>
Mon, 22 Jan 2018 16:08:27 +0000 (17:08 +0100)
src/include/gnunet_json_lib.h
src/json/json_helper.c
src/json/test_json.c

index c12badcd9c7eb207d3a459658d041e893e363516..49120982ab5520871cacd39507aa1b4a4c29ce98 100644 (file)
@@ -248,6 +248,16 @@ struct GNUNET_JSON_Specification
 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 ******************* */
 
index 194ec5c76a5ff581cd02f32290ba49b1fec73e32..c7eca97eccfb2ff18c3d5169e82c0e6094980e27 100644 (file)
@@ -943,4 +943,52 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
 }
 
 
+/**
+ * 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 */
index 09a154678bba08f0d3022ca20892238e580ad835..adab817b9c55d7d680e93cc5db19e3eb598899c6 100644 (file)
@@ -195,6 +195,44 @@ test_rsa ()
 }
 
 
+/**
+ * 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[])
@@ -210,6 +248,8 @@ main(int argc,
     return 1;
   if (0 != test_rsa ())
     return 1;
+  if (0 != test_boolean ())
+    return 1;
   /* FIXME: test EdDSA signature conversion... */
   return 0;
 }