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




commit 785ce9e476056d5a17060de8307906683bd9b844
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 6d1e099807..2e32ce7414 100644
--- a/gtk/json/gtkjsonparser.c
+++ b/gtk/json/gtkjsonparser.c
@@ -870,6 +870,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]