blob: a9dc2da58b88be13f41c49a6e8425862431357bc [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 Vasko57c8d432022-11-30 15:35:56 +01007 * Copyright (c) 2015 - 2022 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>
Radek Krejci47fab892020-11-05 17:02:41 +010017#include <stdint.h>
Radek Krejci5536d282020-08-04 23:27:44 +020018#include <stdlib.h>
Radek Krejci5536d282020-08-04 23:27:44 +020019
Radek Krejci13a57b62019-07-19 13:04:09 +020020#include "common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010021#include "context.h"
Radek Krejci5536d282020-08-04 23:27:44 +020022#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010023#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020024#include "out_internal.h"
Radek Krejci5536d282020-08-04 23:27:44 +020025#include "parser_data.h"
Michal Vaskob4750962022-10-06 15:33:35 +020026#include "plugins_exts/metadata.h"
Radek Krejci5536d282020-08-04 23:27:44 +020027#include "plugins_types.h"
28#include "printer_data.h"
29#include "printer_internal.h"
30#include "set.h"
31#include "tree.h"
32#include "tree_data.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020033#include "tree_schema.h"
34
35/**
Radek Krejci5536d282020-08-04 23:27:44 +020036 * @brief JSON printer context.
37 */
38struct jsonpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020039 struct ly_out *out; /**< output specification */
Michal Vaskoaa22f422021-12-02 10:48:32 +010040 const struct lyd_node *root; /**< root node of the subtree being printed */
Michal Vasko26743a22022-03-29 14:48:10 +020041 const struct lyd_node *parent; /**< parent of the node being printed */
Radek Krejci1deb5be2020-08-26 16:43:36 +020042 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
43 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
44 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020045
Radek Krejci1deb5be2020-08-26 16:43:36 +020046 uint16_t level_printed; /* level where some data were already printed */
Michal Vaskoaa22f422021-12-02 10:48:32 +010047 struct ly_set open; /* currently open array(s) */
Radek Krejci5536d282020-08-04 23:27:44 +020048 const struct lyd_node *print_sibling_metadata;
49};
50
51/**
52 * @brief Mark that something was already written in the current level,
53 * used to decide if a comma is expected between the items
54 */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010055#define LEVEL_PRINTED pctx->level_printed = pctx->level
Radek Krejci5536d282020-08-04 23:27:44 +020056
57#define PRINT_COMMA \
Michal Vasko61ad1ff2022-02-10 15:48:39 +010058 if (pctx->level_printed >= pctx->level) { \
59 ly_print_(pctx->out, ",%s", (DO_FORMAT ? "\n" : "")); \
Radek Krejci5536d282020-08-04 23:27:44 +020060 }
61
Michal Vasko61ad1ff2022-02-10 15:48:39 +010062static LY_ERR json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node);
Radek Krejci5536d282020-08-04 23:27:44 +020063
64/**
Michal Vaskob2c61572022-05-20 10:35:33 +020065 * @brief Compare 2 nodes, despite it is regular data node or an opaq node, and
Radek Krejci5536d282020-08-04 23:27:44 +020066 * decide if they corresponds to the same schema node.
67 *
Radek Krejci5536d282020-08-04 23:27:44 +020068 * @return 1 - matching nodes, 0 - non-matching nodes
69 */
70static int
71matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
72{
73 assert(node1 || node2);
74
75 if (!node1 || !node2) {
76 return 0;
77 } else if (node1->schema != node2->schema) {
78 return 0;
79 }
80 if (!node1->schema) {
81 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020082 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
83 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vasko26bbb272022-08-02 14:54:33 +020084
Michal Vaskoad92b672020-11-12 13:11:31 +010085 if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) {
Radek Krejci5536d282020-08-04 23:27:44 +020086 return 0;
87 }
88 }
89
90 return 1;
91}
92
93/**
94 * @brief Open the JSON array ('[') for the specified @p node
95 *
96 * @param[in] ctx JSON printer context.
97 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020098 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020099 */
Michal Vaskob0099a92020-08-31 14:55:23 +0200100static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100101json_print_array_open(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200102{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100103 ly_print_(pctx->out, "[%s", DO_FORMAT ? "\n" : "");
104 LY_CHECK_RET(ly_set_add(&pctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200105 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200106
107 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200108}
109
110/**
111 * @brief Get know if the array for the provided @p node is currently open.
112 *
113 * @param[in] ctx JSON printer context.
114 * @param[in] node Data node to check.
115 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
116 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
117 */
118static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100119is_open_array(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200120{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100121 if (pctx->open.count && matching_node(node, pctx->open.dnodes[pctx->open.count - 1])) {
Radek Krejci5536d282020-08-04 23:27:44 +0200122 return 1;
123 } else {
124 return 0;
125 }
126}
127
128/**
129 * @brief Close the most inner JSON array.
130 *
131 * @param[in] ctx JSON printer context.
132 */
133static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100134json_print_array_close(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200135{
Radek Krejci5536d282020-08-04 23:27:44 +0200136 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100137 ly_set_rm_index(&pctx->open, pctx->open.count - 1, NULL);
138 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200139}
140
141/**
142 * @brief Get the node's module name to use as the @p node prefix in JSON.
Radek Krejci31bc3f52021-04-26 11:09:58 +0200143 *
Radek Krejci5536d282020-08-04 23:27:44 +0200144 * @param[in] node Node to process.
145 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
146 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
147 */
148static const char *
149node_prefix(const struct lyd_node *node)
150{
151 if (node->schema) {
152 return node->schema->module->name;
153 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200154 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200155 const struct lys_module *mod;
156
157 switch (onode->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200158 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100159 return onode->name.module_name;
Radek Krejci8df109d2021-04-23 12:19:08 +0200160 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100161 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200162 if (!mod) {
163 return NULL;
164 }
165 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100166 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200167 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200168 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200169 }
170 }
171
172 return NULL;
173}
174
175/**
176 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
177 *
178 * Accepts both regulard a well as opaq nodes.
179 *
180 * @param[in] node1 The first node to compare.
181 * @param[in] node2 The second node to compare.
182 * @return 0 in case the nodes' modules are the same
183 * @return 1 in case the nodes belongs to different modules
184 */
185int
186json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
187{
188 assert(node1 || node2);
189
190 if (!node1 || !node2) {
191 return 1;
192 } else if (node1->schema && node2->schema) {
193 if (node1->schema->module == node2->schema->module) {
194 /* belongs to the same module */
195 return 0;
196 } else {
197 /* different modules */
198 return 1;
199 }
200 } else {
201 const char *pref1 = node_prefix(node1);
202 const char *pref2 = node_prefix(node2);
Michal Vasko26bbb272022-08-02 14:54:33 +0200203
Michal Vasko69730152020-10-09 16:30:07 +0200204 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200205 return 0;
206 } else {
207 return 1;
208 }
209 }
210}
211
212/**
213 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
214 *
215 * @param[in] out The output handler.
216 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200217 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200218 */
Michal Vasko5233e962020-08-14 14:26:20 +0200219static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200220json_print_string(struct ly_out *out, const char *text)
221{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200222 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200223
224 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200225 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200226 }
227
Michal Vasko5233e962020-08-14 14:26:20 +0200228 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200229 for (i = n = 0; text[i]; i++) {
230 const unsigned char ascii = text[i];
Michal Vasko26bbb272022-08-02 14:54:33 +0200231
Radek Krejci5536d282020-08-04 23:27:44 +0200232 if (ascii < 0x20) {
233 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200234 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200235 } else {
236 switch (ascii) {
237 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200238 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200239 break;
240 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200241 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200242 break;
243 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200244 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200245 n++;
246 }
247 }
248 }
Michal Vasko5233e962020-08-14 14:26:20 +0200249 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200250
Michal Vasko5233e962020-08-14 14:26:20 +0200251 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200252}
253
254/**
255 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
256 *
257 * @param[in] ctx JSON printer context.
258 * @param[in] node The data node being printed.
259 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
260 * @return LY_ERR value.
261 */
262static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263json_print_member(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200264{
265 PRINT_COMMA;
Michal Vasko26743a22022-03-29 14:48:10 +0200266 if ((LEVEL == 1) || json_nscmp(node, pctx->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200267 /* print "namespace" */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100268 ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200269 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200270 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100271 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200272 }
273
274 return LY_SUCCESS;
275}
276
277/**
278 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
279 *
280 * @param[in] ctx JSON printer context.
281 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
282 * @param[in] format Format to decide how to process the @p prefix.
Michal Vaskoad92b672020-11-12 13:11:31 +0100283 * @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 +0200284 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
285 * @return LY_ERR value.
286 */
287static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100288json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VALUE_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100289 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200290{
Michal Vaskoad92b672020-11-12 13:11:31 +0100291 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200292
293 PRINT_COMMA;
294
295 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100296 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200297 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200298 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100299 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200300 break;
Michal Vasko89762652023-02-09 13:58:37 +0100301 case LY_VALUE_XML: {
302 const struct lys_module *mod = NULL;
303
304 if (name->module_ns) {
305 mod = ly_ctx_get_module_implemented_ns(pctx->ctx, name->module_ns);
306 }
Radek Krejci5536d282020-08-04 23:27:44 +0200307 if (mod) {
308 module_name = mod->name;
309 }
310 break;
Michal Vasko89762652023-02-09 13:58:37 +0100311 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100312 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200313 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100314 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200315 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100316
317 name_str = name->name;
318 } else {
319 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200320 }
321
322 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200323 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100324 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 +0200325 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100326 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200327 }
328
329 return LY_SUCCESS;
330}
331
332/**
333 * @brief Print data value.
334 *
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100335 * @param[in] pctx JSON printer context.
336 * @param[in] ctx Context used to print the value.
Radek Krejci5536d282020-08-04 23:27:44 +0200337 * @param[in] val Data value to be printed.
338 * @return LY_ERR value.
339 */
340static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100341json_print_value(struct jsonpr_ctx *pctx, const struct ly_ctx *ctx, const struct lyd_value *val)
Radek Krejci5536d282020-08-04 23:27:44 +0200342{
aPiecek0f6bf3e2021-08-25 15:47:49 +0200343 ly_bool dynamic;
Michal Vasko183b9112021-11-04 16:02:24 +0100344 LY_DATA_TYPE basetype;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100345 const char *value = val->realtype->plugin->print(ctx, val, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200346
Michal Vasko183b9112021-11-04 16:02:24 +0100347 basetype = val->realtype->basetype;
348
349print_val:
Radek Krejci5536d282020-08-04 23:27:44 +0200350 /* leafref is not supported */
Michal Vasko183b9112021-11-04 16:02:24 +0100351 switch (basetype) {
352 case LY_TYPE_UNION:
353 /* use the resolved type */
354 basetype = val->subvalue->value.realtype->basetype;
355 goto print_val;
356
Radek Krejci5536d282020-08-04 23:27:44 +0200357 case LY_TYPE_BINARY:
358 case LY_TYPE_STRING:
359 case LY_TYPE_BITS:
360 case LY_TYPE_ENUM:
361 case LY_TYPE_INST:
362 case LY_TYPE_INT64:
363 case LY_TYPE_UINT64:
364 case LY_TYPE_DEC64:
365 case LY_TYPE_IDENT:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100366 json_print_string(pctx->out, value);
Radek Krejci5536d282020-08-04 23:27:44 +0200367 break;
368
369 case LY_TYPE_INT8:
370 case LY_TYPE_INT16:
371 case LY_TYPE_INT32:
372 case LY_TYPE_UINT8:
373 case LY_TYPE_UINT16:
374 case LY_TYPE_UINT32:
375 case LY_TYPE_BOOL:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100376 ly_print_(pctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200377 break;
378
379 case LY_TYPE_EMPTY:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100380 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200381 break;
382
383 default:
384 /* error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100385 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200386 }
387
388 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200389 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200390 }
391
392 return LY_SUCCESS;
393}
394
395/**
396 * @brief Print all the attributes of the opaq node.
397 *
398 * @param[in] ctx JSON printer context.
399 * @param[in] node Opaq node where the attributes are placed.
400 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
401 * @return LY_ERR value.
402 */
403static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100404json_print_attribute(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200405{
406 struct lyd_attr *attr;
407
408 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200409 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200410 LEVEL_PRINTED;
411 }
412
413 for (attr = node->attr; attr; attr = attr->next) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100414 json_print_member2(pctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200415
Michal Vasko69bb5fe2022-12-01 13:52:21 +0100416 if (attr->hints & (LYD_VALHINT_STRING | LYD_VALHINT_OCTNUM | LYD_VALHINT_HEXNUM | LYD_VALHINT_NUM64)) {
417 json_print_string(pctx->out, attr->value);
418 } else if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100419 ly_print_(pctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200420 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100421 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200422 } else {
Michal Vasko69bb5fe2022-12-01 13:52:21 +0100423 /* unknown value format with no hints, use universal string */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100424 json_print_string(pctx->out, attr->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200425 }
426 LEVEL_PRINTED;
427 }
428
429 return LY_SUCCESS;
430}
431
432/**
433 * @brief Print all the metadata of the node.
434 *
435 * @param[in] ctx JSON printer context.
436 * @param[in] node Node where the metadata are placed.
437 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
438 * @return LY_ERR value.
439 */
440static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100441json_print_metadata(struct jsonpr_ctx *pctx, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200442{
443 struct lyd_meta *meta;
444
445 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200446 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200447 LEVEL_PRINTED;
448 }
449
450 for (meta = node->meta; meta; meta = meta->next) {
451 PRINT_COMMA;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100452 ly_print_(pctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
453 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &meta->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200454 LEVEL_PRINTED;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
462 *
463 * @param[in] ctx JSON printer context.
464 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200465 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200466 * @return LY_ERR value.
467 */
468static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100469json_print_attributes(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200470{
471 const struct lys_module *wdmod = NULL;
472
Michal Vasko6f39c822022-12-01 10:12:14 +0100473 if (node->schema && (node->schema->nodetype != LYS_CONTAINER) && (node->flags & LYD_DEFAULT) &&
474 (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200475 /* we have implicit OR explicit default node */
476 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200477 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200478 }
479
Michal Vaskob68c3b42022-05-20 10:36:40 +0200480 if (node->schema && (node->meta || wdmod)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200481 if (inner) {
Michal Vaskof2b0a042022-12-01 13:52:51 +0100482 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(node), LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200483 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100484 LY_CHECK_RET(json_print_member(pctx, node, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200485 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100486 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200487 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100488 LY_CHECK_RET(json_print_metadata(pctx, node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200489 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100490 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200491 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200492 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200493 if (inner) {
Michal Vaskof2b0a042022-12-01 13:52:51 +0100494 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(node), LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200495 } else {
Michal Vaskof2b0a042022-12-01 13:52:51 +0100496 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(node), ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100497 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200498 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100499 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200500 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100501 LY_CHECK_RET(json_print_attribute(pctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200502 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100503 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200504 LEVEL_PRINTED;
505 }
506
507 return LY_SUCCESS;
508}
509
510/**
511 * @brief Print leaf data node including its metadata.
512 *
513 * @param[in] ctx JSON printer context.
514 * @param[in] node Data node to print.
515 * @return LY_ERR value.
516 */
517static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100518json_print_leaf(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200519{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100520 LY_CHECK_RET(json_print_member(pctx, node, 0));
521 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200522 LEVEL_PRINTED;
523
524 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100525 json_print_attributes(pctx, node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200526
527 return LY_SUCCESS;
528}
529
530/**
Michal Vaskobbdadda2022-01-06 11:40:10 +0100531 * @brief Print anydata/anyxml content.
Radek Krejci5536d282020-08-04 23:27:44 +0200532 *
533 * @param[in] ctx JSON printer context.
534 * @param[in] any Anydata node to print.
535 * @return LY_ERR value.
536 */
537static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100538json_print_any_content(struct jsonpr_ctx *pctx, struct lyd_node_any *any)
Radek Krejci5536d282020-08-04 23:27:44 +0200539{
540 LY_ERR ret = LY_SUCCESS;
541 struct lyd_node *iter;
Michal Vasko26743a22022-03-29 14:48:10 +0200542 const struct lyd_node *prev_parent;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100543 uint32_t prev_opts, temp_lo = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200544
Michal Vasko76096ec2022-02-24 16:06:16 +0100545 assert(any->schema->nodetype & LYD_NODE_ANY);
Radek Krejci5536d282020-08-04 23:27:44 +0200546
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100547 if ((any->schema->nodetype == LYS_ANYDATA) && (any->value_type != LYD_ANYDATA_DATATREE)) {
548 LOGINT_RET(pctx->ctx);
549 }
550
Radek Krejci5536d282020-08-04 23:27:44 +0200551 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200552 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200553
554 /* turn logging off */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100555 ly_temp_log_options(&temp_lo);
Radek Krejci5536d282020-08-04 23:27:44 +0200556
557 /* try to parse it into a data tree */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100558 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 +0200559 /* successfully parsed */
560 free(any->value.mem);
561 any->value.tree = iter;
562 any->value_type = LYD_ANYDATA_DATATREE;
563 }
564
Michal Vaskod4a6d042022-12-08 08:34:29 +0100565 /* turn logging on again */
566 ly_temp_log_options(NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200567 }
568
569 switch (any->value_type) {
570 case LYD_ANYDATA_DATATREE:
Michal Vasko76096ec2022-02-24 16:06:16 +0100571 /* print as an object */
572 ly_print_(pctx->out, "{%s", DO_FORMAT ? "\n" : "");
573 LEVEL_INC;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100574
Radek Krejci5536d282020-08-04 23:27:44 +0200575 /* close opening tag and print data */
Michal Vasko26743a22022-03-29 14:48:10 +0200576 prev_parent = pctx->parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100577 prev_opts = pctx->options;
Michal Vasko26743a22022-03-29 14:48:10 +0200578 pctx->parent = &any->node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100579 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci5536d282020-08-04 23:27:44 +0200580 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100581 ret = json_print_node(pctx, iter);
Radek Krejci5536d282020-08-04 23:27:44 +0200582 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
583 }
Michal Vasko26743a22022-03-29 14:48:10 +0200584 pctx->parent = prev_parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100585 pctx->options = prev_opts;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100586
Michal Vasko76096ec2022-02-24 16:06:16 +0100587 /* terminate the object */
Michal Vasko23b51a82022-03-29 14:22:34 +0200588 LEVEL_DEC;
Michal Vasko76096ec2022-02-24 16:06:16 +0100589 if (DO_FORMAT) {
590 ly_print_(pctx->out, "\n%*s}", INDENT);
591 } else {
592 ly_print_(pctx->out, "}");
Michal Vaskobbdadda2022-01-06 11:40:10 +0100593 }
Radek Krejci5536d282020-08-04 23:27:44 +0200594 break;
595 case LYD_ANYDATA_JSON:
Michal Vasko76096ec2022-02-24 16:06:16 +0100596 if (!any->value.json) {
597 /* no content */
598 if (any->schema->nodetype == LYS_ANYXML) {
599 ly_print_(pctx->out, "null");
600 } else {
601 ly_print_(pctx->out, "{}");
602 }
603 } else {
604 /* print without escaping special characters */
605 ly_print_(pctx->out, "%s", any->value.json);
Radek Krejci5536d282020-08-04 23:27:44 +0200606 }
Radek Krejci5536d282020-08-04 23:27:44 +0200607 break;
608 case LYD_ANYDATA_STRING:
609 case LYD_ANYDATA_XML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100610 if (!any->value.str) {
611 /* no content */
612 if (any->schema->nodetype == LYS_ANYXML) {
613 ly_print_(pctx->out, "null");
614 } else {
615 ly_print_(pctx->out, "{}");
616 }
617 } else {
Michal Vaskobbdadda2022-01-06 11:40:10 +0100618 /* print as a string */
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100619 json_print_string(pctx->out, any->value.str);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100620 }
Michal Vasko76096ec2022-02-24 16:06:16 +0100621 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200622 case LYD_ANYDATA_LYB:
Michal Vasko76096ec2022-02-24 16:06:16 +0100623 /* LYB format is not supported */
624 LOGWRN(pctx->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100625 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200626 }
627
628 return LY_SUCCESS;
629}
630
631/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100632 * @brief Print content of a single container/list data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100633 * The envelope specific to nodes are expected to be printed by the caller.
Radek Krejci5536d282020-08-04 23:27:44 +0200634 *
635 * @param[in] ctx JSON printer context.
636 * @param[in] node Data node to print.
637 * @return LY_ERR value.
638 */
639static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100640json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200641{
642 struct lyd_node *child;
Michal Vasko26743a22022-03-29 14:48:10 +0200643 const struct lyd_node *prev_parent;
Michal Vasko23b51a82022-03-29 14:22:34 +0200644 struct lyd_node_opaq *opaq = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200645 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200646
Michal Vasko630d9892020-12-08 17:11:08 +0100647 LY_LIST_FOR(lyd_child(node), child) {
Michal Vasko8db584d2022-03-30 13:42:48 +0200648 if (lyd_node_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100649 break;
650 }
651 }
652 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200653 has_content = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200654 }
Michal Vasko23b51a82022-03-29 14:22:34 +0200655 if (!node->schema) {
656 opaq = (struct lyd_node_opaq *)node;
657 }
Radek Krejci5536d282020-08-04 23:27:44 +0200658
Michal Vasko1a85d332021-08-27 10:35:28 +0200659 if ((node->schema && (node->schema->nodetype == LYS_LIST)) ||
Michal Vasko23b51a82022-03-29 14:22:34 +0200660 (opaq && (opaq->hints != LYD_HINT_DATA) && (opaq->hints & LYD_NODEHINT_LIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100661 ly_print_(pctx->out, "%s%*s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ?
Michal Vasko1a85d332021-08-27 10:35:28 +0200662 (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : "");
663 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100664 ly_print_(pctx->out, "%s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200665 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200666 }
667 LEVEL_INC;
668
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100669 json_print_attributes(pctx, node, 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200670
Michal Vasko76096ec2022-02-24 16:06:16 +0100671 /* print children */
Michal Vasko26743a22022-03-29 14:48:10 +0200672 prev_parent = pctx->parent;
673 pctx->parent = node;
Michal Vasko76096ec2022-02-24 16:06:16 +0100674 LY_LIST_FOR(lyd_child(node), child) {
675 LY_CHECK_RET(json_print_node(pctx, child));
Radek Krejci5536d282020-08-04 23:27:44 +0200676 }
Michal Vasko26743a22022-03-29 14:48:10 +0200677 pctx->parent = prev_parent;
Radek Krejci5536d282020-08-04 23:27:44 +0200678
Radek Krejci5536d282020-08-04 23:27:44 +0200679 LEVEL_DEC;
680 if (DO_FORMAT && has_content) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100681 ly_print_(pctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200682 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100683 ly_print_(pctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200684 }
685 LEVEL_PRINTED;
686
687 return LY_SUCCESS;
688}
689
690/**
691 * @brief Print container data node including its metadata.
692 *
693 * @param[in] ctx JSON printer context.
694 * @param[in] node Data node to print.
695 * @return LY_ERR value.
696 */
697static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100698json_print_container(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200699{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100700 LY_CHECK_RET(json_print_member(pctx, node, 0));
701 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200702
703 return LY_SUCCESS;
704}
705
706/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100707 * @brief Print anydata/anyxml data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100708 *
709 * @param[in] ctx JSON printer context.
710 * @param[in] node Data node to print.
711 * @return LY_ERR value.
712 */
713static int
Michal Vasko76096ec2022-02-24 16:06:16 +0100714json_print_any(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskobbdadda2022-01-06 11:40:10 +0100715{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100716 LY_CHECK_RET(json_print_member(pctx, node, 0));
717 LY_CHECK_RET(json_print_any_content(pctx, (struct lyd_node_any *)node));
Michal Vaskobbdadda2022-01-06 11:40:10 +0100718 LEVEL_PRINTED;
719
720 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100721 json_print_attributes(pctx, node, 0);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100722
723 return LY_SUCCESS;
724}
725
726/**
Michal Vaskoaa22f422021-12-02 10:48:32 +0100727 * @brief Check whether a node is the last printed instance of a (leaf-)list.
728 *
729 * @param[in] ctx JSON printer context.
730 * @param[in] node Last printed node.
731 * @return Whether it is the last printed instance or not.
732 */
733static ly_bool
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100734json_print_array_is_last_inst(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskoaa22f422021-12-02 10:48:32 +0100735{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100736 if (!is_open_array(pctx, node)) {
Michal Vasko06217f32021-12-09 09:25:50 +0100737 /* no array open */
738 return 0;
739 }
740
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100741 if ((pctx->root == node) && !(pctx->options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100742 /* the only printed instance */
743 return 1;
744 }
745
Michal Vasko06217f32021-12-09 09:25:50 +0100746 if (!node->next || (node->next->schema != node->schema)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100747 /* last instance */
748 return 1;
749 }
750
751 return 0;
752}
753
754/**
Radek Krejci5536d282020-08-04 23:27:44 +0200755 * @brief Print single leaf-list or list instance.
756 *
757 * In case of list, metadata are printed inside the list object. For the leaf-list,
758 * metadata are marked in the context for later printing after closing the array next to it using
759 * json_print_metadata_leaflist().
760 *
761 * @param[in] ctx JSON printer context.
762 * @param[in] node Data node to print.
763 * @return LY_ERR value.
764 */
765static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100766json_print_leaf_list(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200767{
Michal Vasko57c8d432022-11-30 15:35:56 +0100768 const struct lys_module *wdmod = NULL;
769
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100770 if (!is_open_array(pctx, node)) {
771 LY_CHECK_RET(json_print_member(pctx, node, 0));
772 LY_CHECK_RET(json_print_array_open(pctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200773 if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100774 ly_print_(pctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200775 }
Radek Krejci5536d282020-08-04 23:27:44 +0200776 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100777 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200778 }
779
780 if (node->schema->nodetype == LYS_LIST) {
Michal Vasko2fe10342022-12-01 11:54:02 +0100781 /* print list's content */
782 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200783 } else {
784 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko57c8d432022-11-30 15:35:56 +0100785
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100786 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200787
Michal Vasko57c8d432022-11-30 15:35:56 +0100788 if (!pctx->print_sibling_metadata) {
789 if ((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
790 /* we have implicit OR explicit default node, get with-defaults module */
791 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
792 }
793 if (node->meta || wdmod) {
794 /* we will be printing metadata for these siblings */
795 pctx->print_sibling_metadata = node;
796 }
Radek Krejci5536d282020-08-04 23:27:44 +0200797 }
798 }
799
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100800 if (json_print_array_is_last_inst(pctx, node)) {
801 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200802 }
803
804 return LY_SUCCESS;
805}
806
807/**
808 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
809 * This function is supposed to be called when the leaf-list array is closed.
810 *
811 * @param[in] ctx JSON printer context.
812 * @return LY_ERR value.
813 */
814static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100815json_print_metadata_leaflist(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200816{
817 const struct lyd_node *prev, *node, *iter;
Michal Vasko57c8d432022-11-30 15:35:56 +0100818 const struct lys_module *wdmod = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200819
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100820 if (!pctx->print_sibling_metadata) {
Radek Krejci5536d282020-08-04 23:27:44 +0200821 return LY_SUCCESS;
822 }
823
Michal Vasko57c8d432022-11-30 15:35:56 +0100824 if (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG)) {
825 /* get with-defaults module */
826 wdmod = ly_ctx_get_module_implemented(pctx->ctx, "ietf-netconf-with-defaults");
827 }
828
829 /* node is the first instance of the leaf-list */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100830 for (node = pctx->print_sibling_metadata, prev = pctx->print_sibling_metadata->prev;
Radek Krejci5536d282020-08-04 23:27:44 +0200831 prev->next && matching_node(node, prev);
832 node = prev, prev = node->prev) {}
833
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100834 LY_CHECK_RET(json_print_member(pctx, node, 1));
835 ly_print_(pctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200836 LEVEL_INC;
837 LY_LIST_FOR(node, iter) {
838 PRINT_COMMA;
Michal Vasko57c8d432022-11-30 15:35:56 +0100839 if (iter->meta || (iter->flags & LYD_DEFAULT)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100840 ly_print_(pctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200841 LEVEL_INC;
Michal Vasko57c8d432022-11-30 15:35:56 +0100842 LY_CHECK_RET(json_print_metadata(pctx, iter, (iter->flags & LYD_DEFAULT) ? wdmod : NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200843 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100844 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200845 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100846 ly_print_(pctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200847 }
848 LEVEL_PRINTED;
849 if (!matching_node(iter, iter->next)) {
850 break;
851 }
852 }
853 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100854 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200855 LEVEL_PRINTED;
856
857 return LY_SUCCESS;
858}
859
860/**
861 * @brief Print opaq data node including its attributes.
862 *
863 * @param[in] ctx JSON printer context.
864 * @param[in] node Opaq node to print.
865 * @return LY_ERR value.
866 */
867static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100868json_print_opaq(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200869{
Radek Krejci857189e2020-09-01 13:26:36 +0200870 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200871
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200872 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100873 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200874 first = 0;
875 }
Michal Vasko9e685082021-01-29 14:49:09 +0100876 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200877 last = 0;
878 }
879 }
880
881 if (first) {
Michal Vasko26743a22022-03-29 14:48:10 +0200882 LY_CHECK_RET(json_print_member2(pctx, pctx->parent, node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200883
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200884 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100885 LY_CHECK_RET(json_print_array_open(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200886 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200887 if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100888 ly_print_(pctx->out, "%*s", INDENT);
Michal Vasko1a85d332021-08-27 10:35:28 +0200889 }
890 } else if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100891 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200892 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200893 if (node->child || (node->hints & LYD_NODEHINT_LIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100894 LY_CHECK_RET(json_print_inner(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200895 LEVEL_PRINTED;
896 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200897 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100898 ly_print_(pctx->out, "[null]");
Michal Vaskocea58712022-04-01 14:37:08 +0200899 } else if ((node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) && !(node->hints & LYD_VALHINT_NUM64)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100900 ly_print_(pctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200901 } else {
Michal Vaskocea58712022-04-01 14:37:08 +0200902 /* string or a large number */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100903 ly_print_(pctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200904 }
905 LEVEL_PRINTED;
906
907 /* attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100908 json_print_attributes(pctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200909
910 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200911 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100912 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200913 LEVEL_PRINTED;
914 }
915
916 return LY_SUCCESS;
917}
918
919/**
920 * @brief Print all the types of data node including its metadata.
921 *
922 * @param[in] ctx JSON printer context.
923 * @param[in] node Data node to print.
924 * @return LY_ERR value.
925 */
926static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100927json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200928{
Michal Vasko8db584d2022-03-30 13:42:48 +0200929 if (!lyd_node_should_print(node, pctx->options)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100930 if (json_print_array_is_last_inst(pctx, node)) {
931 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200932 }
933 return LY_SUCCESS;
934 }
935
936 if (!node->schema) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100937 LY_CHECK_RET(json_print_opaq(pctx, (const struct lyd_node_opaq *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200938 } else {
939 switch (node->schema->nodetype) {
940 case LYS_RPC:
941 case LYS_ACTION:
942 case LYS_NOTIF:
943 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100944 LY_CHECK_RET(json_print_container(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200945 break;
946 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100947 LY_CHECK_RET(json_print_leaf(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200948 break;
949 case LYS_LEAFLIST:
950 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100951 LY_CHECK_RET(json_print_leaf_list(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200952 break;
Michal Vasko76096ec2022-02-24 16:06:16 +0100953 case LYS_ANYDATA:
Radek Krejci5536d282020-08-04 23:27:44 +0200954 case LYS_ANYXML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100955 LY_CHECK_RET(json_print_any(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200956 break;
957 default:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100958 LOGINT(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200959 return EXIT_FAILURE;
960 }
961 }
962
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100963 pctx->level_printed = pctx->level;
Radek Krejci5536d282020-08-04 23:27:44 +0200964
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100965 if (pctx->print_sibling_metadata && !matching_node(node->next, pctx->print_sibling_metadata)) {
966 json_print_metadata_leaflist(pctx);
967 pctx->print_sibling_metadata = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200968 }
969
970 return LY_SUCCESS;
971}
972
973LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200974json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200975{
976 const struct lyd_node *node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100977 struct jsonpr_ctx pctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200978 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200979
Radek Krejci2e874772020-08-28 16:36:33 +0200980 if (!root) {
981 ly_print_(out, "{}%s", delimiter);
982 ly_print_flush(out);
983 return LY_SUCCESS;
984 }
985
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100986 pctx.out = out;
Michal Vasko26743a22022-03-29 14:48:10 +0200987 pctx.parent = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100988 pctx.level = 1;
989 pctx.level_printed = 0;
990 pctx.options = options;
991 pctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200992
993 /* start */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100994 ly_print_(pctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200995
996 /* content */
997 LY_LIST_FOR(root, node) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100998 pctx.root = node;
999 LY_CHECK_RET(json_print_node(&pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +02001000 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
1001 break;
1002 }
1003 }
1004
1005 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +02001006 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +02001007
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001008 assert(!pctx.open.count);
1009 ly_set_erase(&pctx.open, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +02001010
1011 ly_print_flush(out);
1012 return LY_SUCCESS;
1013}