From 3a9354e012de04040950cf24dd471d192fccd9d3 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 17 Jan 2021 14:00:44 +0100
Subject: [PATCH] BASE: Completion call for the json parser

---
 inc/lektor/json.h | 12 +++++++-----
 src/base/json.c   | 10 ++++------
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/inc/lektor/json.h b/inc/lektor/json.h
index 2ae81839..cbfa1ebb 100644
--- a/inc/lektor/json.h
+++ b/inc/lektor/json.h
@@ -12,15 +12,17 @@
 
 // *INDENT-OFF*
 typedef void(*json_parse_callback)(
-        const char *key,        /* Json key, a string                       */
-        const char *value,      /* Json value, a string                     */
-        int new_obj,            /* 1 when encountered a new object          */
-        void *user              /* The user pointer from the parse function */
+        const char *key,        /* Json key, a string                               */
+        const char *value,      /* Json value, a string                             */
+        int completed,          /* 1 when all values in the object has been parsed  */
+        void *user              /* The user pointer from the parse function         */
 );
 // *INDENT-ON*
 
 /* Parse only one level of json, simple but only what we need for lektor. The
- * callback is used when a couple key/value is found for the correct level. */
+ * callback is used when a couple key/value is found for the correct level.
+ * When the json object has been completed, call the callback with the
+ * 'completed' argument at 1, 'key' and 'value' will be NULL. */
 int json_parse(const char *str, int level, json_parse_callback, void *user);
 
 #endif /* __LKT_JSON_H__ */
diff --git a/src/base/json.c b/src/base/json.c
index a634bb67..faae37fb 100644
--- a/src/base/json.c
+++ b/src/base/json.c
@@ -94,7 +94,6 @@ int
 json_parse(const char *str, int asked_level, json_parse_callback call, void *user)
 {
     int level    = 0;
-    int new_obj  = 1;
     int is_paren = 0;
     char key[LKT_LINE_MAX];
     char val[LKT_LINE_MAX];
@@ -105,14 +104,15 @@ json_parse(const char *str, int asked_level, json_parse_callback call, void *use
 
         /* Begin of a block */
         if ((len = strspn(str, __JSON_BEGIN))) {
-            new_obj = 1;
             level  += len;
             str    += len;
         }
 
         /* End of a block */
         else if ((len = strspn(str, __JSON_END))) {
-            new_obj = 1;
+            if (level == asked_level) {
+                call(NULL, NULL, 1, user);
+            }
             str    += len;
             level  -= len;
         }
@@ -127,9 +127,7 @@ json_parse(const char *str, int asked_level, json_parse_callback call, void *use
             __NEXT_JSON(str, len, is_paren, val);
 
             if (asked_level == level)
-                call(key, val, new_obj, user);
-
-            new_obj = 0;
+                call(key, val, 0, user);
         }
 
         if (level <= 0) {
-- 
GitLab