blob: 79824a1459f494758febf4035dda40e6fd0e72ca [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 Krejci535ea9f2020-05-29 16:01:05 +020020#include "config.h"
Radek Krejci5536d282020-08-04 23:27:44 +020021#include "log.h"
22#include "parser_data.h"
23#include "plugins_types.h"
24#include "printer_data.h"
25#include "printer_internal.h"
26#include "set.h"
27#include "tree.h"
28#include "tree_data.h"
29#include "tree_data_internal.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020030#include "tree_schema.h"
31
32/**
Radek Krejci5536d282020-08-04 23:27:44 +020033 * @brief JSON printer context.
34 */
35struct jsonpr_ctx {
36 struct ly_out *out; /**< output specification */
37 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
38 int options; /**< [Data printer flags](@ref dataprinterflags) */
39 const struct ly_ctx *ctx; /**< libyang context */
40
41 unsigned int level_printed; /* level where some dara were already printed */
42 struct ly_set open; /* currently open array(s) */
43 const struct lyd_node *print_sibling_metadata;
44};
45
46/**
47 * @brief Mark that something was already written in the current level,
48 * used to decide if a comma is expected between the items
49 */
50#define LEVEL_PRINTED ctx->level_printed = ctx->level
51
52#define PRINT_COMMA \
53 if (ctx->level_printed >= ctx->level) {\
54 ly_print(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\
55 }
56
57static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node);
58
59/**
Radek Krejci5536d282020-08-04 23:27:44 +020060 * Compare 2 nodes, despite it is regular data node or an opaq node, and
61 * decide if they corresponds to the same schema node.
62 *
63 * TODO: rewrite lyd_compare_single and use it instead of this
64 *
65 * @return 1 - matching nodes, 0 - non-matching nodes
66 */
67static int
68matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
69{
70 assert(node1 || node2);
71
72 if (!node1 || !node2) {
73 return 0;
74 } else if (node1->schema != node2->schema) {
75 return 0;
76 }
77 if (!node1->schema) {
78 /* compare node names */
79 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq*)node1;
80 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq*)node2;
81 if (onode1->name != onode2->name || onode1->prefix.id != onode2->prefix.id) {
82 return 0;
83 }
84 }
85
86 return 1;
87}
88
89/**
90 * @brief Open the JSON array ('[') for the specified @p node
91 *
92 * @param[in] ctx JSON printer context.
93 * @param[in] node First node of the array.
94 */
95static void
96json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node)
97{
98 /* leaf-list's content is always printed on a single line */
99 ly_print(ctx->out, "[%s", (!node->schema || node->schema->nodetype != LYS_LEAFLIST) && DO_FORMAT ? "\n" : "");
100 ly_set_add(&ctx->open, (void*)node, 0);
101 LEVEL_INC;
102}
103
104/**
105 * @brief Get know if the array for the provided @p node is currently open.
106 *
107 * @param[in] ctx JSON printer context.
108 * @param[in] node Data node to check.
109 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
110 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
111 */
112static int
113is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node)
114{
115 if (ctx->open.count && matching_node(node, (const struct lyd_node*)ctx->open.objs[ctx->open.count - 1])) {
116 return 1;
117 } else {
118 return 0;
119 }
120}
121
122/**
123 * @brief Close the most inner JSON array.
124 *
125 * @param[in] ctx JSON printer context.
126 */
127static void
128json_print_array_close(struct jsonpr_ctx *ctx)
129{
130 const struct lysc_node *schema = ((const struct lyd_node*)ctx->open.objs[ctx->open.count - 1])->schema;
131
132 LEVEL_DEC;
133 ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL);
134 if (schema && schema->nodetype == LYS_LEAFLIST) {
135 /* leaf-list's content is always printed on a single line */
136 ly_print(ctx->out, "]");
137 } else {
138 ly_print(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
139 }
140}
141
142/**
143 * @brief Get the node's module name to use as the @p node prefix in JSON.
144 * @param[in] node Node to process.
145 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
146 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
147 */
148static const char *
149node_prefix(const struct lyd_node *node)
150{
151 if (node->schema) {
152 return node->schema->module->name;
153 } else {
154 struct lyd_node_opaq *onode = (struct lyd_node_opaq*)node;
155 const struct lys_module *mod;
156
157 switch (onode->format) {
158 case LYD_JSON:
159 return onode->prefix.module_name;
160 case LYD_XML:
161 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->prefix.module_ns);
162 if (!mod) {
163 return NULL;
164 }
165 return mod->name;
Radek Krejci5536d282020-08-04 23:27:44 +0200166 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200167 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200168 /* cannot be created */
169 LOGINT(LYD_NODE_CTX(node));
170 }
171 }
172
173 return NULL;
174}
175
176/**
177 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
178 *
179 * Accepts both regulard a well as opaq nodes.
180 *
181 * @param[in] node1 The first node to compare.
182 * @param[in] node2 The second node to compare.
183 * @return 0 in case the nodes' modules are the same
184 * @return 1 in case the nodes belongs to different modules
185 */
186int
187json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
188{
189 assert(node1 || node2);
190
191 if (!node1 || !node2) {
192 return 1;
193 } else if (node1->schema && node2->schema) {
194 if (node1->schema->module == node2->schema->module) {
195 /* belongs to the same module */
196 return 0;
197 } else {
198 /* different modules */
199 return 1;
200 }
201 } else {
202 const char *pref1 = node_prefix(node1);
203 const char *pref2 = node_prefix(node2);
204 if ((pref1 && pref2) && pref1 == pref2) {
205 return 0;
206 } else {
207 return 1;
208 }
209 }
210}
211
212/**
213 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
214 *
215 * @param[in] out The output handler.
216 * @param[in] text The string to print.
217 * @return The number of printed characters.
218 */
219static int
220json_print_string(struct ly_out *out, const char *text)
221{
222 unsigned int i, n;
223
224 if (!text) {
225 return 0;
226 }
227
228 ly_write(out, "\"", 1);
229 for (i = n = 0; text[i]; i++) {
230 const unsigned char ascii = text[i];
231 if (ascii < 0x20) {
232 /* control character */
233 n += ly_print(out, "\\u%.4X", ascii);
234 } else {
235 switch (ascii) {
236 case '"':
237 n += ly_print(out, "\\\"");
238 break;
239 case '\\':
240 n += ly_print(out, "\\\\");
241 break;
242 default:
243 ly_write(out, &text[i], 1);
244 n++;
245 }
246 }
247 }
248 ly_write(out, "\"", 1);
249
250 return n + 2;
251}
252
253/**
254 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
255 *
256 * @param[in] ctx JSON printer context.
257 * @param[in] node The data node being printed.
258 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
259 * @return LY_ERR value.
260 */
261static LY_ERR
262json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, int is_attr)
263{
264 PRINT_COMMA;
265 if (LEVEL == 1 || json_nscmp(node, (const struct lyd_node*)node->parent)) {
266 /* print "namespace" */
267 ly_print(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
268 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
269 } else {
270 ly_print(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "",
271 node->schema->name, DO_FORMAT ? " " : "");
272 }
273
274 return LY_SUCCESS;
275}
276
277/**
278 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
279 *
280 * @param[in] ctx JSON printer context.
281 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
282 * @param[in] format Format to decide how to process the @p prefix.
283 * @param[in] prefix Prefix structure to provide prefix string if prefix to print.
284 * @param[in] name Name of the memeber to print.
285 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
286 * @return LY_ERR value.
287 */
288static LY_ERR
289json_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)
290{
291 const char *module_name = NULL;
292
293 PRINT_COMMA;
294
295 /* determine prefix string */
296 if (prefix) {
297 const struct lys_module *mod;
298
299 switch (format) {
300 case LYD_JSON:
301 module_name = prefix->module_name;
302 break;
303 case LYD_XML:
304 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, prefix->module_ns);
305 if (mod) {
306 module_name = mod->name;
307 }
308 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200309 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200310 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200311 /* cannot be created */
312 LOGINT_RET(ctx->ctx);
313 }
314 }
315
316 /* print the member */
317 if (module_name && (!parent || node_prefix(parent) != module_name)) {
318 ly_print(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name, DO_FORMAT ? " " : "");
319 } else {
320 ly_print(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name, DO_FORMAT ? " " : "");
321 }
322
323 return LY_SUCCESS;
324}
325
326/**
327 * @brief Print data value.
328 *
329 * @param[in] ctx JSON printer context.
330 * @param[in] val Data value to be printed.
331 * @return LY_ERR value.
332 */
333static LY_ERR
334json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val)
335{
336 int dynamic = 0;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200337 const char *value = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Radek Krejci5536d282020-08-04 23:27:44 +0200338
339 /* leafref is not supported */
340 switch (val->realtype->basetype) {
341 case LY_TYPE_BINARY:
342 case LY_TYPE_STRING:
343 case LY_TYPE_BITS:
344 case LY_TYPE_ENUM:
345 case LY_TYPE_INST:
346 case LY_TYPE_INT64:
347 case LY_TYPE_UINT64:
348 case LY_TYPE_DEC64:
349 case LY_TYPE_IDENT:
350 json_print_string(ctx->out, value);
351 break;
352
353 case LY_TYPE_INT8:
354 case LY_TYPE_INT16:
355 case LY_TYPE_INT32:
356 case LY_TYPE_UINT8:
357 case LY_TYPE_UINT16:
358 case LY_TYPE_UINT32:
359 case LY_TYPE_BOOL:
360 ly_print(ctx->out, "%s", value[0] ? value : "null");
361 break;
362
363 case LY_TYPE_EMPTY:
364 ly_print(ctx->out, "[null]");
365 break;
366
367 default:
368 /* error */
369 LOGINT_RET(ctx->ctx);
370 }
371
372 if (dynamic) {
373 free((char*)value);
374 }
375
376 return LY_SUCCESS;
377}
378
379/**
380 * @brief Print all the attributes of the opaq node.
381 *
382 * @param[in] ctx JSON printer context.
383 * @param[in] node Opaq node where the attributes are placed.
384 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
385 * @return LY_ERR value.
386 */
387static LY_ERR
388json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
389{
390 struct lyd_attr *attr;
391
392 if (wdmod) {
393 ly_print(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
394 LEVEL_PRINTED;
395 }
396
397 for (attr = node->attr; attr; attr = attr->next) {
398 PRINT_COMMA;
399 json_print_member2(ctx, (struct lyd_node*)node, attr->format, &attr->prefix, attr->name, 0);
400
401 if (attr->hint & (LYD_NODE_OPAQ_ISBOOLEAN | LYD_NODE_OPAQ_ISNUMBER)) {
402 ly_print(ctx->out, "%s", attr->value[0] ? attr->value : "null");
403 } else if (attr->hint & LYD_NODE_OPAQ_ISEMPTY) {
404 ly_print(ctx->out, "[null]");
405 } else {
406 json_print_string(ctx->out, attr->value);
407 }
408 LEVEL_PRINTED;
409 }
410
411 return LY_SUCCESS;
412}
413
414/**
415 * @brief Print all the metadata of the node.
416 *
417 * @param[in] ctx JSON printer context.
418 * @param[in] node Node where the metadata are placed.
419 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
420 * @return LY_ERR value.
421 */
422static LY_ERR
423json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
424{
425 struct lyd_meta *meta;
426
427 if (wdmod) {
428 ly_print(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
429 LEVEL_PRINTED;
430 }
431
432 for (meta = node->meta; meta; meta = meta->next) {
433 PRINT_COMMA;
434 ly_print(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
435 LY_CHECK_RET(json_print_value(ctx, &meta->value));
436 LEVEL_PRINTED;
437 }
438
439 return LY_SUCCESS;
440}
441
442/**
443 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
444 *
445 * @param[in] ctx JSON printer context.
446 * @param[in] node Data node where the attributes/metadata are placed.
447 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
448 * @return LY_ERR value.
449 */
450static LY_ERR
451json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, int inner)
452{
453 const struct lys_module *wdmod = NULL;
454
455 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
456 /* we have implicit OR explicit default node */
457 /* get with-defaults module */
458 wdmod = ly_ctx_get_module_implemented(LYD_NODE_CTX(node), "ietf-netconf-with-defaults");
459 }
460
461 if (node->schema && node->meta) {
462 if (inner) {
463 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
464 } else {
465 LY_CHECK_RET(json_print_member(ctx, node, 1));
466 }
467 ly_print(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
468 LEVEL_INC;
469 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
470 LEVEL_DEC;
471 ly_print(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
472 LEVEL_PRINTED;
473 } else if (!node->schema && ((struct lyd_node_opaq*)node)->attr) {
474 if (inner) {
475 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
476 } else {
477 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq*)node)->format,
478 &((struct lyd_node_opaq*)node)->prefix, ((struct lyd_node_opaq*)node)->name, 1));
479 }
480 ly_print(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
481 LEVEL_INC;
482 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq*)node, wdmod));
483 LEVEL_DEC;
484 ly_print(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
485 LEVEL_PRINTED;
486 }
487
488 return LY_SUCCESS;
489}
490
491/**
492 * @brief Print leaf data node including its metadata.
493 *
494 * @param[in] ctx JSON printer context.
495 * @param[in] node Data node to print.
496 * @return LY_ERR value.
497 */
498static LY_ERR
499json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
500{
501 LY_CHECK_RET(json_print_member(ctx, node, 0));
502 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term*)node)->value));
503 LEVEL_PRINTED;
504
505 /* print attributes as sibling */
506 json_print_attributes(ctx, node, 0);
507
508 return LY_SUCCESS;
509}
510
511/**
512 * @brief Print anydata data node including its metadata.
513 *
514 * @param[in] ctx JSON printer context.
515 * @param[in] any Anydata node to print.
516 * @return LY_ERR value.
517 */
518static LY_ERR
519json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
520{
521 LY_ERR ret = LY_SUCCESS;
522 struct lyd_node *iter;
523 int prev_opts, prev_lo;
524
525 if (!any->value.tree) {
526 /* no content */
527 return LY_SUCCESS;
528 }
529
530 if (any->value_type == LYD_ANYDATA_LYB) {
531 int parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
532
533 /* turn logging off */
534 prev_lo = ly_log_options(0);
535
536 /* try to parse it into a data tree */
537 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
538 /* successfully parsed */
539 free(any->value.mem);
540 any->value.tree = iter;
541 any->value_type = LYD_ANYDATA_DATATREE;
542 }
543
544 /* turn loggin on again */
545 ly_log_options(prev_lo);
546 }
547
548 switch (any->value_type) {
549 case LYD_ANYDATA_DATATREE:
550 /* close opening tag and print data */
551 prev_opts = ctx->options;
552 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
553
554 LY_LIST_FOR(any->value.tree, iter) {
555 ret = json_print_node(ctx, iter);
556 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
557 }
558
559 ctx->options = prev_opts;
560 break;
561 case LYD_ANYDATA_JSON:
562 /* print without escaping special characters */
563 if (!any->value.str[0]) {
564 return LY_SUCCESS;
565 }
566 ly_print(ctx->out, "%*s%s", INDENT, any->value.str);
567 break;
568 case LYD_ANYDATA_STRING:
569 case LYD_ANYDATA_XML:
570 case LYD_ANYDATA_LYB:
571 /* JSON and LYB format is not supported */
572 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
573 return LY_SUCCESS;
574 }
575
576 return LY_SUCCESS;
577}
578
579/**
580 * @brief Print content of a single container/list data node including its metadata.
581 * The envelope specific to container and list are expected to be printed by the caller.
582 *
583 * @param[in] ctx JSON printer context.
584 * @param[in] node Data node to print.
585 * @return LY_ERR value.
586 */
587static LY_ERR
588json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
589{
590 struct lyd_node *child;
591 struct lyd_node *children = lyd_node_children(node, 0);
592 int has_content = 0;
593
594 if (node->meta || children) {
595 has_content = 1;
596 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any*)node)->value.tree) {
597 has_content = 1;
598 }
599
600 if (!node->schema || node->schema->nodetype != LYS_LIST) {
601 ly_print(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
602 (DO_FORMAT && has_content) ? "\n" : "");
603 } else {
604 ly_print(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
605 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
606 }
607 LEVEL_INC;
608
609 json_print_attributes(ctx, node, 1);
610
611 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
612 /* print children */
613 LY_LIST_FOR(children, child) {
614 LY_CHECK_RET(json_print_node(ctx, child));
615 }
616 } else {
617 /* anydata */
618 json_print_anydata(ctx, (struct lyd_node_any *)node);
619 }
620
621
622 LEVEL_DEC;
623 if (DO_FORMAT && has_content) {
624 ly_print(ctx->out, "\n%*s}", INDENT);
625 } else {
626 ly_print(ctx->out, "}");
627 }
628 LEVEL_PRINTED;
629
630 return LY_SUCCESS;
631}
632
633/**
634 * @brief Print container data node including its metadata.
635 *
636 * @param[in] ctx JSON printer context.
637 * @param[in] node Data node to print.
638 * @return LY_ERR value.
639 */
640static int
641json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
642{
643 LY_CHECK_RET(json_print_member(ctx, node, 0));
644 LY_CHECK_RET(json_print_inner(ctx, node));
645
646 return LY_SUCCESS;
647}
648
649/**
650 * @brief Print single leaf-list or list instance.
651 *
652 * In case of list, metadata are printed inside the list object. For the leaf-list,
653 * metadata are marked in the context for later printing after closing the array next to it using
654 * json_print_metadata_leaflist().
655 *
656 * @param[in] ctx JSON printer context.
657 * @param[in] node Data node to print.
658 * @return LY_ERR value.
659 */
660static LY_ERR
661json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
662{
663 if (!is_open_array(ctx, node)) {
664 LY_CHECK_RET(json_print_member(ctx, node, 0));
665 json_print_array_open(ctx, node);
666 } else if (node->schema->nodetype == LYS_LEAFLIST) {
667 ly_print(ctx->out, ",");
668 }
669
670 if (node->schema->nodetype == LYS_LIST) {
671 if (!lyd_node_children(node, 0)) {
672 /* empty, e.g. in case of filter */
673 ly_print(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
674 LEVEL_PRINTED;
675 } else {
676 /* print list's content */
677 LY_CHECK_RET(json_print_inner(ctx, node));
678 }
679 } else {
680 assert(node->schema->nodetype == LYS_LEAFLIST);
681 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term*)node)->value));
682
683 if (node->meta && !ctx->print_sibling_metadata) {
684 ctx->print_sibling_metadata = node;
685 }
686 }
687
688 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
689 json_print_array_close(ctx);
690 }
691
692 return LY_SUCCESS;
693}
694
695/**
696 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
697 * This function is supposed to be called when the leaf-list array is closed.
698 *
699 * @param[in] ctx JSON printer context.
700 * @return LY_ERR value.
701 */
702static LY_ERR
703json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
704{
705 const struct lyd_node *prev, *node, *iter;
706
707 if (!ctx->print_sibling_metadata) {
708 return LY_SUCCESS;
709 }
710
711 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
712 prev->next && matching_node(node, prev);
713 node = prev, prev = node->prev) {}
714
715 /* node is the first instance of the leaf-list */
716
717 LY_CHECK_RET(json_print_member(ctx, node, 1));
718 ly_print(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
719 LEVEL_INC;
720 LY_LIST_FOR(node, iter) {
721 PRINT_COMMA;
722 if (iter->meta) {
723 ly_print(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
724 LEVEL_INC;
725 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
726 LEVEL_DEC;
727 ly_print(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
728 } else {
729 ly_print(ctx->out, "null");
730 }
731 LEVEL_PRINTED;
732 if (!matching_node(iter, iter->next)) {
733 break;
734 }
735 }
736 LEVEL_DEC;
737 ly_print(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
738 LEVEL_PRINTED;
739
740 return LY_SUCCESS;
741}
742
743/**
744 * @brief Print opaq data node including its attributes.
745 *
746 * @param[in] ctx JSON printer context.
747 * @param[in] node Opaq node to print.
748 * @return LY_ERR value.
749 */
750static LY_ERR
751json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
752{
753 int first = 1, last = 1;
754
755 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
756 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq*)node->prev;
757 const struct lyd_node_opaq *next = (const struct lyd_node_opaq*)node->next;
758 if (prev->next && matching_node((const struct lyd_node*)prev, (const struct lyd_node*)node)) {
759 first = 0;
760 }
761 if (next && matching_node((const struct lyd_node*)node, (const struct lyd_node*)next)) {
762 last = 0;
763 }
764 }
765
766 if (first) {
767 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0));
768
769 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
770 json_print_array_open(ctx, (struct lyd_node*)node);
771 LEVEL_INC;
772 }
773 }
774 if (node->child || (node->hint & LYD_NODE_OPAQ_ISLIST)) {
775 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node*)node));
776 LEVEL_PRINTED;
777 } else {
778 if (node->hint & LYD_VALUE_PARSE_ISEMPTY) {
779 ly_print(ctx->out, "[null]");
780 } else if (node->hint & (LYD_VALUE_PARSE_ISBOOLEAN | LYD_VALUE_PARSE_ISNUMBER)) {
781 ly_print(ctx->out, "%s", node->value);
782 } else {
783 /* string */
784 ly_print(ctx->out, "\"%s\"", node->value);
785 }
786 LEVEL_PRINTED;
787
788 /* attributes */
789 json_print_attributes(ctx, (const struct lyd_node*)node, 0);
790
791 }
792 if (last && (node->hint & LYD_NODE_OPAQ_ISLIST)) {
793 json_print_array_close(ctx);
794 LEVEL_DEC;
795 LEVEL_PRINTED;
796 }
797
798 return LY_SUCCESS;
799}
800
801/**
802 * @brief Print all the types of data node including its metadata.
803 *
804 * @param[in] ctx JSON printer context.
805 * @param[in] node Data node to print.
806 * @return LY_ERR value.
807 */
808static LY_ERR
809json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
810{
811 if (!ly_should_print(node, ctx->options)) {
812 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
813 json_print_array_close(ctx);
814 }
815 return LY_SUCCESS;
816 }
817
818 if (!node->schema) {
819 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
820 } else {
821 switch (node->schema->nodetype) {
822 case LYS_RPC:
823 case LYS_ACTION:
824 case LYS_NOTIF:
825 case LYS_CONTAINER:
826 LY_CHECK_RET(json_print_container(ctx, node));
827 break;
828 case LYS_LEAF:
829 LY_CHECK_RET(json_print_leaf(ctx, node));
830 break;
831 case LYS_LEAFLIST:
832 case LYS_LIST:
833 LY_CHECK_RET(json_print_leaf_list(ctx, node));
834 break;
835 case LYS_ANYXML:
836 case LYS_ANYDATA:
837 LY_CHECK_RET(json_print_container(ctx, node));
838 break;
839 default:
840 LOGINT(node->schema->module->ctx);
841 return EXIT_FAILURE;
842 }
843 }
844
845 ctx->level_printed = ctx->level;
846
847 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
848 json_print_metadata_leaflist(ctx);
849 ctx->print_sibling_metadata = NULL;
850 }
851
852 return LY_SUCCESS;
853}
854
855LY_ERR
856json_print_data(struct ly_out *out, const struct lyd_node *root, int options)
857{
858 const struct lyd_node *node;
859 struct jsonpr_ctx ctx = {0};
860 const char *delimiter = (options & LYD_PRINT_FORMAT) ? "\n" : "";
861
862 ctx.out = out;
863 ctx.level = 1;
864 ctx.level_printed = 0;
865 ctx.options = options;
866 ctx.ctx = LYD_NODE_CTX(root);
867
868 /* start */
869 ly_print(ctx.out, "{%s", delimiter);
870
871 /* content */
872 LY_LIST_FOR(root, node) {
873 LY_CHECK_RET(json_print_node(&ctx, node));
874 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
875 break;
876 }
877 }
878
879 /* end */
880 ly_print(out, "%s}%s", delimiter, delimiter);
881
882 assert(!ctx.open.count);
883 ly_set_erase(&ctx.open, NULL);
884
885 ly_print_flush(out);
886 return LY_SUCCESS;
887}