blob: f14f5d1fe5ed0e430e6dac69fbcff03f16e2fb22 [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 const struct lys_module *mod;
298
299 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200300 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100301 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200302 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200303 case LY_VALUE_XML:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100304 mod = ly_ctx_get_module_implemented_ns(pctx->ctx, name->module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200305 if (mod) {
306 module_name = mod->name;
307 }
308 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100309 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200310 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100311 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200312 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100313
314 name_str = name->name;
315 } else {
316 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200317 }
318
319 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200320 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100321 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 +0200322 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100323 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200324 }
325
326 return LY_SUCCESS;
327}
328
329/**
330 * @brief Print data value.
331 *
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100332 * @param[in] pctx JSON printer context.
333 * @param[in] ctx Context used to print the value.
Radek Krejci5536d282020-08-04 23:27:44 +0200334 * @param[in] val Data value to be printed.
335 * @return LY_ERR value.
336 */
337static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100338json_print_value(struct jsonpr_ctx *pctx, const struct ly_ctx *ctx, const struct lyd_value *val)
Radek Krejci5536d282020-08-04 23:27:44 +0200339{
aPiecek0f6bf3e2021-08-25 15:47:49 +0200340 ly_bool dynamic;
Michal Vasko183b9112021-11-04 16:02:24 +0100341 LY_DATA_TYPE basetype;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100342 const char *value = val->realtype->plugin->print(ctx, val, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200343
Michal Vasko183b9112021-11-04 16:02:24 +0100344 basetype = val->realtype->basetype;
345
346print_val:
Radek Krejci5536d282020-08-04 23:27:44 +0200347 /* leafref is not supported */
Michal Vasko183b9112021-11-04 16:02:24 +0100348 switch (basetype) {
349 case LY_TYPE_UNION:
350 /* use the resolved type */
351 basetype = val->subvalue->value.realtype->basetype;
352 goto print_val;
353
Radek Krejci5536d282020-08-04 23:27:44 +0200354 case LY_TYPE_BINARY:
355 case LY_TYPE_STRING:
356 case LY_TYPE_BITS:
357 case LY_TYPE_ENUM:
358 case LY_TYPE_INST:
359 case LY_TYPE_INT64:
360 case LY_TYPE_UINT64:
361 case LY_TYPE_DEC64:
362 case LY_TYPE_IDENT:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100363 json_print_string(pctx->out, value);
Radek Krejci5536d282020-08-04 23:27:44 +0200364 break;
365
366 case LY_TYPE_INT8:
367 case LY_TYPE_INT16:
368 case LY_TYPE_INT32:
369 case LY_TYPE_UINT8:
370 case LY_TYPE_UINT16:
371 case LY_TYPE_UINT32:
372 case LY_TYPE_BOOL:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100373 ly_print_(pctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200374 break;
375
376 case LY_TYPE_EMPTY:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100377 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200378 break;
379
380 default:
381 /* error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100382 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200383 }
384
385 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200386 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200387 }
388
389 return LY_SUCCESS;
390}
391
392/**
393 * @brief Print all the attributes of the opaq node.
394 *
395 * @param[in] ctx JSON printer context.
396 * @param[in] node Opaq node where the attributes are placed.
397 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
398 * @return LY_ERR value.
399 */
400static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100401json_print_attribute(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200402{
403 struct lyd_attr *attr;
404
405 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200406 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200407 LEVEL_PRINTED;
408 }
409
410 for (attr = node->attr; attr; attr = attr->next) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100411 json_print_member2(pctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200412
Michal Vasko69bb5fe2022-12-01 13:52:21 +0100413 if (attr->hints & (LYD_VALHINT_STRING | LYD_VALHINT_OCTNUM | LYD_VALHINT_HEXNUM | LYD_VALHINT_NUM64)) {
414 json_print_string(pctx->out, attr->value);
415 } else if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100416 ly_print_(pctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200417 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100418 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200419 } else {
Michal Vasko69bb5fe2022-12-01 13:52:21 +0100420 /* unknown value format with no hints, use universal string */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100421 json_print_string(pctx->out, attr->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200422 }
423 LEVEL_PRINTED;
424 }
425
426 return LY_SUCCESS;
427}
428
429/**
430 * @brief Print all the metadata of the node.
431 *
432 * @param[in] ctx JSON printer context.
433 * @param[in] node Node where the metadata are placed.
434 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
435 * @return LY_ERR value.
436 */
437static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100438json_print_metadata(struct jsonpr_ctx *pctx, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200439{
440 struct lyd_meta *meta;
441
442 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200443 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200444 LEVEL_PRINTED;
445 }
446
447 for (meta = node->meta; meta; meta = meta->next) {
448 PRINT_COMMA;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100449 ly_print_(pctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
450 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &meta->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200451 LEVEL_PRINTED;
452 }
453
454 return LY_SUCCESS;
455}
456
457/**
458 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
459 *
460 * @param[in] ctx JSON printer context.
461 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200462 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200463 * @return LY_ERR value.
464 */
465static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100466json_print_attributes(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200467{
468 const struct lys_module *wdmod = NULL;
469
Michal Vasko6f39c822022-12-01 10:12:14 +0100470 if (node->schema && (node->schema->nodetype != LYS_CONTAINER) && (node->flags & LYD_DEFAULT) &&
471 (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200472 /* we have implicit OR explicit default node */
473 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200474 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200475 }
476
Michal Vaskob68c3b42022-05-20 10:36:40 +0200477 if (node->schema && (node->meta || wdmod)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200478 if (inner) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100479 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200480 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100481 LY_CHECK_RET(json_print_member(pctx, node, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200482 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100483 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200484 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100485 LY_CHECK_RET(json_print_metadata(pctx, node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200486 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100487 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200488 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200489 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200490 if (inner) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100491 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200492 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100493 LY_CHECK_RET(json_print_member2(pctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100494 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200495 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100496 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200497 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100498 LY_CHECK_RET(json_print_attribute(pctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200499 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100500 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200501 LEVEL_PRINTED;
502 }
503
504 return LY_SUCCESS;
505}
506
507/**
508 * @brief Print leaf data node including its metadata.
509 *
510 * @param[in] ctx JSON printer context.
511 * @param[in] node Data node to print.
512 * @return LY_ERR value.
513 */
514static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100515json_print_leaf(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200516{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100517 LY_CHECK_RET(json_print_member(pctx, node, 0));
518 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200519 LEVEL_PRINTED;
520
521 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100522 json_print_attributes(pctx, node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200523
524 return LY_SUCCESS;
525}
526
527/**
Michal Vaskobbdadda2022-01-06 11:40:10 +0100528 * @brief Print anydata/anyxml content.
Radek Krejci5536d282020-08-04 23:27:44 +0200529 *
530 * @param[in] ctx JSON printer context.
531 * @param[in] any Anydata node to print.
532 * @return LY_ERR value.
533 */
534static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100535json_print_any_content(struct jsonpr_ctx *pctx, struct lyd_node_any *any)
Radek Krejci5536d282020-08-04 23:27:44 +0200536{
537 LY_ERR ret = LY_SUCCESS;
538 struct lyd_node *iter;
Michal Vasko26743a22022-03-29 14:48:10 +0200539 const struct lyd_node *prev_parent;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200540 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200541
Michal Vasko76096ec2022-02-24 16:06:16 +0100542 assert(any->schema->nodetype & LYD_NODE_ANY);
Radek Krejci5536d282020-08-04 23:27:44 +0200543
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100544 if ((any->schema->nodetype == LYS_ANYDATA) && (any->value_type != LYD_ANYDATA_DATATREE)) {
545 LOGINT_RET(pctx->ctx);
546 }
547
Radek Krejci5536d282020-08-04 23:27:44 +0200548 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200549 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200550
551 /* turn logging off */
552 prev_lo = ly_log_options(0);
553
554 /* try to parse it into a data tree */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100555 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 +0200556 /* successfully parsed */
557 free(any->value.mem);
558 any->value.tree = iter;
559 any->value_type = LYD_ANYDATA_DATATREE;
560 }
561
562 /* turn loggin on again */
563 ly_log_options(prev_lo);
564 }
565
566 switch (any->value_type) {
567 case LYD_ANYDATA_DATATREE:
Michal Vasko76096ec2022-02-24 16:06:16 +0100568 /* print as an object */
569 ly_print_(pctx->out, "{%s", DO_FORMAT ? "\n" : "");
570 LEVEL_INC;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100571
Radek Krejci5536d282020-08-04 23:27:44 +0200572 /* close opening tag and print data */
Michal Vasko26743a22022-03-29 14:48:10 +0200573 prev_parent = pctx->parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100574 prev_opts = pctx->options;
Michal Vasko26743a22022-03-29 14:48:10 +0200575 pctx->parent = &any->node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100576 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci5536d282020-08-04 23:27:44 +0200577 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100578 ret = json_print_node(pctx, iter);
Radek Krejci5536d282020-08-04 23:27:44 +0200579 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
580 }
Michal Vasko26743a22022-03-29 14:48:10 +0200581 pctx->parent = prev_parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100582 pctx->options = prev_opts;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100583
Michal Vasko76096ec2022-02-24 16:06:16 +0100584 /* terminate the object */
Michal Vasko23b51a82022-03-29 14:22:34 +0200585 LEVEL_DEC;
Michal Vasko76096ec2022-02-24 16:06:16 +0100586 if (DO_FORMAT) {
587 ly_print_(pctx->out, "\n%*s}", INDENT);
588 } else {
589 ly_print_(pctx->out, "}");
Michal Vaskobbdadda2022-01-06 11:40:10 +0100590 }
Radek Krejci5536d282020-08-04 23:27:44 +0200591 break;
592 case LYD_ANYDATA_JSON:
Michal Vasko76096ec2022-02-24 16:06:16 +0100593 if (!any->value.json) {
594 /* no content */
595 if (any->schema->nodetype == LYS_ANYXML) {
596 ly_print_(pctx->out, "null");
597 } else {
598 ly_print_(pctx->out, "{}");
599 }
600 } else {
601 /* print without escaping special characters */
602 ly_print_(pctx->out, "%s", any->value.json);
Radek Krejci5536d282020-08-04 23:27:44 +0200603 }
Radek Krejci5536d282020-08-04 23:27:44 +0200604 break;
605 case LYD_ANYDATA_STRING:
606 case LYD_ANYDATA_XML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100607 if (!any->value.str) {
608 /* no content */
609 if (any->schema->nodetype == LYS_ANYXML) {
610 ly_print_(pctx->out, "null");
611 } else {
612 ly_print_(pctx->out, "{}");
613 }
614 } else {
Michal Vaskobbdadda2022-01-06 11:40:10 +0100615 /* print as a string */
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100616 json_print_string(pctx->out, any->value.str);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100617 }
Michal Vasko76096ec2022-02-24 16:06:16 +0100618 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200619 case LYD_ANYDATA_LYB:
Michal Vasko76096ec2022-02-24 16:06:16 +0100620 /* LYB format is not supported */
621 LOGWRN(pctx->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100622 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200623 }
624
625 return LY_SUCCESS;
626}
627
628/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100629 * @brief Print content of a single container/list data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100630 * The envelope specific to nodes are expected to be printed by the caller.
Radek Krejci5536d282020-08-04 23:27:44 +0200631 *
632 * @param[in] ctx JSON printer context.
633 * @param[in] node Data node to print.
634 * @return LY_ERR value.
635 */
636static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100637json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200638{
639 struct lyd_node *child;
Michal Vasko26743a22022-03-29 14:48:10 +0200640 const struct lyd_node *prev_parent;
Michal Vasko23b51a82022-03-29 14:22:34 +0200641 struct lyd_node_opaq *opaq = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200642 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200643
Michal Vasko630d9892020-12-08 17:11:08 +0100644 LY_LIST_FOR(lyd_child(node), child) {
Michal Vasko8db584d2022-03-30 13:42:48 +0200645 if (lyd_node_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100646 break;
647 }
648 }
649 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200650 has_content = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200651 }
Michal Vasko23b51a82022-03-29 14:22:34 +0200652 if (!node->schema) {
653 opaq = (struct lyd_node_opaq *)node;
654 }
Radek Krejci5536d282020-08-04 23:27:44 +0200655
Michal Vasko1a85d332021-08-27 10:35:28 +0200656 if ((node->schema && (node->schema->nodetype == LYS_LIST)) ||
Michal Vasko23b51a82022-03-29 14:22:34 +0200657 (opaq && (opaq->hints != LYD_HINT_DATA) && (opaq->hints & LYD_NODEHINT_LIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100658 ly_print_(pctx->out, "%s%*s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ?
Michal Vasko1a85d332021-08-27 10:35:28 +0200659 (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : "");
660 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100661 ly_print_(pctx->out, "%s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200662 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200663 }
664 LEVEL_INC;
665
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100666 json_print_attributes(pctx, node, 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200667
Michal Vasko76096ec2022-02-24 16:06:16 +0100668 /* print children */
Michal Vasko26743a22022-03-29 14:48:10 +0200669 prev_parent = pctx->parent;
670 pctx->parent = node;
Michal Vasko76096ec2022-02-24 16:06:16 +0100671 LY_LIST_FOR(lyd_child(node), child) {
672 LY_CHECK_RET(json_print_node(pctx, child));
Radek Krejci5536d282020-08-04 23:27:44 +0200673 }
Michal Vasko26743a22022-03-29 14:48:10 +0200674 pctx->parent = prev_parent;
Radek Krejci5536d282020-08-04 23:27:44 +0200675
Radek Krejci5536d282020-08-04 23:27:44 +0200676 LEVEL_DEC;
677 if (DO_FORMAT && has_content) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100678 ly_print_(pctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200679 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100680 ly_print_(pctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200681 }
682 LEVEL_PRINTED;
683
684 return LY_SUCCESS;
685}
686
687/**
688 * @brief Print container data node including its metadata.
689 *
690 * @param[in] ctx JSON printer context.
691 * @param[in] node Data node to print.
692 * @return LY_ERR value.
693 */
694static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100695json_print_container(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200696{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100697 LY_CHECK_RET(json_print_member(pctx, node, 0));
698 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200699
700 return LY_SUCCESS;
701}
702
703/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100704 * @brief Print anydata/anyxml data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100705 *
706 * @param[in] ctx JSON printer context.
707 * @param[in] node Data node to print.
708 * @return LY_ERR value.
709 */
710static int
Michal Vasko76096ec2022-02-24 16:06:16 +0100711json_print_any(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskobbdadda2022-01-06 11:40:10 +0100712{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100713 LY_CHECK_RET(json_print_member(pctx, node, 0));
714 LY_CHECK_RET(json_print_any_content(pctx, (struct lyd_node_any *)node));
Michal Vaskobbdadda2022-01-06 11:40:10 +0100715 LEVEL_PRINTED;
716
717 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100718 json_print_attributes(pctx, node, 0);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100719
720 return LY_SUCCESS;
721}
722
723/**
Michal Vaskoaa22f422021-12-02 10:48:32 +0100724 * @brief Check whether a node is the last printed instance of a (leaf-)list.
725 *
726 * @param[in] ctx JSON printer context.
727 * @param[in] node Last printed node.
728 * @return Whether it is the last printed instance or not.
729 */
730static ly_bool
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100731json_print_array_is_last_inst(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskoaa22f422021-12-02 10:48:32 +0100732{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100733 if (!is_open_array(pctx, node)) {
Michal Vasko06217f32021-12-09 09:25:50 +0100734 /* no array open */
735 return 0;
736 }
737
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100738 if ((pctx->root == node) && !(pctx->options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100739 /* the only printed instance */
740 return 1;
741 }
742
Michal Vasko06217f32021-12-09 09:25:50 +0100743 if (!node->next || (node->next->schema != node->schema)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100744 /* last instance */
745 return 1;
746 }
747
748 return 0;
749}
750
751/**
Radek Krejci5536d282020-08-04 23:27:44 +0200752 * @brief Print single leaf-list or list instance.
753 *
754 * In case of list, metadata are printed inside the list object. For the leaf-list,
755 * metadata are marked in the context for later printing after closing the array next to it using
756 * json_print_metadata_leaflist().
757 *
758 * @param[in] ctx JSON printer context.
759 * @param[in] node Data node to print.
760 * @return LY_ERR value.
761 */
762static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100763json_print_leaf_list(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200764{
Michal Vasko57c8d432022-11-30 15:35:56 +0100765 const struct lys_module *wdmod = NULL;
766
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100767 if (!is_open_array(pctx, node)) {
768 LY_CHECK_RET(json_print_member(pctx, node, 0));
769 LY_CHECK_RET(json_print_array_open(pctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200770 if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100771 ly_print_(pctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200772 }
Radek Krejci5536d282020-08-04 23:27:44 +0200773 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100774 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200775 }
776
777 if (node->schema->nodetype == LYS_LIST) {
Michal Vasko2fe10342022-12-01 11:54:02 +0100778 /* print list's content */
779 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200780 } else {
781 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko57c8d432022-11-30 15:35:56 +0100782
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100783 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200784
Michal Vasko57c8d432022-11-30 15:35:56 +0100785 if (!pctx->print_sibling_metadata) {
786 if ((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
787 /* we have implicit OR explicit default node, get with-defaults module */
788 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
789 }
790 if (node->meta || wdmod) {
791 /* we will be printing metadata for these siblings */
792 pctx->print_sibling_metadata = node;
793 }
Radek Krejci5536d282020-08-04 23:27:44 +0200794 }
795 }
796
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100797 if (json_print_array_is_last_inst(pctx, node)) {
798 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200799 }
800
801 return LY_SUCCESS;
802}
803
804/**
805 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
806 * This function is supposed to be called when the leaf-list array is closed.
807 *
808 * @param[in] ctx JSON printer context.
809 * @return LY_ERR value.
810 */
811static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100812json_print_metadata_leaflist(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200813{
814 const struct lyd_node *prev, *node, *iter;
Michal Vasko57c8d432022-11-30 15:35:56 +0100815 const struct lys_module *wdmod = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200816
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100817 if (!pctx->print_sibling_metadata) {
Radek Krejci5536d282020-08-04 23:27:44 +0200818 return LY_SUCCESS;
819 }
820
Michal Vasko57c8d432022-11-30 15:35:56 +0100821 if (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG)) {
822 /* get with-defaults module */
823 wdmod = ly_ctx_get_module_implemented(pctx->ctx, "ietf-netconf-with-defaults");
824 }
825
826 /* node is the first instance of the leaf-list */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100827 for (node = pctx->print_sibling_metadata, prev = pctx->print_sibling_metadata->prev;
Radek Krejci5536d282020-08-04 23:27:44 +0200828 prev->next && matching_node(node, prev);
829 node = prev, prev = node->prev) {}
830
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100831 LY_CHECK_RET(json_print_member(pctx, node, 1));
832 ly_print_(pctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200833 LEVEL_INC;
834 LY_LIST_FOR(node, iter) {
835 PRINT_COMMA;
Michal Vasko57c8d432022-11-30 15:35:56 +0100836 if (iter->meta || (iter->flags & LYD_DEFAULT)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100837 ly_print_(pctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200838 LEVEL_INC;
Michal Vasko57c8d432022-11-30 15:35:56 +0100839 LY_CHECK_RET(json_print_metadata(pctx, iter, (iter->flags & LYD_DEFAULT) ? wdmod : NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200840 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100841 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200842 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100843 ly_print_(pctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200844 }
845 LEVEL_PRINTED;
846 if (!matching_node(iter, iter->next)) {
847 break;
848 }
849 }
850 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100851 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200852 LEVEL_PRINTED;
853
854 return LY_SUCCESS;
855}
856
857/**
858 * @brief Print opaq data node including its attributes.
859 *
860 * @param[in] ctx JSON printer context.
861 * @param[in] node Opaq node to print.
862 * @return LY_ERR value.
863 */
864static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100865json_print_opaq(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200866{
Radek Krejci857189e2020-09-01 13:26:36 +0200867 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200868
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200869 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100870 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200871 first = 0;
872 }
Michal Vasko9e685082021-01-29 14:49:09 +0100873 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200874 last = 0;
875 }
876 }
877
878 if (first) {
Michal Vasko26743a22022-03-29 14:48:10 +0200879 LY_CHECK_RET(json_print_member2(pctx, pctx->parent, node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200880
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200881 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100882 LY_CHECK_RET(json_print_array_open(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200883 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200884 if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100885 ly_print_(pctx->out, "%*s", INDENT);
Michal Vasko1a85d332021-08-27 10:35:28 +0200886 }
887 } else if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100888 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200889 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200890 if (node->child || (node->hints & LYD_NODEHINT_LIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100891 LY_CHECK_RET(json_print_inner(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200892 LEVEL_PRINTED;
893 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200894 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100895 ly_print_(pctx->out, "[null]");
Michal Vaskocea58712022-04-01 14:37:08 +0200896 } else if ((node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) && !(node->hints & LYD_VALHINT_NUM64)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100897 ly_print_(pctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200898 } else {
Michal Vaskocea58712022-04-01 14:37:08 +0200899 /* string or a large number */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100900 ly_print_(pctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200901 }
902 LEVEL_PRINTED;
903
904 /* attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100905 json_print_attributes(pctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200906
907 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200908 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100909 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200910 LEVEL_PRINTED;
911 }
912
913 return LY_SUCCESS;
914}
915
916/**
917 * @brief Print all the types of data node including its metadata.
918 *
919 * @param[in] ctx JSON printer context.
920 * @param[in] node Data node to print.
921 * @return LY_ERR value.
922 */
923static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100924json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200925{
Michal Vasko8db584d2022-03-30 13:42:48 +0200926 if (!lyd_node_should_print(node, pctx->options)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100927 if (json_print_array_is_last_inst(pctx, node)) {
928 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200929 }
930 return LY_SUCCESS;
931 }
932
933 if (!node->schema) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100934 LY_CHECK_RET(json_print_opaq(pctx, (const struct lyd_node_opaq *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200935 } else {
936 switch (node->schema->nodetype) {
937 case LYS_RPC:
938 case LYS_ACTION:
939 case LYS_NOTIF:
940 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100941 LY_CHECK_RET(json_print_container(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200942 break;
943 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100944 LY_CHECK_RET(json_print_leaf(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200945 break;
946 case LYS_LEAFLIST:
947 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100948 LY_CHECK_RET(json_print_leaf_list(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200949 break;
Michal Vasko76096ec2022-02-24 16:06:16 +0100950 case LYS_ANYDATA:
Radek Krejci5536d282020-08-04 23:27:44 +0200951 case LYS_ANYXML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100952 LY_CHECK_RET(json_print_any(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200953 break;
954 default:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100955 LOGINT(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200956 return EXIT_FAILURE;
957 }
958 }
959
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100960 pctx->level_printed = pctx->level;
Radek Krejci5536d282020-08-04 23:27:44 +0200961
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100962 if (pctx->print_sibling_metadata && !matching_node(node->next, pctx->print_sibling_metadata)) {
963 json_print_metadata_leaflist(pctx);
964 pctx->print_sibling_metadata = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200965 }
966
967 return LY_SUCCESS;
968}
969
970LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200971json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200972{
973 const struct lyd_node *node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100974 struct jsonpr_ctx pctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200975 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200976
Radek Krejci2e874772020-08-28 16:36:33 +0200977 if (!root) {
978 ly_print_(out, "{}%s", delimiter);
979 ly_print_flush(out);
980 return LY_SUCCESS;
981 }
982
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100983 pctx.out = out;
Michal Vasko26743a22022-03-29 14:48:10 +0200984 pctx.parent = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100985 pctx.level = 1;
986 pctx.level_printed = 0;
987 pctx.options = options;
988 pctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200989
990 /* start */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100991 ly_print_(pctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200992
993 /* content */
994 LY_LIST_FOR(root, node) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100995 pctx.root = node;
996 LY_CHECK_RET(json_print_node(&pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200997 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
998 break;
999 }
1000 }
1001
1002 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +02001003 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +02001004
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001005 assert(!pctx.open.count);
1006 ly_set_erase(&pctx.open, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +02001007
1008 ly_print_flush(out);
1009 return LY_SUCCESS;
1010}