blob: 3c7a6fd3682696363d9166ead32b846102748b0f [file] [log] [blame]
Radek Krejci13a57b62019-07-19 13:04:09 +02001/**
Michal Vasko90932a92020-02-12 14:33:03 +01002 * @file printer_json.c
Radek Krejci13a57b62019-07-19 13:04:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01004 * @brief JSON printer for libyang data structure
Radek Krejci13a57b62019-07-19 13:04:09 +02005 *
Radek Krejci5536d282020-08-04 23:27:44 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejci13a57b62019-07-19 13:04:09 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci5536d282020-08-04 23:27:44 +020015#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010016#include <stdint.h>
Radek Krejci5536d282020-08-04 23:27:44 +020017#include <stdlib.h>
Radek Krejci5536d282020-08-04 23:27:44 +020018
Radek Krejci13a57b62019-07-19 13:04:09 +020019#include "common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010020#include "context.h"
Radek Krejci5536d282020-08-04 23:27:44 +020021#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010022#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "out_internal.h"
Radek Krejci5536d282020-08-04 23:27:44 +020024#include "parser_data.h"
Michal Vaskob4750962022-10-06 15:33:35 +020025#include "plugins_exts/metadata.h"
Radek Krejci5536d282020-08-04 23:27:44 +020026#include "plugins_types.h"
27#include "printer_data.h"
28#include "printer_internal.h"
29#include "set.h"
30#include "tree.h"
31#include "tree_data.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020032#include "tree_schema.h"
33
34/**
Radek Krejci5536d282020-08-04 23:27:44 +020035 * @brief JSON printer context.
36 */
37struct jsonpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020038 struct ly_out *out; /**< output specification */
Michal Vaskoaa22f422021-12-02 10:48:32 +010039 const struct lyd_node *root; /**< root node of the subtree being printed */
Michal Vasko26743a22022-03-29 14:48:10 +020040 const struct lyd_node *parent; /**< parent of the node being printed */
Radek Krejci1deb5be2020-08-26 16:43:36 +020041 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
42 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
43 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020044
Radek Krejci1deb5be2020-08-26 16:43:36 +020045 uint16_t level_printed; /* level where some data were already printed */
Michal Vaskoaa22f422021-12-02 10:48:32 +010046 struct ly_set open; /* currently open array(s) */
Radek Krejci5536d282020-08-04 23:27:44 +020047 const struct lyd_node *print_sibling_metadata;
48};
49
50/**
51 * @brief Mark that something was already written in the current level,
52 * used to decide if a comma is expected between the items
53 */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010054#define LEVEL_PRINTED pctx->level_printed = pctx->level
Radek Krejci5536d282020-08-04 23:27:44 +020055
56#define PRINT_COMMA \
Michal Vasko61ad1ff2022-02-10 15:48:39 +010057 if (pctx->level_printed >= pctx->level) { \
58 ly_print_(pctx->out, ",%s", (DO_FORMAT ? "\n" : "")); \
Radek Krejci5536d282020-08-04 23:27:44 +020059 }
60
Michal Vasko61ad1ff2022-02-10 15:48:39 +010061static LY_ERR json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node);
Radek Krejci5536d282020-08-04 23:27:44 +020062
63/**
Michal Vaskob2c61572022-05-20 10:35:33 +020064 * @brief Compare 2 nodes, despite it is regular data node or an opaq node, and
Radek Krejci5536d282020-08-04 23:27:44 +020065 * decide if they corresponds to the same schema node.
66 *
Radek Krejci5536d282020-08-04 23:27:44 +020067 * @return 1 - matching nodes, 0 - non-matching nodes
68 */
69static int
70matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
71{
72 assert(node1 || node2);
73
74 if (!node1 || !node2) {
75 return 0;
76 } else if (node1->schema != node2->schema) {
77 return 0;
78 }
79 if (!node1->schema) {
80 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020081 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
82 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vasko26bbb272022-08-02 14:54:33 +020083
Michal Vaskoad92b672020-11-12 13:11:31 +010084 if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) {
Radek Krejci5536d282020-08-04 23:27:44 +020085 return 0;
86 }
87 }
88
89 return 1;
90}
91
92/**
93 * @brief Open the JSON array ('[') for the specified @p node
94 *
95 * @param[in] ctx JSON printer context.
96 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020097 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020098 */
Michal Vaskob0099a92020-08-31 14:55:23 +020099static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100100json_print_array_open(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200101{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100102 ly_print_(pctx->out, "[%s", DO_FORMAT ? "\n" : "");
103 LY_CHECK_RET(ly_set_add(&pctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200104 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200105
106 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200107}
108
109/**
110 * @brief Get know if the array for the provided @p node is currently open.
111 *
112 * @param[in] ctx JSON printer context.
113 * @param[in] node Data node to check.
114 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
115 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
116 */
117static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100118is_open_array(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200119{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100120 if (pctx->open.count && matching_node(node, pctx->open.dnodes[pctx->open.count - 1])) {
Radek Krejci5536d282020-08-04 23:27:44 +0200121 return 1;
122 } else {
123 return 0;
124 }
125}
126
127/**
128 * @brief Close the most inner JSON array.
129 *
130 * @param[in] ctx JSON printer context.
131 */
132static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100133json_print_array_close(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200134{
Radek Krejci5536d282020-08-04 23:27:44 +0200135 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100136 ly_set_rm_index(&pctx->open, pctx->open.count - 1, NULL);
137 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200138}
139
140/**
141 * @brief Get the node's module name to use as the @p node prefix in JSON.
Radek Krejci31bc3f52021-04-26 11:09:58 +0200142 *
Radek Krejci5536d282020-08-04 23:27:44 +0200143 * @param[in] node Node to process.
144 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
145 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
146 */
147static const char *
148node_prefix(const struct lyd_node *node)
149{
150 if (node->schema) {
151 return node->schema->module->name;
152 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200153 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200154 const struct lys_module *mod;
155
156 switch (onode->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200157 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100158 return onode->name.module_name;
Radek Krejci8df109d2021-04-23 12:19:08 +0200159 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100160 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200161 if (!mod) {
162 return NULL;
163 }
164 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100165 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200166 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200167 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200168 }
169 }
170
171 return NULL;
172}
173
174/**
175 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
176 *
177 * Accepts both regulard a well as opaq nodes.
178 *
179 * @param[in] node1 The first node to compare.
180 * @param[in] node2 The second node to compare.
181 * @return 0 in case the nodes' modules are the same
182 * @return 1 in case the nodes belongs to different modules
183 */
184int
185json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
186{
187 assert(node1 || node2);
188
189 if (!node1 || !node2) {
190 return 1;
191 } else if (node1->schema && node2->schema) {
192 if (node1->schema->module == node2->schema->module) {
193 /* belongs to the same module */
194 return 0;
195 } else {
196 /* different modules */
197 return 1;
198 }
199 } else {
200 const char *pref1 = node_prefix(node1);
201 const char *pref2 = node_prefix(node2);
Michal Vasko26bbb272022-08-02 14:54:33 +0200202
Michal Vasko69730152020-10-09 16:30:07 +0200203 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200204 return 0;
205 } else {
206 return 1;
207 }
208 }
209}
210
211/**
212 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
213 *
214 * @param[in] out The output handler.
215 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200216 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200217 */
Michal Vasko5233e962020-08-14 14:26:20 +0200218static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200219json_print_string(struct ly_out *out, const char *text)
220{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200221 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200222
223 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200224 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200225 }
226
Michal Vasko5233e962020-08-14 14:26:20 +0200227 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200228 for (i = n = 0; text[i]; i++) {
229 const unsigned char ascii = text[i];
Michal Vasko26bbb272022-08-02 14:54:33 +0200230
Radek Krejci5536d282020-08-04 23:27:44 +0200231 if (ascii < 0x20) {
232 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200233 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200234 } else {
235 switch (ascii) {
236 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200237 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200238 break;
239 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200240 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200241 break;
242 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200243 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200244 n++;
245 }
246 }
247 }
Michal Vasko5233e962020-08-14 14:26:20 +0200248 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200249
Michal Vasko5233e962020-08-14 14:26:20 +0200250 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200251}
252
253/**
254 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
255 *
256 * @param[in] ctx JSON printer context.
257 * @param[in] node The data node being printed.
258 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
259 * @return LY_ERR value.
260 */
261static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100262json_print_member(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200263{
264 PRINT_COMMA;
Michal Vasko26743a22022-03-29 14:48:10 +0200265 if ((LEVEL == 1) || json_nscmp(node, pctx->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200266 /* print "namespace" */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100267 ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200268 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200269 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100270 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200271 }
272
273 return LY_SUCCESS;
274}
275
276/**
277 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
278 *
279 * @param[in] ctx JSON printer context.
280 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
281 * @param[in] format Format to decide how to process the @p prefix.
Michal Vaskoad92b672020-11-12 13:11:31 +0100282 * @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 +0200283 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
284 * @return LY_ERR value.
285 */
286static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100287json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VALUE_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100288 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200289{
Michal Vaskoad92b672020-11-12 13:11:31 +0100290 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200291
292 PRINT_COMMA;
293
294 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100295 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200296 const struct lys_module *mod;
297
298 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200299 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100300 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200301 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200302 case LY_VALUE_XML:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100303 mod = ly_ctx_get_module_implemented_ns(pctx->ctx, name->module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200304 if (mod) {
305 module_name = mod->name;
306 }
307 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100308 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200309 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100310 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200311 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100312
313 name_str = name->name;
314 } else {
315 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200316 }
317
318 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200319 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100320 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 +0200321 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100322 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200323 }
324
325 return LY_SUCCESS;
326}
327
328/**
329 * @brief Print data value.
330 *
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100331 * @param[in] pctx JSON printer context.
332 * @param[in] ctx Context used to print the value.
Radek Krejci5536d282020-08-04 23:27:44 +0200333 * @param[in] val Data value to be printed.
334 * @return LY_ERR value.
335 */
336static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100337json_print_value(struct jsonpr_ctx *pctx, const struct ly_ctx *ctx, const struct lyd_value *val)
Radek Krejci5536d282020-08-04 23:27:44 +0200338{
aPiecek0f6bf3e2021-08-25 15:47:49 +0200339 ly_bool dynamic;
Michal Vasko183b9112021-11-04 16:02:24 +0100340 LY_DATA_TYPE basetype;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100341 const char *value = val->realtype->plugin->print(ctx, val, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200342
Michal Vasko183b9112021-11-04 16:02:24 +0100343 basetype = val->realtype->basetype;
344
345print_val:
Radek Krejci5536d282020-08-04 23:27:44 +0200346 /* leafref is not supported */
Michal Vasko183b9112021-11-04 16:02:24 +0100347 switch (basetype) {
348 case LY_TYPE_UNION:
349 /* use the resolved type */
350 basetype = val->subvalue->value.realtype->basetype;
351 goto print_val;
352
Radek Krejci5536d282020-08-04 23:27:44 +0200353 case LY_TYPE_BINARY:
354 case LY_TYPE_STRING:
355 case LY_TYPE_BITS:
356 case LY_TYPE_ENUM:
357 case LY_TYPE_INST:
358 case LY_TYPE_INT64:
359 case LY_TYPE_UINT64:
360 case LY_TYPE_DEC64:
361 case LY_TYPE_IDENT:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100362 json_print_string(pctx->out, value);
Radek Krejci5536d282020-08-04 23:27:44 +0200363 break;
364
365 case LY_TYPE_INT8:
366 case LY_TYPE_INT16:
367 case LY_TYPE_INT32:
368 case LY_TYPE_UINT8:
369 case LY_TYPE_UINT16:
370 case LY_TYPE_UINT32:
371 case LY_TYPE_BOOL:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100372 ly_print_(pctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200373 break;
374
375 case LY_TYPE_EMPTY:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100376 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200377 break;
378
379 default:
380 /* error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100381 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200382 }
383
384 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200385 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200386 }
387
388 return LY_SUCCESS;
389}
390
391/**
392 * @brief Print all the attributes of the opaq node.
393 *
394 * @param[in] ctx JSON printer context.
395 * @param[in] node Opaq node where the attributes are placed.
396 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
397 * @return LY_ERR value.
398 */
399static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100400json_print_attribute(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200401{
402 struct lyd_attr *attr;
403
404 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200405 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200406 LEVEL_PRINTED;
407 }
408
409 for (attr = node->attr; attr; attr = attr->next) {
410 PRINT_COMMA;
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 Vaskofeca4fb2020-10-05 08:58:40 +0200413 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100414 ly_print_(pctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200415 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100416 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200417 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100418 json_print_string(pctx->out, attr->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200419 }
420 LEVEL_PRINTED;
421 }
422
423 return LY_SUCCESS;
424}
425
426/**
427 * @brief Print all the metadata of the node.
428 *
429 * @param[in] ctx JSON printer context.
430 * @param[in] node Node where the metadata are placed.
431 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
432 * @return LY_ERR value.
433 */
434static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100435json_print_metadata(struct jsonpr_ctx *pctx, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200436{
437 struct lyd_meta *meta;
438
439 if (wdmod) {
Michal Vaskob68c3b42022-05-20 10:36:40 +0200440 ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200441 LEVEL_PRINTED;
442 }
443
444 for (meta = node->meta; meta; meta = meta->next) {
445 PRINT_COMMA;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100446 ly_print_(pctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
447 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &meta->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200448 LEVEL_PRINTED;
449 }
450
451 return LY_SUCCESS;
452}
453
454/**
455 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
456 *
457 * @param[in] ctx JSON printer context.
458 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200459 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200460 * @return LY_ERR value.
461 */
462static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100463json_print_attributes(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200464{
465 const struct lys_module *wdmod = NULL;
466
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100467 if ((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200468 /* we have implicit OR explicit default node */
469 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200470 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200471 }
472
Michal Vaskob68c3b42022-05-20 10:36:40 +0200473 if (node->schema && (node->meta || wdmod)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200474 if (inner) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100475 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200476 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100477 LY_CHECK_RET(json_print_member(pctx, node, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200478 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100479 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200480 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100481 LY_CHECK_RET(json_print_metadata(pctx, node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200482 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100483 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200484 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200485 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200486 if (inner) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100487 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200488 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100489 LY_CHECK_RET(json_print_member2(pctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100490 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200491 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100492 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200493 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100494 LY_CHECK_RET(json_print_attribute(pctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200495 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100496 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200497 LEVEL_PRINTED;
498 }
499
500 return LY_SUCCESS;
501}
502
503/**
504 * @brief Print leaf data node including its metadata.
505 *
506 * @param[in] ctx JSON printer context.
507 * @param[in] node Data node to print.
508 * @return LY_ERR value.
509 */
510static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100511json_print_leaf(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200512{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100513 LY_CHECK_RET(json_print_member(pctx, node, 0));
514 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200515 LEVEL_PRINTED;
516
517 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100518 json_print_attributes(pctx, node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200519
520 return LY_SUCCESS;
521}
522
523/**
Michal Vaskobbdadda2022-01-06 11:40:10 +0100524 * @brief Print anydata/anyxml content.
Radek Krejci5536d282020-08-04 23:27:44 +0200525 *
526 * @param[in] ctx JSON printer context.
527 * @param[in] any Anydata node to print.
528 * @return LY_ERR value.
529 */
530static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100531json_print_any_content(struct jsonpr_ctx *pctx, struct lyd_node_any *any)
Radek Krejci5536d282020-08-04 23:27:44 +0200532{
533 LY_ERR ret = LY_SUCCESS;
534 struct lyd_node *iter;
Michal Vasko26743a22022-03-29 14:48:10 +0200535 const struct lyd_node *prev_parent;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200536 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200537
Michal Vasko76096ec2022-02-24 16:06:16 +0100538 assert(any->schema->nodetype & LYD_NODE_ANY);
Radek Krejci5536d282020-08-04 23:27:44 +0200539
540 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200541 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200542
543 /* turn logging off */
544 prev_lo = ly_log_options(0);
545
546 /* try to parse it into a data tree */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100547 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 +0200548 /* successfully parsed */
549 free(any->value.mem);
550 any->value.tree = iter;
551 any->value_type = LYD_ANYDATA_DATATREE;
552 }
553
554 /* turn loggin on again */
555 ly_log_options(prev_lo);
556 }
557
558 switch (any->value_type) {
559 case LYD_ANYDATA_DATATREE:
Michal Vasko76096ec2022-02-24 16:06:16 +0100560 /* print as an object */
561 ly_print_(pctx->out, "{%s", DO_FORMAT ? "\n" : "");
562 LEVEL_INC;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100563
Radek Krejci5536d282020-08-04 23:27:44 +0200564 /* close opening tag and print data */
Michal Vasko26743a22022-03-29 14:48:10 +0200565 prev_parent = pctx->parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100566 prev_opts = pctx->options;
Michal Vasko26743a22022-03-29 14:48:10 +0200567 pctx->parent = &any->node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100568 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci5536d282020-08-04 23:27:44 +0200569 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100570 ret = json_print_node(pctx, iter);
Radek Krejci5536d282020-08-04 23:27:44 +0200571 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
572 }
Michal Vasko26743a22022-03-29 14:48:10 +0200573 pctx->parent = prev_parent;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100574 pctx->options = prev_opts;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100575
Michal Vasko76096ec2022-02-24 16:06:16 +0100576 /* terminate the object */
Michal Vasko23b51a82022-03-29 14:22:34 +0200577 LEVEL_DEC;
Michal Vasko76096ec2022-02-24 16:06:16 +0100578 if (DO_FORMAT) {
579 ly_print_(pctx->out, "\n%*s}", INDENT);
580 } else {
581 ly_print_(pctx->out, "}");
Michal Vaskobbdadda2022-01-06 11:40:10 +0100582 }
Radek Krejci5536d282020-08-04 23:27:44 +0200583 break;
584 case LYD_ANYDATA_JSON:
Michal Vasko76096ec2022-02-24 16:06:16 +0100585 if (!any->value.json) {
586 /* no content */
587 if (any->schema->nodetype == LYS_ANYXML) {
588 ly_print_(pctx->out, "null");
589 } else {
590 ly_print_(pctx->out, "{}");
591 }
592 } else {
593 /* print without escaping special characters */
594 ly_print_(pctx->out, "%s", any->value.json);
Radek Krejci5536d282020-08-04 23:27:44 +0200595 }
Radek Krejci5536d282020-08-04 23:27:44 +0200596 break;
597 case LYD_ANYDATA_STRING:
598 case LYD_ANYDATA_XML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100599 if (!any->value.str) {
600 /* no content */
601 if (any->schema->nodetype == LYS_ANYXML) {
602 ly_print_(pctx->out, "null");
603 } else {
604 ly_print_(pctx->out, "{}");
605 }
606 } else {
Michal Vaskobbdadda2022-01-06 11:40:10 +0100607 /* print as a string */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100608 ly_print_(pctx->out, "\"%s\"", any->value.str);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100609 }
Michal Vasko76096ec2022-02-24 16:06:16 +0100610 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200611 case LYD_ANYDATA_LYB:
Michal Vasko76096ec2022-02-24 16:06:16 +0100612 /* LYB format is not supported */
613 LOGWRN(pctx->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100614 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200615 }
616
617 return LY_SUCCESS;
618}
619
620/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100621 * @brief Print content of a single container/list data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100622 * The envelope specific to nodes are expected to be printed by the caller.
Radek Krejci5536d282020-08-04 23:27:44 +0200623 *
624 * @param[in] ctx JSON printer context.
625 * @param[in] node Data node to print.
626 * @return LY_ERR value.
627 */
628static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100629json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200630{
631 struct lyd_node *child;
Michal Vasko26743a22022-03-29 14:48:10 +0200632 const struct lyd_node *prev_parent;
Michal Vasko23b51a82022-03-29 14:22:34 +0200633 struct lyd_node_opaq *opaq = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200634 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200635
Michal Vasko630d9892020-12-08 17:11:08 +0100636 LY_LIST_FOR(lyd_child(node), child) {
Michal Vasko8db584d2022-03-30 13:42:48 +0200637 if (lyd_node_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100638 break;
639 }
640 }
641 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200642 has_content = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200643 }
Michal Vasko23b51a82022-03-29 14:22:34 +0200644 if (!node->schema) {
645 opaq = (struct lyd_node_opaq *)node;
646 }
Radek Krejci5536d282020-08-04 23:27:44 +0200647
Michal Vasko1a85d332021-08-27 10:35:28 +0200648 if ((node->schema && (node->schema->nodetype == LYS_LIST)) ||
Michal Vasko23b51a82022-03-29 14:22:34 +0200649 (opaq && (opaq->hints != LYD_HINT_DATA) && (opaq->hints & LYD_NODEHINT_LIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100650 ly_print_(pctx->out, "%s%*s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ?
Michal Vasko1a85d332021-08-27 10:35:28 +0200651 (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : "");
652 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100653 ly_print_(pctx->out, "%s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200654 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200655 }
656 LEVEL_INC;
657
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100658 json_print_attributes(pctx, node, 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200659
Michal Vasko76096ec2022-02-24 16:06:16 +0100660 /* print children */
Michal Vasko26743a22022-03-29 14:48:10 +0200661 prev_parent = pctx->parent;
662 pctx->parent = node;
Michal Vasko76096ec2022-02-24 16:06:16 +0100663 LY_LIST_FOR(lyd_child(node), child) {
664 LY_CHECK_RET(json_print_node(pctx, child));
Radek Krejci5536d282020-08-04 23:27:44 +0200665 }
Michal Vasko26743a22022-03-29 14:48:10 +0200666 pctx->parent = prev_parent;
Radek Krejci5536d282020-08-04 23:27:44 +0200667
Radek Krejci5536d282020-08-04 23:27:44 +0200668 LEVEL_DEC;
669 if (DO_FORMAT && has_content) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100670 ly_print_(pctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200671 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100672 ly_print_(pctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200673 }
674 LEVEL_PRINTED;
675
676 return LY_SUCCESS;
677}
678
679/**
680 * @brief Print container data node including its metadata.
681 *
682 * @param[in] ctx JSON printer context.
683 * @param[in] node Data node to print.
684 * @return LY_ERR value.
685 */
686static int
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100687json_print_container(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200688{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100689 LY_CHECK_RET(json_print_member(pctx, node, 0));
690 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200691
692 return LY_SUCCESS;
693}
694
695/**
Michal Vasko76096ec2022-02-24 16:06:16 +0100696 * @brief Print anydata/anyxml data node including its metadata.
Michal Vaskobbdadda2022-01-06 11:40:10 +0100697 *
698 * @param[in] ctx JSON printer context.
699 * @param[in] node Data node to print.
700 * @return LY_ERR value.
701 */
702static int
Michal Vasko76096ec2022-02-24 16:06:16 +0100703json_print_any(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskobbdadda2022-01-06 11:40:10 +0100704{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100705 LY_CHECK_RET(json_print_member(pctx, node, 0));
706 LY_CHECK_RET(json_print_any_content(pctx, (struct lyd_node_any *)node));
Michal Vaskobbdadda2022-01-06 11:40:10 +0100707 LEVEL_PRINTED;
708
709 /* print attributes as sibling */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100710 json_print_attributes(pctx, node, 0);
Michal Vaskobbdadda2022-01-06 11:40:10 +0100711
712 return LY_SUCCESS;
713}
714
715/**
Michal Vaskoaa22f422021-12-02 10:48:32 +0100716 * @brief Check whether a node is the last printed instance of a (leaf-)list.
717 *
718 * @param[in] ctx JSON printer context.
719 * @param[in] node Last printed node.
720 * @return Whether it is the last printed instance or not.
721 */
722static ly_bool
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100723json_print_array_is_last_inst(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskoaa22f422021-12-02 10:48:32 +0100724{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100725 if (!is_open_array(pctx, node)) {
Michal Vasko06217f32021-12-09 09:25:50 +0100726 /* no array open */
727 return 0;
728 }
729
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100730 if ((pctx->root == node) && !(pctx->options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100731 /* the only printed instance */
732 return 1;
733 }
734
Michal Vasko06217f32021-12-09 09:25:50 +0100735 if (!node->next || (node->next->schema != node->schema)) {
Michal Vaskoaa22f422021-12-02 10:48:32 +0100736 /* last instance */
737 return 1;
738 }
739
740 return 0;
741}
742
743/**
Radek Krejci5536d282020-08-04 23:27:44 +0200744 * @brief Print single leaf-list or list instance.
745 *
746 * In case of list, metadata are printed inside the list object. For the leaf-list,
747 * metadata are marked in the context for later printing after closing the array next to it using
748 * json_print_metadata_leaflist().
749 *
750 * @param[in] ctx JSON printer context.
751 * @param[in] node Data node to print.
752 * @return LY_ERR value.
753 */
754static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100755json_print_leaf_list(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200756{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100757 if (!is_open_array(pctx, node)) {
758 LY_CHECK_RET(json_print_member(pctx, node, 0));
759 LY_CHECK_RET(json_print_array_open(pctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200760 if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100761 ly_print_(pctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200762 }
Radek Krejci5536d282020-08-04 23:27:44 +0200763 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100764 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200765 }
766
767 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200768 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200769 /* empty, e.g. in case of filter */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100770 ly_print_(pctx->out, "%s%snull", (pctx->level_printed >= pctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200771 LEVEL_PRINTED;
772 } else {
773 /* print list's content */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100774 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200775 }
776 } else {
777 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100778 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200779
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100780 if (node->meta && !pctx->print_sibling_metadata) {
781 pctx->print_sibling_metadata = node;
Radek Krejci5536d282020-08-04 23:27:44 +0200782 }
783 }
784
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100785 if (json_print_array_is_last_inst(pctx, node)) {
786 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200787 }
788
789 return LY_SUCCESS;
790}
791
792/**
793 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
794 * This function is supposed to be called when the leaf-list array is closed.
795 *
796 * @param[in] ctx JSON printer context.
797 * @return LY_ERR value.
798 */
799static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100800json_print_metadata_leaflist(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200801{
802 const struct lyd_node *prev, *node, *iter;
803
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100804 if (!pctx->print_sibling_metadata) {
Radek Krejci5536d282020-08-04 23:27:44 +0200805 return LY_SUCCESS;
806 }
807
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100808 for (node = pctx->print_sibling_metadata, prev = pctx->print_sibling_metadata->prev;
Radek Krejci5536d282020-08-04 23:27:44 +0200809 prev->next && matching_node(node, prev);
810 node = prev, prev = node->prev) {}
811
812 /* node is the first instance of the leaf-list */
813
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100814 LY_CHECK_RET(json_print_member(pctx, node, 1));
815 ly_print_(pctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200816 LEVEL_INC;
817 LY_LIST_FOR(node, iter) {
818 PRINT_COMMA;
819 if (iter->meta) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100820 ly_print_(pctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200821 LEVEL_INC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100822 LY_CHECK_RET(json_print_metadata(pctx, iter, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200823 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100824 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200825 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100826 ly_print_(pctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200827 }
828 LEVEL_PRINTED;
829 if (!matching_node(iter, iter->next)) {
830 break;
831 }
832 }
833 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100834 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200835 LEVEL_PRINTED;
836
837 return LY_SUCCESS;
838}
839
840/**
841 * @brief Print opaq data node including its attributes.
842 *
843 * @param[in] ctx JSON printer context.
844 * @param[in] node Opaq node to print.
845 * @return LY_ERR value.
846 */
847static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100848json_print_opaq(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200849{
Radek Krejci857189e2020-09-01 13:26:36 +0200850 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200851
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200852 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100853 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200854 first = 0;
855 }
Michal Vasko9e685082021-01-29 14:49:09 +0100856 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200857 last = 0;
858 }
859 }
860
861 if (first) {
Michal Vasko26743a22022-03-29 14:48:10 +0200862 LY_CHECK_RET(json_print_member2(pctx, pctx->parent, node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200863
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200864 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100865 LY_CHECK_RET(json_print_array_open(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200866 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200867 if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100868 ly_print_(pctx->out, "%*s", INDENT);
Michal Vasko1a85d332021-08-27 10:35:28 +0200869 }
870 } else if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100871 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200872 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200873 if (node->child || (node->hints & LYD_NODEHINT_LIST)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100874 LY_CHECK_RET(json_print_inner(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200875 LEVEL_PRINTED;
876 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200877 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100878 ly_print_(pctx->out, "[null]");
Michal Vaskocea58712022-04-01 14:37:08 +0200879 } else if ((node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) && !(node->hints & LYD_VALHINT_NUM64)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100880 ly_print_(pctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200881 } else {
Michal Vaskocea58712022-04-01 14:37:08 +0200882 /* string or a large number */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100883 ly_print_(pctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200884 }
885 LEVEL_PRINTED;
886
887 /* attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100888 json_print_attributes(pctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200889
890 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200891 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100892 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200893 LEVEL_PRINTED;
894 }
895
896 return LY_SUCCESS;
897}
898
899/**
900 * @brief Print all the types of data node including its metadata.
901 *
902 * @param[in] ctx JSON printer context.
903 * @param[in] node Data node to print.
904 * @return LY_ERR value.
905 */
906static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100907json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200908{
Michal Vasko8db584d2022-03-30 13:42:48 +0200909 if (!lyd_node_should_print(node, pctx->options)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100910 if (json_print_array_is_last_inst(pctx, node)) {
911 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200912 }
913 return LY_SUCCESS;
914 }
915
916 if (!node->schema) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100917 LY_CHECK_RET(json_print_opaq(pctx, (const struct lyd_node_opaq *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200918 } else {
919 switch (node->schema->nodetype) {
920 case LYS_RPC:
921 case LYS_ACTION:
922 case LYS_NOTIF:
923 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100924 LY_CHECK_RET(json_print_container(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200925 break;
926 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100927 LY_CHECK_RET(json_print_leaf(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200928 break;
929 case LYS_LEAFLIST:
930 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100931 LY_CHECK_RET(json_print_leaf_list(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200932 break;
Michal Vasko76096ec2022-02-24 16:06:16 +0100933 case LYS_ANYDATA:
Radek Krejci5536d282020-08-04 23:27:44 +0200934 case LYS_ANYXML:
Michal Vasko76096ec2022-02-24 16:06:16 +0100935 LY_CHECK_RET(json_print_any(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200936 break;
937 default:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100938 LOGINT(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200939 return EXIT_FAILURE;
940 }
941 }
942
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100943 pctx->level_printed = pctx->level;
Radek Krejci5536d282020-08-04 23:27:44 +0200944
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100945 if (pctx->print_sibling_metadata && !matching_node(node->next, pctx->print_sibling_metadata)) {
946 json_print_metadata_leaflist(pctx);
947 pctx->print_sibling_metadata = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200948 }
949
950 return LY_SUCCESS;
951}
952
953LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200954json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200955{
956 const struct lyd_node *node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100957 struct jsonpr_ctx pctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200958 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200959
Radek Krejci2e874772020-08-28 16:36:33 +0200960 if (!root) {
961 ly_print_(out, "{}%s", delimiter);
962 ly_print_flush(out);
963 return LY_SUCCESS;
964 }
965
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100966 pctx.out = out;
Michal Vasko26743a22022-03-29 14:48:10 +0200967 pctx.parent = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100968 pctx.level = 1;
969 pctx.level_printed = 0;
970 pctx.options = options;
971 pctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200972
973 /* start */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100974 ly_print_(pctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200975
976 /* content */
977 LY_LIST_FOR(root, node) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100978 pctx.root = node;
979 LY_CHECK_RET(json_print_node(&pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200980 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
981 break;
982 }
983 }
984
985 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200986 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200987
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100988 assert(!pctx.open.count);
989 ly_set_erase(&pctx.open, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200990
991 ly_print_flush(out);
992 return LY_SUCCESS;
993}