diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2023-01-10 09:49:28 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2023-01-10 16:10:18 +0100 |
commit | 4c7524a84a40062cf9692819577bf0ca44a15aa6 (patch) | |
tree | 9e8fd27909469fb4e21f8ffabd4ece0bb7136031 /include/jsmn.h | |
parent | 2f5b41fdd8e2b611fba3d465cf5b631fc534b9b8 (diff) | |
download | barebox-4c7524a84a40062cf9692819577bf0ca44a15aa6.tar.gz |
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 'include/jsmn.h')
-rw-r--r-- | include/jsmn.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/include/jsmn.h b/include/jsmn.h index 394ffc4674..8f6db8d534 100644 --- a/include/jsmn.h +++ b/include/jsmn.h @@ -84,6 +84,63 @@ JSMN_API void jsmn_init(jsmn_parser *parser); JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, jsmntok_t *tokens, const unsigned int num_tokens); +/** Returns `true` if `token` is a string and equal to `str`. */ +JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *token); + +/** Returns `true` if `token` is to `str`. */ +JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token); + +/** Returns the token after the value at `tokens[0]`. */ +JSMN_API const jsmntok_t *jsmn_skip_value(const jsmntok_t *tokens); + +/** + * Returns a pointer to the value associated with `key` inside `json` starting + * at `tokens[0]`, which is expected to be an object, or returns `NULL` if `key` + * cannot be found. + */ +JSMN_API const jsmntok_t *jsmn_find_value(const char *key, const char *json, + const jsmntok_t *tokens); + +/** + * Locate the token at `path` inside `json` in a manner similar to JSONPath. + * + * Example: + * \code + * { + * "date": "...", + * "product": { + * "serial": "1234", + * } + * } + * \endcode + * + * To locate the serial number in the JSON object above, you would use the + * JSONPath expression "$.product.serial". The same thing can be accomplished + * with this call: + * + * \code + * const char *path[] = {"product", "serial", NULL}; + * const jsmntok_t *token = jsmn_locate(path, json, tokens); + * \endcode + * + * \remark This function cannot search inside arrays. + * + * @param path Null-terminated list of path elements. + * @param json JSON string to search in. + * @param tokens Tokens for `json`. + * + * @return Pointer to the value token or `NULL` if the token could not be found. + */ +JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, + const jsmntok_t *tokens); + +/** + * Similar to `jsmn_locate` but returns a copy of the value or `NULL` if the + * value does not exist or is not a string. The caller takes ownership of the + * pointer returned. + */ +JSMN_API char *jsmn_strcpy(const char *path[], const char *json, const jsmntok_t *tokens); + #ifdef __cplusplus } #endif |