[tracker/wip/sam/resource: 10/13] libtracker-sparql: Work on JSON-LD serialisation for tracker-resource
- From: Sam Thursfield <sthursfield src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/wip/sam/resource: 10/13] libtracker-sparql: Work on JSON-LD serialisation for tracker-resource
- Date: Thu, 7 Apr 2016 17:35:54 +0000 (UTC)
commit dead010d0c7cfaa3df42792b94d344dca0a63329
Author: Sam Thursfield <sam afuera me uk>
Date: Thu Apr 7 18:03:00 2016 +0100
libtracker-sparql: Work on JSON-LD serialisation for tracker-resource
src/libtracker-sparql/tracker-resource.c | 73 +++++++++++++++++++++++------
src/libtracker-sparql/tracker-resource.h | 2 +
2 files changed, 60 insertions(+), 15 deletions(-)
---
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 5e2dc59..f964d9b 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -18,6 +18,7 @@
*/
#include <glib.h>
+#include <json-glib/json-glib.h>
#include <string.h>
@@ -391,20 +392,62 @@ tracker_resource_identifier_compare_func (TrackerResource *resource,
return strcmp (priv->identifier, identifier);
}
-GString *
-tracker_resource_print_jsonld_string (TrackerResource *resource)
+
+
+/* FIXME: this could hit an infinite loop if there are circular resource
+ * relationships, make sure those are tested & detected.
+ */
+/* This is not exposed publically right now because then everything including
+ * tracker-resource.h would need to pull in the json-glib dependency ...
+ */
+static JsonNode *
+tracker_resource_serialize_jsonld (TrackerResource *self,
+ GError **error)
+{
+ /* FIXME: generate a context ! */
+
+ TrackerResourcePrivate *priv = GET_PRIVATE (self);
+ JsonBuilder *builder;
+ JsonNode *result;
+
+ builder = json_builder_new ();
+ json_builder_begin_object (builder);
+
+ json_builder_set_member_name (builder, "@id");
+ json_builder_add_string_value (builder, priv->identifier);
+
+ json_builder_end_object (builder);
+
+ result = json_builder_get_root (builder);
+ g_object_unref (builder);
+ return result;
+};
+
+
+char *
+tracker_resource_print_jsonld (TrackerResource *self,
+ GError **error)
{
- GString *string;
-
- string = g_string_new ("");
-
- /* How will this work then? */
- /* Ignore formatting for now */
- /*{
- @context: {
- "foo": "prefix"
- },
- "nie:title": title
- }*/
- return string;
+ GError *sub_error = NULL;
+ JsonNode *json_root_node;
+ JsonGenerator *generator;
+ char *result;
+
+ json_root_node = tracker_resource_serialize_jsonld (self, &sub_error);
+
+ if (json_root_node == NULL) {
+ g_propagate_error (error, sub_error);
+ return NULL;
+ }
+
+ generator = json_generator_new ();
+ json_generator_set_root (generator, json_root_node);
+ json_generator_set_pretty (generator, TRUE);
+
+ result = json_generator_to_data (generator, NULL);
+
+ g_object_unref (json_root_node);
+ g_object_unref (generator);
+
+ return result;
}
diff --git a/src/libtracker-sparql/tracker-resource.h b/src/libtracker-sparql/tracker-resource.h
index 162c1ca..7abf6d5 100644
--- a/src/libtracker-sparql/tracker-resource.h
+++ b/src/libtracker-sparql/tracker-resource.h
@@ -63,6 +63,8 @@ const char *tracker_resource_get_first_uri (TrackerResource *self, const char *p
gint tracker_resource_identifier_compare_func (TrackerResource *resource, const char *identifier);
+char *tracker_resource_print_jsonld (TrackerResource *self, GError **error);
+
G_END_DECLS
#endif /* __LIBTRACKER_RESOURCE_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]