blob: 3b45a6cc8c6d3c4385931c29003b04381baba8ae [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.
Radek Krejci31bc3f52021-04-26 11:09:58 +0200140 *
Radek Krejci5536d282020-08-04 23:27:44 +0200141 * @param[in] node Node to process.
142 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
143 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
144 */
145static const char *
146node_prefix(const struct lyd_node *node)
147{
148 if (node->schema) {
149 return node->schema->module->name;
150 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200151 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200152 const struct lys_module *mod;
153
154 switch (onode->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200155 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100156 return onode->name.module_name;
Radek Krejci8df109d2021-04-23 12:19:08 +0200157 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100158 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200159 if (!mod) {
160 return NULL;
161 }
162 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100163 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200164 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200165 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200166 }
167 }
168
169 return NULL;
170}
171
172/**
173 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
174 *
175 * Accepts both regulard a well as opaq nodes.
176 *
177 * @param[in] node1 The first node to compare.
178 * @param[in] node2 The second node to compare.
179 * @return 0 in case the nodes' modules are the same
180 * @return 1 in case the nodes belongs to different modules
181 */
182int
183json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
184{
185 assert(node1 || node2);
186
187 if (!node1 || !node2) {
188 return 1;
189 } else if (node1->schema && node2->schema) {
190 if (node1->schema->module == node2->schema->module) {
191 /* belongs to the same module */
192 return 0;
193 } else {
194 /* different modules */
195 return 1;
196 }
197 } else {
198 const char *pref1 = node_prefix(node1);
199 const char *pref2 = node_prefix(node2);
Michal Vasko69730152020-10-09 16:30:07 +0200200 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200201 return 0;
202 } else {
203 return 1;
204 }
205 }
206}
207
208/**
209 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
210 *
211 * @param[in] out The output handler.
212 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200213 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200214 */
Michal Vasko5233e962020-08-14 14:26:20 +0200215static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200216json_print_string(struct ly_out *out, const char *text)
217{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200218 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200219
220 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200221 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200222 }
223
Michal Vasko5233e962020-08-14 14:26:20 +0200224 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200225 for (i = n = 0; text[i]; i++) {
226 const unsigned char ascii = text[i];
227 if (ascii < 0x20) {
228 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200229 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200230 } else {
231 switch (ascii) {
232 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200233 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200234 break;
235 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200236 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200237 break;
238 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200239 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200240 n++;
241 }
242 }
243 }
Michal Vasko5233e962020-08-14 14:26:20 +0200244 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200245
Michal Vasko5233e962020-08-14 14:26:20 +0200246 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200247}
248
249/**
250 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
251 *
252 * @param[in] ctx JSON printer context.
253 * @param[in] node The data node being printed.
254 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
255 * @return LY_ERR value.
256 */
257static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200258json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200259{
260 PRINT_COMMA;
Michal Vasko69730152020-10-09 16:30:07 +0200261 if ((LEVEL == 1) || json_nscmp(node, (const struct lyd_node *)node->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200262 /* print "namespace" */
Michal Vasko5233e962020-08-14 14:26:20 +0200263 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200264 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200265 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200266 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200267 node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200268 }
269
270 return LY_SUCCESS;
271}
272
273/**
274 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
275 *
276 * @param[in] ctx JSON printer context.
277 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
278 * @param[in] format Format to decide how to process the @p prefix.
Michal Vaskoad92b672020-11-12 13:11:31 +0100279 * @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 +0200280 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
281 * @return LY_ERR value.
282 */
283static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200284json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LY_VALUE_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100285 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200286{
Michal Vaskoad92b672020-11-12 13:11:31 +0100287 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200288
289 PRINT_COMMA;
290
291 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100292 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200293 const struct lys_module *mod;
294
295 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200296 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100297 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200298 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200299 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100300 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, name->module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200301 if (mod) {
302 module_name = mod->name;
303 }
304 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100305 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200306 /* cannot be created */
307 LOGINT_RET(ctx->ctx);
308 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100309
310 name_str = name->name;
311 } else {
312 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200313 }
314
315 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200316 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100317 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 +0200318 } else {
Michal Vaskoad92b672020-11-12 13:11:31 +0100319 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200320 }
321
322 return LY_SUCCESS;
323}
324
325/**
326 * @brief Print data value.
327 *
328 * @param[in] ctx JSON printer context.
329 * @param[in] val Data value to be printed.
330 * @return LY_ERR value.
331 */
332static LY_ERR
333json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val)
334{
aPiecek0f6bf3e2021-08-25 15:47:49 +0200335 ly_bool dynamic;
Michal Vasko183b9112021-11-04 16:02:24 +0100336 LY_DATA_TYPE basetype;
Radek Krejci224d4b42021-04-23 13:54:59 +0200337 const char *value = val->realtype->plugin->print(ctx->ctx, val, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200338
Michal Vasko183b9112021-11-04 16:02:24 +0100339 basetype = val->realtype->basetype;
340
341print_val:
Radek Krejci5536d282020-08-04 23:27:44 +0200342 /* leafref is not supported */
Michal Vasko183b9112021-11-04 16:02:24 +0100343 switch (basetype) {
344 case LY_TYPE_UNION:
345 /* use the resolved type */
346 basetype = val->subvalue->value.realtype->basetype;
347 goto print_val;
348
Radek Krejci5536d282020-08-04 23:27:44 +0200349 case LY_TYPE_BINARY:
350 case LY_TYPE_STRING:
351 case LY_TYPE_BITS:
352 case LY_TYPE_ENUM:
353 case LY_TYPE_INST:
354 case LY_TYPE_INT64:
355 case LY_TYPE_UINT64:
356 case LY_TYPE_DEC64:
357 case LY_TYPE_IDENT:
358 json_print_string(ctx->out, value);
359 break;
360
361 case LY_TYPE_INT8:
362 case LY_TYPE_INT16:
363 case LY_TYPE_INT32:
364 case LY_TYPE_UINT8:
365 case LY_TYPE_UINT16:
366 case LY_TYPE_UINT32:
367 case LY_TYPE_BOOL:
Michal Vasko5233e962020-08-14 14:26:20 +0200368 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200369 break;
370
371 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200372 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200373 break;
374
375 default:
376 /* error */
377 LOGINT_RET(ctx->ctx);
378 }
379
380 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200381 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200382 }
383
384 return LY_SUCCESS;
385}
386
387/**
388 * @brief Print all the attributes of the opaq node.
389 *
390 * @param[in] ctx JSON printer context.
391 * @param[in] node Opaq node where the attributes are placed.
392 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
393 * @return LY_ERR value.
394 */
395static LY_ERR
396json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
397{
398 struct lyd_attr *attr;
399
400 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200401 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200402 LEVEL_PRINTED;
403 }
404
405 for (attr = node->attr; attr; attr = attr->next) {
406 PRINT_COMMA;
Michal Vasko9e685082021-01-29 14:49:09 +0100407 json_print_member2(ctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200408
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200409 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200410 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200411 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200412 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200413 } else {
414 json_print_string(ctx->out, attr->value);
415 }
416 LEVEL_PRINTED;
417 }
418
419 return LY_SUCCESS;
420}
421
422/**
423 * @brief Print all the metadata of the node.
424 *
425 * @param[in] ctx JSON printer context.
426 * @param[in] node Node where the metadata are placed.
427 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
428 * @return LY_ERR value.
429 */
430static LY_ERR
431json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
432{
433 struct lyd_meta *meta;
434
435 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200436 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200437 LEVEL_PRINTED;
438 }
439
440 for (meta = node->meta; meta; meta = meta->next) {
441 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200442 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200443 LY_CHECK_RET(json_print_value(ctx, &meta->value));
444 LEVEL_PRINTED;
445 }
446
447 return LY_SUCCESS;
448}
449
450/**
451 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
452 *
453 * @param[in] ctx JSON printer context.
454 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200455 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200456 * @return LY_ERR value.
457 */
458static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200459json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200460{
461 const struct lys_module *wdmod = NULL;
462
463 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
464 /* we have implicit OR explicit default node */
465 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200466 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200467 }
468
469 if (node->schema && node->meta) {
470 if (inner) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200471 LY_CHECK_RET(json_print_member2(ctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200472 } else {
473 LY_CHECK_RET(json_print_member(ctx, node, 1));
474 }
Michal Vasko5233e962020-08-14 14:26:20 +0200475 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200476 LEVEL_INC;
477 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
478 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200479 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200480 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200481 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200482 if (inner) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200483 LY_CHECK_RET(json_print_member2(ctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200484 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200485 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100486 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200487 }
Michal Vasko5233e962020-08-14 14:26:20 +0200488 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200489 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200490 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200491 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200492 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200493 LEVEL_PRINTED;
494 }
495
496 return LY_SUCCESS;
497}
498
499/**
500 * @brief Print leaf data node including its metadata.
501 *
502 * @param[in] ctx JSON printer context.
503 * @param[in] node Data node to print.
504 * @return LY_ERR value.
505 */
506static LY_ERR
507json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
508{
509 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vasko22df3f02020-08-24 13:29:22 +0200510 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200511 LEVEL_PRINTED;
512
513 /* print attributes as sibling */
514 json_print_attributes(ctx, node, 0);
515
516 return LY_SUCCESS;
517}
518
519/**
520 * @brief Print anydata data node including its metadata.
521 *
522 * @param[in] ctx JSON printer context.
523 * @param[in] any Anydata node to print.
524 * @return LY_ERR value.
525 */
526static LY_ERR
527json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
528{
529 LY_ERR ret = LY_SUCCESS;
530 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200531 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200532
533 if (!any->value.tree) {
534 /* no content */
535 return LY_SUCCESS;
536 }
537
538 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200539 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200540
541 /* turn logging off */
542 prev_lo = ly_log_options(0);
543
544 /* try to parse it into a data tree */
545 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
546 /* successfully parsed */
547 free(any->value.mem);
548 any->value.tree = iter;
549 any->value_type = LYD_ANYDATA_DATATREE;
550 }
551
552 /* turn loggin on again */
553 ly_log_options(prev_lo);
554 }
555
556 switch (any->value_type) {
557 case LYD_ANYDATA_DATATREE:
558 /* close opening tag and print data */
559 prev_opts = ctx->options;
560 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
561
562 LY_LIST_FOR(any->value.tree, iter) {
563 ret = json_print_node(ctx, iter);
564 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
565 }
566
567 ctx->options = prev_opts;
568 break;
569 case LYD_ANYDATA_JSON:
570 /* print without escaping special characters */
571 if (!any->value.str[0]) {
572 return LY_SUCCESS;
573 }
Michal Vasko5233e962020-08-14 14:26:20 +0200574 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200575 break;
576 case LYD_ANYDATA_STRING:
577 case LYD_ANYDATA_XML:
578 case LYD_ANYDATA_LYB:
579 /* JSON and LYB format is not supported */
580 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
581 return LY_SUCCESS;
582 }
583
584 return LY_SUCCESS;
585}
586
587/**
588 * @brief Print content of a single container/list data node including its metadata.
589 * The envelope specific to container and list are expected to be printed by the caller.
590 *
591 * @param[in] ctx JSON printer context.
592 * @param[in] node Data node to print.
593 * @return LY_ERR value.
594 */
595static LY_ERR
596json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
597{
598 struct lyd_node *child;
Radek Krejci857189e2020-09-01 13:26:36 +0200599 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200600
Michal Vasko630d9892020-12-08 17:11:08 +0100601 LY_LIST_FOR(lyd_child(node), child) {
602 if (ly_should_print(child, ctx->options)) {
603 break;
604 }
605 }
606 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200607 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200608 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200609 has_content = 1;
610 }
611
Michal Vasko1a85d332021-08-27 10:35:28 +0200612 if ((node->schema && (node->schema->nodetype == LYS_LIST)) ||
613 (!node->schema && (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST))) {
614 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ?
615 (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : "");
616 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200617 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200618 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200619 }
620 LEVEL_INC;
621
622 json_print_attributes(ctx, node, 1);
623
624 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
625 /* print children */
Michal Vasko630d9892020-12-08 17:11:08 +0100626 LY_LIST_FOR(lyd_child(node), child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200627 LY_CHECK_RET(json_print_node(ctx, child));
628 }
629 } else {
630 /* anydata */
631 json_print_anydata(ctx, (struct lyd_node_any *)node);
632 }
633
Radek Krejci5536d282020-08-04 23:27:44 +0200634 LEVEL_DEC;
635 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200636 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200637 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200638 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200639 }
640 LEVEL_PRINTED;
641
642 return LY_SUCCESS;
643}
644
645/**
646 * @brief Print container data node including its metadata.
647 *
648 * @param[in] ctx JSON printer context.
649 * @param[in] node Data node to print.
650 * @return LY_ERR value.
651 */
652static int
653json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
654{
655 LY_CHECK_RET(json_print_member(ctx, node, 0));
656 LY_CHECK_RET(json_print_inner(ctx, node));
657
658 return LY_SUCCESS;
659}
660
661/**
662 * @brief Print single leaf-list or list instance.
663 *
664 * In case of list, metadata are printed inside the list object. For the leaf-list,
665 * metadata are marked in the context for later printing after closing the array next to it using
666 * json_print_metadata_leaflist().
667 *
668 * @param[in] ctx JSON printer context.
669 * @param[in] node Data node to print.
670 * @return LY_ERR value.
671 */
672static LY_ERR
673json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
674{
675 if (!is_open_array(ctx, node)) {
676 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200677 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200678 if (node->schema->nodetype == LYS_LEAFLIST) {
Christian Hoppsbe51c942021-04-02 16:29:51 -0400679 ly_print_(ctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200680 }
Radek Krejci5536d282020-08-04 23:27:44 +0200681 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Christian Hoppsbe51c942021-04-02 16:29:51 -0400682 ly_print_(ctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200683 }
684
685 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200686 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200687 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200688 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200689 LEVEL_PRINTED;
690 } else {
691 /* print list's content */
692 LY_CHECK_RET(json_print_inner(ctx, node));
693 }
694 } else {
695 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200696 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200697
698 if (node->meta && !ctx->print_sibling_metadata) {
699 ctx->print_sibling_metadata = node;
700 }
701 }
702
Michal Vasko69730152020-10-09 16:30:07 +0200703 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200704 json_print_array_close(ctx);
705 }
706
707 return LY_SUCCESS;
708}
709
710/**
711 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
712 * This function is supposed to be called when the leaf-list array is closed.
713 *
714 * @param[in] ctx JSON printer context.
715 * @return LY_ERR value.
716 */
717static LY_ERR
718json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
719{
720 const struct lyd_node *prev, *node, *iter;
721
722 if (!ctx->print_sibling_metadata) {
723 return LY_SUCCESS;
724 }
725
726 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
727 prev->next && matching_node(node, prev);
728 node = prev, prev = node->prev) {}
729
730 /* node is the first instance of the leaf-list */
731
732 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200733 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200734 LEVEL_INC;
735 LY_LIST_FOR(node, iter) {
736 PRINT_COMMA;
737 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200738 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200739 LEVEL_INC;
740 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
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 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200744 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200745 }
746 LEVEL_PRINTED;
747 if (!matching_node(iter, iter->next)) {
748 break;
749 }
750 }
751 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200752 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200753 LEVEL_PRINTED;
754
755 return LY_SUCCESS;
756}
757
758/**
759 * @brief Print opaq data node including its attributes.
760 *
761 * @param[in] ctx JSON printer context.
762 * @param[in] node Opaq node to print.
763 * @return LY_ERR value.
764 */
765static LY_ERR
766json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
767{
Radek Krejci857189e2020-09-01 13:26:36 +0200768 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200769
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200770 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100771 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200772 first = 0;
773 }
Michal Vasko9e685082021-01-29 14:49:09 +0100774 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200775 last = 0;
776 }
777 }
778
779 if (first) {
Michal Vasko9e685082021-01-29 14:49:09 +0100780 LY_CHECK_RET(json_print_member2(ctx, lyd_parent(&node->node), node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200781
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200782 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100783 LY_CHECK_RET(json_print_array_open(ctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200784 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200785 if (node->hints & LYD_NODEHINT_LEAFLIST) {
786 ly_print_(ctx->out, "%*s", INDENT);
787 }
788 } else if (node->hints & LYD_NODEHINT_LEAFLIST) {
789 ly_print_(ctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200790 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200791 if (node->child || (node->hints & LYD_NODEHINT_LIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100792 LY_CHECK_RET(json_print_inner(ctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200793 LEVEL_PRINTED;
794 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200795 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200796 ly_print_(ctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200797 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200798 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200799 } else {
800 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200801 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200802 }
803 LEVEL_PRINTED;
804
805 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200806 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200807
808 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200809 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200810 json_print_array_close(ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200811 LEVEL_PRINTED;
812 }
813
814 return LY_SUCCESS;
815}
816
817/**
818 * @brief Print all the types of data node including its metadata.
819 *
820 * @param[in] ctx JSON printer context.
821 * @param[in] node Data node to print.
822 * @return LY_ERR value.
823 */
824static LY_ERR
825json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
826{
827 if (!ly_should_print(node, ctx->options)) {
Michal Vasko69730152020-10-09 16:30:07 +0200828 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200829 json_print_array_close(ctx);
830 }
831 return LY_SUCCESS;
832 }
833
834 if (!node->schema) {
835 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
836 } else {
837 switch (node->schema->nodetype) {
838 case LYS_RPC:
839 case LYS_ACTION:
840 case LYS_NOTIF:
841 case LYS_CONTAINER:
842 LY_CHECK_RET(json_print_container(ctx, node));
843 break;
844 case LYS_LEAF:
845 LY_CHECK_RET(json_print_leaf(ctx, node));
846 break;
847 case LYS_LEAFLIST:
848 case LYS_LIST:
849 LY_CHECK_RET(json_print_leaf_list(ctx, node));
850 break;
851 case LYS_ANYXML:
852 case LYS_ANYDATA:
853 LY_CHECK_RET(json_print_container(ctx, node));
854 break;
855 default:
856 LOGINT(node->schema->module->ctx);
857 return EXIT_FAILURE;
858 }
859 }
860
861 ctx->level_printed = ctx->level;
862
863 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
864 json_print_metadata_leaflist(ctx);
865 ctx->print_sibling_metadata = NULL;
866 }
867
868 return LY_SUCCESS;
869}
870
871LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200872json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200873{
874 const struct lyd_node *node;
875 struct jsonpr_ctx ctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200876 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200877
Radek Krejci2e874772020-08-28 16:36:33 +0200878 if (!root) {
879 ly_print_(out, "{}%s", delimiter);
880 ly_print_flush(out);
881 return LY_SUCCESS;
882 }
883
Radek Krejci5536d282020-08-04 23:27:44 +0200884 ctx.out = out;
885 ctx.level = 1;
886 ctx.level_printed = 0;
887 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200888 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200889
890 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200891 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200892
893 /* content */
894 LY_LIST_FOR(root, node) {
895 LY_CHECK_RET(json_print_node(&ctx, node));
896 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
897 break;
898 }
899 }
900
901 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200902 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200903
904 assert(!ctx.open.count);
905 ly_set_erase(&ctx.open, NULL);
906
907 ly_print_flush(out);
908 return LY_SUCCESS;
909}