blob: 29ca0e7c56d4f2e38e7a66b969afcf4fd769b4e0 [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 Vasko57c8d432022-11-30 15:35:56 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01005 * @brief JSON printer for libyang data structure
Radek Krejci13a57b62019-07-19 13:04:09 +02006 *
Michal Vaskoa4e5eb42023-09-18 08:44:21 +02007 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
Radek Krejci13a57b62019-07-19 13:04:09 +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
Radek Krejci5536d282020-08-04 23:27:44 +020016#include <assert.h>
Michal Vasko7730e302023-05-25 10:01:19 +020017#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010018#include <stdint.h>
Radek Krejci5536d282020-08-04 23:27:44 +020019#include <stdlib.h>
Radek Krejci5536d282020-08-04 23:27:44 +020020
Radek Krejci47fab892020-11-05 17:02:41 +010021#include "context.h"
Radek Krejci5536d282020-08-04 23:27:44 +020022#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010023#include "ly_common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "out_internal.h"
Radek Krejci5536d282020-08-04 23:27:44 +020026#include "parser_data.h"
Michal Vaskob4750962022-10-06 15:33:35 +020027#include "plugins_exts/metadata.h"
Radek Krejci5536d282020-08-04 23:27:44 +020028#include "plugins_types.h"
29#include "printer_data.h"
30#include "printer_internal.h"
31#include "set.h"
32#include "tree.h"
33#include "tree_data.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020034#include "tree_schema.h"
35
36/**
Radek Krejci5536d282020-08-04 23:27:44 +020037 * @brief JSON printer context.
38 */
39struct jsonpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020040 struct ly_out *out; /**< output specification */
Michal Vaskoaa22f422021-12-02 10:48:32 +010041 const struct lyd_node *root; /**< root node of the subtree being printed */
Michal Vasko26743a22022-03-29 14:48:10 +020042 const struct lyd_node *parent; /**< parent of the node being printed */
Radek Krejci1deb5be2020-08-26 16:43:36 +020043 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
44 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
45 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020046
Radek Krejci1deb5be2020-08-26 16:43:36 +020047 uint16_t level_printed; /* level where some data were already printed */
Michal Vaskoaa22f422021-12-02 10:48:32 +010048 struct ly_set open; /* currently open array(s) */
Michal Vaskoa4e5eb42023-09-18 08:44:21 +020049 const struct lyd_node *first_leaflist; /**< first printed leaf-list instance, used when printing its metadata/attributes */
Radek Krejci5536d282020-08-04 23:27:44 +020050};
51
52/**
53 * @brief Mark that something was already written in the current level,
54 * used to decide if a comma is expected between the items
55 */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010056#define LEVEL_PRINTED pctx->level_printed = pctx->level
Radek Krejci5536d282020-08-04 23:27:44 +020057
58#define PRINT_COMMA \
Michal Vasko61ad1ff2022-02-10 15:48:39 +010059 if (pctx->level_printed >= pctx->level) { \
60 ly_print_(pctx->out, ",%s", (DO_FORMAT ? "\n" : "")); \
Radek Krejci5536d282020-08-04 23:27:44 +020061 }
62
Michal Vasko61ad1ff2022-02-10 15:48:39 +010063static LY_ERR json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node);
Radek Krejci5536d282020-08-04 23:27:44 +020064
65/**
Michal Vaskob2c61572022-05-20 10:35:33 +020066 * @brief Compare 2 nodes, despite it is regular data node or an opaq node, and
Radek Krejci5536d282020-08-04 23:27:44 +020067 * decide if they corresponds to the same schema node.
68 *
Radek Krejci5536d282020-08-04 23:27:44 +020069 * @return 1 - matching nodes, 0 - non-matching nodes
70 */
71static int
72matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
73{
74 assert(node1 || node2);
75
76 if (!node1 || !node2) {
77 return 0;
78 } else if (node1->schema != node2->schema) {
79 return 0;
80 }
81 if (!node1->schema) {
82 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020083 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
84 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vasko26bbb272022-08-02 14:54:33 +020085
Michal Vaskoad92b672020-11-12 13:11:31 +010086 if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) {
Radek Krejci5536d282020-08-04 23:27:44 +020087 return 0;
88 }
89 }
90
91 return 1;
92}
93
94/**
95 * @brief Open the JSON array ('[') for the specified @p node
96 *
97 * @param[in] ctx JSON printer context.
98 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020099 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200100 */
Michal Vaskob0099a92020-08-31 14:55:23 +0200101static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100102json_print_array_open(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200103{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100104 ly_print_(pctx->out, "[%s", DO_FORMAT ? "\n" : "");
105 LY_CHECK_RET(ly_set_add(&pctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200106 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200107
108 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200109}
110
111/**
112 * @brief Get know if the array for the provided @p node is currently open.
113 *
114 * @param[in] ctx JSON printer context.
115 * @param[in] node Data node to check.
116 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
117 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
118 */
119static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100120is_open_array(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200121{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100122 if (pctx->open.count && matching_node(node, pctx->open.dnodes[pctx->open.count - 1])) {
Radek Krejci5536d282020-08-04 23:27:44 +0200123 return 1;
124 } else {
125 return 0;
126 }
127}
128
129/**
130 * @brief Close the most inner JSON array.
131 *
132 * @param[in] ctx JSON printer context.
133 */
134static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100135json_print_array_close(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200136{
Radek Krejci5536d282020-08-04 23:27:44 +0200137 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100138 ly_set_rm_index(&pctx->open, pctx->open.count - 1, NULL);
139 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200140}
141
142/**
143 * @brief Get the node's module name to use as the @p node prefix in JSON.
Radek Krejci31bc3f52021-04-26 11:09:58 +0200144 *
Radek Krejci5536d282020-08-04 23:27:44 +0200145 * @param[in] node Node to process.
146 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
147 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
148 */
149static const char *
150node_prefix(const struct lyd_node *node)
151{
152 if (node->schema) {
153 return node->schema->module->name;
154 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200155 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200156 const struct lys_module *mod;
157
158 switch (onode->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200159 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100160 return onode->name.module_name;
Radek Krejci8df109d2021-04-23 12:19:08 +0200161 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100162 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200163 if (!mod) {
164 return NULL;
165 }
166 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100167 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200168 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200169 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200170 }
171 }
172
173 return NULL;
174}
175
176/**
177 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
178 *
179 * Accepts both regulard a well as opaq nodes.
180 *
181 * @param[in] node1 The first node to compare.
182 * @param[in] node2 The second node to compare.
183 * @return 0 in case the nodes' modules are the same
184 * @return 1 in case the nodes belongs to different modules
185 */
186int
187json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
188{
189 assert(node1 || node2);
190
191 if (!node1 || !node2) {
192 return 1;
193 } else if (node1->schema && node2->schema) {
194 if (node1->schema->module == node2->schema->module) {
195 /* belongs to the same module */
196 return 0;
197 } else {
198 /* different modules */
199 return 1;
200 }
201 } else {
202 const char *pref1 = node_prefix(node1);
203 const char *pref2 = node_prefix(node2);
Michal Vasko26bbb272022-08-02 14:54:33 +0200204
Michal Vasko69730152020-10-09 16:30:07 +0200205 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200206 return 0;
207 } else {
208 return 1;
209 }
210 }
211}
212
213/**
214 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
215 *
216 * @param[in] out The output handler.
217 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200218 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200219 */
Michal Vasko5233e962020-08-14 14:26:20 +0200220static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200221json_print_string(struct ly_out *out, const char *text)
222{
Michal Vasko7730e302023-05-25 10:01:19 +0200223 uint64_t i;
Radek Krejci5536d282020-08-04 23:27:44 +0200224
225 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200226 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200227 }
228
Michal Vasko5233e962020-08-14 14:26:20 +0200229 ly_write_(out, "\"", 1);
Michal Vasko7730e302023-05-25 10:01:19 +0200230 for (i = 0; text[i]; i++) {
231 const unsigned char byte = text[i];
Michal Vasko26bbb272022-08-02 14:54:33 +0200232
Michal Vasko7730e302023-05-25 10:01:19 +0200233 switch (byte) {
234 case '"':
235 ly_print_(out, "\\\"");
236 break;
237 case '\\':
238 ly_print_(out, "\\\\");
239 break;
240 case '\r':
241 ly_print_(out, "\\r");
242 break;
243 case '\t':
244 ly_print_(out, "\\t");
245 break;
246 default:
247 if (iscntrl(byte)) {
248 /* control character */
249 ly_print_(out, "\\u%.4X", byte);
250 } else {
251 /* printable character (even non-ASCII UTF8) */
Michal Vasko5233e962020-08-14 14:26:20 +0200252 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200253 }
Michal Vasko7730e302023-05-25 10:01:19 +0200254 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200255 }
256 }
Michal Vasko5233e962020-08-14 14:26:20 +0200257 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200258
Michal Vasko5233e962020-08-14 14:26:20 +0200259 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200260}
261
262/**
263 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
264 *
265 * @param[in] ctx JSON printer context.
266 * @param[in] node The data node being printed.
267 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
268 * @return LY_ERR value.
269 */
270static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100271json_print_member(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200272{
273 PRINT_COMMA;
Michal Vasko26743a22022-03-29 14:48:10 +0200274 if ((LEVEL == 1) || json_nscmp(node, pctx->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200275 /* print "namespace" */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100276 ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200277 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200278 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100279 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200280 }
281
282 return LY_SUCCESS;
283}
284
285/**
286 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
287 *
288 * @param[in] ctx JSON printer context.
289 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
290 * @param[in] format Format to decide how to process the @p prefix.
Michal Vaskoad92b672020-11-12 13:11:31 +0100291 * @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 +0200292 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
293 * @return LY_ERR value.
294 */
295static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100296json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VALUE_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100297 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200298{
Michal Vaskoad92b672020-11-12 13:11:31 +0100299 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200300
301 PRINT_COMMA;
302
303 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100304 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200305 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200306 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100307 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200308 break;
Michal Vasko89762652023-02-09 13:58:37 +0100309 case LY_VALUE_XML: {
310 const struct lys_module *mod = NULL;
311
312 if (name->module_ns) {
313 mod = ly_ctx_get_module_implemented_ns(pctx->ctx, name->module_ns);
314 }
Radek Krejci5536d282020-08-04 23:27:44 +0200315 if (mod) {
316 module_name = mod->name;
317 }
318 break;
Michal Vasko89762652023-02-09 13:58:37 +0100319 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100320 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200321 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100322 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200323 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100324
325 name_str = name->name;
326 } else {
327 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200328 }
329
330 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200331 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100332 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 +0200333 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100334 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200335 }
336
337 return LY_SUCCESS;
338}
339
340/**
341 * @brief Print data value.
342 *
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100343 * @param[in] pctx JSON printer context.
344 * @param[in] ctx Context used to print the value.
Radek Krejci5536d282020-08-04 23:27:44 +0200345 * @param[in] val Data value to be printed.
Michal Vasko32f067f2023-06-01 12:31:14 +0200346 * @param[in] local_mod Module of the current node.
Radek Krejci5536d282020-08-04 23:27:44 +0200347 * @return LY_ERR value.
348 */
349static LY_ERR
Michal Vasko32f067f2023-06-01 12:31:14 +0200350json_print_value(struct jsonpr_ctx *pctx, const struct ly_ctx *ctx, const struct lyd_value *val,
351 const struct lys_module *local_mod)
Radek Krejci5536d282020-08-04 23:27:44 +0200352{
aPiecek0f6bf3e2021-08-25 15:47:49 +0200353 ly_bool dynamic;
Michal Vasko183b9112021-11-04 16:02:24 +0100354 LY_DATA_TYPE basetype;
Michal Vasko32f067f2023-06-01 12:31:14 +0200355 const char *value;
Radek Krejci5536d282020-08-04 23:27:44 +0200356
Michal Vasko32f067f2023-06-01 12:31:14 +0200357 value = val->realtype->plugin->print(ctx, val, LY_VALUE_JSON, (void *)local_mod, &dynamic, NULL);
358 LY_CHECK_RET(!value, LY_EINVAL);
Michal Vasko183b9112021-11-04 16:02:24 +0100359 basetype = val->realtype->basetype;
360
361print_val:
Radek Krejci5536d282020-08-04 23:27:44 +0200362 /* leafref is not supported */
Michal Vasko183b9112021-11-04 16:02:24 +0100363 switch (basetype) {
364 case LY_TYPE_UNION:
365 /* use the resolved type */
Ilyes Ben Hamouda0ee30a82023-09-07 08:46:33 +0200366 val = &val->subvalue->value;
367 basetype = val->realtype->basetype;
Michal Vasko183b9112021-11-04 16:02:24 +0100368 goto print_val;
369
Radek Krejci5536d282020-08-04 23:27:44 +0200370 case LY_TYPE_BINARY:
371 case LY_TYPE_STRING:
372 case LY_TYPE_BITS:
373 case LY_TYPE_ENUM:
374 case LY_TYPE_INST:
375 case LY_TYPE_INT64:
376 case LY_TYPE_UINT64:
377 case LY_TYPE_DEC64:
378 case LY_TYPE_IDENT:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100379 json_print_string(pctx->out, value);
Radek Krejci5536d282020-08-04 23:27:44 +0200380 break;
381
382 case LY_TYPE_INT8:
383 case LY_TYPE_INT16:
384 case LY_TYPE_INT32:
385 case LY_TYPE_UINT8:
386 case LY_TYPE_UINT16:
387 case LY_TYPE_UINT32:
388 case LY_TYPE_BOOL:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100389 ly_print_(pctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200390 break;
391
392 case LY_TYPE_EMPTY:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100393 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200394 break;
395
396 default:
397 /* error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100398 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200399 }
400
401 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200402 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200403 }
404
405 return LY_SUCCESS;
406}
407
408/**
409 * @brief Print all the attributes of the opaq node.
410 *
411 * @param[in] ctx JSON printer context.
412 * @param[in] node Opaq node where the attributes are placed.
Radek Krejci5536d282020-08-04 23:27:44 +0200413 * @return LY_ERR value.
414 */
415static LY_ERR
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200416json_print_attribute(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200417{
418 struct lyd_attr *attr;
419
Radek Krejci5536d282020-08-04 23:27:44 +0200420 for (attr = node->attr; attr; attr = attr->next) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100421 json_print_member2(pctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200422
Michal Vasko69bb5fe2022-12-01 13:52:21 +0100423 if (attr->hints & (LYD_VALHINT_STRING | LYD_VALHINT_OCTNUM | LYD_VALHINT_HEXNUM | LYD_VALHINT_NUM64)) {
424 json_print_string(pctx->out, attr->value);
425 } else if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100426 ly_print_(pctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200427 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100428 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200429 } else {
Michal Vasko69bb5fe2022-12-01 13:52:21 +0100430 /* unknown value format with no hints, use universal string */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100431 json_print_string(pctx->out, attr->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200432 }
433 LEVEL_PRINTED;
434 }
435
436 return LY_SUCCESS;
437}
438
439/**
440 * @brief Print all the metadata of the node.
441 *
442 * @param[in] ctx JSON printer context.
443 * @param[in] node Node where the metadata are placed.
444 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
445 * @return LY_ERR value.
446 */
447static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100448json_print_metadata(struct jsonpr_ctx *pctx, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200449{
450 struct lyd_meta *meta;
451
452 if (wdmod) {
aPiecekf312c8e2023-05-11 09:09:39 +0200453 ly_print_(pctx->out, "%*s\"%s:default\":%strue", INDENT, wdmod->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200454 LEVEL_PRINTED;
455 }
456
457 for (meta = node->meta; meta; meta = meta->next) {
aPiecek6cf1d162023-11-08 16:07:00 +0100458 if (!lyd_metadata_should_print(meta)) {
459 continue;
460 }
Radek Krejci5536d282020-08-04 23:27:44 +0200461 PRINT_COMMA;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100462 ly_print_(pctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Michal Vasko32f067f2023-06-01 12:31:14 +0200463 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &meta->value, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200464 LEVEL_PRINTED;
465 }
466
467 return LY_SUCCESS;
468}
469
470/**
aPiecek6cf1d162023-11-08 16:07:00 +0100471 * @brief Check if a value can be printed for at least one metadata.
472 *
473 * @param[in] node Node to check.
474 * @return 1 if node has printable meta otherwise 0.
475 */
476static ly_bool
477node_has_printable_meta(const struct lyd_node *node)
478{
479 struct lyd_meta *iter;
480
481 if (!node->meta) {
482 return 0;
483 }
484
485 LY_LIST_FOR(node->meta, iter) {
486 if (lyd_metadata_should_print(iter)) {
487 return 1;
488 }
489 }
490
491 return 0;
492}
493
494/**
Radek Krejci5536d282020-08-04 23:27:44 +0200495 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
496 *
497 * @param[in] ctx JSON printer context.
498 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200499 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200500 * @return LY_ERR value.
501 */
502static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100503json_print_attributes(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200504{
505 const struct lys_module *wdmod = NULL;
506
Igor Ryzhovd73f43b2024-01-26 01:13:52 +0200507 if (node->schema && (node->schema->nodetype != LYS_CONTAINER) && (((node->flags & LYD_DEFAULT) &&
508 (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
509 ((pctx->options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node)))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200510 /* we have implicit OR explicit default node */
511 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200512 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200513 }
514
aPiecek6cf1d162023-11-08 16:07:00 +0100515 if (node->schema && (wdmod || node_has_printable_meta(node))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200516 if (inner) {
Michal Vaskof2b0a042022-12-01 13:52:51 +0100517 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(node), LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200518 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100519 LY_CHECK_RET(json_print_member(pctx, node, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200520 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100521 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200522 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100523 LY_CHECK_RET(json_print_metadata(pctx, node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200524 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100525 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200526 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200527 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200528 if (inner) {
Michal Vaskof2b0a042022-12-01 13:52:51 +0100529 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(node), LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200530 } else {
Michal Vaskof2b0a042022-12-01 13:52:51 +0100531 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(node), ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100532 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200533 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100534 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200535 LEVEL_INC;
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200536 LY_CHECK_RET(json_print_attribute(pctx, (struct lyd_node_opaq *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200537 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100538 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200539 LEVEL_PRINTED;
540 }
541
542 return LY_SUCCESS;
543}
544
545/**
546 * @brief Print leaf data node including its metadata.
547 *
548 * @param[in] ctx JSON printer context.
549 * @param[in] node Data node to print.
550 * @return LY_ERR value.
551 */
552static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100553json_print_leaf(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200554{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100555 LY_CHECK_RET(json_print_member(pctx, node, 0));
Michal Vasko32f067f2023-06-01 12:31:14 +0200556 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value, node->schema->module));
Radek Krejci5536d282020-08-04 23:27:44 +0200557 LEVEL_PRINTED;
558
559 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100560 json_print_attributes(pctx, node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200561
562 return LY_SUCCESS;
563}
564
565/**
Michal Vaskobbdadda2022-01-06 11:40:10 +0100566 * @brief Print anydata/anyxml content.
Radek Krejci5536d282020-08-04 23:27:44 +0200567 *
568 * @param[in] ctx JSON printer context.
569 * @param[in] any Anydata node to print.
570 * @return LY_ERR value.
571 */
572static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100573json_print_any_content(struct jsonpr_ctx *pctx, struct lyd_node_any *any)
Radek Krejci5536d282020-08-04 23:27:44 +0200574{
575 LY_ERR ret = LY_SUCCESS;
576 struct lyd_node *iter;
Michal Vasko26743a22022-03-29 14:48:10 +0200577 const struct lyd_node *prev_parent;
Michal Vasko59004e32024-01-30 16:09:54 +0100578 uint32_t prev_opts, *prev_lo, temp_lo = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200579
Michal Vasko76096ec2022-02-24 16:06:16 +0100580 assert(any->schema->nodetype & LYD_NODE_ANY);
Radek Krejci5536d282020-08-04 23:27:44 +0200581
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100582 if ((any->schema->nodetype == LYS_ANYDATA) && (any->value_type != LYD_ANYDATA_DATATREE)) {
583 LOGINT_RET(pctx->ctx);
584 }
585
Radek Krejci5536d282020-08-04 23:27:44 +0200586 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200587 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200588
589 /* turn logging off */
Michal Vasko59004e32024-01-30 16:09:54 +0100590 prev_lo = ly_temp_log_options(&temp_lo);
Radek Krejci5536d282020-08-04 23:27:44 +0200591
592 /* try to parse it into a data tree */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100593 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 +0200594 /* successfully parsed */
595 free(any->value.mem);
596 any->value.tree = iter;
597 any->value_type = LYD_ANYDATA_DATATREE;
598 }
599
Michal Vaskod4a6d042022-12-08 08:34:29 +0100600 /* turn logging on again */
Michal Vasko59004e32024-01-30 16:09:54 +0100601 ly_temp_log_options(prev_lo);
Radek Krejci5536d282020-08-04 23:27:44 +0200602 }
603
604 switch (any->value_type) {
605 case LYD_ANYDATA_DATATREE:
Michal Vasko76096ec2022-02-24 16:06:16 +0100606 /* print as an object */
607 ly_print_(pctx->out, "{%s", DO_FORMAT ? "\n" : "");
608 LEVEL_INC;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100609
Radek Krejci5536d282020-08-04 23:27:44 +0200610 /* close opening tag and print data */
Michal Vasko26743a22022-03-29 14:48:10 +0200611 prev_parent = pctx->parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100612 prev_opts = pctx->options;
Michal Vasko26743a22022-03-29 14:48:10 +0200613 pctx->parent = &any->node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100614 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci5536d282020-08-04 23:27:44 +0200615 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100616 ret = json_print_node(pctx, iter);
Radek Krejci5536d282020-08-04 23:27:44 +0200617 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
618 }
Michal Vasko26743a22022-03-29 14:48:10 +0200619 pctx->parent = prev_parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100620 pctx->options = prev_opts;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100621
Michal Vasko76096ec2022-02-24 16:06:16 +0100622 /* terminate the object */
Michal Vasko23b51a82022-03-29 14:22:34 +0200623 LEVEL_DEC;
Michal Vasko76096ec2022-02-24 16:06:16 +0100624 if (DO_FORMAT) {
625 ly_print_(pctx->out, "\n%*s}", INDENT);
626 } else {
627 ly_print_(pctx->out, "}");
Michal Vaskobbdadda2022-01-06 11:40:10 +0100628 }
Radek Krejci5536d282020-08-04 23:27:44 +0200629 break;
630 case LYD_ANYDATA_JSON:
Michal Vasko76096ec2022-02-24 16:06:16 +0100631 if (!any->value.json) {
632 /* no content */
633 if (any->schema->nodetype == LYS_ANYXML) {
634 ly_print_(pctx->out, "null");
635 } else {
636 ly_print_(pctx->out, "{}");
637 }
638 } else {
639 /* print without escaping special characters */
640 ly_print_(pctx->out, "%s", any->value.json);
Radek Krejci5536d282020-08-04 23:27:44 +0200641 }
Radek Krejci5536d282020-08-04 23:27:44 +0200642 break;
643 case LYD_ANYDATA_STRING:
644 case LYD_ANYDATA_XML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100645 if (!any->value.str) {
646 /* no content */
647 if (any->schema->nodetype == LYS_ANYXML) {
648 ly_print_(pctx->out, "null");
649 } else {
650 ly_print_(pctx->out, "{}");
651 }
652 } else {
Michal Vaskobbdadda2022-01-06 11:40:10 +0100653 /* print as a string */
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100654 json_print_string(pctx->out, any->value.str);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100655 }
Michal Vasko76096ec2022-02-24 16:06:16 +0100656 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200657 case LYD_ANYDATA_LYB:
Michal Vasko76096ec2022-02-24 16:06:16 +0100658 /* LYB format is not supported */
659 LOGWRN(pctx->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100660 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200661 }
662
663 return LY_SUCCESS;
664}
665
666/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100667 * @brief Print content of a single container/list data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100668 * The envelope specific to nodes are expected to be printed by the caller.
Radek Krejci5536d282020-08-04 23:27:44 +0200669 *
670 * @param[in] ctx JSON printer context.
671 * @param[in] node Data node to print.
672 * @return LY_ERR value.
673 */
674static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100675json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200676{
677 struct lyd_node *child;
Michal Vasko26743a22022-03-29 14:48:10 +0200678 const struct lyd_node *prev_parent;
Michal Vasko23b51a82022-03-29 14:22:34 +0200679 struct lyd_node_opaq *opaq = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200680 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200681
Michal Vasko630d9892020-12-08 17:11:08 +0100682 LY_LIST_FOR(lyd_child(node), child) {
Michal Vasko8db584d2022-03-30 13:42:48 +0200683 if (lyd_node_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100684 break;
685 }
686 }
687 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200688 has_content = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200689 }
Michal Vasko23b51a82022-03-29 14:22:34 +0200690 if (!node->schema) {
691 opaq = (struct lyd_node_opaq *)node;
692 }
Radek Krejci5536d282020-08-04 23:27:44 +0200693
Michal Vasko1a85d332021-08-27 10:35:28 +0200694 if ((node->schema && (node->schema->nodetype == LYS_LIST)) ||
Michal Vasko23b51a82022-03-29 14:22:34 +0200695 (opaq && (opaq->hints != LYD_HINT_DATA) && (opaq->hints & LYD_NODEHINT_LIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100696 ly_print_(pctx->out, "%s%*s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ?
Michal Vasko1a85d332021-08-27 10:35:28 +0200697 (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : "");
698 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100699 ly_print_(pctx->out, "%s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200700 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200701 }
702 LEVEL_INC;
703
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100704 json_print_attributes(pctx, node, 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200705
Michal Vasko76096ec2022-02-24 16:06:16 +0100706 /* print children */
Michal Vasko26743a22022-03-29 14:48:10 +0200707 prev_parent = pctx->parent;
708 pctx->parent = node;
Michal Vasko76096ec2022-02-24 16:06:16 +0100709 LY_LIST_FOR(lyd_child(node), child) {
710 LY_CHECK_RET(json_print_node(pctx, child));
Radek Krejci5536d282020-08-04 23:27:44 +0200711 }
Michal Vasko26743a22022-03-29 14:48:10 +0200712 pctx->parent = prev_parent;
Radek Krejci5536d282020-08-04 23:27:44 +0200713
Radek Krejci5536d282020-08-04 23:27:44 +0200714 LEVEL_DEC;
715 if (DO_FORMAT && has_content) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100716 ly_print_(pctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200717 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100718 ly_print_(pctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200719 }
720 LEVEL_PRINTED;
721
722 return LY_SUCCESS;
723}
724
725/**
726 * @brief Print container data node including its metadata.
727 *
728 * @param[in] ctx JSON printer context.
729 * @param[in] node Data node to print.
730 * @return LY_ERR value.
731 */
732static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100733json_print_container(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200734{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100735 LY_CHECK_RET(json_print_member(pctx, node, 0));
736 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200737
738 return LY_SUCCESS;
739}
740
741/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100742 * @brief Print anydata/anyxml data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100743 *
744 * @param[in] ctx JSON printer context.
745 * @param[in] node Data node to print.
746 * @return LY_ERR value.
747 */
748static int
Michal Vasko76096ec2022-02-24 16:06:16 +0100749json_print_any(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskobbdadda2022-01-06 11:40:10 +0100750{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100751 LY_CHECK_RET(json_print_member(pctx, node, 0));
752 LY_CHECK_RET(json_print_any_content(pctx, (struct lyd_node_any *)node));
Michal Vaskobbdadda2022-01-06 11:40:10 +0100753 LEVEL_PRINTED;
754
755 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100756 json_print_attributes(pctx, node, 0);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100757
758 return LY_SUCCESS;
759}
760
761/**
Michal Vaskoaa22f422021-12-02 10:48:32 +0100762 * @brief Check whether a node is the last printed instance of a (leaf-)list.
763 *
764 * @param[in] ctx JSON printer context.
765 * @param[in] node Last printed node.
766 * @return Whether it is the last printed instance or not.
767 */
768static ly_bool
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100769json_print_array_is_last_inst(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskoaa22f422021-12-02 10:48:32 +0100770{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100771 if (!is_open_array(pctx, node)) {
Michal Vasko06217f32021-12-09 09:25:50 +0100772 /* no array open */
773 return 0;
774 }
775
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100776 if ((pctx->root == node) && !(pctx->options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100777 /* the only printed instance */
778 return 1;
779 }
780
Michal Vasko06217f32021-12-09 09:25:50 +0100781 if (!node->next || (node->next->schema != node->schema)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100782 /* last instance */
783 return 1;
784 }
785
786 return 0;
787}
788
789/**
Radek Krejci5536d282020-08-04 23:27:44 +0200790 * @brief Print single leaf-list or list instance.
791 *
792 * In case of list, metadata are printed inside the list object. For the leaf-list,
793 * metadata are marked in the context for later printing after closing the array next to it using
794 * json_print_metadata_leaflist().
795 *
796 * @param[in] ctx JSON printer context.
797 * @param[in] node Data node to print.
798 * @return LY_ERR value.
799 */
800static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100801json_print_leaf_list(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200802{
Michal Vasko57c8d432022-11-30 15:35:56 +0100803 const struct lys_module *wdmod = NULL;
804
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100805 if (!is_open_array(pctx, node)) {
806 LY_CHECK_RET(json_print_member(pctx, node, 0));
807 LY_CHECK_RET(json_print_array_open(pctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200808 if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100809 ly_print_(pctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200810 }
Radek Krejci5536d282020-08-04 23:27:44 +0200811 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100812 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200813 }
814
815 if (node->schema->nodetype == LYS_LIST) {
Michal Vasko2fe10342022-12-01 11:54:02 +0100816 /* print list's content */
817 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200818 } else {
819 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko57c8d432022-11-30 15:35:56 +0100820
Michal Vasko32f067f2023-06-01 12:31:14 +0200821 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value, node->schema->module));
Radek Krejci5536d282020-08-04 23:27:44 +0200822
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200823 if (!pctx->first_leaflist) {
Igor Ryzhovd73f43b2024-01-26 01:13:52 +0200824 if (((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
825 ((pctx->options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko57c8d432022-11-30 15:35:56 +0100826 /* we have implicit OR explicit default node, get with-defaults module */
827 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
828 }
aPiecek6cf1d162023-11-08 16:07:00 +0100829 if (wdmod || node_has_printable_meta(node)) {
Michal Vasko57c8d432022-11-30 15:35:56 +0100830 /* we will be printing metadata for these siblings */
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200831 pctx->first_leaflist = node;
Michal Vasko57c8d432022-11-30 15:35:56 +0100832 }
Radek Krejci5536d282020-08-04 23:27:44 +0200833 }
834 }
835
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100836 if (json_print_array_is_last_inst(pctx, node)) {
837 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200838 }
839
840 return LY_SUCCESS;
841}
842
843/**
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200844 * @brief Print leaf-list's metadata or opaque nodes attributes.
Radek Krejci5536d282020-08-04 23:27:44 +0200845 * This function is supposed to be called when the leaf-list array is closed.
846 *
847 * @param[in] ctx JSON printer context.
848 * @return LY_ERR value.
849 */
850static LY_ERR
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200851json_print_meta_attr_leaflist(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200852{
853 const struct lyd_node *prev, *node, *iter;
Igor Ryzhovd73f43b2024-01-26 01:13:52 +0200854 const struct lys_module *wdmod = NULL, *iter_wdmod;
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200855 const struct lyd_node_opaq *opaq = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200856
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200857 assert(pctx->first_leaflist);
Radek Krejci5536d282020-08-04 23:27:44 +0200858
Michal Vasko57c8d432022-11-30 15:35:56 +0100859 if (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG)) {
860 /* get with-defaults module */
861 wdmod = ly_ctx_get_module_implemented(pctx->ctx, "ietf-netconf-with-defaults");
862 }
863
864 /* node is the first instance of the leaf-list */
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200865 for (node = pctx->first_leaflist, prev = pctx->first_leaflist->prev;
Radek Krejci5536d282020-08-04 23:27:44 +0200866 prev->next && matching_node(node, prev);
867 node = prev, prev = node->prev) {}
868
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200869 if (node->schema) {
870 LY_CHECK_RET(json_print_member(pctx, node, 1));
871 } else {
872 opaq = (struct lyd_node_opaq *)node;
873 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(node), opaq->format, &opaq->name, 1));
874 }
875
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100876 ly_print_(pctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200877 LEVEL_INC;
878 LY_LIST_FOR(node, iter) {
879 PRINT_COMMA;
Igor Ryzhovd73f43b2024-01-26 01:13:52 +0200880 if (iter->schema && ((iter->flags & LYD_DEFAULT) || ((pctx->options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(iter)))) {
881 iter_wdmod = wdmod;
882 } else {
883 iter_wdmod = NULL;
884 }
aPiecek6cf1d162023-11-08 16:07:00 +0100885 if ((iter->schema && (node_has_printable_meta(iter) || iter_wdmod)) || (opaq && opaq->attr)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100886 ly_print_(pctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200887 LEVEL_INC;
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200888
889 if (iter->schema) {
Igor Ryzhovd73f43b2024-01-26 01:13:52 +0200890 LY_CHECK_RET(json_print_metadata(pctx, iter, iter_wdmod));
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200891 } else {
892 LY_CHECK_RET(json_print_attribute(pctx, (struct lyd_node_opaq *)iter));
893 }
894
Radek Krejci5536d282020-08-04 23:27:44 +0200895 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100896 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200897 } else {
Igor Ryzhovaf4a5db2024-01-26 01:19:39 +0200898 ly_print_(pctx->out, "%*snull", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200899 }
900 LEVEL_PRINTED;
901 if (!matching_node(iter, iter->next)) {
902 break;
903 }
904 }
905 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100906 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200907 LEVEL_PRINTED;
908
909 return LY_SUCCESS;
910}
911
912/**
913 * @brief Print opaq data node including its attributes.
914 *
915 * @param[in] ctx JSON printer context.
916 * @param[in] node Opaq node to print.
917 * @return LY_ERR value.
918 */
919static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100920json_print_opaq(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200921{
Radek Krejci857189e2020-09-01 13:26:36 +0200922 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200923
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200924 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100925 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200926 first = 0;
927 }
Michal Vasko9e685082021-01-29 14:49:09 +0100928 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200929 last = 0;
930 }
931 }
932
933 if (first) {
Michal Vasko26743a22022-03-29 14:48:10 +0200934 LY_CHECK_RET(json_print_member2(pctx, pctx->parent, node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200935
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200936 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100937 LY_CHECK_RET(json_print_array_open(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200938 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200939 if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100940 ly_print_(pctx->out, "%*s", INDENT);
Michal Vasko1a85d332021-08-27 10:35:28 +0200941 }
942 } else if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100943 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200944 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200945 if (node->child || (node->hints & LYD_NODEHINT_LIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100946 LY_CHECK_RET(json_print_inner(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200947 LEVEL_PRINTED;
948 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200949 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100950 ly_print_(pctx->out, "[null]");
Michal Vaskocea58712022-04-01 14:37:08 +0200951 } else if ((node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) && !(node->hints & LYD_VALHINT_NUM64)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100952 ly_print_(pctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200953 } else {
Michal Vaskocea58712022-04-01 14:37:08 +0200954 /* string or a large number */
Michal Vasko5e2742d2023-05-25 10:01:42 +0200955 json_print_string(pctx->out, node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200956 }
957 LEVEL_PRINTED;
958
Michal Vaskoa4e5eb42023-09-18 08:44:21 +0200959 if (!(node->hints & LYD_NODEHINT_LEAFLIST)) {
960 /* attributes */
961 json_print_attributes(pctx, (const struct lyd_node *)node, 0);
962 } else if (!pctx->first_leaflist && node->attr) {
963 /* attributes printed later */
964 pctx->first_leaflist = &node->node;
965 }
Radek Krejci5536d282020-08-04 23:27:44 +0200966
967 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200968 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100969 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200970 LEVEL_PRINTED;
971 }
972
973 return LY_SUCCESS;
974}
975
976/**
977 * @brief Print all the types of data node including its metadata.
978 *
979 * @param[in] ctx JSON printer context.
980 * @param[in] node Data node to print.
981 * @return LY_ERR value.
982 */
983static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100984json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200985{
Michal Vasko8db584d2022-03-30 13:42:48 +0200986 if (!lyd_node_should_print(node, pctx->options)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100987 if (json_print_array_is_last_inst(pctx, node)) {
988 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200989 }
990 return LY_SUCCESS;
991 }
992
993 if (!node->schema) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100994 LY_CHECK_RET(json_print_opaq(pctx, (const struct lyd_node_opaq *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200995 } else {
996 switch (node->schema->nodetype) {
997 case LYS_RPC:
998 case LYS_ACTION:
999 case LYS_NOTIF:
1000 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001001 LY_CHECK_RET(json_print_container(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +02001002 break;
1003 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001004 LY_CHECK_RET(json_print_leaf(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +02001005 break;
1006 case LYS_LEAFLIST:
1007 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001008 LY_CHECK_RET(json_print_leaf_list(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +02001009 break;
Michal Vasko76096ec2022-02-24 16:06:16 +01001010 case LYS_ANYDATA:
Radek Krejci5536d282020-08-04 23:27:44 +02001011 case LYS_ANYXML:
Michal Vasko76096ec2022-02-24 16:06:16 +01001012 LY_CHECK_RET(json_print_any(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +02001013 break;
1014 default:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001015 LOGINT(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +02001016 return EXIT_FAILURE;
1017 }
1018 }
1019
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001020 pctx->level_printed = pctx->level;
Radek Krejci5536d282020-08-04 23:27:44 +02001021
Michal Vaskoa4e5eb42023-09-18 08:44:21 +02001022 if (pctx->first_leaflist && !matching_node(node->next, pctx->first_leaflist)) {
1023 json_print_meta_attr_leaflist(pctx);
1024 pctx->first_leaflist = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +02001025 }
1026
1027 return LY_SUCCESS;
1028}
1029
1030LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001031json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +02001032{
1033 const struct lyd_node *node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001034 struct jsonpr_ctx pctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +02001035 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +02001036
Radek Krejci2e874772020-08-28 16:36:33 +02001037 if (!root) {
1038 ly_print_(out, "{}%s", delimiter);
1039 ly_print_flush(out);
1040 return LY_SUCCESS;
1041 }
1042
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001043 pctx.out = out;
Michal Vasko26743a22022-03-29 14:48:10 +02001044 pctx.parent = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001045 pctx.level = 1;
1046 pctx.level_printed = 0;
1047 pctx.options = options;
1048 pctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +02001049
1050 /* start */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001051 ly_print_(pctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +02001052
1053 /* content */
1054 LY_LIST_FOR(root, node) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001055 pctx.root = node;
1056 LY_CHECK_RET(json_print_node(&pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +02001057 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
1058 break;
1059 }
1060 }
1061
1062 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +02001063 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +02001064
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001065 assert(!pctx.open.count);
1066 ly_set_erase(&pctx.open, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +02001067
1068 ly_print_flush(out);
1069 return LY_SUCCESS;
1070}