summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2023-01-10 09:49:28 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2023-01-10 16:10:18 +0100
commit4c7524a84a40062cf9692819577bf0ca44a15aa6 (patch)
tree9e8fd27909469fb4e21f8ffabd4ece0bb7136031 /lib
parent2f5b41fdd8e2b611fba3d465cf5b631fc534b9b8 (diff)
downloadbarebox-4c7524a84a40062cf9692819577bf0ca44a15aa6.tar.gz
barebox-4c7524a84a40062cf9692819577bf0ca44a15aa6.tar.xz
lib: extend jsmn with simple JSONPath lookup helpers
Board code may not be interested in iterating fully over the JSON blob and instead just wants to lookup a value at a specific JSONPath. Add helpers to facilitate this. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20230110084930.3439001-3-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/jsmn.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/jsmn.c b/lib/jsmn.c
index 3a68f89337..7bdcc90f2f 100644
--- a/lib/jsmn.c
+++ b/lib/jsmn.c
@@ -369,3 +369,89 @@ JSMN_API void jsmn_init(jsmn_parser *parser) {
parser->toknext = 0;
parser->toksuper = -1;
}
+
+JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token)
+{
+ size_t token_size = token->end - token->start;
+ return strlen(val) == token_size
+ && strncmp(json + token->start, val, token_size) == 0;
+}
+
+JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *token)
+{
+ return token->type == JSMN_STRING && jsmn_eq(str, json, token);
+}
+
+JSMN_API const jsmntok_t *jsmn_skip_value(const jsmntok_t *tokens)
+{
+ int max_index = tokens[0].end;
+ do {
+ ++tokens;
+ } while (tokens[0].start < max_index);
+ return &tokens[0];
+}
+
+JSMN_API const jsmntok_t *jsmn_find_value(const char *key, const char *json,
+ const jsmntok_t *tokens)
+{
+ int items;
+ if (tokens[0].type != JSMN_OBJECT || tokens[0].size == 0)
+ return NULL;
+
+ items = tokens[0].size;
+ ++tokens;
+
+ do {
+ if (jsmn_str_eq(key, json, tokens))
+ return &tokens[1];
+ tokens = --items ? jsmn_skip_value(&tokens[1]) : NULL;
+ } while (tokens);
+
+ return NULL;
+}
+
+JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json,
+ const jsmntok_t *tokens)
+{
+ int i = 0;
+ while (path[i] != NULL) {
+ const jsmntok_t *value = jsmn_find_value(path[i], json, tokens);
+ if (!value)
+ return NULL;
+
+ switch (value->type) {
+ case JSMN_OBJECT:
+ case JSMN_ARRAY:
+ tokens = value;
+ ++i;
+ break;
+ case JSMN_UNDEFINED:
+ case JSMN_STRING:
+ case JSMN_PRIMITIVE:
+ return value;
+ }
+ }
+
+ return tokens;
+}
+
+JSMN_API char *jsmn_strcpy(const char *path[], const char *json,
+ const jsmntok_t *tokens)
+{
+ const jsmntok_t *node;
+ int value_size;
+ char *value;
+
+ node = jsmn_locate(path, json, tokens);
+ if (!node || node->type != JSMN_STRING)
+ return NULL;
+
+ value_size = node->end - node->start;
+ value = malloc(value_size + 1);
+ if (value) {
+ strncpy(value, json + node->start, value_size);
+ value[value_size] = '\0';
+ }
+
+ return value;
+}