gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet] branch master updated: implement typed JSON packer


From: gnunet
Subject: [gnunet] branch master updated: implement typed JSON packer
Date: Mon, 26 Jul 2021 16:35:51 +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 90095d19b implement typed JSON packer
90095d19b is described below

commit 90095d19b4da40e79dce43a31af398a8925bcc4a
Author: Christian Grothoff <grothoff@gnunet.org>
AuthorDate: Mon Jul 26 16:32:34 2021 +0200

    implement typed JSON packer
---
 src/include/gnunet_json_lib.h |  21 +--
 src/json/Makefile.am          |   5 +-
 src/json/json.c               |  34 -----
 src/json/json_pack.c          | 291 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 296 insertions(+), 55 deletions(-)

diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index 27a4a20cd..144ddb64a 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -553,26 +553,9 @@ struct GNUNET_JSON_PackSpec
   const char *field_name;
 
   /**
-   * Function that will do the packing.
+   * Object to pack.
    */
-  GNUNET_JSON_PackCallback *packer;
-
-  /**
-   * Closure for @e packer.
-   */
-  void *packer_cls;
-
-  /**
-   * Pointer to be provided to the packer.
-   */
-  const void *value_ptr;
-
-  /**
-   * Numeric value for the packer, could be
-   * the length of the data in @e value_ptr,
-   * or something different depending on the type.
-   */
-  uint64_t value_num;
+  json_t *object;
   
   /**
    * True if a NULL (or 0) argument is allowed. In this
diff --git a/src/json/Makefile.am b/src/json/Makefile.am
index d4ea38adf..3c19f96bf 100644
--- a/src/json/Makefile.am
+++ b/src/json/Makefile.am
@@ -16,9 +16,10 @@ libgnunetjson_la_LDFLAGS = \
 libgnunetjson_la_CFLAGS = $(MHD_CFLAGS) $(AM_CFLAGS)
 libgnunetjson_la_SOURCES = \
   json.c \
-  json_mhd.c \
   json_generator.c \
-  json_helper.c
+  json_helper.c \
+  json_mhd.c \
+  json_pack.c
 libgnunetjson_la_LIBADD = \
   $(top_builddir)/src/util/libgnunetutil.la \
   -ljansson \
diff --git a/src/json/json.c b/src/json/json.c
index d55189804..4dabd4d22 100644
--- a/src/json/json.c
+++ b/src/json/json.c
@@ -28,19 +28,6 @@
 #include "gnunet_json_lib.h"
 
 
-/**
- * Navigate and parse data in a JSON tree.  Tries to parse the @a root
- * to find all of the values given in the @a spec.  If one of the
- * entries in @a spec cannot be found or parsed, the name of the JSON
- * field is returned in @a error_json_name, and the offset of the
- * entry in @a spec is returned in @a error_line.
- *
- * @param root the JSON node to start the navigation at.
- * @param spec parse specification array
- * @param[out] error_json_name which JSON field was problematic
- * @param[out] which index into @a spec did we encounter an error
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
- */
 enum GNUNET_GenericReturnValue
 GNUNET_JSON_parse (const json_t *root,
                    struct GNUNET_JSON_Specification *spec,
@@ -85,12 +72,6 @@ GNUNET_JSON_parse (const json_t *root,
 }
 
 
-/**
- * Set the "optional" flag for a parser specification entry.
- *
- * @param spec specification to modify
- * @return spec copy of @a spec with optional bit set
- */
 struct GNUNET_JSON_Specification
 GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec)
 {
@@ -101,12 +82,6 @@ GNUNET_JSON_spec_mark_optional (struct 
GNUNET_JSON_Specification spec)
 }
 
 
-/**
- * Frees all elements allocated during a #GNUNET_JSON_parse()
- * operation.
- *
- * @param spec specification of the parse operation
- */
 void
 GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec)
 {
@@ -151,15 +126,6 @@ set_json (struct GNUNET_GETOPT_CommandLineProcessorContext 
*ctx,
 }
 
 
-/**
- * Allow user to specify a JSON input value.
- *
- * @param shortName short name of the option
- * @param name long name of the option
- * @param argumentHelp help text for the option argument
- * @param description long help text for the option
- * @param[out] val set to the JSON specified at the command line
- */
 struct GNUNET_GETOPT_CommandLineOption
 GNUNET_JSON_getopt (char shortName,
                     const char *name,
diff --git a/src/json/json_pack.c b/src/json/json_pack.c
new file mode 100644
index 000000000..88edaca5f
--- /dev/null
+++ b/src/json/json_pack.c
@@ -0,0 +1,291 @@
+/*
+   This file is part of GNUnet
+   Copyright (C) 2021 GNUnet e.V.
+
+   GNUnet is free software: you can redistribute it and/or modify it
+   under the terms of the GNU Affero General Public License as published
+   by the Free Software Foundation, either version 3 of the License,
+   or (at your option) any later version.
+
+   GNUnet is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Affero General Public License for more details.
+
+   You should have received a copy of the GNU Affero General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
+ */
+/**
+ * @file json/json_pack.c
+ * @brief functions to pack JSON objects
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "gnunet_json_lib.h"
+
+
+json_t *
+GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[])
+{
+  json_t *ret;
+
+  ret = json_object ();
+  GNUNET_assert (NULL != ret);
+  for (unsigned int i=0;
+       NULL != spec[i].field_name;
+       i++)
+  {
+    if (NULL == spec[i].object)
+    {
+      GNUNET_assert (spec[i].allow_null);
+    }
+    else
+    {
+      GNUNET_assert (0 ==
+                     json_object_set_new (ret,
+                                          spec[i].field_name,
+                                          spec[i].object));
+      spec[i].object = NULL;
+    }
+  }
+  return ret;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_end_(void)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = NULL
+  };
+
+  return ps;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in)
+{
+  in.allow_null = true;
+  return in;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_bool (const char *name,
+                       bool b)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = json_boolean (b)
+  };
+  
+  return ps;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_string (const char *name,
+                         const char *s)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = json_string (s)
+  };
+  
+  return ps;  
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_uint64 (const char *name,
+                         uint64_t num)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = json_integer ((json_int_t) num)
+  };
+  
+#if JSON_INTEGER_IS_LONG_LONG
+  GNUNET_assert (num <= LONG_LONG_MAX);
+#else
+  GNUNET_assert (num <= LONG_MAX);
+#endif
+  return ps;  
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_int64 (const char *name,
+                         int64_t num)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = json_integer ((json_int_t) num)
+  };
+  
+#if JSON_INTEGER_IS_LONG_LONG
+  GNUNET_assert (num <= LONG_LONG_MAX);
+  GNUNET_assert (num >= LONG_LONG_MIN);
+#else
+  GNUNET_assert (num <= LONG_MAX);
+  GNUNET_assert (num >= LONG_MIN);
+#endif
+  return ps;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_object_steal (const char *name,
+                               json_t *o)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = o
+  };
+
+  if (NULL == o)
+    return ps;
+  GNUNET_assert (json_is_object (o));
+  return ps;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_object_incref (const char *name,
+                                json_t *o)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = o
+  };
+
+  if (NULL == o)
+    return ps;
+  (void) json_incref (o);
+  GNUNET_assert (json_is_object (o));
+  return ps;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_array_steal (const char *name,
+                              json_t *a)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = a
+  };
+
+  if (NULL == a)
+    return ps;
+  GNUNET_assert (json_is_array (a));
+  return ps;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_array_incref (const char *name,
+                               json_t *a)
+{
+  struct GNUNET_JSON_PackSpec ps = {
+    .field_name = name,
+    .object = a
+  };
+
+  if (NULL == a)
+    return ps;
+  (void) json_incref (a);
+  GNUNET_assert (json_is_array (a));  
+  return ps;
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_data_varsize (const char *name,
+                               const void *blob,
+                               size_t blob_size)
+{
+  json_t *json;
+
+  json = GNUNET_JSON_from_data (blob,
+                                blob_size);
+  GNUNET_assert (NULL != json);
+  return GNUNET_JSON_pack_object_steal (name,
+                                        json);
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_time_abs (const char *name,
+                           struct GNUNET_TIME_Absolute at)
+{
+  json_t *json;
+
+  json = GNUNET_JSON_from_time_abs (at);
+  GNUNET_assert (NULL != json);
+  return GNUNET_JSON_pack_object_steal (name,
+                                        json);
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_time_abs_nbo (const char *name,
+                               struct GNUNET_TIME_AbsoluteNBO at)
+{
+  return GNUNET_JSON_pack_time_abs (name,
+                                    GNUNET_TIME_absolute_ntoh (at));
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_time_rel (const char *name,
+                           struct GNUNET_TIME_Relative rt)
+{
+  json_t *json;
+
+  json = GNUNET_JSON_from_time_rel (rt);
+  GNUNET_assert (NULL != json);
+  return GNUNET_JSON_pack_object_steal (name,
+                                        json);
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_time_rel_nbo (const char *name,
+                               struct GNUNET_TIME_RelativeNBO rt)
+{
+  return GNUNET_JSON_pack_time_rel (name,
+                                    GNUNET_TIME_relative_ntoh (rt));
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_rsa_public_key (const char *name,
+                                 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
+{
+  json_t *json;
+
+  json = GNUNET_JSON_from_rsa_public_key (pk);
+  GNUNET_assert (NULL != json);
+  return GNUNET_JSON_pack_object_steal (name,
+                                        json);
+}
+
+
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_rsa_signature (const char *name,
+                                const struct GNUNET_CRYPTO_RsaSignature *sig)
+{
+  json_t *json;
+
+  json = GNUNET_JSON_from_rsa_signature (sig);
+  GNUNET_assert (NULL != json);
+  return GNUNET_JSON_pack_object_steal (name,
+                                        json);
+}
+
+
+/* end of json_pack.c */

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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