GNOME Bugzilla – Bug 707382
JSON to GVariant conversion can't convert between strings and numbers
Last modified: 2013-12-02 14:38:09 UTC
Created attachment 253970 [details] [review] Patch implementing string-to-boolean and string-to-number conversions Consider the following JSON: ["123"] Trying to convert it to GVariant with signature "(i)" fails, as json-glib won't try to convert a string to a number. It would be nice if json-glib could do that. Let me give a use case: I am composing a JSON object from several data sources, some of which don't have type info, so I represent those as strings. Then I convert that JSON to GVariant and pass it to a DBus method call. Note that I only get to know DBus method signature at runtime. I tried to implement arbitrary value-to-value conversion using g_value_transform(), but it turned out g_value_transform() doesn't support string-to-number conversions. For my purposes, I only need to be able to convert strings to other types. So, I wrote a patch that introduces support for that, without supporting other types of conversions. If that's acceptable, I'd like the patch to be reviewed.
Review of attachment 253970 [details] [review]: thanks for your patch. I'm not entirely convinced by this, as it looks like a way to circumvent the type safety of the system, but I don't have a strong objection. there are a couple of coding style issues to be addressed before pushing the commit upstream. thanks again! ::: json-glib/json-gvariant.c @@ +1139,3 @@ } + if (JSON_NODE_TYPE(json_node) == JSON_NODE_VALUE && coding style: missing space between macro and parenthesis. @@ +1140,3 @@ + if (JSON_NODE_TYPE(json_node) == JSON_NODE_VALUE && + json_node_get_value_type(json_node) == G_TYPE_STRING) coding style: missing space between function and parenthesis. @@ +1158,3 @@ + variant = gvariant_simple_from_string (str, class, error); + goto out; + case G_VARIANT_CLASS_UINT16: coding style: use break with the empty default condition.
Created attachment 263301 [details] [review] Updated version of the patch Thanks for review. I updated my patch with coding style changes you suggested.
Review of attachment 263301 [details] [review]: okay.
attachment 263301 [details] [review] pushed to master