blob: 5981a1bc9e8e8e612d207619bac5fafb520ad82d [file] [log] [blame]
Radek Krejci13a57b62019-07-19 13:04:09 +02001/**
Michal Vasko90932a92020-02-12 14:33:03 +01002 * @file printer_json.c
Radek Krejci13a57b62019-07-19 13:04:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01004 * @brief JSON printer for libyang data structure
Radek Krejci13a57b62019-07-19 13:04:09 +02005 *
Radek Krejci5536d282020-08-04 23:27:44 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejci13a57b62019-07-19 13:04:09 +02007 *
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
Radek Krejci5536d282020-08-04 23:27:44 +020015#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010016#include <stdint.h>
Radek Krejci5536d282020-08-04 23:27:44 +020017#include <stdlib.h>
Radek Krejci5536d282020-08-04 23:27:44 +020018
Radek Krejci13a57b62019-07-19 13:04:09 +020019#include "common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010020#include "context.h"
Radek Krejci5536d282020-08-04 23:27:44 +020021#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010022#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "out_internal.h"
Radek Krejci5536d282020-08-04 23:27:44 +020024#include "parser_data.h"
25#include "plugins_types.h"
26#include "printer_data.h"
27#include "printer_internal.h"
28#include "set.h"
29#include "tree.h"
30#include "tree_data.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020031#include "tree_schema.h"
32
33/**
Radek Krejci5536d282020-08-04 23:27:44 +020034 * @brief JSON printer context.
35 */
36struct jsonpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020037 struct ly_out *out; /**< output specification */
Michal Vaskoaa22f422021-12-02 10:48:32 +010038 const struct lyd_node *root; /**< root node of the subtree being printed */
Michal Vasko26743a22022-03-29 14:48:10 +020039 const struct lyd_node *parent; /**< parent of the node being printed */
Radek Krejci1deb5be2020-08-26 16:43:36 +020040 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
41 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
42 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020043
Radek Krejci1deb5be2020-08-26 16:43:36 +020044 uint16_t level_printed; /* level where some data were already printed */
Michal Vaskoaa22f422021-12-02 10:48:32 +010045 struct ly_set open; /* currently open array(s) */
Radek Krejci5536d282020-08-04 23:27:44 +020046 const struct lyd_node *print_sibling_metadata;
47};
48
49/**
50 * @brief Mark that something was already written in the current level,
51 * used to decide if a comma is expected between the items
52 */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010053#define LEVEL_PRINTED pctx->level_printed = pctx->level
Radek Krejci5536d282020-08-04 23:27:44 +020054
55#define PRINT_COMMA \
Michal Vasko61ad1ff2022-02-10 15:48:39 +010056 if (pctx->level_printed >= pctx->level) { \
57 ly_print_(pctx->out, ",%s", (DO_FORMAT ? "\n" : "")); \
Radek Krejci5536d282020-08-04 23:27:44 +020058 }
59
Michal Vasko61ad1ff2022-02-10 15:48:39 +010060static LY_ERR json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node);
Radek Krejci5536d282020-08-04 23:27:44 +020061
62/**
Michal Vaskob2c61572022-05-20 10:35:33 +020063 * @brief Compare 2 nodes, despite it is regular data node or an opaq node, and
Radek Krejci5536d282020-08-04 23:27:44 +020064 * decide if they corresponds to the same schema node.
65 *
Radek Krejci5536d282020-08-04 23:27:44 +020066 * @return 1 - matching nodes, 0 - non-matching nodes
67 */
68static int
69matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
70{
71 assert(node1 || node2);
72
73 if (!node1 || !node2) {
74 return 0;
75 } else if (node1->schema != node2->schema) {
76 return 0;
77 }
78 if (!node1->schema) {
79 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020080 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
81 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +010082 if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) {
Radek Krejci5536d282020-08-04 23:27:44 +020083 return 0;
84 }
85 }
86
87 return 1;
88}
89
90/**
91 * @brief Open the JSON array ('[') for the specified @p node
92 *
93 * @param[in] ctx JSON printer context.
94 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020095 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020096 */
Michal Vaskob0099a92020-08-31 14:55:23 +020097static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +010098json_print_array_open(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +020099{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100100 ly_print_(pctx->out, "[%s", DO_FORMAT ? "\n" : "");
101 LY_CHECK_RET(ly_set_add(&pctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200102 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200103
104 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200105}
106
107/**
108 * @brief Get know if the array for the provided @p node is currently open.
109 *
110 * @param[in] ctx JSON printer context.
111 * @param[in] node Data node to check.
112 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
113 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
114 */
115static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100116is_open_array(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200117{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100118 if (pctx->open.count && matching_node(node, pctx->open.dnodes[pctx->open.count - 1])) {
Radek Krejci5536d282020-08-04 23:27:44 +0200119 return 1;
120 } else {
121 return 0;
122 }
123}
124
125/**
126 * @brief Close the most inner JSON array.
127 *
128 * @param[in] ctx JSON printer context.
129 */
130static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100131json_print_array_close(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200132{
Radek Krejci5536d282020-08-04 23:27:44 +0200133 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100134 ly_set_rm_index(&pctx->open, pctx->open.count - 1, NULL);
135 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200136}
137
138/**
139 * @brief Get the node's module name to use as the @p node prefix in JSON.
Radek Krejci31bc3f52021-04-26 11:09:58 +0200140 *
Radek Krejci5536d282020-08-04 23:27:44 +0200141 * @param[in] node Node to process.
142 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
143 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
144 */
145static const char *
146node_prefix(const struct lyd_node *node)
147{
148 if (node->schema) {
149 return node->schema->module->name;
150 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200151 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200152 const struct lys_module *mod;
153
154 switch (onode->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200155 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100156 return onode->name.module_name;
Radek Krejci8df109d2021-04-23 12:19:08 +0200157 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100158 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200159 if (!mod) {
160 return NULL;
161 }
162 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100163 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200164 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200165 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200166 }
167 }
168
169 return NULL;
170}
171
172/**
173 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
174 *
175 * Accepts both regulard a well as opaq nodes.
176 *
177 * @param[in] node1 The first node to compare.
178 * @param[in] node2 The second node to compare.
179 * @return 0 in case the nodes' modules are the same
180 * @return 1 in case the nodes belongs to different modules
181 */
182int
183json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
184{
185 assert(node1 || node2);
186
187 if (!node1 || !node2) {
188 return 1;
189 } else if (node1->schema && node2->schema) {
190 if (node1->schema->module == node2->schema->module) {
191 /* belongs to the same module */
192 return 0;
193 } else {
194 /* different modules */
195 return 1;
196 }
197 } else {
198 const char *pref1 = node_prefix(node1);
199 const char *pref2 = node_prefix(node2);
Michal Vasko69730152020-10-09 16:30:07 +0200200 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200201 return 0;
202 } else {
203 return 1;
204 }
205 }
206}
207
208/**
209 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
210 *
211 * @param[in] out The output handler.
212 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200213 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200214 */
Michal Vasko5233e962020-08-14 14:26:20 +0200215static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200216json_print_string(struct ly_out *out, const char *text)
217{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200218 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200219
220 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200221 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200222 }
223
Michal Vasko5233e962020-08-14 14:26:20 +0200224 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200225 for (i = n = 0; text[i]; i++) {
226 const unsigned char ascii = text[i];
227 if (ascii < 0x20) {
228 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200229 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200230 } else {
231 switch (ascii) {
232 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200233 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200234 break;
235 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200236 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200237 break;
238 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200239 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200240 n++;
241 }
242 }
243 }
Michal Vasko5233e962020-08-14 14:26:20 +0200244 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200245
Michal Vasko5233e962020-08-14 14:26:20 +0200246 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200247}
248
249/**
250 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
251 *
252 * @param[in] ctx JSON printer context.
253 * @param[in] node The data node being printed.
254 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
255 * @return LY_ERR value.
256 */
257static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100258json_print_member(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200259{
260 PRINT_COMMA;
Michal Vasko26743a22022-03-29 14:48:10 +0200261 if ((LEVEL == 1) || json_nscmp(node, pctx->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200262 /* print "namespace" */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263 ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200264 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200265 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100266 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200267 }
268
269 return LY_SUCCESS;
270}
271
272/**
273 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
274 *
275 * @param[in] ctx JSON printer context.
276 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
277 * @param[in] format Format to decide how to process the @p prefix.
Michal Vaskoad92b672020-11-12 13:11:31 +0100278 * @param[in] name Name structure to provide name and prefix to print. If NULL, only "" name is printed.
Radek Krejci5536d282020-08-04 23:27:44 +0200279 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
280 * @return LY_ERR value.
281 */
282static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100283json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VALUE_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100284 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200285{
Michal Vaskoad92b672020-11-12 13:11:31 +0100286 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200287
288 PRINT_COMMA;
289
290 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100291 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200292 const struct lys_module *mod;
293
294 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200295 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100296 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200297 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200298 case LY_VALUE_XML:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299 mod = ly_ctx_get_module_implemented_ns(pctx->ctx, name->module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200300 if (mod) {
301 module_name = mod->name;
302 }
303 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100304 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200305 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100306 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200307 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100308
309 name_str = name->name;
310 } else {
311 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200312 }
313
314 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200315 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100316 ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200317 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100318 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200319 }
320
321 return LY_SUCCESS;
322}
323
324/**
325 * @brief Print data value.
326 *
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100327 * @param[in] pctx JSON printer context.
328 * @param[in] ctx Context used to print the value.
Radek Krejci5536d282020-08-04 23:27:44 +0200329 * @param[in] val Data value to be printed.
330 * @return LY_ERR value.
331 */
332static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100333json_print_value(struct jsonpr_ctx *pctx, const struct ly_ctx *ctx, const struct lyd_value *val)
Radek Krejci5536d282020-08-04 23:27:44 +0200334{
aPiecek0f6bf3e2021-08-25 15:47:49 +0200335 ly_bool dynamic;
Michal Vasko183b9112021-11-04 16:02:24 +0100336 LY_DATA_TYPE basetype;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100337 const char *value = val->realtype->plugin->print(ctx, val, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200338
Michal Vasko183b9112021-11-04 16:02:24 +0100339 basetype = val->realtype->basetype;
340
341print_val:
Radek Krejci5536d282020-08-04 23:27:44 +0200342 /* leafref is not supported */
Michal Vasko183b9112021-11-04 16:02:24 +0100343 switch (basetype) {
344 case LY_TYPE_UNION:
345 /* use the resolved type */
346 basetype = val->subvalue->value.realtype->basetype;
347 goto print_val;
348
Radek Krejci5536d282020-08-04 23:27:44 +0200349 case LY_TYPE_BINARY:
350 case LY_TYPE_STRING:
351 case LY_TYPE_BITS:
352 case LY_TYPE_ENUM:
353 case LY_TYPE_INST:
354 case LY_TYPE_INT64:
355 case LY_TYPE_UINT64:
356 case LY_TYPE_DEC64:
357 case LY_TYPE_IDENT:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100358 json_print_string(pctx->out, value);
Radek Krejci5536d282020-08-04 23:27:44 +0200359 break;
360
361 case LY_TYPE_INT8:
362 case LY_TYPE_INT16:
363 case LY_TYPE_INT32:
364 case LY_TYPE_UINT8:
365 case LY_TYPE_UINT16:
366 case LY_TYPE_UINT32:
367 case LY_TYPE_BOOL:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100368 ly_print_(pctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200369 break;
370
371 case LY_TYPE_EMPTY:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100372 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200373 break;
374
375 default:
376 /* error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100377 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200378 }
379
380 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200381 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200382 }
383
384 return LY_SUCCESS;
385}
386
387/**
388 * @brief Print all the attributes of the opaq node.
389 *
390 * @param[in] ctx JSON printer context.
391 * @param[in] node Opaq node where the attributes are placed.
392 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
393 * @return LY_ERR value.
394 */
395static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100396json_print_attribute(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200397{
398 struct lyd_attr *attr;
399
400 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200401 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200402 LEVEL_PRINTED;
403 }
404
405 for (attr = node->attr; attr; attr = attr->next) {
406 PRINT_COMMA;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100407 json_print_member2(pctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200408
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200409 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100410 ly_print_(pctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200411 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100412 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200413 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100414 json_print_string(pctx->out, attr->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200415 }
416 LEVEL_PRINTED;
417 }
418
419 return LY_SUCCESS;
420}
421
422/**
423 * @brief Print all the metadata of the node.
424 *
425 * @param[in] ctx JSON printer context.
426 * @param[in] node Node where the metadata are placed.
427 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
428 * @return LY_ERR value.
429 */
430static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100431json_print_metadata(struct jsonpr_ctx *pctx, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200432{
433 struct lyd_meta *meta;
434
435 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200436 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200437 LEVEL_PRINTED;
438 }
439
440 for (meta = node->meta; meta; meta = meta->next) {
441 PRINT_COMMA;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100442 ly_print_(pctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
443 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &meta->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200444 LEVEL_PRINTED;
445 }
446
447 return LY_SUCCESS;
448}
449
450/**
451 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
452 *
453 * @param[in] ctx JSON printer context.
454 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200455 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200456 * @return LY_ERR value.
457 */
458static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100459json_print_attributes(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200460{
461 const struct lys_module *wdmod = NULL;
462
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100463 if ((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200464 /* we have implicit OR explicit default node */
465 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200466 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200467 }
468
Michal Vaskob68c3b42022-05-20 10:36:40 +0200469 if (node->schema && (node->meta || wdmod)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200470 if (inner) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100471 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200472 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100473 LY_CHECK_RET(json_print_member(pctx, node, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200474 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100475 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200476 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100477 LY_CHECK_RET(json_print_metadata(pctx, node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200478 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100479 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200480 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200481 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200482 if (inner) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100483 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200484 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100485 LY_CHECK_RET(json_print_member2(pctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100486 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200487 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100488 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200489 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100490 LY_CHECK_RET(json_print_attribute(pctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200491 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100492 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200493 LEVEL_PRINTED;
494 }
495
496 return LY_SUCCESS;
497}
498
499/**
500 * @brief Print leaf data node including its metadata.
501 *
502 * @param[in] ctx JSON printer context.
503 * @param[in] node Data node to print.
504 * @return LY_ERR value.
505 */
506static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100507json_print_leaf(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200508{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100509 LY_CHECK_RET(json_print_member(pctx, node, 0));
510 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200511 LEVEL_PRINTED;
512
513 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100514 json_print_attributes(pctx, node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200515
516 return LY_SUCCESS;
517}
518
519/**
Michal Vaskobbdadda2022-01-06 11:40:10 +0100520 * @brief Print anydata/anyxml content.
Radek Krejci5536d282020-08-04 23:27:44 +0200521 *
522 * @param[in] ctx JSON printer context.
523 * @param[in] any Anydata node to print.
524 * @return LY_ERR value.
525 */
526static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100527json_print_any_content(struct jsonpr_ctx *pctx, struct lyd_node_any *any)
Radek Krejci5536d282020-08-04 23:27:44 +0200528{
529 LY_ERR ret = LY_SUCCESS;
530 struct lyd_node *iter;
Michal Vasko26743a22022-03-29 14:48:10 +0200531 const struct lyd_node *prev_parent;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200532 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200533
Michal Vasko76096ec2022-02-24 16:06:16 +0100534 assert(any->schema->nodetype & LYD_NODE_ANY);
Radek Krejci5536d282020-08-04 23:27:44 +0200535
536 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200537 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200538
539 /* turn logging off */
540 prev_lo = ly_log_options(0);
541
542 /* try to parse it into a data tree */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100543 if (lyd_parse_data_mem(pctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
Radek Krejci5536d282020-08-04 23:27:44 +0200544 /* successfully parsed */
545 free(any->value.mem);
546 any->value.tree = iter;
547 any->value_type = LYD_ANYDATA_DATATREE;
548 }
549
550 /* turn loggin on again */
551 ly_log_options(prev_lo);
552 }
553
554 switch (any->value_type) {
555 case LYD_ANYDATA_DATATREE:
Michal Vasko76096ec2022-02-24 16:06:16 +0100556 /* print as an object */
557 ly_print_(pctx->out, "{%s", DO_FORMAT ? "\n" : "");
558 LEVEL_INC;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100559
Radek Krejci5536d282020-08-04 23:27:44 +0200560 /* close opening tag and print data */
Michal Vasko26743a22022-03-29 14:48:10 +0200561 prev_parent = pctx->parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100562 prev_opts = pctx->options;
Michal Vasko26743a22022-03-29 14:48:10 +0200563 pctx->parent = &any->node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100564 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci5536d282020-08-04 23:27:44 +0200565 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100566 ret = json_print_node(pctx, iter);
Radek Krejci5536d282020-08-04 23:27:44 +0200567 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
568 }
Michal Vasko26743a22022-03-29 14:48:10 +0200569 pctx->parent = prev_parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100570 pctx->options = prev_opts;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100571
Michal Vasko76096ec2022-02-24 16:06:16 +0100572 /* terminate the object */
Michal Vasko23b51a82022-03-29 14:22:34 +0200573 LEVEL_DEC;
Michal Vasko76096ec2022-02-24 16:06:16 +0100574 if (DO_FORMAT) {
575 ly_print_(pctx->out, "\n%*s}", INDENT);
576 } else {
577 ly_print_(pctx->out, "}");
Michal Vaskobbdadda2022-01-06 11:40:10 +0100578 }
Radek Krejci5536d282020-08-04 23:27:44 +0200579 break;
580 case LYD_ANYDATA_JSON:
Michal Vasko76096ec2022-02-24 16:06:16 +0100581 if (!any->value.json) {
582 /* no content */
583 if (any->schema->nodetype == LYS_ANYXML) {
584 ly_print_(pctx->out, "null");
585 } else {
586 ly_print_(pctx->out, "{}");
587 }
588 } else {
589 /* print without escaping special characters */
590 ly_print_(pctx->out, "%s", any->value.json);
Radek Krejci5536d282020-08-04 23:27:44 +0200591 }
Radek Krejci5536d282020-08-04 23:27:44 +0200592 break;
593 case LYD_ANYDATA_STRING:
594 case LYD_ANYDATA_XML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100595 if (!any->value.str) {
596 /* no content */
597 if (any->schema->nodetype == LYS_ANYXML) {
598 ly_print_(pctx->out, "null");
599 } else {
600 ly_print_(pctx->out, "{}");
601 }
602 } else {
Michal Vaskobbdadda2022-01-06 11:40:10 +0100603 /* print as a string */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100604 ly_print_(pctx->out, "\"%s\"", any->value.str);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100605 }
Michal Vasko76096ec2022-02-24 16:06:16 +0100606 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200607 case LYD_ANYDATA_LYB:
Michal Vasko76096ec2022-02-24 16:06:16 +0100608 /* LYB format is not supported */
609 LOGWRN(pctx->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100610 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200611 }
612
613 return LY_SUCCESS;
614}
615
616/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100617 * @brief Print content of a single container/list data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100618 * The envelope specific to nodes are expected to be printed by the caller.
Radek Krejci5536d282020-08-04 23:27:44 +0200619 *
620 * @param[in] ctx JSON printer context.
621 * @param[in] node Data node to print.
622 * @return LY_ERR value.
623 */
624static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100625json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200626{
627 struct lyd_node *child;
Michal Vasko26743a22022-03-29 14:48:10 +0200628 const struct lyd_node *prev_parent;
Michal Vasko23b51a82022-03-29 14:22:34 +0200629 struct lyd_node_opaq *opaq = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200630 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200631
Michal Vasko630d9892020-12-08 17:11:08 +0100632 LY_LIST_FOR(lyd_child(node), child) {
Michal Vasko8db584d2022-03-30 13:42:48 +0200633 if (lyd_node_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100634 break;
635 }
636 }
637 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200638 has_content = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200639 }
Michal Vasko23b51a82022-03-29 14:22:34 +0200640 if (!node->schema) {
641 opaq = (struct lyd_node_opaq *)node;
642 }
Radek Krejci5536d282020-08-04 23:27:44 +0200643
Michal Vasko1a85d332021-08-27 10:35:28 +0200644 if ((node->schema && (node->schema->nodetype == LYS_LIST)) ||
Michal Vasko23b51a82022-03-29 14:22:34 +0200645 (opaq && (opaq->hints != LYD_HINT_DATA) && (opaq->hints & LYD_NODEHINT_LIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100646 ly_print_(pctx->out, "%s%*s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ?
Michal Vasko1a85d332021-08-27 10:35:28 +0200647 (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : "");
648 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100649 ly_print_(pctx->out, "%s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200650 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200651 }
652 LEVEL_INC;
653
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100654 json_print_attributes(pctx, node, 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200655
Michal Vasko76096ec2022-02-24 16:06:16 +0100656 /* print children */
Michal Vasko26743a22022-03-29 14:48:10 +0200657 prev_parent = pctx->parent;
658 pctx->parent = node;
Michal Vasko76096ec2022-02-24 16:06:16 +0100659 LY_LIST_FOR(lyd_child(node), child) {
660 LY_CHECK_RET(json_print_node(pctx, child));
Radek Krejci5536d282020-08-04 23:27:44 +0200661 }
Michal Vasko26743a22022-03-29 14:48:10 +0200662 pctx->parent = prev_parent;
Radek Krejci5536d282020-08-04 23:27:44 +0200663
Radek Krejci5536d282020-08-04 23:27:44 +0200664 LEVEL_DEC;
665 if (DO_FORMAT && has_content) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100666 ly_print_(pctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200667 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100668 ly_print_(pctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200669 }
670 LEVEL_PRINTED;
671
672 return LY_SUCCESS;
673}
674
675/**
676 * @brief Print container data node including its metadata.
677 *
678 * @param[in] ctx JSON printer context.
679 * @param[in] node Data node to print.
680 * @return LY_ERR value.
681 */
682static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100683json_print_container(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200684{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100685 LY_CHECK_RET(json_print_member(pctx, node, 0));
686 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200687
688 return LY_SUCCESS;
689}
690
691/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100692 * @brief Print anydata/anyxml data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100693 *
694 * @param[in] ctx JSON printer context.
695 * @param[in] node Data node to print.
696 * @return LY_ERR value.
697 */
698static int
Michal Vasko76096ec2022-02-24 16:06:16 +0100699json_print_any(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskobbdadda2022-01-06 11:40:10 +0100700{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100701 LY_CHECK_RET(json_print_member(pctx, node, 0));
702 LY_CHECK_RET(json_print_any_content(pctx, (struct lyd_node_any *)node));
Michal Vaskobbdadda2022-01-06 11:40:10 +0100703 LEVEL_PRINTED;
704
705 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100706 json_print_attributes(pctx, node, 0);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100707
708 return LY_SUCCESS;
709}
710
711/**
Michal Vaskoaa22f422021-12-02 10:48:32 +0100712 * @brief Check whether a node is the last printed instance of a (leaf-)list.
713 *
714 * @param[in] ctx JSON printer context.
715 * @param[in] node Last printed node.
716 * @return Whether it is the last printed instance or not.
717 */
718static ly_bool
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100719json_print_array_is_last_inst(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskoaa22f422021-12-02 10:48:32 +0100720{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100721 if (!is_open_array(pctx, node)) {
Michal Vasko06217f32021-12-09 09:25:50 +0100722 /* no array open */
723 return 0;
724 }
725
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100726 if ((pctx->root == node) && !(pctx->options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100727 /* the only printed instance */
728 return 1;
729 }
730
Michal Vasko06217f32021-12-09 09:25:50 +0100731 if (!node->next || (node->next->schema != node->schema)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100732 /* last instance */
733 return 1;
734 }
735
736 return 0;
737}
738
739/**
Radek Krejci5536d282020-08-04 23:27:44 +0200740 * @brief Print single leaf-list or list instance.
741 *
742 * In case of list, metadata are printed inside the list object. For the leaf-list,
743 * metadata are marked in the context for later printing after closing the array next to it using
744 * json_print_metadata_leaflist().
745 *
746 * @param[in] ctx JSON printer context.
747 * @param[in] node Data node to print.
748 * @return LY_ERR value.
749 */
750static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100751json_print_leaf_list(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200752{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100753 if (!is_open_array(pctx, node)) {
754 LY_CHECK_RET(json_print_member(pctx, node, 0));
755 LY_CHECK_RET(json_print_array_open(pctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200756 if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100757 ly_print_(pctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200758 }
Radek Krejci5536d282020-08-04 23:27:44 +0200759 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100760 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200761 }
762
763 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200764 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200765 /* empty, e.g. in case of filter */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100766 ly_print_(pctx->out, "%s%snull", (pctx->level_printed >= pctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200767 LEVEL_PRINTED;
768 } else {
769 /* print list's content */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100770 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200771 }
772 } else {
773 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100774 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200775
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100776 if (node->meta && !pctx->print_sibling_metadata) {
777 pctx->print_sibling_metadata = node;
Radek Krejci5536d282020-08-04 23:27:44 +0200778 }
779 }
780
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100781 if (json_print_array_is_last_inst(pctx, node)) {
782 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200783 }
784
785 return LY_SUCCESS;
786}
787
788/**
789 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
790 * This function is supposed to be called when the leaf-list array is closed.
791 *
792 * @param[in] ctx JSON printer context.
793 * @return LY_ERR value.
794 */
795static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100796json_print_metadata_leaflist(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200797{
798 const struct lyd_node *prev, *node, *iter;
799
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100800 if (!pctx->print_sibling_metadata) {
Radek Krejci5536d282020-08-04 23:27:44 +0200801 return LY_SUCCESS;
802 }
803
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100804 for (node = pctx->print_sibling_metadata, prev = pctx->print_sibling_metadata->prev;
Radek Krejci5536d282020-08-04 23:27:44 +0200805 prev->next && matching_node(node, prev);
806 node = prev, prev = node->prev) {}
807
808 /* node is the first instance of the leaf-list */
809
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100810 LY_CHECK_RET(json_print_member(pctx, node, 1));
811 ly_print_(pctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200812 LEVEL_INC;
813 LY_LIST_FOR(node, iter) {
814 PRINT_COMMA;
815 if (iter->meta) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100816 ly_print_(pctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200817 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100818 LY_CHECK_RET(json_print_metadata(pctx, iter, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200819 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100820 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200821 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100822 ly_print_(pctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200823 }
824 LEVEL_PRINTED;
825 if (!matching_node(iter, iter->next)) {
826 break;
827 }
828 }
829 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100830 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200831 LEVEL_PRINTED;
832
833 return LY_SUCCESS;
834}
835
836/**
837 * @brief Print opaq data node including its attributes.
838 *
839 * @param[in] ctx JSON printer context.
840 * @param[in] node Opaq node to print.
841 * @return LY_ERR value.
842 */
843static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100844json_print_opaq(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200845{
Radek Krejci857189e2020-09-01 13:26:36 +0200846 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200847
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200848 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100849 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200850 first = 0;
851 }
Michal Vasko9e685082021-01-29 14:49:09 +0100852 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200853 last = 0;
854 }
855 }
856
857 if (first) {
Michal Vasko26743a22022-03-29 14:48:10 +0200858 LY_CHECK_RET(json_print_member2(pctx, pctx->parent, node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200859
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200860 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100861 LY_CHECK_RET(json_print_array_open(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200862 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200863 if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100864 ly_print_(pctx->out, "%*s", INDENT);
Michal Vasko1a85d332021-08-27 10:35:28 +0200865 }
866 } else if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100867 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200868 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200869 if (node->child || (node->hints & LYD_NODEHINT_LIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100870 LY_CHECK_RET(json_print_inner(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200871 LEVEL_PRINTED;
872 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200873 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100874 ly_print_(pctx->out, "[null]");
Michal Vaskocea58712022-04-01 14:37:08 +0200875 } else if ((node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) && !(node->hints & LYD_VALHINT_NUM64)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100876 ly_print_(pctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200877 } else {
Michal Vaskocea58712022-04-01 14:37:08 +0200878 /* string or a large number */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100879 ly_print_(pctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200880 }
881 LEVEL_PRINTED;
882
883 /* attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100884 json_print_attributes(pctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200885
886 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200887 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100888 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200889 LEVEL_PRINTED;
890 }
891
892 return LY_SUCCESS;
893}
894
895/**
896 * @brief Print all the types of data node including its metadata.
897 *
898 * @param[in] ctx JSON printer context.
899 * @param[in] node Data node to print.
900 * @return LY_ERR value.
901 */
902static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100903json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200904{
Michal Vasko8db584d2022-03-30 13:42:48 +0200905 if (!lyd_node_should_print(node, pctx->options)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100906 if (json_print_array_is_last_inst(pctx, node)) {
907 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200908 }
909 return LY_SUCCESS;
910 }
911
912 if (!node->schema) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100913 LY_CHECK_RET(json_print_opaq(pctx, (const struct lyd_node_opaq *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200914 } else {
915 switch (node->schema->nodetype) {
916 case LYS_RPC:
917 case LYS_ACTION:
918 case LYS_NOTIF:
919 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100920 LY_CHECK_RET(json_print_container(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200921 break;
922 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100923 LY_CHECK_RET(json_print_leaf(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200924 break;
925 case LYS_LEAFLIST:
926 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100927 LY_CHECK_RET(json_print_leaf_list(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200928 break;
Michal Vasko76096ec2022-02-24 16:06:16 +0100929 case LYS_ANYDATA:
Radek Krejci5536d282020-08-04 23:27:44 +0200930 case LYS_ANYXML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100931 LY_CHECK_RET(json_print_any(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200932 break;
933 default:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100934 LOGINT(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200935 return EXIT_FAILURE;
936 }
937 }
938
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100939 pctx->level_printed = pctx->level;
Radek Krejci5536d282020-08-04 23:27:44 +0200940
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100941 if (pctx->print_sibling_metadata && !matching_node(node->next, pctx->print_sibling_metadata)) {
942 json_print_metadata_leaflist(pctx);
943 pctx->print_sibling_metadata = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200944 }
945
946 return LY_SUCCESS;
947}
948
949LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200950json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200951{
952 const struct lyd_node *node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100953 struct jsonpr_ctx pctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200954 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200955
Radek Krejci2e874772020-08-28 16:36:33 +0200956 if (!root) {
957 ly_print_(out, "{}%s", delimiter);
958 ly_print_flush(out);
959 return LY_SUCCESS;
960 }
961
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100962 pctx.out = out;
Michal Vasko26743a22022-03-29 14:48:10 +0200963 pctx.parent = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100964 pctx.level = 1;
965 pctx.level_printed = 0;
966 pctx.options = options;
967 pctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200968
969 /* start */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100970 ly_print_(pctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200971
972 /* content */
973 LY_LIST_FOR(root, node) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100974 pctx.root = node;
975 LY_CHECK_RET(json_print_node(&pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200976 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
977 break;
978 }
979 }
980
981 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200982 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200983
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100984 assert(!pctx.open.count);
985 ly_set_erase(&pctx.open, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200986
987 ly_print_flush(out);
988 return LY_SUCCESS;
989}