[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet] branch master updated: NEWS: add support for encoding/decoding
From: |
gnunet |
Subject: |
[gnunet] branch master updated: NEWS: add support for encoding/decoding double values as part of JSON to libgnunetjson |
Date: |
Fri, 13 Oct 2023 23:52:23 +0200 |
This is an automated email from the git hooks/post-receive script.
grothoff pushed a commit to branch master
in repository gnunet.
The following commit(s) were added to refs/heads/master by this push:
new e1a3225cd NEWS: add support for encoding/decoding double values as
part of JSON to libgnunetjson
e1a3225cd is described below
commit e1a3225cda96b8fd99d437ba3c53a9badd341693
Author: Christian Grothoff <grothoff@gnunet.org>
AuthorDate: Fri Oct 13 23:51:45 2023 +0200
NEWS: add support for encoding/decoding double values as part of JSON to
libgnunetjson
---
src/include/gnunet_json_lib.h | 24 ++++++++++++++++++++++++
src/json/json_helper.c | 43 +++++++++++++++++++++++++++++++++++++++++++
src/json/json_pack.c | 13 +++++++++++++
3 files changed, 80 insertions(+)
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index 26c68fed3..c3204026b 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -290,6 +290,17 @@ GNUNET_JSON_spec_bool (const char *name,
bool *b);
+/**
+ * double.
+ *
+ * @param name name of the JSON field
+ * @param[out] f where to store the double found under @a name
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_double (const char *name,
+ double *f);
+
+
/**
* 8-bit integer.
*
@@ -702,6 +713,19 @@ GNUNET_JSON_pack_bool (const char *name,
bool b);
+/**
+ * Generate packer instruction for a JSON field of type
+ * double.
+ *
+ * @param name name of the field to add to the object
+ * @param f double value
+ * @return json pack specification
+ */
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_double (const char *name,
+ double f);
+
+
/**
* Generate packer instruction for a JSON field of type
* string.
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 515cc4c7f..4795285f4 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -489,6 +489,49 @@ GNUNET_JSON_spec_bool (const char *name,
}
+/**
+ * Parse given JSON object to a double.
+ *
+ * @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 enum GNUNET_GenericReturnValue
+parse_double (void *cls,
+ json_t *root,
+ struct GNUNET_JSON_Specification *spec)
+{
+ double *f = spec->ptr;
+
+ if (! json_is_real (root))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ *f = json_real_value (root);
+ return GNUNET_OK;
+}
+
+
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_double (const char *name,
+ double *f)
+{
+ struct GNUNET_JSON_Specification ret = {
+ .parser = &parse_double,
+ .cleaner = NULL,
+ .cls = NULL,
+ .field = name,
+ .ptr = f,
+ .ptr_size = sizeof(double),
+ .size_ptr = NULL
+ };
+
+ return ret;
+}
+
+
/**
* Parse given JSON object to a uint8_t.
*
diff --git a/src/json/json_pack.c b/src/json/json_pack.c
index 816373eaf..18487c3f4 100644
--- a/src/json/json_pack.c
+++ b/src/json/json_pack.c
@@ -98,6 +98,19 @@ GNUNET_JSON_pack_bool (const char *name,
}
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_double (const char *name,
+ double f)
+{
+ struct GNUNET_JSON_PackSpec ps = {
+ .field_name = name,
+ .object = json_real (f)
+ };
+
+ return ps;
+}
+
+
struct GNUNET_JSON_PackSpec
GNUNET_JSON_pack_string (const char *name,
const char *s)
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnunet] branch master updated: NEWS: add support for encoding/decoding double values as part of JSON to libgnunetjson,
gnunet <=