[libshumate] vector: Add JSON utilities



commit 4c956ec1e9e6db14706d47e8720df9837522b91b
Author: James Westman <james jwestman net>
Date:   Tue Sep 14 18:18:55 2021 -0500

    vector: Add JSON utilities
    
    This commit adds a dependency on json-glib.

 meson.build                                   |  2 +
 shumate/meson.build                           |  5 +++
 shumate/vector/shumate-vector-utils-private.h | 25 ++++++++++++
 shumate/vector/shumate-vector-utils.c         | 59 +++++++++++++++++++++++++++
 4 files changed, 91 insertions(+)
---
diff --git a/meson.build b/meson.build
index 1cd5e5e..f1f74b1 100644
--- a/meson.build
+++ b/meson.build
@@ -60,6 +60,7 @@ libsoup_req = '>= 2.42'
 introspection_req = '>= 0.6.3'
 vala_req = '>= 0.11.0'
 gtk_doc_req = '>= 1.9'
+json_glib_req = '>= 1.6'
 
 glib_dep = dependency('glib-2.0', version: glib_req)
 gobject_dep = dependency('gobject-2.0', version: glib_req)
@@ -68,6 +69,7 @@ cairo_dep = dependency('cairo', version: cairo_req)
 sqlite_dep = dependency('sqlite3', version: sqlite_req)
 libsoup_dep = dependency('libsoup-2.4', version: libsoup_req)
 gtk_dep = dependency('gtk4')
+json_glib_dep = dependency('json-glib-1.0', version: json_glib_req)
 
 introspection_dep = dependency('gobject-introspection-1.0', version: introspection_req, required: false)
 vapigen_dep = dependency('vapigen', version: vala_req, required: false)
diff --git a/shumate/meson.build b/shumate/meson.build
index 0326fd4..4cd3ccc 100644
--- a/shumate/meson.build
+++ b/shumate/meson.build
@@ -25,6 +25,8 @@ libshumate_public_h = [
 libshumate_private_h = [
   'shumate-kinetic-scrolling-private.h',
   'shumate-marker-private.h',
+
+  'vector/shumate-vector-utils-private.h',
 ]
 
 libshumate_sources = [
@@ -49,6 +51,8 @@ libshumate_sources = [
   'shumate-tile.c',
   'shumate-vector-style.c',
   'shumate-viewport.c',
+
+  'vector/shumate-vector-utils.c',
 ]
 
 libshumate_deps = [
@@ -60,6 +64,7 @@ libshumate_deps = [
   cairo_dep,
   sqlite_dep,
   libsoup_dep,
+  json_glib_dep,
 ]
 
 libshumate_c_args = [
diff --git a/shumate/vector/shumate-vector-utils-private.h b/shumate/vector/shumate-vector-utils-private.h
new file mode 100644
index 0000000..6d72720
--- /dev/null
+++ b/shumate/vector/shumate-vector-utils-private.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2021 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <json-glib/json-glib.h>
+#include "shumate-vector-style.h"
+
+
+gboolean shumate_vector_json_get_object (JsonNode *node, JsonObject **dest, GError **error);
+gboolean shumate_vector_json_get_array (JsonNode *node, JsonArray **dest, GError **error);
diff --git a/shumate/vector/shumate-vector-utils.c b/shumate/vector/shumate-vector-utils.c
new file mode 100644
index 0000000..39e8584
--- /dev/null
+++ b/shumate/vector/shumate-vector-utils.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2021 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "shumate-vector-utils-private.h"
+
+gboolean
+shumate_vector_json_get_object (JsonNode *node, JsonObject **dest, GError **error)
+{
+  g_assert (node != NULL);
+  g_assert (dest != NULL);
+
+  if (!JSON_NODE_HOLDS_OBJECT (node))
+    {
+      g_set_error (error,
+                   SHUMATE_STYLE_ERROR,
+                   SHUMATE_STYLE_ERROR_MALFORMED_STYLE,
+                   "Expected object, got %s",
+                   json_node_type_name (node));
+      return FALSE;
+    }
+
+  *dest = json_node_get_object (node);
+  return TRUE;
+}
+
+
+gboolean
+shumate_vector_json_get_array (JsonNode *node, JsonArray **dest, GError **error)
+{
+  g_assert (node != NULL);
+  g_assert (dest != NULL);
+
+  if (!JSON_NODE_HOLDS_ARRAY (node))
+    {
+      g_set_error (error,
+                   SHUMATE_STYLE_ERROR,
+                   SHUMATE_STYLE_ERROR_MALFORMED_STYLE,
+                   "Expected array, got %s",
+                   json_node_type_name (node));
+      return FALSE;
+    }
+
+  *dest = json_node_get_array (node);
+  return TRUE;
+}


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