summaryrefslogtreecommitdiffstats
path: root/src/json.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 04:23:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 04:23:18 +0000
commit16527d165de55f2727f55b752b30d44a7e61e927 (patch)
tree0840ee6e0e109a9e85f0d80ff36f92587c6a5538 /src/json.h
parentReleasing progress-linux version 2.1.30-2.1~progress7.99u1. (diff)
downloadlibyang2-16527d165de55f2727f55b752b30d44a7e61e927.tar.xz
libyang2-16527d165de55f2727f55b752b30d44a7e61e927.zip
Merging upstream version 2.1.148.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/json.h')
-rw-r--r--src/json.h89
1 files changed, 50 insertions, 39 deletions
diff --git a/src/json.h b/src/json.h
index 53efe2a..61c561a 100644
--- a/src/json.h
+++ b/src/json.h
@@ -1,9 +1,10 @@
/**
* @file json.h
* @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
* @brief Generic JSON format parser routines.
*
- * Copyright (c) 2020 CESNET, z.s.p.o.
+ * Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -24,6 +25,9 @@
struct ly_ctx;
struct ly_in;
+#define LYJSON_STRING_BUF_START 24
+#define LYJSON_STRING_BUF_STEP 128
+
/* Macro to test if character is whitespace */
#define is_jsonws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)
@@ -35,27 +39,27 @@ struct ly_in;
LY_CHECK_RET(ly_set_add(&CTX->status, (void *)(uintptr_t)(STATUS), 1, NULL))
/* Macro to pop JSON parser status */
-#define LYJSON_STATUS_POP_RET(CTX) \
+#define LYJSON_STATUS_POP(CTX) \
assert(CTX->status.count); CTX->status.count--;
/**
* @brief Status of the parser providing information what is expected next (which function is supposed to be called).
*/
enum LYJSON_PARSER_STATUS {
- LYJSON_ERROR, /* JSON parser error - value is used as an error return code */
- LYJSON_ROOT, /* JSON document root, used internally */
- LYJSON_OBJECT, /* JSON object */
- LYJSON_OBJECT_CLOSED, /* JSON object closed */
- LYJSON_OBJECT_EMPTY, /* empty JSON object { } */
- LYJSON_ARRAY, /* JSON array */
- LYJSON_ARRAY_CLOSED, /* JSON array closed */
- LYJSON_ARRAY_EMPTY, /* empty JSON array */
- LYJSON_NUMBER, /* JSON number value */
- LYJSON_STRING, /* JSON string value */
- LYJSON_FALSE, /* JSON false value */
- LYJSON_TRUE, /* JSON true value */
- LYJSON_NULL, /* JSON null value */
- LYJSON_END /* end of input data */
+ LYJSON_ERROR = 0, /* JSON parser error - value is used as an error return code */
+ LYJSON_OBJECT, /* JSON object */
+ LYJSON_OBJECT_NEXT, /* JSON object next item */
+ LYJSON_OBJECT_CLOSED, /* JSON object closed */
+ LYJSON_ARRAY, /* JSON array */
+ LYJSON_ARRAY_NEXT, /* JSON array next item */
+ LYJSON_ARRAY_CLOSED, /* JSON array closed */
+ LYJSON_OBJECT_NAME, /* JSON object name */
+ LYJSON_NUMBER, /* JSON number value */
+ LYJSON_STRING, /* JSON string value */
+ LYJSON_TRUE, /* JSON true value */
+ LYJSON_FALSE, /* JSON false value */
+ LYJSON_NULL, /* JSON null value */
+ LYJSON_END /* end of input data */
};
struct lyjson_ctx {
@@ -64,10 +68,9 @@ struct lyjson_ctx {
struct ly_set status; /* stack of ::LYJSON_PARSER_STATUS values corresponding to the JSON items being processed */
- const char *value; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT */
- size_t value_len; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT */
- ly_bool dynamic; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT */
- uint32_t depth; /* current number of nested blocks, see ::LY_MAX_BLOCK_DEPTH */
+ const char *value; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT_NAME */
+ size_t value_len; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT_NAME */
+ ly_bool dynamic; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT_NAME */
struct {
enum LYJSON_PARSER_STATUS status;
@@ -75,38 +78,44 @@ struct lyjson_ctx {
const char *value;
size_t value_len;
ly_bool dynamic;
- uint32_t depth;
const char *input;
} backup;
};
/**
- * @brief Create a new JSON parser context and start parsing.
+ * @brief Get string representation of the JSON context status (token).
*
- * @param[in] ctx libyang context.
- * @param[in] in JSON string data to parse.
- * @param[in] subtree Whether this is a special case of parsing a subtree (starting with object name).
- * @param[out] jsonctx New JSON context with status referring the parsed value.
- * @return LY_ERR value.
+ * @param[in] status Context status (aka JSON token)
+ * @return String representation of the @p status.
*/
-LY_ERR lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, ly_bool subtree, struct lyjson_ctx **jsonctx);
+const char *lyjson_token2str(enum LYJSON_PARSER_STATUS status);
/**
- * @brief Get status of the parser as the last/previous parsed token
+ * @brief Get current status of the parser.
*
- * @param[in] jsonctx JSON context to check.
- * @param[in] index Index of the token, starting by 0 for the last token
- * @return ::LYJSON_ERROR in case of invalid index, other ::LYJSON_PARSER_STATUS corresponding to the token.
+ * @param[in] jsonctx JSON parser context to check.
+ * @return ::LYJSON_PARSER_STATUS according to the last parsed token.
*/
-enum LYJSON_PARSER_STATUS lyjson_ctx_status(struct lyjson_ctx *jsonctx, uint32_t index);
+enum LYJSON_PARSER_STATUS lyjson_ctx_status(struct lyjson_ctx *jsonctx);
/**
- * @brief Get string representation of the JSON context status (token).
+ * @brief Get current nesting (object/array) depth.
*
- * @param[in] status Context status (aka JSON token)
- * @return String representation of the @p status.
+ * @param[in] jsonctx JSON parser context to check.
+ * @return Current nesting depth.
*/
-const char *lyjson_token2str(enum LYJSON_PARSER_STATUS status);
+uint32_t lyjson_ctx_depth(struct lyjson_ctx *jsonctx);
+
+/**
+ * @brief Create a new JSON parser context and start parsing.
+ *
+ * @param[in] ctx libyang context.
+ * @param[in] in JSON string data to parse.
+ * @param[in] subtree Whether this is a special case of parsing a subtree (starting with object name).
+ * @param[out] jsonctx New JSON parser context with status referring the parsed value.
+ * @return LY_ERR value.
+ */
+LY_ERR lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyjson_ctx **jsonctx);
/**
* @brief Move to the next JSON artifact and update parser status.
@@ -119,12 +128,14 @@ LY_ERR lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *st
/**
* @brief Backup the JSON parser context's state To restore the backup, use ::lyjson_ctx_restore().
+ *
* @param[in] jsonctx JSON parser context to backup.
*/
void lyjson_ctx_backup(struct lyjson_ctx *jsonctx);
/**
- * @brief REstore the JSON parser context's state from the backup created by ::lyjson_ctx_backup().
+ * @brief Restore the JSON parser context's state from the backup created by ::lyjson_ctx_backup().
+ *
* @param[in] jsonctx JSON parser context to restore.
*/
void lyjson_ctx_restore(struct lyjson_ctx *jsonctx);
@@ -132,7 +143,7 @@ void lyjson_ctx_restore(struct lyjson_ctx *jsonctx);
/**
* @brief Remove the allocated working memory of the context.
*
- * @param[in] jsonctx JSON context to clear.
+ * @param[in] jsonctx JSON parser context to clear.
*/
void lyjson_ctx_free(struct lyjson_ctx *jsonctx);