[json-glib] Add simple JSON/string utilities
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib] Add simple JSON/string utilities
- Date: Tue, 9 Jun 2015 11:59:37 +0000 (UTC)
commit b6546a62362bafcb0aa2d97175a15c4218cc6023
Author: Emmanuele Bassi <ebassi gnome org>
Date: Tue Jun 9 12:23:12 2015 +0100
Add simple JSON/string utilities
Wrap JsonParser and JsonGenerator in simple, easy to call functions that
manage the objects for you.
doc/reference/json-glib-docs.xml | 5 ++
doc/reference/json-glib-sections.txt | 6 ++
json-glib/Makefile.am | 2 +
json-glib/json-glib.h | 1 +
json-glib/json-utils.c | 103 ++++++++++++++++++++++++++++++++++
json-glib/json-utils.h | 40 +++++++++++++
json-glib/json-version-macros.h | 16 +++++
7 files changed, 173 insertions(+), 0 deletions(-)
---
diff --git a/doc/reference/json-glib-docs.xml b/doc/reference/json-glib-docs.xml
index 1c45a74..0ab2454 100644
--- a/doc/reference/json-glib-docs.xml
+++ b/doc/reference/json-glib-docs.xml
@@ -112,6 +112,11 @@
<xi:include href="xml/json-builder.xml"/>
</chapter>
+ <chapter>
+ <title>General Purpose API</title>
+ <xi:include href="xml/json-utils.xml"/>
+ </chapter>
+
</part>
<part id="json-advanced">
diff --git a/doc/reference/json-glib-sections.txt b/doc/reference/json-glib-sections.txt
index 89c1120..6331304 100644
--- a/doc/reference/json-glib-sections.txt
+++ b/doc/reference/json-glib-sections.txt
@@ -407,3 +407,9 @@ JSON_IS_PATH
json_path_get_type
json_path_error_quark
</SECTION>
+
+<SECTION>
+<FILE>json-utils</FILE>
+json_from_string
+json_to_string
+</SECTION>
diff --git a/json-glib/Makefile.am b/json-glib/Makefile.am
index 13e024a..1755879 100644
--- a/json-glib/Makefile.am
+++ b/json-glib/Makefile.am
@@ -23,6 +23,7 @@ source_h = \
json-path.h \
json-reader.h \
json-types.h \
+ json-utils.h \
json-version-macros.h \
$(NULL)
@@ -48,6 +49,7 @@ source_c = \
json-reader.c \
json-scanner.c \
json-serializable.c \
+ json-utils.c \
json-value.c \
$(NULL)
diff --git a/json-glib/json-glib.h b/json-glib/json-glib.h
index 8a0e7f7..939ce0e 100644
--- a/json-glib/json-glib.h
+++ b/json-glib/json-glib.h
@@ -33,6 +33,7 @@
#include <json-glib/json-parser.h>
#include <json-glib/json-path.h>
#include <json-glib/json-reader.h>
+#include <json-glib/json-utils.h>
#include <json-glib/json-version.h>
#include <json-glib/json-version-macros.h>
diff --git a/json-glib/json-utils.c b/json-glib/json-utils.c
new file mode 100644
index 0000000..78583cd
--- /dev/null
+++ b/json-glib/json-utils.c
@@ -0,0 +1,103 @@
+/* json-utils.c - JSON utility API
+ *
+ * This file is part of JSON-GLib
+ * Copyright 2015 Emmanuele Bassi
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * SECTION:json-utils
+ * @Title: Utility API
+ * @Short_description: Various utility functions
+ *
+ * Various utility functions.
+ */
+
+#include "config.h"
+
+#include "json-utils.h"
+#include "json-parser.h"
+#include "json-generator.h"
+
+/**
+ * json_from_string:
+ * @str: a valid UTF-8 string containing JSON data
+ * @error: return location for a #GError
+ *
+ * Parses the string in @str and returns a #JsonNode representing
+ * the JSON tree.
+ *
+ * In case of parsing error, this function returns %NULL and sets
+ * @error appropriately.
+ *
+ * Returns: (transfer full): a #JsonNode, or %NULL
+ *
+ * Since: 1.2
+ */
+JsonNode *
+json_from_string (const char *str,
+ GError **error)
+{
+ JsonParser *parser;
+ JsonNode *retval;
+
+ g_return_val_if_fail (str != NULL, NULL);
+
+ error = NULL;
+ parser = json_parser_new ();
+ if (!json_parser_load_from_data (parser, str, -1, error))
+ {
+ g_object_unref (parser);
+ return NULL;
+ }
+
+ retval = json_node_copy (json_parser_get_root (parser));
+
+ g_object_unref (parser);
+
+ return retval;
+}
+
+/**
+ * json_to_string:
+ * @node: a #JsonNode
+ * @pretty: whether the output should be prettyfied for printing
+ *
+ * Generates a stringified JSON representation of the contents of
+ * the passed @node.
+ *
+ * Returns: (transfer full): the string representation of the #JsonNode
+ *
+ * Since: 1.2
+ */
+char *
+json_to_string (JsonNode *node,
+ gboolean pretty)
+{
+ JsonGenerator *generator;
+ char *retval;
+
+ g_return_val_if_fail (node != NULL, NULL);
+
+ generator = json_generator_new ();
+ json_generator_set_pretty (generator, pretty);
+ json_generator_set_root (generator, node);
+
+ retval = json_generator_to_data (generator, NULL);
+
+ g_object_unref (generator);
+
+ return retval;
+}
diff --git a/json-glib/json-utils.h b/json-glib/json-utils.h
new file mode 100644
index 0000000..e709ecd
--- /dev/null
+++ b/json-glib/json-utils.h
@@ -0,0 +1,40 @@
+/* json-utils.h - JSON utility API
+ *
+ * This file is part of JSON-GLib
+ * Copyright 2015 Emmanuele Bassi
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __JSON_UTILS_H__
+#define __JSON_UTILS_H__
+
+#if !defined(__JSON_GLIB_INSIDE__) && !defined(JSON_COMPILATION)
+#error "Only <json-glib/json-glib.h> can be included directly."
+#endif
+
+#include <json-glib/json-types.h>
+
+G_BEGIN_DECLS
+
+JSON_AVAILABLE_IN_1_2
+JsonNode * json_from_string (const char *str,
+ GError **error);
+JSON_AVAILABLE_IN_1_2
+char * json_to_string (JsonNode *node,
+ gboolean pretty);
+
+G_END_DECLS
+
+#endif /* __JSON_UTILS_H__ */
diff --git a/json-glib/json-version-macros.h b/json-glib/json-version-macros.h
index dd2ae1c..efba7f4 100644
--- a/json-glib/json-version-macros.h
+++ b/json-glib/json-version-macros.h
@@ -43,6 +43,8 @@
/* XXX: Each new cycle should add a new version symbol here */
#define JSON_VERSION_1_0 (G_ENCODE_VERSION (1, 0))
+#define JSON_VERSION_1_2 (G_ENCODE_VERSION (1, 2))
+
/* evaluates to the current stable version; for development cycles,
* this means the next stable target
*/
@@ -132,4 +134,18 @@
# define JSON_AVAILABLE_IN_1_0 _JSON_EXTERN
#endif
+#if JSON_VERSION_MIN_REQUIRED >= JSON_VERSION_1_2
+# define JSON_DEPRECATED_IN_1_2 JSON_DEPRECATED
+# define JSON_DEPRECATED_IN_1_2_FOR(f) JSON_DEPRECATED_FOR(f)
+#else
+# define JSON_DEPRECATED_IN_1_2 _JSON_EXTERN
+# define JSON_DEPRECATED_IN_1_2_FOR(f) _JSON_EXTERN
+#endif
+
+#if JSON_VERSION_MAX_ALLOWED < JSON_VERSION_1_2
+# define JSON_AVAILABLE_IN_1_2 JSON_UNAVAILABLE(1, 2)
+#else
+# define JSON_AVAILABLE_IN_1_2 _JSON_EXTERN
+#endif
+
#endif /* __JSON_VERSION_MACROS_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]