[libgdata] core: Add new JSON internal parser helper methods
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgdata] core: Add new JSON internal parser helper methods
- Date: Sun, 19 Apr 2015 23:15:30 +0000 (UTC)
commit 750bb7ffe6395d85d930948eaf93c0f323f3ef8d
Author: Philip Withnall <philip tecnocode co uk>
Date: Sun Apr 19 18:39:20 2015 +0100
core: Add new JSON internal parser helper methods
New internal API:
• gdata_parser_int_from_json_member()
• gdata_parser_strv_from_json_member()
These will be useful in upcoming changes to port to v3 of the YouTube
API.
https://bugzilla.gnome.org/show_bug.cgi?id=687597
gdata/gdata-parser.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++
gdata/gdata-parser.h | 12 ++++
2 files changed, 181 insertions(+), 0 deletions(-)
---
diff --git a/gdata/gdata-parser.c b/gdata/gdata-parser.c
index ee74595..6d45954 100644
--- a/gdata/gdata-parser.c
+++ b/gdata/gdata-parser.c
@@ -797,6 +797,75 @@ gdata_parser_string_from_json_member (JsonReader *reader, const gchar *member_na
}
/*
+ * gdata_parser_int_from_json_member:
+ * @reader: #JsonReader cursor object to read JSON node from
+ * @member_name: the name of the member to parse
+ * @options: a bitwise combination of parsing options from #GDataParserOptions,
+ * or %P_NONE
+ * @output: the return location for the parsed integer content
+ * @success: the return location for a value which is %TRUE if the integer was
+ * parsed successfully, %FALSE if an error was encountered, and undefined if
+ * @element didn't match @element_name
+ * @error: a #GError, or %NULL
+ *
+ * Gets the integer content of @element if its name is @element_name, subject to
+ * various checks specified by @options.
+ *
+ * If @element doesn't match @element_name, %FALSE will be returned, @error will
+ * be unset and @success will be unset.
+ *
+ * If @element matches @element_name but one of the checks specified by @options
+ * fails, %TRUE will be returned, @error will be set to a
+* %GDATA_SERVICE_ERROR_PROTOCOL_ERROR error and @success will be set to %FALSE.
+ *
+ * If @element matches @element_name and all of the checks specified by @options
+ * pass, %TRUE will be returned, @error will be unset and @success will be set
+ * to %TRUE.
+ *
+ * The reason for returning the success of the parsing in @success is so that
+ * calls to gdata_parser_int_from_element() can be chained together in a large
+ * "or" statement based on their return values, for the purposes of determining
+ * whether any of the calls matched a given @element. If any of the calls to
+ * gdata_parser_int_from_element() return %TRUE, the value of @success can be
+ * examined.
+ *
+ * Return value: %TRUE if @element matched @element_name, %FALSE otherwise
+ *
+ * Since: UNRELEASED
+ */
+gboolean
+gdata_parser_int_from_json_member (JsonReader *reader,
+ const gchar *member_name,
+ GDataParserOptions options,
+ gint64 *output, gboolean *success,
+ GError **error)
+{
+ gint64 value;
+ const GError *child_error = NULL;
+
+ /* Check if there's such element */
+ if (g_strcmp0 (json_reader_get_member_name (reader), member_name) != 0) {
+ return FALSE;
+ }
+
+ value = json_reader_get_int_value (reader);
+ child_error = json_reader_get_error (reader);
+
+ if (child_error != NULL) {
+ *success = gdata_parser_error_from_json_error (reader,
+ child_error,
+ error);
+ return TRUE;
+ }
+
+ /* Success! */
+ *output = value;
+ *success = TRUE;
+
+ return TRUE;
+}
+
+/*
* gdata_parser_int64_time_from_json_member:
* @reader: #JsonReader cursor object to read JSON node from
* @element_name: the name of the element to parse
@@ -923,6 +992,106 @@ gdata_parser_boolean_from_json_member (JsonReader *reader, const gchar *member_n
return TRUE;
}
+/*
+ * gdata_parser_strv_from_json_member:
+ * @reader: #JsonReader cursor object to read JSON node from
+ * @element_name: the name of the element to parse
+ * @options: a bitwise combination of parsing options from #GDataParserOptions,
+ * or %P_NONE
+ * @output: (out callee-allocates) (transfer full): the return location for the
+ * parsed string array
+ * @success: the return location for a value which is %TRUE if the string array
+ * was parsed successfully, %FALSE if an error was encountered, and undefined
+ * if @element didn't match @element_name
+ * @error: a #GError, or %NULL
+ *
+ * Gets the string array of @element if its name is @element_name, subject to
+ * various checks specified by @options. It expects the @element to be an array
+ * of strings.
+ *
+ * If @element doesn't match @element_name, %FALSE will be returned, @error will
+ * be unset and @success will be unset.
+ *
+ * If @element matches @element_name but one of the checks specified by @options
+ * fails, %TRUE will be returned, @error will be set to a
+ * %GDATA_SERVICE_ERROR_PROTOCOL_ERROR error and @success will be set to %FALSE.
+ *
+ * If @element matches @element_name and all of the checks specified by @options
+ * pass, %TRUE will be returned, @error will be unset and @success will be set
+ * to %TRUE.
+ *
+ * The reason for returning the success of the parsing in @success is so that
+ * calls to gdata_parser_strv_from_element() can be chained together in a large
+ * "or" statement based on their return values, for the purposes of determining
+ * whether any of the calls matched a given @element. If any of the calls to
+ * gdata_parser_strv_from_element() return %TRUE, the value of @success can be
+ * examined.
+ *
+ * Return value: %TRUE if @element matched @element_name, %FALSE otherwise
+ *
+ * Since: UNRELEASED
+ */
+gboolean
+gdata_parser_strv_from_json_member (JsonReader *reader,
+ const gchar *member_name,
+ GDataParserOptions options,
+ gchar ***output, gboolean *success,
+ GError **error)
+{
+ guint i, len;
+ GPtrArray *out;
+ const GError *child_error = NULL;
+
+ /* Check if there's such element */
+ if (g_strcmp0 (json_reader_get_member_name (reader),
+ member_name) != 0) {
+ return FALSE;
+ }
+
+ /* Check if the output strv has already been set. The JSON parser
+ * guarantees this can't happen. */
+ g_assert (!(options & P_NO_DUPES) || *output == NULL);
+
+ len = json_reader_count_elements (reader);
+ child_error = json_reader_get_error (reader);
+
+ if (child_error != NULL) {
+ *success = gdata_parser_error_from_json_error (reader,
+ child_error,
+ error);
+ return TRUE;
+ }
+
+ out = g_ptr_array_new_full (len + 1 /* NULL terminator */, g_free);
+
+ for (i = 0; i < len; i++) {
+ const gchar *val;
+
+ json_reader_read_element (reader, i);
+ val = json_reader_get_string_value (reader);
+ child_error = json_reader_get_error (reader);
+
+ if (child_error != NULL) {
+ *success = gdata_parser_error_from_json_error (reader,
+ child_error,
+ error);
+ json_reader_end_element (reader);
+ g_ptr_array_unref (out);
+ return TRUE;
+ }
+
+ g_ptr_array_add (out, g_strdup (val));
+
+ json_reader_end_element (reader);
+ }
+
+ /* Success! */
+ *output = (gchar **) g_ptr_array_free (out, FALSE);
+ *success = TRUE;
+
+ return TRUE;
+}
+
void
gdata_parser_string_append_escaped (GString *xml_string, const gchar *pre, const gchar *element_content,
const gchar *post)
{
diff --git a/gdata/gdata-parser.h b/gdata/gdata-parser.h
index ed832d8..84e1079 100644
--- a/gdata/gdata-parser.h
+++ b/gdata/gdata-parser.h
@@ -89,10 +89,22 @@ gboolean gdata_parser_object_from_element (xmlNode *element, const gchar *elemen
gpointer /* GDataParsable ** */ _output, gboolean *success,
GError **error);
gboolean gdata_parser_string_from_json_member (JsonReader *reader, const gchar *member_name,
GDataParserOptions options,
gchar **output, gboolean *success, GError **error);
+gboolean
+gdata_parser_int_from_json_member (JsonReader *reader,
+ const gchar *member_name,
+ GDataParserOptions options,
+ gint64 *output, gboolean *success,
+ GError **error);
gboolean gdata_parser_int64_time_from_json_member (JsonReader *reader, const gchar *member_name,
GDataParserOptions options,
gint64 *output, gboolean *success, GError **error);
gboolean gdata_parser_boolean_from_json_member (JsonReader *reader, const gchar *member_name,
GDataParserOptions options,
gboolean *output, gboolean *success, GError **error);
+gboolean
+gdata_parser_strv_from_json_member (JsonReader *reader,
+ const gchar *member_name,
+ GDataParserOptions options,
+ gchar ***output, gboolean *success,
+ GError **error);
void gdata_parser_string_append_escaped (GString *xml_string, const gchar *pre, const gchar
*element_content, const gchar *post);
gchar *gdata_parser_utf8_trim_whitespace (const gchar *s) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]