[gtk/wip/otte/json: 11/21] json: Add gtk_json_parser_select_member()




commit 3a36ef9f1273f5daee6747dd60f72ef315a7e9b8
Author: Benjamin Otte <otte redhat com>
Date:   Mon Nov 29 09:40:55 2021 +0100

    json: Add gtk_json_parser_select_member()
    
    This is modeled after JsonReader.selectName() from the Java Moshi
    package, see 
https://square.github.io/moshi/1.x/moshi/com/squareup/moshi/JsonReader.html#selectName-com.squareup.moshi.JsonReader.Options-
    
    It returns the ID of the current member name in the passed i char array
    or -1 if it is not found.
    
    This allows writing code like:
      do {
        switch (gtk_json_parser_select_member (parser, { "foo", "bar", NULL })
        {
        case 0:
          /* handle the "foo" member */
          break;
        case 1:
          /* handle the "bar" member */
          break;
        default:
          /* do nothing or complain or whatever */
          break;
        }
      } while (gtk_json_parser_next (parser));
    
    The impementation so far is the simplest one possible, but could be
    significantly improved.

 gtk/json/gtkjsonparser.c        | 24 ++++++++++++++++++++++++
 gtk/json/gtkjsonparserprivate.h |  2 ++
 2 files changed, 26 insertions(+)
---
diff --git a/gtk/json/gtkjsonparser.c b/gtk/json/gtkjsonparser.c
index 8bf929cfba..a2054c373c 100644
--- a/gtk/json/gtkjsonparser.c
+++ b/gtk/json/gtkjsonparser.c
@@ -873,6 +873,30 @@ gtk_json_parser_get_member_name (GtkJsonParser *self)
   return gtk_json_unescape_string (self->block->member_name);
 }
 
+gssize
+gtk_json_parser_select_member (GtkJsonParser      *self,
+                               const char * const *options)
+{
+  char *member_name;
+  gssize i;
+
+  member_name = gtk_json_parser_get_member_name (self);
+  if (member_name == NULL)
+    return -1;
+
+  for (i = 0; options[i]; i++)
+    {
+      if (strcmp (member_name, options[i]) == 0)
+        break;
+    }
+  if (options[i] == NULL)
+    i = -1;
+
+  g_free (member_name);
+
+  return i;
+}
+
 gboolean
 gtk_json_parser_get_boolean (GtkJsonParser *self)
 {
diff --git a/gtk/json/gtkjsonparserprivate.h b/gtk/json/gtkjsonparserprivate.h
index 854042ad71..f3ff43fd88 100644
--- a/gtk/json/gtkjsonparserprivate.h
+++ b/gtk/json/gtkjsonparserprivate.h
@@ -47,6 +47,8 @@ gboolean                gtk_json_parser_next                    (GtkJsonParser
 GtkJsonNode             gtk_json_parser_get_node                (GtkJsonParser          *self);
 const GError *          gtk_json_parser_get_error               (GtkJsonParser          *self) G_GNUC_PURE;
 char *                  gtk_json_parser_get_member_name         (GtkJsonParser          *self);
+gssize                  gtk_json_parser_select_member           (GtkJsonParser          *self,
+                                                                 const char * const     *options);
 
 gboolean                gtk_json_parser_get_boolean             (GtkJsonParser          *self);
 double                  gtk_json_parser_get_number              (GtkJsonParser          *self);


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