diff --git a/inc/lektor/json.h b/inc/lektor/json.h index 2ae8183908b5619a5babe0933610f832385f0eaf..cbfa1ebb16a445d71fce6e9d4877b2ccb2a7171f 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 a634bb674beded5e3ca5d4b349182dec415ec005..faae37fbd095b108cffbf06e2d78d481c5329cc9 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) {