blob: c0d6ab6ca0ac5fcd1709477c2c712a17bd0333b4 [file] [log] [blame]
Radek Krejci50f0c6b2020-06-18 16:31:48 +02001/**
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
24struct ly_ctx;
Radek Krejci47fab892020-11-05 17:02:41 +010025struct ly_in;
Radek Krejci50f0c6b2020-06-18 16:31:48 +020026
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 Vaskoc08b9d22022-02-10 15:48:39 +010033/* 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 Krejci50f0c6b2020-06-18 16:31:48 +020041/**
42 * @brief Status of the parser providing information what is expected next (which function is supposed to be called).
43 */
44enum 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 */
49 LYJSON_OBJECT_EMPTY, /* empty JSON object { }*/
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
61struct lyjson_ctx {
Radek Krejci1798aae2020-07-14 13:26:06 +020062 const struct ly_ctx *ctx;
Radek Krejci1798aae2020-07-14 13:26:06 +020063 struct ly_in *in; /* input structure */
Radek Krejci50f0c6b2020-06-18 16:31:48 +020064
Radek Krejci84d7fd72021-07-14 18:32:21 +020065 struct ly_set status; /* stack of ::LYJSON_PARSER_STATUS values corresponding to the JSON items being processed */
Radek Krejci50f0c6b2020-06-18 16:31:48 +020066
Radek Krejci84d7fd72021-07-14 18:32:21 +020067 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 */
aPiecek93582ed2021-05-25 14:49:06 +020070 uint32_t depth; /* current number of nested blocks, see ::LY_MAX_BLOCK_DEPTH */
Radek Krejci50f0c6b2020-06-18 16:31:48 +020071
72 struct {
73 enum LYJSON_PARSER_STATUS status;
74 uint32_t status_count;
75 const char *value;
76 size_t value_len;
Radek Krejci857189e2020-09-01 13:26:36 +020077 ly_bool dynamic;
aPiecek93582ed2021-05-25 14:49:06 +020078 uint32_t depth;
Radek Krejci50f0c6b2020-06-18 16:31:48 +020079 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 Vaskoc08b9d22022-02-10 15:48:39 +010088 * @param[in] subtree Whether this is a special case of parsing a subtree (starting with object name).
Radek Krejci84d7fd72021-07-14 18:32:21 +020089 * @param[out] jsonctx New JSON context with status referring the parsed value.
Radek Krejci50f0c6b2020-06-18 16:31:48 +020090 * @return LY_ERR value.
91 */
Michal Vaskoc08b9d22022-02-10 15:48:39 +010092LY_ERR lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, ly_bool subtree, struct lyjson_ctx **jsonctx);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020093
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 Krejci84d7fd72021-07-14 18:32:21 +020099 * @return ::LYJSON_ERROR in case of invalid index, other ::LYJSON_PARSER_STATUS corresponding to the token.
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200100 */
101enum 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 Vasko22df3f02020-08-24 13:29:22 +0200109const char *lyjson_token2str(enum LYJSON_PARSER_STATUS status);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200110
111/**
Radek Krejci84d7fd72021-07-14 18:32:21 +0200112 * @brief Move to the next JSON artifact and update parser status.
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200113 *
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 */
118LY_ERR lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status);
119
120/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200121 * @brief Backup the JSON parser context's state To restore the backup, use ::lyjson_ctx_restore().
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200122 * @param[in] jsonctx JSON parser context to backup.
123 */
124void lyjson_ctx_backup(struct lyjson_ctx *jsonctx);
125
126/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200127 * @brief REstore the JSON parser context's state from the backup created by ::lyjson_ctx_backup().
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200128 * @param[in] jsonctx JSON parser context to restore.
129 */
130void 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 */
137void lyjson_ctx_free(struct lyjson_ctx *jsonctx);
138
139#endif /* LY_JSON_H_ */