gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet] branch master updated: add support to GNUNET_JSON_pack_ for NUL


From: gnunet
Subject: [gnunet] branch master updated: add support to GNUNET_JSON_pack_ for NULL field name in objects
Date: Thu, 09 Jan 2025 17:17:20 +0100

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 d4622827d add support to GNUNET_JSON_pack_ for NULL field name in 
objects
d4622827d is described below

commit d4622827d20968ef270803a1db7fe02dad465394
Author: Iván Ávalos <avalos@disroot.org>
AuthorDate: Thu Jan 9 17:10:13 2025 +0100

    add support to GNUNET_JSON_pack_ for NULL field name in objects
    
    Signed-off-by: Christian Grothoff <christian@grothoff.org>
---
 src/include/gnunet_json_lib.h | 15 ++++++++---
 src/lib/json/Makefile.am      |  2 +-
 src/lib/json/json_pack.c      | 58 ++++++++++++++++++++++++++++++++++++-------
 3 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index 629c1171a..17e79fb1f 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -724,7 +724,9 @@ typedef json_t *
 struct GNUNET_JSON_PackSpec
 {
   /**
-   * Name of the field to pack.
+   * Name of the field to pack. If (and only if) @e object contains an actual
+   * JSON object, NULL will pack the keys in the top level of the resulting
+   * JSON object rather than under a field.
    */
   const char *field_name;
 
@@ -740,6 +742,11 @@ struct GNUNET_JSON_PackSpec
    * the generated object) and not be serialized at all.
    */
   bool allow_null;
+
+  /**
+   * True if last element in the spec array.
+   */
+  bool final;
 };
 
 
@@ -856,7 +863,8 @@ GNUNET_JSON_pack_int64 (const char *name,
  * JSON object where the reference is taken over by
  * the packer.
  *
- * @param name name of the field to add to the object
+ * @param name name of the field to add to the object, if NULL, the keys of
+ *        @a o will be placed in the top level of the resulting object
  * @param o object to steal
  * @return json pack specification
  */
@@ -870,7 +878,8 @@ GNUNET_JSON_pack_object_steal (const char *name,
  * reference counter is incremented by the packer.  Note that a deep copy is
  * not performed.
  *
- * @param name name of the field to add to the object
+ * @param name name of the field to add to the object, if NULL, the keys of
+ *        @a o will be placed in the top level of the resulting object
  * @param o object to increment reference counter of
  * @return json pack specification
  */
diff --git a/src/lib/json/Makefile.am b/src/lib/json/Makefile.am
index c60fd1457..5ad356ea6 100644
--- a/src/lib/json/Makefile.am
+++ b/src/lib/json/Makefile.am
@@ -11,7 +11,7 @@ lib_LTLIBRARIES = \
 
 libgnunetjson_la_LDFLAGS = \
   $(GN_LIBINTL) \
-  -version-info 1:0:1 \
+  -version-info 2:0:0 \
   -no-undefined
 libgnunetjson_la_CFLAGS = \
   $(MHD_CFLAGS) \
diff --git a/src/lib/json/json_pack.c b/src/lib/json/json_pack.c
index d298e6efe..b76b3cfa9 100644
--- a/src/lib/json/json_pack.c
+++ b/src/lib/json/json_pack.c
@@ -39,7 +39,7 @@ GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[])
   ret = json_object ();
   GNUNET_assert (NULL != ret);
   for (unsigned int i = 0;
-       NULL != spec[i].field_name;
+       ! spec[i].final;
        i++)
   {
     if (NULL == spec[i].object)
@@ -54,10 +54,16 @@ GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[])
     }
     else
     {
-      GNUNET_assert (0 ==
-                     json_object_set_new (ret,
-                                          spec[i].field_name,
-                                          spec[i].object));
+      if (NULL == spec[i].field_name)
+        GNUNET_assert (0 ==
+                       json_object_update_new (ret,
+                                               spec[i].object));
+      else
+        GNUNET_assert (0 ==
+                       json_object_set_new (ret,
+                                            spec[i].field_name,
+                                            spec[i].object));
+
       spec[i].object = NULL;
     }
   }
@@ -69,7 +75,7 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_end_ (void)
 {
   struct GNUNET_JSON_PackSpec ps = {
-    .field_name = NULL
+    .final = true
   };
 
   return ps;
@@ -88,6 +94,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_bool (const char *name,
                        bool b)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = json_boolean (b)
@@ -101,11 +109,12 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_double (const char *name,
                          double f)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = json_real (f)
   };
-
   return ps;
 }
 
@@ -114,6 +123,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_string (const char *name,
                          const char *s)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = json_string (s)
@@ -127,6 +138,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_uint64 (const char *name,
                          uint64_t num)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = json_integer ((json_int_t) num)
@@ -145,6 +158,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_int64 (const char *name,
                         int64_t num)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = json_integer ((json_int_t) num)
@@ -210,6 +225,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_array_steal (const char *name,
                               json_t *a)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = a
@@ -232,6 +249,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_array_incref (const char *name,
                                json_t *a)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = a
@@ -256,6 +275,8 @@ GNUNET_JSON_pack_data_varsize (const char *name,
                                const void *blob,
                                size_t blob_size)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = (NULL != blob)
@@ -273,6 +294,8 @@ GNUNET_JSON_pack_data64_varsize (const char *name,
                                  const void *blob,
                                  size_t blob_size)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = (NULL != blob)
@@ -289,6 +312,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_timestamp (const char *name,
                             struct GNUNET_TIME_Timestamp t)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name
   };
@@ -319,6 +344,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_time_rel (const char *name,
                            struct GNUNET_TIME_Relative rt)
 {
+  GNUNET_assert (NULL != name);
+
   json_t *json;
 
   json = GNUNET_JSON_from_time_rel (rt);
@@ -341,6 +368,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_rsa_public_key (const char *name,
                                  const struct GNUNET_CRYPTO_RsaPublicKey *pk)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = GNUNET_JSON_from_rsa_public_key (pk)
@@ -354,6 +383,8 @@ struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_rsa_signature (const char *name,
                                 const struct GNUNET_CRYPTO_RsaSignature *sig)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
     .object = GNUNET_JSON_from_rsa_signature (sig)
@@ -365,8 +396,11 @@ GNUNET_JSON_pack_rsa_signature (const char *name,
 
 struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_unblinded_signature (const char *name,
-                                      const struct 
GNUNET_CRYPTO_UnblindedSignature *sig)
+                                      const struct
+                                      GNUNET_CRYPTO_UnblindedSignature *sig)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name
   };
@@ -402,8 +436,11 @@ GNUNET_JSON_pack_unblinded_signature (const char *name,
 
 struct GNUNET_JSON_PackSpec
 GNUNET_JSON_pack_blinded_message (const char *name,
-                                  const struct GNUNET_CRYPTO_BlindedMessage 
*msg)
+                                  const struct GNUNET_CRYPTO_BlindedMessage 
*msg
+                                  )
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
   };
@@ -446,6 +483,8 @@ GNUNET_JSON_pack_blinded_sig (
   const char *name,
   const struct GNUNET_CRYPTO_BlindedSignature *sig)
 {
+  GNUNET_assert (NULL != name);
+
   struct GNUNET_JSON_PackSpec ps = {
     .field_name = name,
   };
@@ -477,4 +516,5 @@ GNUNET_JSON_pack_blinded_sig (
   return ps;
 }
 
+
 /* 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]