GNOME Bugzilla – Bug 728509
Doesn't know how to serialize/deserialize its own boxed types
Last modified: 2017-09-05 10:40:51 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?
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?
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.
-- 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.