After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 728509 - Doesn't know how to serialize/deserialize its own boxed types
Doesn't know how to serialize/deserialize its own boxed types
Status: RESOLVED OBSOLETE
Product: json-glib
Classification: Core
Component: GObject
git master
Other Linux
: Normal enhancement
: ---
Assigned To: json-glib-maint
json-glib-maint
Depends on:
Blocks:
 
 
Reported: 2014-04-18 14:59 UTC by Matthew Barnes
Modified: 2017-09-05 10:40 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Matthew Barnes 2014-04-18 14:59:20 UTC
I have a case where I'm trying to deserialize JSON data into a complex object.

For technical reasons, part of that object is too complex to deserialize directly.  So my solution was to define a boxed GObject property of type JSON_TYPE_OBJECT and just stash the raw data to be processed from my GInitable.init() method.

The property is construct-only, so implementing the JsonSerializable interface won't help me because json-glib can only use json_deserialize_pspec() for construct-only properties.

But I was surprised to find (at least afaict) json-glib doesn't know how to handle GObject properties of its own boxed types, like JsonObject or JsonArray.  So I'm having to teach it how myself by registering serialize/deserialize callbacks.

Maybe I'm doing something wrong, but those cases seem trivial.  Shouldn't they come built-in?
Comment 1 Emmanuele Bassi (:ebassi) 2015-08-18 12:57:23 UTC
I'm pretty sure you're doing something wrong; serializing a JsonObject would just yield a string with the JSON data inside it. At that point, why not serialize the data into JSON and stuff it into a string property?
Comment 2 Matthew Barnes 2015-08-18 14:29:43 UTC
This was quite awhile ago so I'm trying to recall details from memory.  But the problem was more with deserializing than serializing.

I had a GObject class with various construct-only properties that I was trying to construct from JSON data.

The class implemented JsonSerializable for serializing itself, but because the properties were construct-only I was hitting this limitation on deserializing:

https://git.gnome.org/browse/json-glib/tree/json-glib/json-gobject.c#n199

That meant providing my own deserialize_property() method for these construct-only properties was useless, and I was stuck relying solely on json_deserialize_pspec().

That was fine for all the properties except one.

There was a particular chunk of JSON data that I just wanted to retain as raw JSON data and not deserialize.  So I defined the GObject property type for that chunk as JSON_TYPE_OBJECT, expecting json_deserialize_pspec() to just act as a passthrough for that type -- iow, just return its input -- but was surprised to find it didn't have that trivial case built in.

So I had to teach it how myself:

  static JsonNode *
  serialize_json_object (gconstpointer boxed)
  {
    JsonNode *json_node;

    json_node = json_node_new (JSON_NODE_OBJECT);
    json_node_set_object (json_node, (JsonObject *) boxed);

    return json_node;
  }

  static gpointer
  deserialize_json_object (JsonNode *json_node)
  {
    return json_node_dup_object (json_node);
  }

  json_boxed_register_serialize_func (JSON_TYPE_OBJECT,
                                      JSON_NODE_OBJECT,
                                      serialize_json_object);

  json_boxed_register_deserialize_func (JSON_TYPE_OBJECT,
                                        JSON_NODE_OBJECT,
                                        deserialize_json_object);

This bug is just suggesting that json-glib should have at least deserialize handlers for its own boxed types built in.
Comment 3 Emmanuele Bassi (:ebassi) 2017-09-05 10:40:51 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/json-glib/issues/12.