gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet] 01/02: add JSON spec'er for booleans


From: gnunet
Subject: [gnunet] 01/02: add JSON spec'er for booleans
Date: Tue, 21 Apr 2020 13:28:15 +0200

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

grothoff pushed a commit to branch master
in repository gnunet.

commit 5f5a1c7b44f659058175da1f69abfa709f688890
Author: Christian Grothoff <address@hidden>
AuthorDate: Tue Apr 21 12:15:16 2020 +0200

    add JSON spec'er for booleans
---
 src/include/gnunet_json_lib.h | 11 +++++++++
 src/json/json_helper.c        | 54 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)

diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index f6cabd589..27996f18d 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -218,6 +218,17 @@ struct GNUNET_JSON_Specification
 GNUNET_JSON_spec_json (const char *name, json_t **jsonp);
 
 
+/**
+ * boolean.
+ *
+ * @param name name of the JSON field
+ * @param[out] b where to store the boolean found under @a name
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_bool (const char *name,
+                       bool *b);
+
+
 /**
  * 8-bit integer.
  *
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 74a92ce9f..02bd6bfab 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -326,6 +326,60 @@ GNUNET_JSON_spec_json (const char *name,
 }
 
 
+/**
+ * Parse given JSON object to a bool.
+ *
+ * @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_bool (void *cls,
+            json_t *root,
+            struct GNUNET_JSON_Specification *spec)
+{
+  bool *b = spec->ptr;
+
+  if (json_true () == root)
+  {
+    *b = true;
+    return GNUNET_OK;
+  }
+  if (json_false () == root)
+  {
+    *b = false;
+    return GNUNET_OK;
+  }
+  GNUNET_break_op (0);
+  return GNUNET_SYSERR;
+}
+
+
+/**
+ * boolean.
+ *
+ * @param name name of the JSON field
+ * @param[out] b where to store the boolean found under @a name
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_bool (const char *name,
+                       bool *b)
+{
+  struct GNUNET_JSON_Specification ret = {
+    .parser = &parse_bool,
+    .cleaner = NULL,
+    .cls = NULL,
+    .field = name,
+    .ptr = b,
+    .ptr_size = sizeof(bool),
+    .size_ptr = NULL
+  };
+
+  return ret;
+}
+
+
 /**
  * Parse given JSON object to a uint8_t.
  *

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



reply via email to

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