[jsonrpc-glib] message: allow NULL for strings in message building/parsing
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [jsonrpc-glib] message: allow NULL for strings in message building/parsing
- Date: Thu, 28 Dec 2017 09:02:42 +0000 (UTC)
commit 7739b1814bf860264ee46fde276797f1a963fcc3
Author: Christian Hergert <chergert redhat com>
Date: Thu Dec 28 01:02:26 2017 -0800
message: allow NULL for strings in message building/parsing
This is useful to allow passing through NULL for strings so that consumers
don't need to continually convert null to "" and reverse.
src/jsonrpc-message.c | 50 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 42 insertions(+), 8 deletions(-)
---
diff --git a/src/jsonrpc-message.c b/src/jsonrpc-message.c
index 74246e4..92f58ca 100644
--- a/src/jsonrpc-message.c
+++ b/src/jsonrpc-message.c
@@ -128,7 +128,12 @@ jsonrpc_message_build_object (GVariantBuilder *builder,
default:
if (IS_PUT_STRING (valptr))
- g_variant_builder_add (builder, "s", ((JsonrpcMessagePutString *)valptr)->val);
+ {
+ if (((JsonrpcMessagePutString *)valptr)->val != NULL)
+ g_variant_builder_add (builder, "s", ((JsonrpcMessagePutString *)valptr)->val);
+ else
+ g_variant_builder_add (builder, "ms", NULL);
+ }
else if (IS_PUT_INT32 (valptr))
g_variant_builder_add (builder, "i", ((JsonrpcMessagePutInt32 *)valptr)->val);
else if (IS_PUT_INT64 (valptr))
@@ -138,7 +143,12 @@ jsonrpc_message_build_object (GVariantBuilder *builder,
else if (IS_PUT_DOUBLE (valptr))
g_variant_builder_add (builder, "d", ((JsonrpcMessagePutDouble *)valptr)->val);
else
- g_variant_builder_add (builder, "s", (const char *)valptr);
+ {
+ if (valptr != NULL)
+ g_variant_builder_add (builder, "s", (const char *)valptr);
+ else
+ g_variant_builder_add (builder, "ms", NULL);
+ }
break;
}
@@ -192,7 +202,12 @@ jsonrpc_message_build_array (GVariantBuilder *builder,
default:
if (IS_PUT_STRING (valptr))
- g_variant_builder_add (builder, "s", ((JsonrpcMessagePutString *)valptr)->val);
+ {
+ if (((JsonrpcMessagePutString *)valptr)->val != NULL)
+ g_variant_builder_add (builder, "s", ((JsonrpcMessagePutString *)valptr)->val);
+ else
+ g_variant_builder_add (builder, "ms", NULL);
+ }
else if (IS_PUT_INT32 (valptr))
g_variant_builder_add (builder, "i", ((JsonrpcMessagePutInt32 *)valptr)->val);
else if (IS_PUT_INT64 (valptr))
@@ -202,7 +217,12 @@ jsonrpc_message_build_array (GVariantBuilder *builder,
else if (IS_PUT_DOUBLE (valptr))
g_variant_builder_add (builder, "d", ((JsonrpcMessagePutDouble *)valptr)->val);
else
- g_variant_builder_add (builder, "s", (const char *)valptr);
+ {
+ if (valptr != NULL)
+ g_variant_builder_add (builder, "s", (const char *)valptr);
+ else
+ g_variant_builder_add (builder, "ms", NULL);
+ }
break;
}
@@ -347,7 +367,10 @@ jsonrpc_message_parse_object (GVariantDict *dict,
else if (IS_GET_VARIANT (valptr))
ret = !!(*((JsonrpcMessageGetVariant *)valptr)->variantptr = g_variant_dict_lookup_value (dict, key,
NULL));
else if (IS_GET_STRING (valptr))
- ret = g_variant_dict_lookup (dict, key, "&s", ((JsonrpcMessageGetString *)valptr)->valptr);
+ {
+ ret = g_variant_dict_lookup (dict, key, "&s", ((JsonrpcMessageGetString *)valptr)->valptr) ||
+ g_variant_dict_lookup (dict, key, "m&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))
@@ -360,7 +383,10 @@ jsonrpc_message_parse_object (GVariantDict *dict,
{
/* Assume the string is a raw string, so compare for equality */
const gchar *valstr = NULL;
- ret = g_variant_dict_lookup (dict, key, "&s", &valstr) && g_strcmp0 (valstr, (const char *)valptr) ==
0;
+ ret = g_variant_dict_lookup (dict, key, "&s", &valstr) ||
+ g_variant_dict_lookup (dict, key, "m&s", &valstr);
+ if (ret)
+ ret = g_strcmp0 (valstr, (const gchar *)valptr) == 0;
}
if (!ret || !param)
@@ -434,6 +460,8 @@ jsonrpc_message_parse_array_va (GVariantIter *iter,
{
if ((ret = g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)))
*((JsonrpcMessageGetString *)valptr)->valptr = g_variant_get_string (value, NULL);
+ else if ((ret = g_variant_is_of_type (value, G_VARIANT_TYPE ("ms"))))
+ g_variant_get (value, "m&s", ((JsonrpcMessageGetString *)valptr)->valptr);
}
else if (IS_GET_INT32 (valptr))
{
@@ -457,9 +485,15 @@ jsonrpc_message_parse_array_va (GVariantIter *iter,
}
else
{
+ const gchar *val = NULL;
+
/* Assume the string is a raw string, so compare for equality */
- ret = g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) &&
- (0 == g_strcmp0 (g_variant_get_string (value, NULL), (const gchar *)valptr));
+ if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
+ val = g_variant_get_string (value, NULL);
+ else if (g_variant_is_of_type (value, G_VARIANT_TYPE ("ms")))
+ g_variant_get (value, "m&s", &val);
+
+ ret = g_strcmp0 (val, (const gchar *)valptr) == 0;
}
if (!ret || !param)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]