blob: 61c561a8d9f9552f1fdfddd3b97bd4712fbc0782 [file] [log] [blame]
Radek Krejci50f0c6b2020-06-18 16:31:48 +02001/**
2 * @file json.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko09e04632023-03-22 14:34:10 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci50f0c6b2020-06-18 16:31:48 +02005 * @brief Generic JSON format parser routines.
6 *
Michal Vasko09e04632023-03-22 14:34:10 +01007 * Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
Radek Krejci50f0c6b2020-06-18 16:31:48 +02008 *
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
25struct ly_ctx;
Radek Krejci47fab892020-11-05 17:02:41 +010026struct ly_in;
Radek Krejci50f0c6b2020-06-18 16:31:48 +020027
Michal Vasko09e04632023-03-22 14:34:10 +010028#define LYJSON_STRING_BUF_START 24
29#define LYJSON_STRING_BUF_STEP 128
30
Radek Krejci50f0c6b2020-06-18 16:31:48 +020031/* 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 Vasko61ad1ff2022-02-10 15:48:39 +010037/* 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 Vasko09e04632023-03-22 14:34:10 +010042#define LYJSON_STATUS_POP(CTX) \
Michal Vasko61ad1ff2022-02-10 15:48:39 +010043 assert(CTX->status.count); CTX->status.count--;
44
Radek Krejci50f0c6b2020-06-18 16:31:48 +020045/**
46 * @brief Status of the parser providing information what is expected next (which function is supposed to be called).
47 */
48enum LYJSON_PARSER_STATUS {
Michal Vasko09e04632023-03-22 14:34:10 +010049 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 Krejci50f0c6b2020-06-18 16:31:48 +020063};
64
65struct lyjson_ctx {
Radek Krejci1798aae2020-07-14 13:26:06 +020066 const struct ly_ctx *ctx;
Radek Krejci1798aae2020-07-14 13:26:06 +020067 struct ly_in *in; /* input structure */
Radek Krejci50f0c6b2020-06-18 16:31:48 +020068
Radek Krejci84d7fd72021-07-14 18:32:21 +020069 struct ly_set status; /* stack of ::LYJSON_PARSER_STATUS values corresponding to the JSON items being processed */
Radek Krejci50f0c6b2020-06-18 16:31:48 +020070
Michal Vasko09e04632023-03-22 14:34:10 +010071 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 Krejci50f0c6b2020-06-18 16:31:48 +020074
75 struct {
76 enum LYJSON_PARSER_STATUS status;
77 uint32_t status_count;
78 const char *value;
79 size_t value_len;
Radek Krejci857189e2020-09-01 13:26:36 +020080 ly_bool dynamic;
Radek Krejci50f0c6b2020-06-18 16:31:48 +020081 const char *input;
82 } backup;
83};
84
85/**
Radek Krejci50f0c6b2020-06-18 16:31:48 +020086 * @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 Vasko22df3f02020-08-24 13:29:22 +020091const char *lyjson_token2str(enum LYJSON_PARSER_STATUS status);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020092
93/**
Michal Vasko09e04632023-03-22 14:34:10 +010094 * @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 */
99enum 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 */
107uint32_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 */
118LY_ERR lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyjson_ctx **jsonctx);
119
120/**
Radek Krejci84d7fd72021-07-14 18:32:21 +0200121 * @brief Move to the next JSON artifact and update parser status.
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200122 *
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 */
127LY_ERR lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status);
128
129/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200130 * @brief Backup the JSON parser context's state To restore the backup, use ::lyjson_ctx_restore().
Michal Vasko09e04632023-03-22 14:34:10 +0100131 *
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200132 * @param[in] jsonctx JSON parser context to backup.
133 */
134void lyjson_ctx_backup(struct lyjson_ctx *jsonctx);
135
136/**
Michal Vasko09e04632023-03-22 14:34:10 +0100137 * @brief Restore the JSON parser context's state from the backup created by ::lyjson_ctx_backup().
138 *
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200139 * @param[in] jsonctx JSON parser context to restore.
140 */
141void lyjson_ctx_restore(struct lyjson_ctx *jsonctx);
142
143/**
144 * @brief Remove the allocated working memory of the context.
145 *
Michal Vasko09e04632023-03-22 14:34:10 +0100146 * @param[in] jsonctx JSON parser context to clear.
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200147 */
148void lyjson_ctx_free(struct lyjson_ctx *jsonctx);
149
150#endif /* LY_JSON_H_ */