blob: ad9fc3ae8e4d6cf3dba053588f277bbc0608dd79 [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>
16#include <stdlib.h>
17#include <string.h>
18
Radek Krejci13a57b62019-07-19 13:04:09 +020019#include "common.h"
Radek Krejci5536d282020-08-04 23:27:44 +020020#include "log.h"
21#include "parser_data.h"
22#include "plugins_types.h"
23#include "printer_data.h"
24#include "printer_internal.h"
25#include "set.h"
26#include "tree.h"
27#include "tree_data.h"
28#include "tree_data_internal.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020029#include "tree_schema.h"
30
31/**
Radek Krejci5536d282020-08-04 23:27:44 +020032 * @brief JSON printer context.
33 */
34struct jsonpr_ctx {
35 struct ly_out *out; /**< output specification */
36 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
37 int options; /**< [Data printer flags](@ref dataprinterflags) */
38 const struct ly_ctx *ctx; /**< libyang context */
39
40 unsigned int level_printed; /* level where some dara were already printed */
41 struct ly_set open; /* currently open array(s) */
42 const struct lyd_node *print_sibling_metadata;
43};
44
45/**
46 * @brief Mark that something was already written in the current level,
47 * used to decide if a comma is expected between the items
48 */
49#define LEVEL_PRINTED ctx->level_printed = ctx->level
50
51#define PRINT_COMMA \
52 if (ctx->level_printed >= ctx->level) {\
Michal Vasko5233e962020-08-14 14:26:20 +020053 ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\
Radek Krejci5536d282020-08-04 23:27:44 +020054 }
55
56static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node);
57
58/**
Radek Krejci5536d282020-08-04 23:27:44 +020059 * Compare 2 nodes, despite it is regular data node or an opaq node, and
60 * decide if they corresponds to the same schema node.
61 *
62 * TODO: rewrite lyd_compare_single and use it instead of this
63 *
64 * @return 1 - matching nodes, 0 - non-matching nodes
65 */
66static int
67matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
68{
69 assert(node1 || node2);
70
71 if (!node1 || !node2) {
72 return 0;
73 } else if (node1->schema != node2->schema) {
74 return 0;
75 }
76 if (!node1->schema) {
77 /* compare node names */
78 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq*)node1;
79 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq*)node2;
80 if (onode1->name != onode2->name || onode1->prefix.id != onode2->prefix.id) {
81 return 0;
82 }
83 }
84
85 return 1;
86}
87
88/**
89 * @brief Open the JSON array ('[') for the specified @p node
90 *
91 * @param[in] ctx JSON printer context.
92 * @param[in] node First node of the array.
93 */
94static void
95json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node)
96{
97 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +020098 ly_print_(ctx->out, "[%s", (!node->schema || node->schema->nodetype != LYS_LEAFLIST) && DO_FORMAT ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +020099 ly_set_add(&ctx->open, (void*)node, 0);
100 LEVEL_INC;
101}
102
103/**
104 * @brief Get know if the array for the provided @p node is currently open.
105 *
106 * @param[in] ctx JSON printer context.
107 * @param[in] node Data node to check.
108 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
109 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
110 */
111static int
112is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node)
113{
114 if (ctx->open.count && matching_node(node, (const struct lyd_node*)ctx->open.objs[ctx->open.count - 1])) {
115 return 1;
116 } else {
117 return 0;
118 }
119}
120
121/**
122 * @brief Close the most inner JSON array.
123 *
124 * @param[in] ctx JSON printer context.
125 */
126static void
127json_print_array_close(struct jsonpr_ctx *ctx)
128{
129 const struct lysc_node *schema = ((const struct lyd_node*)ctx->open.objs[ctx->open.count - 1])->schema;
130
131 LEVEL_DEC;
132 ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL);
133 if (schema && schema->nodetype == LYS_LEAFLIST) {
134 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +0200135 ly_print_(ctx->out, "]");
Radek Krejci5536d282020-08-04 23:27:44 +0200136 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200137 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200138 }
139}
140
141/**
142 * @brief Get the node's module name to use as the @p node prefix in JSON.
143 * @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 {
153 struct lyd_node_opaq *onode = (struct lyd_node_opaq*)node;
154 const struct lys_module *mod;
155
156 switch (onode->format) {
157 case LYD_JSON:
158 return onode->prefix.module_name;
159 case LYD_XML:
160 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->prefix.module_ns);
161 if (!mod) {
162 return NULL;
163 }
164 return mod->name;
Radek Krejci5536d282020-08-04 23:27:44 +0200165 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200166 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200167 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200168 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200169 }
170 }
171
172 return NULL;
173}
174
175/**
176 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
177 *
178 * Accepts both regulard a well as opaq nodes.
179 *
180 * @param[in] node1 The first node to compare.
181 * @param[in] node2 The second node to compare.
182 * @return 0 in case the nodes' modules are the same
183 * @return 1 in case the nodes belongs to different modules
184 */
185int
186json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
187{
188 assert(node1 || node2);
189
190 if (!node1 || !node2) {
191 return 1;
192 } else if (node1->schema && node2->schema) {
193 if (node1->schema->module == node2->schema->module) {
194 /* belongs to the same module */
195 return 0;
196 } else {
197 /* different modules */
198 return 1;
199 }
200 } else {
201 const char *pref1 = node_prefix(node1);
202 const char *pref2 = node_prefix(node2);
203 if ((pref1 && pref2) && pref1 == pref2) {
204 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{
221 unsigned int i, n;
222
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];
230 if (ascii < 0x20) {
231 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200232 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200233 } else {
234 switch (ascii) {
235 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200236 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200237 break;
238 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200239 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200240 break;
241 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200242 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200243 n++;
244 }
245 }
246 }
Michal Vasko5233e962020-08-14 14:26:20 +0200247 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200248
Michal Vasko5233e962020-08-14 14:26:20 +0200249 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200250}
251
252/**
253 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
254 *
255 * @param[in] ctx JSON printer context.
256 * @param[in] node The data node being printed.
257 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
258 * @return LY_ERR value.
259 */
260static LY_ERR
261json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, int is_attr)
262{
263 PRINT_COMMA;
264 if (LEVEL == 1 || json_nscmp(node, (const struct lyd_node*)node->parent)) {
265 /* print "namespace" */
Michal Vasko5233e962020-08-14 14:26:20 +0200266 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Radek Krejci5536d282020-08-04 23:27:44 +0200267 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
268 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200269 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "",
Radek Krejci5536d282020-08-04 23:27:44 +0200270 node->schema->name, DO_FORMAT ? " " : "");
271 }
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.
282 * @param[in] prefix Prefix structure to provide prefix string if prefix to print.
283 * @param[in] name Name of the memeber to print.
284 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
285 * @return LY_ERR value.
286 */
287static LY_ERR
288json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LYD_FORMAT format, const struct ly_prefix *prefix, const char *name, int is_attr)
289{
290 const char *module_name = NULL;
291
292 PRINT_COMMA;
293
294 /* determine prefix string */
295 if (prefix) {
296 const struct lys_module *mod;
297
298 switch (format) {
299 case LYD_JSON:
300 module_name = prefix->module_name;
301 break;
302 case LYD_XML:
303 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, prefix->module_ns);
304 if (mod) {
305 module_name = mod->name;
306 }
307 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200308 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200309 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200310 /* cannot be created */
311 LOGINT_RET(ctx->ctx);
312 }
313 }
314
315 /* print the member */
316 if (module_name && (!parent || node_prefix(parent) != module_name)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200317 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200318 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200319 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name, 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{
335 int dynamic = 0;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200336 const char *value = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Radek Krejci5536d282020-08-04 23:27:44 +0200337
338 /* leafref is not supported */
339 switch (val->realtype->basetype) {
340 case LY_TYPE_BINARY:
341 case LY_TYPE_STRING:
342 case LY_TYPE_BITS:
343 case LY_TYPE_ENUM:
344 case LY_TYPE_INST:
345 case LY_TYPE_INT64:
346 case LY_TYPE_UINT64:
347 case LY_TYPE_DEC64:
348 case LY_TYPE_IDENT:
349 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) {
372 free((char*)value);
373 }
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;
398 json_print_member2(ctx, (struct lyd_node*)node, attr->format, &attr->prefix, attr->name, 0);
399
400 if (attr->hint & (LYD_NODE_OPAQ_ISBOOLEAN | LYD_NODE_OPAQ_ISNUMBER)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200401 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200402 } else if (attr->hint & LYD_NODE_OPAQ_ISEMPTY) {
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.
446 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
447 * @return LY_ERR value.
448 */
449static LY_ERR
450json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, int inner)
451{
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) {
462 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
463 } 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;
472 } else if (!node->schema && ((struct lyd_node_opaq*)node)->attr) {
473 if (inner) {
474 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
475 } else {
476 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq*)node)->format,
477 &((struct lyd_node_opaq*)node)->prefix, ((struct lyd_node_opaq*)node)->name, 1));
478 }
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;
481 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq*)node, wdmod));
482 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));
501 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term*)node)->value));
502 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;
522 int prev_opts, prev_lo;
523
524 if (!any->value.tree) {
525 /* no content */
526 return LY_SUCCESS;
527 }
528
529 if (any->value_type == LYD_ANYDATA_LYB) {
530 int parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
531
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;
590 struct lyd_node *children = lyd_node_children(node, 0);
591 int has_content = 0;
592
593 if (node->meta || children) {
594 has_content = 1;
595 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any*)node)->value.tree) {
596 has_content = 1;
597 }
598
599 if (!node->schema || node->schema->nodetype != LYS_LIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200600 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200601 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200602 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200603 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci5536d282020-08-04 23:27:44 +0200604 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
605 }
606 LEVEL_INC;
607
608 json_print_attributes(ctx, node, 1);
609
610 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
611 /* print children */
612 LY_LIST_FOR(children, child) {
613 LY_CHECK_RET(json_print_node(ctx, child));
614 }
615 } else {
616 /* anydata */
617 json_print_anydata(ctx, (struct lyd_node_any *)node);
618 }
619
Radek Krejci5536d282020-08-04 23:27:44 +0200620 LEVEL_DEC;
621 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200622 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200623 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200624 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200625 }
626 LEVEL_PRINTED;
627
628 return LY_SUCCESS;
629}
630
631/**
632 * @brief Print container data node including its metadata.
633 *
634 * @param[in] ctx JSON printer context.
635 * @param[in] node Data node to print.
636 * @return LY_ERR value.
637 */
638static int
639json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
640{
641 LY_CHECK_RET(json_print_member(ctx, node, 0));
642 LY_CHECK_RET(json_print_inner(ctx, node));
643
644 return LY_SUCCESS;
645}
646
647/**
648 * @brief Print single leaf-list or list instance.
649 *
650 * In case of list, metadata are printed inside the list object. For the leaf-list,
651 * metadata are marked in the context for later printing after closing the array next to it using
652 * json_print_metadata_leaflist().
653 *
654 * @param[in] ctx JSON printer context.
655 * @param[in] node Data node to print.
656 * @return LY_ERR value.
657 */
658static LY_ERR
659json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
660{
661 if (!is_open_array(ctx, node)) {
662 LY_CHECK_RET(json_print_member(ctx, node, 0));
663 json_print_array_open(ctx, node);
664 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200665 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200666 }
667
668 if (node->schema->nodetype == LYS_LIST) {
669 if (!lyd_node_children(node, 0)) {
670 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200671 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200672 LEVEL_PRINTED;
673 } else {
674 /* print list's content */
675 LY_CHECK_RET(json_print_inner(ctx, node));
676 }
677 } else {
678 assert(node->schema->nodetype == LYS_LEAFLIST);
679 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term*)node)->value));
680
681 if (node->meta && !ctx->print_sibling_metadata) {
682 ctx->print_sibling_metadata = node;
683 }
684 }
685
686 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
687 json_print_array_close(ctx);
688 }
689
690 return LY_SUCCESS;
691}
692
693/**
694 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
695 * This function is supposed to be called when the leaf-list array is closed.
696 *
697 * @param[in] ctx JSON printer context.
698 * @return LY_ERR value.
699 */
700static LY_ERR
701json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
702{
703 const struct lyd_node *prev, *node, *iter;
704
705 if (!ctx->print_sibling_metadata) {
706 return LY_SUCCESS;
707 }
708
709 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
710 prev->next && matching_node(node, prev);
711 node = prev, prev = node->prev) {}
712
713 /* node is the first instance of the leaf-list */
714
715 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200716 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200717 LEVEL_INC;
718 LY_LIST_FOR(node, iter) {
719 PRINT_COMMA;
720 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200721 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200722 LEVEL_INC;
723 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
724 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200725 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200726 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200727 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200728 }
729 LEVEL_PRINTED;
730 if (!matching_node(iter, iter->next)) {
731 break;
732 }
733 }
734 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200735 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200736 LEVEL_PRINTED;
737
738 return LY_SUCCESS;
739}
740
741/**
742 * @brief Print opaq data node including its attributes.
743 *
744 * @param[in] ctx JSON printer context.
745 * @param[in] node Opaq node to print.
746 * @return LY_ERR value.
747 */
748static LY_ERR
749json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
750{
751 int first = 1, last = 1;
752
753 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
754 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq*)node->prev;
755 const struct lyd_node_opaq *next = (const struct lyd_node_opaq*)node->next;
756 if (prev->next && matching_node((const struct lyd_node*)prev, (const struct lyd_node*)node)) {
757 first = 0;
758 }
759 if (next && matching_node((const struct lyd_node*)node, (const struct lyd_node*)next)) {
760 last = 0;
761 }
762 }
763
764 if (first) {
765 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0));
766
767 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
768 json_print_array_open(ctx, (struct lyd_node*)node);
769 LEVEL_INC;
770 }
771 }
772 if (node->child || (node->hint & LYD_NODE_OPAQ_ISLIST)) {
773 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node*)node));
774 LEVEL_PRINTED;
775 } else {
776 if (node->hint & LYD_VALUE_PARSE_ISEMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200777 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200778 } else if (node->hint & (LYD_VALUE_PARSE_ISBOOLEAN | LYD_VALUE_PARSE_ISNUMBER)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200779 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200780 } else {
781 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200782 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200783 }
784 LEVEL_PRINTED;
785
786 /* attributes */
787 json_print_attributes(ctx, (const struct lyd_node*)node, 0);
788
789 }
790 if (last && (node->hint & LYD_NODE_OPAQ_ISLIST)) {
791 json_print_array_close(ctx);
792 LEVEL_DEC;
793 LEVEL_PRINTED;
794 }
795
796 return LY_SUCCESS;
797}
798
799/**
800 * @brief Print all the types of data node including its metadata.
801 *
802 * @param[in] ctx JSON printer context.
803 * @param[in] node Data node to print.
804 * @return LY_ERR value.
805 */
806static LY_ERR
807json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
808{
809 if (!ly_should_print(node, ctx->options)) {
810 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
811 json_print_array_close(ctx);
812 }
813 return LY_SUCCESS;
814 }
815
816 if (!node->schema) {
817 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
818 } else {
819 switch (node->schema->nodetype) {
820 case LYS_RPC:
821 case LYS_ACTION:
822 case LYS_NOTIF:
823 case LYS_CONTAINER:
824 LY_CHECK_RET(json_print_container(ctx, node));
825 break;
826 case LYS_LEAF:
827 LY_CHECK_RET(json_print_leaf(ctx, node));
828 break;
829 case LYS_LEAFLIST:
830 case LYS_LIST:
831 LY_CHECK_RET(json_print_leaf_list(ctx, node));
832 break;
833 case LYS_ANYXML:
834 case LYS_ANYDATA:
835 LY_CHECK_RET(json_print_container(ctx, node));
836 break;
837 default:
838 LOGINT(node->schema->module->ctx);
839 return EXIT_FAILURE;
840 }
841 }
842
843 ctx->level_printed = ctx->level;
844
845 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
846 json_print_metadata_leaflist(ctx);
847 ctx->print_sibling_metadata = NULL;
848 }
849
850 return LY_SUCCESS;
851}
852
853LY_ERR
854json_print_data(struct ly_out *out, const struct lyd_node *root, int options)
855{
856 const struct lyd_node *node;
857 struct jsonpr_ctx ctx = {0};
858 const char *delimiter = (options & LYD_PRINT_FORMAT) ? "\n" : "";
859
860 ctx.out = out;
861 ctx.level = 1;
862 ctx.level_printed = 0;
863 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200864 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200865
866 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200867 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200868
869 /* content */
870 LY_LIST_FOR(root, node) {
871 LY_CHECK_RET(json_print_node(&ctx, node));
872 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
873 break;
874 }
875 }
876
877 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200878 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200879
880 assert(!ctx.open.count);
881 ly_set_erase(&ctx.open, NULL);
882
883 ly_print_flush(out);
884 return LY_SUCCESS;
885}