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 | |
| 33 | /** |
| 34 | * @brief Status of the parser providing information what is expected next (which function is supposed to be called). |
| 35 | */ |
| 36 | enum LYJSON_PARSER_STATUS { |
| 37 | LYJSON_ERROR, /* JSON parser error - value is used as an error return code */ |
| 38 | LYJSON_ROOT, /* JSON document root, used internally */ |
| 39 | LYJSON_OBJECT, /* JSON object */ |
| 40 | LYJSON_OBJECT_CLOSED, /* JSON object closed */ |
| 41 | LYJSON_OBJECT_EMPTY, /* empty JSON object { }*/ |
| 42 | LYJSON_ARRAY, /* JSON array */ |
| 43 | LYJSON_ARRAY_CLOSED, /* JSON array closed */ |
| 44 | LYJSON_ARRAY_EMPTY, /* empty JSON array */ |
| 45 | LYJSON_NUMBER, /* JSON number value */ |
| 46 | LYJSON_STRING, /* JSON string value */ |
| 47 | LYJSON_FALSE, /* JSON false value */ |
| 48 | LYJSON_TRUE, /* JSON true value */ |
| 49 | LYJSON_NULL, /* JSON null value */ |
| 50 | LYJSON_END /* end of input data */ |
| 51 | }; |
| 52 | |
| 53 | struct lyjson_ctx { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 54 | const struct ly_ctx *ctx; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 55 | struct ly_in *in; /* input structure */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 56 | |
| 57 | struct ly_set status; /* stack of LYJSON_PARSER_STATUS values corresponding to the JSON items being processed */ |
| 58 | |
| 59 | const char *value; /* LYJSON_STRING, LYJSON_NUMBER, LYJSON_OBJECT */ |
| 60 | size_t value_len; /* LYJSON_STRING, LYJSON_NUMBER, LYJSON_OBJECT */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 61 | ly_bool dynamic; /* LYJSON_STRING, LYJSON_NUMBER, LYJSON_OBJECT */ |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame^] | 62 | 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] | 63 | |
| 64 | struct { |
| 65 | enum LYJSON_PARSER_STATUS status; |
| 66 | uint32_t status_count; |
| 67 | const char *value; |
| 68 | size_t value_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 69 | ly_bool dynamic; |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame^] | 70 | uint32_t depth; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 71 | const char *input; |
| 72 | } backup; |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * @brief Create a new JSON parser context and start parsing. |
| 77 | * |
| 78 | * @param[in] ctx libyang context. |
| 79 | * @param[in] in JSON string data to parse. |
| 80 | * @param[out] jsonctx New JSON context with status ::LYJSON_VALUE. |
| 81 | * @return LY_ERR value. |
| 82 | */ |
| 83 | LY_ERR lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyjson_ctx **jsonctx); |
| 84 | |
| 85 | /** |
| 86 | * @brief Get status of the parser as the last/previous parsed token |
| 87 | * |
| 88 | * @param[in] jsonctx JSON context to check. |
| 89 | * @param[in] index Index of the token, starting by 0 for the last token |
| 90 | * @return LYJSON_ERROR in case of invalid index, other LYJSON_PARSER_STATUS corresponding to the token. |
| 91 | */ |
| 92 | enum LYJSON_PARSER_STATUS lyjson_ctx_status(struct lyjson_ctx *jsonctx, uint32_t index); |
| 93 | |
| 94 | /** |
| 95 | * @brief Get string representation of the JSON context status (token). |
| 96 | * |
| 97 | * @param[in] status Context status (aka JSON token) |
| 98 | * @return String representation of the @p status. |
| 99 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 100 | const char *lyjson_token2str(enum LYJSON_PARSER_STATUS status); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * @brief Move to the next JSON artefact and update parser status. |
| 104 | * |
| 105 | * @param[in] jsonctx XML context to move. |
| 106 | * @param[out] status Optional parameter to provide new parser status |
| 107 | * @return LY_ERR value. |
| 108 | */ |
| 109 | LY_ERR lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status); |
| 110 | |
| 111 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 112 | * @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] | 113 | * @param[in] jsonctx JSON parser context to backup. |
| 114 | */ |
| 115 | void lyjson_ctx_backup(struct lyjson_ctx *jsonctx); |
| 116 | |
| 117 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 118 | * @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] | 119 | * @param[in] jsonctx JSON parser context to restore. |
| 120 | */ |
| 121 | void lyjson_ctx_restore(struct lyjson_ctx *jsonctx); |
| 122 | |
| 123 | /** |
| 124 | * @brief Remove the allocated working memory of the context. |
| 125 | * |
| 126 | * @param[in] jsonctx JSON context to clear. |
| 127 | */ |
| 128 | void lyjson_ctx_free(struct lyjson_ctx *jsonctx); |
| 129 | |
| 130 | #endif /* LY_JSON_H_ */ |