[tracker/wip/carlosg/json-ld-serialization: 164/165] libtracker-sparql: Handle JSON-LD pretty TrackerResource printing specially




commit ef6a057d9ad22df81b78b02be61712ff990efd7e
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Jul 11 20:35:02 2022 +0200

    libtracker-sparql: Handle JSON-LD pretty TrackerResource printing specially
    
    Since we return a very ugly JSON-LD document (i.e. not indented), and
    a large usecase of the tracker_resource_print_rdf() function is to print
    output that is easily human readable, make it sure that the JSON-LD
    document is pretty printed.

 src/libtracker-sparql/tracker-resource.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
---
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 5bb516190..2b098d890 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -1723,7 +1723,6 @@ tracker_resource_print_rdf (TrackerResource         *self,
        g_return_val_if_fail (TRACKER_IS_NAMESPACE_MANAGER (namespaces), NULL);
        g_return_val_if_fail (format < TRACKER_N_RDF_FORMATS, NULL);
 
-#define BUF_SIZE 4096
        deserializer = tracker_deserializer_resource_new (self, namespaces, graph);
        serializer = tracker_serializer_new (TRACKER_SPARQL_CURSOR (deserializer),
                                             namespaces,
@@ -1732,6 +1731,34 @@ tracker_resource_print_rdf (TrackerResource         *self,
 
        str = g_string_new (NULL);
 
+       if (format == TRACKER_RDF_FORMAT_JSON_LD) {
+               JsonParser *parser;
+               JsonGenerator *generator;
+               JsonNode *root;
+
+               /* Special case, ensure that json is pretty printed */
+               parser = json_parser_new ();
+
+               if (!json_parser_load_from_stream (parser,
+                                                  serializer,
+                                                  NULL,
+                                                  NULL)) {
+                       g_object_unref (parser);
+                       return g_string_free (str, FALSE);
+               }
+
+               generator = json_generator_new ();
+               root = json_parser_get_root (parser);
+               json_generator_set_root (generator, root);
+               json_generator_set_pretty (generator, TRUE);
+               json_generator_to_gstring (generator, str);
+               g_object_unref (generator);
+               g_object_unref (parser);
+
+               return g_string_free (str, FALSE);
+       }
+
+#define BUF_SIZE 4096
        while (TRUE) {
                GBytes *bytes;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]