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