blob: 3ac61c85f6b5fcf7cb29cca62765cf6559b4e675 [file] [log] [blame]
Radek Krejci13a57b62019-07-19 13:04:09 +02001/**
Michal Vasko90932a92020-02-12 14:33:03 +01002 * @file printer_json.c
Radek Krejci13a57b62019-07-19 13:04:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01004 * @brief JSON printer for libyang data structure
Radek Krejci13a57b62019-07-19 13:04:09 +02005 *
Radek Krejci5536d282020-08-04 23:27:44 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejci13a57b62019-07-19 13:04:09 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci5536d282020-08-04 23:27:44 +020015#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010016#include <stdint.h>
Radek Krejci5536d282020-08-04 23:27:44 +020017#include <stdlib.h>
Radek Krejci5536d282020-08-04 23:27:44 +020018
Radek Krejci13a57b62019-07-19 13:04:09 +020019#include "common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010020#include "context.h"
Radek Krejci5536d282020-08-04 23:27:44 +020021#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010022#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "out_internal.h"
Radek Krejci5536d282020-08-04 23:27:44 +020024#include "parser_data.h"
25#include "plugins_types.h"
26#include "printer_data.h"
27#include "printer_internal.h"
28#include "set.h"
29#include "tree.h"
30#include "tree_data.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020031#include "tree_schema.h"
32
33/**
Radek Krejci5536d282020-08-04 23:27:44 +020034 * @brief JSON printer context.
35 */
36struct jsonpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020037 struct ly_out *out; /**< output specification */
38 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
39 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
40 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020041
Radek Krejci1deb5be2020-08-26 16:43:36 +020042 uint16_t level_printed; /* level where some data were already printed */
Radek Krejci5536d282020-08-04 23:27:44 +020043 struct ly_set open; /* currently open array(s) */
44 const struct lyd_node *print_sibling_metadata;
45};
46
47/**
48 * @brief Mark that something was already written in the current level,
49 * used to decide if a comma is expected between the items
50 */
51#define LEVEL_PRINTED ctx->level_printed = ctx->level
52
53#define PRINT_COMMA \
54 if (ctx->level_printed >= ctx->level) {\
Michal Vasko5233e962020-08-14 14:26:20 +020055 ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\
Radek Krejci5536d282020-08-04 23:27:44 +020056 }
57
58static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node);
59
60/**
Radek Krejci5536d282020-08-04 23:27:44 +020061 * Compare 2 nodes, despite it is regular data node or an opaq node, and
62 * decide if they corresponds to the same schema node.
63 *
64 * TODO: rewrite lyd_compare_single and use it instead of this
65 *
66 * @return 1 - matching nodes, 0 - non-matching nodes
67 */
68static int
69matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
70{
71 assert(node1 || node2);
72
73 if (!node1 || !node2) {
74 return 0;
75 } else if (node1->schema != node2->schema) {
76 return 0;
77 }
78 if (!node1->schema) {
79 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020080 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
81 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +010082 if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) {
Radek Krejci5536d282020-08-04 23:27:44 +020083 return 0;
84 }
85 }
86
87 return 1;
88}
89
90/**
91 * @brief Open the JSON array ('[') for the specified @p node
92 *
93 * @param[in] ctx JSON printer context.
94 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020095 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020096 */
Michal Vaskob0099a92020-08-31 14:55:23 +020097static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +020098json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node)
99{
Christian Hoppsbe51c942021-04-02 16:29:51 -0400100 ly_print_(ctx->out, "[%s", DO_FORMAT ? "\n" : "");
Michal Vaskob0099a92020-08-31 14:55:23 +0200101 LY_CHECK_RET(ly_set_add(&ctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200102 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200103
104 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200105}
106
107/**
108 * @brief Get know if the array for the provided @p node is currently open.
109 *
110 * @param[in] ctx JSON printer context.
111 * @param[in] node Data node to check.
112 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
113 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
114 */
115static int
116is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node)
117{
Michal Vasko22df3f02020-08-24 13:29:22 +0200118 if (ctx->open.count && matching_node(node, (const struct lyd_node *)ctx->open.objs[ctx->open.count - 1])) {
Radek Krejci5536d282020-08-04 23:27:44 +0200119 return 1;
120 } else {
121 return 0;
122 }
123}
124
125/**
126 * @brief Close the most inner JSON array.
127 *
128 * @param[in] ctx JSON printer context.
129 */
130static void
131json_print_array_close(struct jsonpr_ctx *ctx)
132{
Radek Krejci5536d282020-08-04 23:27:44 +0200133 LEVEL_DEC;
134 ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL);
Christian Hoppsbe51c942021-04-02 16:29:51 -0400135 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200136}
137
138/**
139 * @brief Get the node's module name to use as the @p node prefix in JSON.
140 * @param[in] node Node to process.
141 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
142 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
143 */
144static const char *
145node_prefix(const struct lyd_node *node)
146{
147 if (node->schema) {
148 return node->schema->module->name;
149 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200150 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200151 const struct lys_module *mod;
152
153 switch (onode->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200154 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100155 return onode->name.module_name;
Radek Krejci8df109d2021-04-23 12:19:08 +0200156 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100157 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200158 if (!mod) {
159 return NULL;
160 }
161 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100162 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200163 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200164 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200165 }
166 }
167
168 return NULL;
169}
170
171/**
172 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
173 *
174 * Accepts both regulard a well as opaq nodes.
175 *
176 * @param[in] node1 The first node to compare.
177 * @param[in] node2 The second node to compare.
178 * @return 0 in case the nodes' modules are the same
179 * @return 1 in case the nodes belongs to different modules
180 */
181int
182json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
183{
184 assert(node1 || node2);
185
186 if (!node1 || !node2) {
187 return 1;
188 } else if (node1->schema && node2->schema) {
189 if (node1->schema->module == node2->schema->module) {
190 /* belongs to the same module */
191 return 0;
192 } else {
193 /* different modules */
194 return 1;
195 }
196 } else {
197 const char *pref1 = node_prefix(node1);
198 const char *pref2 = node_prefix(node2);
Michal Vasko69730152020-10-09 16:30:07 +0200199 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200200 return 0;
201 } else {
202 return 1;
203 }
204 }
205}
206
207/**
208 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
209 *
210 * @param[in] out The output handler.
211 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200212 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200213 */
Michal Vasko5233e962020-08-14 14:26:20 +0200214static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200215json_print_string(struct ly_out *out, const char *text)
216{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200217 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200218
219 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200220 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200221 }
222
Michal Vasko5233e962020-08-14 14:26:20 +0200223 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200224 for (i = n = 0; text[i]; i++) {
225 const unsigned char ascii = text[i];
226 if (ascii < 0x20) {
227 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200228 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200229 } else {
230 switch (ascii) {
231 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200232 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200233 break;
234 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200235 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200236 break;
237 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200238 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200239 n++;
240 }
241 }
242 }
Michal Vasko5233e962020-08-14 14:26:20 +0200243 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200244
Michal Vasko5233e962020-08-14 14:26:20 +0200245 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200246}
247
248/**
249 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
250 *
251 * @param[in] ctx JSON printer context.
252 * @param[in] node The data node being printed.
253 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
254 * @return LY_ERR value.
255 */
256static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200257json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200258{
259 PRINT_COMMA;
Michal Vasko69730152020-10-09 16:30:07 +0200260 if ((LEVEL == 1) || json_nscmp(node, (const struct lyd_node *)node->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200261 /* print "namespace" */
Michal Vasko5233e962020-08-14 14:26:20 +0200262 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200263 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200264 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200265 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200266 node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200267 }
268
269 return LY_SUCCESS;
270}
271
272/**
273 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
274 *
275 * @param[in] ctx JSON printer context.
276 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
277 * @param[in] format Format to decide how to process the @p prefix.
Michal Vaskoad92b672020-11-12 13:11:31 +0100278 * @param[in] name Name structure to provide name and prefix to print. If NULL, only "" name is printed.
Radek Krejci5536d282020-08-04 23:27:44 +0200279 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
280 * @return LY_ERR value.
281 */
282static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200283json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LY_VALUE_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100284 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200285{
Michal Vaskoad92b672020-11-12 13:11:31 +0100286 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200287
288 PRINT_COMMA;
289
290 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100291 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200292 const struct lys_module *mod;
293
294 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200295 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100296 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200297 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200298 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100299 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, name->module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200300 if (mod) {
301 module_name = mod->name;
302 }
303 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100304 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200305 /* cannot be created */
306 LOGINT_RET(ctx->ctx);
307 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100308
309 name_str = name->name;
310 } else {
311 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200312 }
313
314 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200315 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100316 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200317 } else {
Michal Vaskoad92b672020-11-12 13:11:31 +0100318 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200319 }
320
321 return LY_SUCCESS;
322}
323
324/**
325 * @brief Print data value.
326 *
327 * @param[in] ctx JSON printer context.
328 * @param[in] val Data value to be printed.
329 * @return LY_ERR value.
330 */
331static LY_ERR
332json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val)
333{
Radek Krejci857189e2020-09-01 13:26:36 +0200334 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200335 const char *value = val->realtype->plugin->print(val, LY_VALUE_JSON, NULL, &dynamic);
Radek Krejci5536d282020-08-04 23:27:44 +0200336
337 /* leafref is not supported */
338 switch (val->realtype->basetype) {
339 case LY_TYPE_BINARY:
340 case LY_TYPE_STRING:
341 case LY_TYPE_BITS:
342 case LY_TYPE_ENUM:
343 case LY_TYPE_INST:
344 case LY_TYPE_INT64:
345 case LY_TYPE_UINT64:
346 case LY_TYPE_DEC64:
347 case LY_TYPE_IDENT:
Michal Vaskoc5b59af2020-11-03 17:13:41 +0100348 case LY_TYPE_UNION:
Radek Krejci5536d282020-08-04 23:27:44 +0200349 json_print_string(ctx->out, value);
350 break;
351
352 case LY_TYPE_INT8:
353 case LY_TYPE_INT16:
354 case LY_TYPE_INT32:
355 case LY_TYPE_UINT8:
356 case LY_TYPE_UINT16:
357 case LY_TYPE_UINT32:
358 case LY_TYPE_BOOL:
Michal Vasko5233e962020-08-14 14:26:20 +0200359 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200360 break;
361
362 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200363 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200364 break;
365
366 default:
367 /* error */
368 LOGINT_RET(ctx->ctx);
369 }
370
371 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200372 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200373 }
374
375 return LY_SUCCESS;
376}
377
378/**
379 * @brief Print all the attributes of the opaq node.
380 *
381 * @param[in] ctx JSON printer context.
382 * @param[in] node Opaq node where the attributes are placed.
383 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
384 * @return LY_ERR value.
385 */
386static LY_ERR
387json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
388{
389 struct lyd_attr *attr;
390
391 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200392 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200393 LEVEL_PRINTED;
394 }
395
396 for (attr = node->attr; attr; attr = attr->next) {
397 PRINT_COMMA;
Michal Vasko9e685082021-01-29 14:49:09 +0100398 json_print_member2(ctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200399
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200400 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200401 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200402 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200403 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200404 } else {
405 json_print_string(ctx->out, attr->value);
406 }
407 LEVEL_PRINTED;
408 }
409
410 return LY_SUCCESS;
411}
412
413/**
414 * @brief Print all the metadata of the node.
415 *
416 * @param[in] ctx JSON printer context.
417 * @param[in] node Node where the metadata are placed.
418 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
419 * @return LY_ERR value.
420 */
421static LY_ERR
422json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
423{
424 struct lyd_meta *meta;
425
426 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200427 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200428 LEVEL_PRINTED;
429 }
430
431 for (meta = node->meta; meta; meta = meta->next) {
432 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200433 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200434 LY_CHECK_RET(json_print_value(ctx, &meta->value));
435 LEVEL_PRINTED;
436 }
437
438 return LY_SUCCESS;
439}
440
441/**
442 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
443 *
444 * @param[in] ctx JSON printer context.
445 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200446 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200447 * @return LY_ERR value.
448 */
449static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200450json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200451{
452 const struct lys_module *wdmod = NULL;
453
454 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
455 /* we have implicit OR explicit default node */
456 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200457 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200458 }
459
460 if (node->schema && node->meta) {
461 if (inner) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200462 LY_CHECK_RET(json_print_member2(ctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200463 } else {
464 LY_CHECK_RET(json_print_member(ctx, node, 1));
465 }
Michal Vasko5233e962020-08-14 14:26:20 +0200466 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200467 LEVEL_INC;
468 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
469 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200470 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200471 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200472 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200473 if (inner) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200474 LY_CHECK_RET(json_print_member2(ctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200475 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200476 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100477 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200478 }
Michal Vasko5233e962020-08-14 14:26:20 +0200479 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200480 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200481 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200482 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200483 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200484 LEVEL_PRINTED;
485 }
486
487 return LY_SUCCESS;
488}
489
490/**
491 * @brief Print leaf data node including its metadata.
492 *
493 * @param[in] ctx JSON printer context.
494 * @param[in] node Data node to print.
495 * @return LY_ERR value.
496 */
497static LY_ERR
498json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
499{
500 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vasko22df3f02020-08-24 13:29:22 +0200501 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200502 LEVEL_PRINTED;
503
504 /* print attributes as sibling */
505 json_print_attributes(ctx, node, 0);
506
507 return LY_SUCCESS;
508}
509
510/**
511 * @brief Print anydata data node including its metadata.
512 *
513 * @param[in] ctx JSON printer context.
514 * @param[in] any Anydata node to print.
515 * @return LY_ERR value.
516 */
517static LY_ERR
518json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
519{
520 LY_ERR ret = LY_SUCCESS;
521 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200522 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200523
524 if (!any->value.tree) {
525 /* no content */
526 return LY_SUCCESS;
527 }
528
529 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200530 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200531
532 /* turn logging off */
533 prev_lo = ly_log_options(0);
534
535 /* try to parse it into a data tree */
536 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
537 /* successfully parsed */
538 free(any->value.mem);
539 any->value.tree = iter;
540 any->value_type = LYD_ANYDATA_DATATREE;
541 }
542
543 /* turn loggin on again */
544 ly_log_options(prev_lo);
545 }
546
547 switch (any->value_type) {
548 case LYD_ANYDATA_DATATREE:
549 /* close opening tag and print data */
550 prev_opts = ctx->options;
551 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
552
553 LY_LIST_FOR(any->value.tree, iter) {
554 ret = json_print_node(ctx, iter);
555 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
556 }
557
558 ctx->options = prev_opts;
559 break;
560 case LYD_ANYDATA_JSON:
561 /* print without escaping special characters */
562 if (!any->value.str[0]) {
563 return LY_SUCCESS;
564 }
Michal Vasko5233e962020-08-14 14:26:20 +0200565 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200566 break;
567 case LYD_ANYDATA_STRING:
568 case LYD_ANYDATA_XML:
569 case LYD_ANYDATA_LYB:
570 /* JSON and LYB format is not supported */
571 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
572 return LY_SUCCESS;
573 }
574
575 return LY_SUCCESS;
576}
577
578/**
579 * @brief Print content of a single container/list data node including its metadata.
580 * The envelope specific to container and list are expected to be printed by the caller.
581 *
582 * @param[in] ctx JSON printer context.
583 * @param[in] node Data node to print.
584 * @return LY_ERR value.
585 */
586static LY_ERR
587json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
588{
589 struct lyd_node *child;
Radek Krejci857189e2020-09-01 13:26:36 +0200590 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200591
Michal Vasko630d9892020-12-08 17:11:08 +0100592 LY_LIST_FOR(lyd_child(node), child) {
593 if (ly_should_print(child, ctx->options)) {
594 break;
595 }
596 }
597 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200598 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200599 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200600 has_content = 1;
601 }
602
Michal Vasko69730152020-10-09 16:30:07 +0200603 if (!node->schema || (node->schema->nodetype != LYS_LIST)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200604 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200605 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200606 } else {
Christian Hoppsbe51c942021-04-02 16:29:51 -0400607 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? (DO_FORMAT ? ",\n" : ",") : "",
Michal Vasko69730152020-10-09 16:30:07 +0200608 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200609 }
610 LEVEL_INC;
611
612 json_print_attributes(ctx, node, 1);
613
614 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
615 /* print children */
Michal Vasko630d9892020-12-08 17:11:08 +0100616 LY_LIST_FOR(lyd_child(node), child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200617 LY_CHECK_RET(json_print_node(ctx, child));
618 }
619 } else {
620 /* anydata */
621 json_print_anydata(ctx, (struct lyd_node_any *)node);
622 }
623
Radek Krejci5536d282020-08-04 23:27:44 +0200624 LEVEL_DEC;
625 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200626 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200627 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200628 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200629 }
630 LEVEL_PRINTED;
631
632 return LY_SUCCESS;
633}
634
635/**
636 * @brief Print container data node including its metadata.
637 *
638 * @param[in] ctx JSON printer context.
639 * @param[in] node Data node to print.
640 * @return LY_ERR value.
641 */
642static int
643json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
644{
645 LY_CHECK_RET(json_print_member(ctx, node, 0));
646 LY_CHECK_RET(json_print_inner(ctx, node));
647
648 return LY_SUCCESS;
649}
650
651/**
652 * @brief Print single leaf-list or list instance.
653 *
654 * In case of list, metadata are printed inside the list object. For the leaf-list,
655 * metadata are marked in the context for later printing after closing the array next to it using
656 * json_print_metadata_leaflist().
657 *
658 * @param[in] ctx JSON printer context.
659 * @param[in] node Data node to print.
660 * @return LY_ERR value.
661 */
662static LY_ERR
663json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
664{
665 if (!is_open_array(ctx, node)) {
666 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200667 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200668 if (node->schema->nodetype == LYS_LEAFLIST) {
Christian Hoppsbe51c942021-04-02 16:29:51 -0400669 ly_print_(ctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200670 }
Radek Krejci5536d282020-08-04 23:27:44 +0200671 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Christian Hoppsbe51c942021-04-02 16:29:51 -0400672 ly_print_(ctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200673 }
674
675 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200676 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200677 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200678 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200679 LEVEL_PRINTED;
680 } else {
681 /* print list's content */
682 LY_CHECK_RET(json_print_inner(ctx, node));
683 }
684 } else {
685 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200686 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200687
688 if (node->meta && !ctx->print_sibling_metadata) {
689 ctx->print_sibling_metadata = node;
690 }
691 }
692
Michal Vasko69730152020-10-09 16:30:07 +0200693 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200694 json_print_array_close(ctx);
695 }
696
697 return LY_SUCCESS;
698}
699
700/**
701 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
702 * This function is supposed to be called when the leaf-list array is closed.
703 *
704 * @param[in] ctx JSON printer context.
705 * @return LY_ERR value.
706 */
707static LY_ERR
708json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
709{
710 const struct lyd_node *prev, *node, *iter;
711
712 if (!ctx->print_sibling_metadata) {
713 return LY_SUCCESS;
714 }
715
716 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
717 prev->next && matching_node(node, prev);
718 node = prev, prev = node->prev) {}
719
720 /* node is the first instance of the leaf-list */
721
722 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200723 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200724 LEVEL_INC;
725 LY_LIST_FOR(node, iter) {
726 PRINT_COMMA;
727 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200728 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200729 LEVEL_INC;
730 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
731 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200732 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200733 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200734 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200735 }
736 LEVEL_PRINTED;
737 if (!matching_node(iter, iter->next)) {
738 break;
739 }
740 }
741 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200742 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200743 LEVEL_PRINTED;
744
745 return LY_SUCCESS;
746}
747
748/**
749 * @brief Print opaq data node including its attributes.
750 *
751 * @param[in] ctx JSON printer context.
752 * @param[in] node Opaq node to print.
753 * @return LY_ERR value.
754 */
755static LY_ERR
756json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
757{
Radek Krejci857189e2020-09-01 13:26:36 +0200758 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200759
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200760 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100761 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200762 first = 0;
763 }
Michal Vasko9e685082021-01-29 14:49:09 +0100764 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200765 last = 0;
766 }
767 }
768
769 if (first) {
Michal Vasko9e685082021-01-29 14:49:09 +0100770 LY_CHECK_RET(json_print_member2(ctx, lyd_parent(&node->node), node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200771
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200772 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100773 LY_CHECK_RET(json_print_array_open(ctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200774 LEVEL_INC;
775 }
776 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200777 if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko9e685082021-01-29 14:49:09 +0100778 LY_CHECK_RET(json_print_inner(ctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200779 LEVEL_PRINTED;
780 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200781 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200782 ly_print_(ctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200783 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200784 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200785 } else {
786 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200787 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200788 }
789 LEVEL_PRINTED;
790
791 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200792 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200793
794 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200795 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200796 json_print_array_close(ctx);
797 LEVEL_DEC;
798 LEVEL_PRINTED;
799 }
800
801 return LY_SUCCESS;
802}
803
804/**
805 * @brief Print all the types of data node including its metadata.
806 *
807 * @param[in] ctx JSON printer context.
808 * @param[in] node Data node to print.
809 * @return LY_ERR value.
810 */
811static LY_ERR
812json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
813{
814 if (!ly_should_print(node, ctx->options)) {
Michal Vasko69730152020-10-09 16:30:07 +0200815 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200816 json_print_array_close(ctx);
817 }
818 return LY_SUCCESS;
819 }
820
821 if (!node->schema) {
822 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
823 } else {
824 switch (node->schema->nodetype) {
825 case LYS_RPC:
826 case LYS_ACTION:
827 case LYS_NOTIF:
828 case LYS_CONTAINER:
829 LY_CHECK_RET(json_print_container(ctx, node));
830 break;
831 case LYS_LEAF:
832 LY_CHECK_RET(json_print_leaf(ctx, node));
833 break;
834 case LYS_LEAFLIST:
835 case LYS_LIST:
836 LY_CHECK_RET(json_print_leaf_list(ctx, node));
837 break;
838 case LYS_ANYXML:
839 case LYS_ANYDATA:
840 LY_CHECK_RET(json_print_container(ctx, node));
841 break;
842 default:
843 LOGINT(node->schema->module->ctx);
844 return EXIT_FAILURE;
845 }
846 }
847
848 ctx->level_printed = ctx->level;
849
850 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
851 json_print_metadata_leaflist(ctx);
852 ctx->print_sibling_metadata = NULL;
853 }
854
855 return LY_SUCCESS;
856}
857
858LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200859json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200860{
861 const struct lyd_node *node;
862 struct jsonpr_ctx ctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200863 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200864
Radek Krejci2e874772020-08-28 16:36:33 +0200865 if (!root) {
866 ly_print_(out, "{}%s", delimiter);
867 ly_print_flush(out);
868 return LY_SUCCESS;
869 }
870
Radek Krejci5536d282020-08-04 23:27:44 +0200871 ctx.out = out;
872 ctx.level = 1;
873 ctx.level_printed = 0;
874 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200875 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200876
877 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200878 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200879
880 /* content */
881 LY_LIST_FOR(root, node) {
882 LY_CHECK_RET(json_print_node(&ctx, node));
883 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
884 break;
885 }
886 }
887
888 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200889 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200890
891 assert(!ctx.open.count);
892 ly_set_erase(&ctx.open, NULL);
893
894 ly_print_flush(out);
895 return LY_SUCCESS;
896}