[gnome-builder] messgae: encode more similarly to json_gvariant_* API



commit 57e889e6287a7439430dabb959843c06c66d2932
Author: Christian Hergert <chergert redhat com>
Date:   Sat Mar 18 00:30:59 2017 -0700

    messgae: encode more similarly to json_gvariant_* API

 contrib/jsonrpc-glib/jsonrpc-message.c |   13 +++++++++++++
 contrib/jsonrpc-glib/jsonrpc-message.h |   21 +++++++++++++++++++++
 contrib/jsonrpc-glib/test-message.c    |   23 ++++++++++++++++++++---
 3 files changed, 54 insertions(+), 3 deletions(-)
---
diff --git a/contrib/jsonrpc-glib/jsonrpc-message.c b/contrib/jsonrpc-glib/jsonrpc-message.c
index 35cc5a4..7f37cd9 100644
--- a/contrib/jsonrpc-glib/jsonrpc-message.c
+++ b/contrib/jsonrpc-glib/jsonrpc-message.c
@@ -39,11 +39,13 @@
 
 #define IS_PUT_STRING(_any)  COMPARE_MAGIC(_any, PUT_STRING)
 #define IS_PUT_INT32(_any)   COMPARE_MAGIC(_any, PUT_INT32)
+#define IS_PUT_INT64(_any)   COMPARE_MAGIC(_any, PUT_INT64)
 #define IS_PUT_BOOLEAN(_any) COMPARE_MAGIC(_any, PUT_BOOLEAN)
 #define IS_PUT_DOUBLE(_any)  COMPARE_MAGIC(_any, PUT_DOUBLE)
 
 #define IS_GET_STRING(_any)  COMPARE_MAGIC(_any, GET_STRING)
 #define IS_GET_INT32(_any)   COMPARE_MAGIC(_any, GET_INT32)
+#define IS_GET_INT64(_any)   COMPARE_MAGIC(_any, GET_INT64)
 #define IS_GET_BOOLEAN(_any) COMPARE_MAGIC(_any, GET_BOOLEAN)
 #define IS_GET_DOUBLE(_any)  COMPARE_MAGIC(_any, GET_DOUBLE)
 #define IS_GET_ITER(_any)    COMPARE_MAGIC(_any, GET_ITER)
@@ -129,6 +131,8 @@ jsonrpc_message_build_object (GVariantBuilder *builder,
         g_variant_builder_add (builder, "s", ((JsonrpcMessagePutString *)valptr)->val);
       else if (IS_PUT_INT32 (valptr))
         g_variant_builder_add (builder, "i", ((JsonrpcMessagePutInt32 *)valptr)->val);
+      else if (IS_PUT_INT64 (valptr))
+        g_variant_builder_add (builder, "x", ((JsonrpcMessagePutInt64 *)valptr)->val);
       else if (IS_PUT_BOOLEAN (valptr))
         g_variant_builder_add (builder, "b", ((JsonrpcMessagePutBoolean *)valptr)->val);
       else if (IS_PUT_DOUBLE (valptr))
@@ -191,6 +195,8 @@ jsonrpc_message_build_array (GVariantBuilder *builder,
         g_variant_builder_add (builder, "s", ((JsonrpcMessagePutString *)valptr)->val);
       else if (IS_PUT_INT32 (valptr))
         g_variant_builder_add (builder, "i", ((JsonrpcMessagePutInt32 *)valptr)->val);
+      else if (IS_PUT_INT64 (valptr))
+        g_variant_builder_add (builder, "x", ((JsonrpcMessagePutInt64 *)valptr)->val);
       else if (IS_PUT_BOOLEAN (valptr))
         g_variant_builder_add (builder, "b", ((JsonrpcMessagePutBoolean *)valptr)->val);
       else if (IS_PUT_DOUBLE (valptr))
@@ -301,6 +307,8 @@ jsonrpc_message_parse_object (GVariantDict *dict,
     ret = g_variant_dict_lookup (dict, key, "&s", ((JsonrpcMessageGetString *)valptr)->valptr);
   else if (IS_GET_INT32 (valptr))
     ret = g_variant_dict_lookup (dict, key, "i", ((JsonrpcMessageGetInt32 *)valptr)->valptr);
+  else if (IS_GET_INT64 (valptr))
+    ret = g_variant_dict_lookup (dict, key, "x", ((JsonrpcMessageGetInt64 *)valptr)->valptr);
   else if (IS_GET_BOOLEAN (valptr))
     ret = g_variant_dict_lookup (dict, key, "b", ((JsonrpcMessageGetBoolean *)valptr)->valptr);
   else if (IS_GET_DOUBLE (valptr))
@@ -389,6 +397,11 @@ jsonrpc_message_parse_array_va (GVariantIter *iter,
       if ((ret = g_variant_is_of_type (value, G_VARIANT_TYPE_INT32)))
         *((JsonrpcMessageGetInt32 *)valptr)->valptr = g_variant_get_int32 (value);
     }
+  else if (IS_GET_INT64 (valptr))
+    {
+      if ((ret = g_variant_is_of_type (value, G_VARIANT_TYPE_INT64)))
+        *((JsonrpcMessageGetInt64 *)valptr)->valptr = g_variant_get_int64 (value);
+    }
   else if (IS_GET_BOOLEAN (valptr))
     {
       if ((ret = g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN)))
diff --git a/contrib/jsonrpc-glib/jsonrpc-message.h b/contrib/jsonrpc-glib/jsonrpc-message.h
index b965883..2c60082 100644
--- a/contrib/jsonrpc-glib/jsonrpc-message.h
+++ b/contrib/jsonrpc-glib/jsonrpc-message.h
@@ -60,6 +60,18 @@ typedef struct
 typedef struct
 {
   JsonrpcMessageMagic magic;
+  gint64 val;
+} JsonrpcMessagePutInt64;
+
+typedef struct
+{
+  JsonrpcMessageMagic magic;
+  gint64 *valptr;
+} JsonrpcMessageGetInt64;
+
+typedef struct
+{
+  JsonrpcMessageMagic magic;
   gboolean val;
 } JsonrpcMessagePutBoolean;
 
@@ -106,6 +118,8 @@ typedef struct
 #define _JSONRPC_MESSAGE_GET_STRING_MAGIC  _JSONRPC_MAGIC("GETS")
 #define _JSONRPC_MESSAGE_PUT_INT32_MAGIC   _JSONRPC_MAGIC("PUTI")
 #define _JSONRPC_MESSAGE_GET_INT32_MAGIC   _JSONRPC_MAGIC("GETI")
+#define _JSONRPC_MESSAGE_PUT_INT64_MAGIC   _JSONRPC_MAGIC("PUTX")
+#define _JSONRPC_MESSAGE_GET_INT64_MAGIC   _JSONRPC_MAGIC("GETX")
 #define _JSONRPC_MESSAGE_PUT_BOOLEAN_MAGIC _JSONRPC_MAGIC("PUTB")
 #define _JSONRPC_MESSAGE_GET_BOOLEAN_MAGIC _JSONRPC_MAGIC("GETB")
 #define _JSONRPC_MESSAGE_PUT_DOUBLE_MAGIC  _JSONRPC_MAGIC("PUTD")
@@ -118,6 +132,8 @@ typedef struct
 #define _JSONRPC_MESSAGE_GET_STRING_MAGIC_C  _JSONRPC_MAGIC_C('G','E','T','S')
 #define _JSONRPC_MESSAGE_PUT_INT32_MAGIC_C   _JSONRPC_MAGIC_C('P','U','T','I')
 #define _JSONRPC_MESSAGE_GET_INT32_MAGIC_C   _JSONRPC_MAGIC_C('G','E','T','I')
+#define _JSONRPC_MESSAGE_PUT_INT64_MAGIC_C   _JSONRPC_MAGIC_C('P','U','T','X')
+#define _JSONRPC_MESSAGE_GET_INT64_MAGIC_C   _JSONRPC_MAGIC_C('G','E','T','X')
 #define _JSONRPC_MESSAGE_PUT_BOOLEAN_MAGIC_C _JSONRPC_MAGIC_C('P','U','T','B')
 #define _JSONRPC_MESSAGE_GET_BOOLEAN_MAGIC_C _JSONRPC_MAGIC_C('G','E','T','B')
 #define _JSONRPC_MESSAGE_PUT_DOUBLE_MAGIC_C  _JSONRPC_MAGIC_C('P','U','T','D')
@@ -143,6 +159,11 @@ typedef struct
 #define JSONRPC_MESSAGE_GET_INT32(_valptr) \
   (&((JsonrpcMessageGetInt32) { .magic = {_JSONRPC_MESSAGE_GET_INT32_MAGIC_C}, .valptr = _valptr }))
 
+#define JSONRPC_MESSAGE_PUT_INT64(_val) \
+  (&((JsonrpcMessagePutInt64) { .magic = {_JSONRPC_MESSAGE_PUT_INT64_MAGIC_C}, .val = _val }))
+#define JSONRPC_MESSAGE_GET_INT64(_valptr) \
+  (&((JsonrpcMessageGetInt64) { .magic = {_JSONRPC_MESSAGE_GET_INT64_MAGIC_C}, .valptr = _valptr }))
+
 #define JSONRPC_MESSAGE_PUT_BOOLEAN(_val) \
   (&((JsonrpcMessagePutBoolean) { .magic = {_JSONRPC_MESSAGE_PUT_BOOLEAN_MAGIC_C}, .val = _val }))
 #define JSONRPC_MESSAGE_GET_BOOLEAN(_valptr) \
diff --git a/contrib/jsonrpc-glib/test-message.c b/contrib/jsonrpc-glib/test-message.c
index 1acb170..04c1875 100644
--- a/contrib/jsonrpc-glib/test-message.c
+++ b/contrib/jsonrpc-glib/test-message.c
@@ -1,18 +1,23 @@
+#include <json-glib/json-glib.h>
+
 #include "jsonrpc-message.h"
 
 static void
 test_basic (void)
 {
   g_autoptr(GVariant) node = NULL;
+  g_autoptr(JsonParser) parser = NULL;
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GVariant) deserialized = NULL;
   const gchar *foo1 = NULL;
-  gint baz_baz_baz = 0;
+  gint64 baz_baz_baz = 0;
   gboolean r;
 
   node = JSONRPC_MESSAGE_NEW (
     "foo", "foo1",
     "bar", "foo2",
     "baz", "{",
-      "baz", "[", "{", "baz", JSONRPC_MESSAGE_PUT_INT32 (123), "}", "]",
+      "baz", "[", "{", "baz", JSONRPC_MESSAGE_PUT_INT64 (123), "}", "]",
     "}"
   );
 
@@ -21,13 +26,25 @@ test_basic (void)
   r = JSONRPC_MESSAGE_PARSE (node,
     "foo", JSONRPC_MESSAGE_GET_STRING (&foo1),
     "baz", "{",
-      "baz", "[", "{", "baz", JSONRPC_MESSAGE_GET_INT32 (&baz_baz_baz), "}", "]",
+      "baz", "[", "{", "baz", JSONRPC_MESSAGE_GET_INT64 (&baz_baz_baz), "}", "]",
     "}"
   );
 
   g_assert_cmpstr (foo1, ==, "foo1");
   g_assert_cmpint (baz_baz_baz, ==, 123);
   g_assert_cmpint (r, ==, 1);
+
+  /* compare json gvariant encoding to ensure it matches */
+#define TESTSTR "{'foo': 'foo1', 'bar': 'foo2', 'baz': {'baz': [{'baz': 123}]}}"
+  parser = json_parser_new ();
+  r = json_parser_load_from_data (parser, TESTSTR, -1, &error);
+  g_assert (r);
+  g_assert_no_error (error);
+  deserialized = json_gvariant_deserialize (json_parser_get_root (parser), NULL, &error);
+  g_assert (deserialized);
+  g_assert_no_error (error);
+  g_assert (g_variant_equal (deserialized, node));
+#undef TESTSTR
 }
 
 static void


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