Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file json.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Generic JSON format parser routines. |
| 5 | * |
| 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef LY_JSON_H_ |
| 16 | #define LY_JSON_H_ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "log.h" |
| 22 | #include "set.h" |
| 23 | |
| 24 | struct ly_ctx; |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | struct ly_in; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 26 | |
| 27 | /* Macro to test if character is whitespace */ |
| 28 | #define is_jsonws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd) |
| 29 | |
| 30 | /* Macro to test if character is valid string character */ |
| 31 | #define is_jsonstrchar(c) (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x5b) || (c >= 0x5d && c <= 0x10ffff)) |
| 32 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 33 | /* Macro to push JSON parser status */ |
| 34 | #define LYJSON_STATUS_PUSH_RET(CTX, STATUS) \ |
| 35 | LY_CHECK_RET(ly_set_add(&CTX->status, (void *)(uintptr_t)(STATUS), 1, NULL)) |
| 36 | |
| 37 | /* Macro to pop JSON parser status */ |
| 38 | #define LYJSON_STATUS_POP_RET(CTX) \ |
| 39 | assert(CTX->status.count); CTX->status.count--; |
| 40 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 41 | /** |
| 42 | * @brief Status of the parser providing information what is expected next (which function is supposed to be called). |
| 43 | */ |
| 44 | enum LYJSON_PARSER_STATUS { |
| 45 | LYJSON_ERROR, /* JSON parser error - value is used as an error return code */ |
| 46 | LYJSON_ROOT, /* JSON document root, used internally */ |
| 47 | LYJSON_OBJECT, /* JSON object */ |
| 48 | LYJSON_OBJECT_CLOSED, /* JSON object closed */ |
Michal Vasko | c6278a9 | 2022-05-20 10:32:40 +0200 | [diff] [blame] | 49 | LYJSON_OBJECT_EMPTY, /* empty JSON object { } */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 50 | LYJSON_ARRAY, /* JSON array */ |
| 51 | LYJSON_ARRAY_CLOSED, /* JSON array closed */ |
| 52 | LYJSON_ARRAY_EMPTY, /* empty JSON array */ |
| 53 | LYJSON_NUMBER, /* JSON number value */ |
| 54 | LYJSON_STRING, /* JSON string value */ |
| 55 | LYJSON_FALSE, /* JSON false value */ |
| 56 | LYJSON_TRUE, /* JSON true value */ |
| 57 | LYJSON_NULL, /* JSON null value */ |
| 58 | LYJSON_END /* end of input data */ |
| 59 | }; |
| 60 | |
| 61 | struct lyjson_ctx { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 62 | const struct ly_ctx *ctx; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 63 | struct ly_in *in; /* input structure */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 64 | |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 65 | struct ly_set status; /* stack of ::LYJSON_PARSER_STATUS values corresponding to the JSON items being processed */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 66 | |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 67 | const char *value; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT */ |
| 68 | size_t value_len; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT */ |
| 69 | ly_bool dynamic; /* ::LYJSON_STRING, ::LYJSON_NUMBER, ::LYJSON_OBJECT */ |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame] | 70 | uint32_t depth; /* current number of nested blocks, see ::LY_MAX_BLOCK_DEPTH */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 71 | |
| 72 | struct { |
| 73 | enum LYJSON_PARSER_STATUS status; |
| 74 | uint32_t status_count; |
| 75 | const char *value; |
| 76 | size_t value_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 77 | ly_bool dynamic; |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame] | 78 | uint32_t depth; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 79 | const char *input; |
| 80 | } backup; |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * @brief Create a new JSON parser context and start parsing. |
| 85 | * |
| 86 | * @param[in] ctx libyang context. |
| 87 | * @param[in] in JSON string data to parse. |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 88 | * @param[in] subtree Whether this is a special case of parsing a subtree (starting with object name). |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 89 | * @param[out] jsonctx New JSON context with status referring the parsed value. |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 90 | * @return LY_ERR value. |
| 91 | */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 92 | LY_ERR lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, ly_bool subtree, struct lyjson_ctx **jsonctx); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * @brief Get status of the parser as the last/previous parsed token |
| 96 | * |
| 97 | * @param[in] jsonctx JSON context to check. |
| 98 | * @param[in] index Index of the token, starting by 0 for the last token |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 99 | * @return ::LYJSON_ERROR in case of invalid index, other ::LYJSON_PARSER_STATUS corresponding to the token. |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 100 | */ |
| 101 | enum LYJSON_PARSER_STATUS lyjson_ctx_status(struct lyjson_ctx *jsonctx, uint32_t index); |
| 102 | |
| 103 | /** |
| 104 | * @brief Get string representation of the JSON context status (token). |
| 105 | * |
| 106 | * @param[in] status Context status (aka JSON token) |
| 107 | * @return String representation of the @p status. |
| 108 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 109 | const char *lyjson_token2str(enum LYJSON_PARSER_STATUS status); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 110 | |
| 111 | /** |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 112 | * @brief Move to the next JSON artifact and update parser status. |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 113 | * |
| 114 | * @param[in] jsonctx XML context to move. |
| 115 | * @param[out] status Optional parameter to provide new parser status |
| 116 | * @return LY_ERR value. |
| 117 | */ |
| 118 | LY_ERR lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status); |
| 119 | |
| 120 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 121 | * @brief Backup the JSON parser context's state To restore the backup, use ::lyjson_ctx_restore(). |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 122 | * @param[in] jsonctx JSON parser context to backup. |
| 123 | */ |
| 124 | void lyjson_ctx_backup(struct lyjson_ctx *jsonctx); |
| 125 | |
| 126 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 127 | * @brief REstore the JSON parser context's state from the backup created by ::lyjson_ctx_backup(). |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 128 | * @param[in] jsonctx JSON parser context to restore. |
| 129 | */ |
| 130 | void lyjson_ctx_restore(struct lyjson_ctx *jsonctx); |
| 131 | |
| 132 | /** |
| 133 | * @brief Remove the allocated working memory of the context. |
| 134 | * |
| 135 | * @param[in] jsonctx JSON context to clear. |
| 136 | */ |
| 137 | void lyjson_ctx_free(struct lyjson_ctx *jsonctx); |
| 138 | |
| 139 | #endif /* LY_JSON_H_ */ |