blob: a0285b23e127f8123c926e488a075340522f3745 [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 {
Radek Krejci1deb5be2020-08-26 16:43:36 +020035 struct ly_out *out; /**< output specification */
36 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
37 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
38 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020039
Radek Krejci1deb5be2020-08-26 16:43:36 +020040 uint16_t level_printed; /* level where some data were already printed */
Radek Krejci5536d282020-08-04 23:27:44 +020041 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 */
Michal Vasko22df3f02020-08-24 13:29:22 +020078 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
79 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Radek Krejci5536d282020-08-04 23:27:44 +020080 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" : "");
Michal Vasko22df3f02020-08-24 13:29:22 +020099 ly_set_add(&ctx->open, (void *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200100 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{
Michal Vasko22df3f02020-08-24 13:29:22 +0200114 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 +0200115 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{
Michal Vasko22df3f02020-08-24 13:29:22 +0200129 const struct lysc_node *schema = ((const struct lyd_node *)ctx->open.objs[ctx->open.count - 1])->schema;
Radek Krejci5536d282020-08-04 23:27:44 +0200130
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 {
Michal Vasko22df3f02020-08-24 13:29:22 +0200153 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200154 const struct lys_module *mod;
155
156 switch (onode->format) {
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{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200221 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200222
223 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200224 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200225 }
226
Michal Vasko5233e962020-08-14 14:26:20 +0200227 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200228 for (i = n = 0; text[i]; i++) {
229 const unsigned char ascii = text[i];
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
Radek Krejci1deb5be2020-08-26 16:43:36 +0200261json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, uint8_t is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200262{
263 PRINT_COMMA;
Michal Vasko22df3f02020-08-24 13:29:22 +0200264 if (LEVEL == 1 || json_nscmp(node, (const struct lyd_node *)node->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200265 /* 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
Radek Krejci1deb5be2020-08-26 16:43:36 +0200288json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LYD_FORMAT format,
289 const struct ly_prefix *prefix, const char *name, uint8_t is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200290{
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)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200318 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200319 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200320 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200321 }
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{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200336 uint8_t 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:
Michal Vasko5233e962020-08-14 14:26:20 +0200360 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200361 break;
362
363 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200364 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200365 break;
366
367 default:
368 /* error */
369 LOGINT_RET(ctx->ctx);
370 }
371
372 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200373 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200374 }
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) {
Michal Vasko5233e962020-08-14 14:26:20 +0200393 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200394 LEVEL_PRINTED;
395 }
396
397 for (attr = node->attr; attr; attr = attr->next) {
398 PRINT_COMMA;
Michal Vasko22df3f02020-08-24 13:29:22 +0200399 json_print_member2(ctx, (struct lyd_node *)node, attr->format, &attr->prefix, attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200400
401 if (attr->hint & (LYD_NODE_OPAQ_ISBOOLEAN | LYD_NODE_OPAQ_ISNUMBER)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200402 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200403 } else if (attr->hint & LYD_NODE_OPAQ_ISEMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200404 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200405 } 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) {
Michal Vasko5233e962020-08-14 14:26:20 +0200428 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200429 LEVEL_PRINTED;
430 }
431
432 for (meta = node->meta; meta; meta = meta->next) {
433 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200434 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200435 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
Radek Krejci1deb5be2020-08-26 16:43:36 +0200451json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, uint8_t inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200452{
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 */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200458 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200459 }
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 }
Michal Vasko5233e962020-08-14 14:26:20 +0200467 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200468 LEVEL_INC;
469 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
470 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200471 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200472 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200473 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200474 if (inner) {
475 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
476 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200477 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));
Radek Krejci5536d282020-08-04 23:27:44 +0200479 }
Michal Vasko5233e962020-08-14 14:26:20 +0200480 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200481 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200482 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200483 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200484 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200485 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));
Michal Vasko22df3f02020-08-24 13:29:22 +0200502 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200503 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;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200523 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200524
525 if (!any->value.tree) {
526 /* no content */
527 return LY_SUCCESS;
528 }
529
530 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200531 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200532
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 }
Michal Vasko5233e962020-08-14 14:26:20 +0200566 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200567 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);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200592 uint8_t has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200593
594 if (node->meta || children) {
595 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200596 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200597 has_content = 1;
598 }
599
600 if (!node->schema || node->schema->nodetype != LYS_LIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200601 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200602 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200603 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200604 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci5536d282020-08-04 23:27:44 +0200605 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
Radek Krejci5536d282020-08-04 23:27:44 +0200621 LEVEL_DEC;
622 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200623 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200624 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200625 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200626 }
627 LEVEL_PRINTED;
628
629 return LY_SUCCESS;
630}
631
632/**
633 * @brief Print container data node including its metadata.
634 *
635 * @param[in] ctx JSON printer context.
636 * @param[in] node Data node to print.
637 * @return LY_ERR value.
638 */
639static int
640json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
641{
642 LY_CHECK_RET(json_print_member(ctx, node, 0));
643 LY_CHECK_RET(json_print_inner(ctx, node));
644
645 return LY_SUCCESS;
646}
647
648/**
649 * @brief Print single leaf-list or list instance.
650 *
651 * In case of list, metadata are printed inside the list object. For the leaf-list,
652 * metadata are marked in the context for later printing after closing the array next to it using
653 * json_print_metadata_leaflist().
654 *
655 * @param[in] ctx JSON printer context.
656 * @param[in] node Data node to print.
657 * @return LY_ERR value.
658 */
659static LY_ERR
660json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
661{
662 if (!is_open_array(ctx, node)) {
663 LY_CHECK_RET(json_print_member(ctx, node, 0));
664 json_print_array_open(ctx, node);
665 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200666 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200667 }
668
669 if (node->schema->nodetype == LYS_LIST) {
670 if (!lyd_node_children(node, 0)) {
671 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200672 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200673 LEVEL_PRINTED;
674 } else {
675 /* print list's content */
676 LY_CHECK_RET(json_print_inner(ctx, node));
677 }
678 } else {
679 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200680 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200681
682 if (node->meta && !ctx->print_sibling_metadata) {
683 ctx->print_sibling_metadata = node;
684 }
685 }
686
687 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
688 json_print_array_close(ctx);
689 }
690
691 return LY_SUCCESS;
692}
693
694/**
695 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
696 * This function is supposed to be called when the leaf-list array is closed.
697 *
698 * @param[in] ctx JSON printer context.
699 * @return LY_ERR value.
700 */
701static LY_ERR
702json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
703{
704 const struct lyd_node *prev, *node, *iter;
705
706 if (!ctx->print_sibling_metadata) {
707 return LY_SUCCESS;
708 }
709
710 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
711 prev->next && matching_node(node, prev);
712 node = prev, prev = node->prev) {}
713
714 /* node is the first instance of the leaf-list */
715
716 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200717 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200718 LEVEL_INC;
719 LY_LIST_FOR(node, iter) {
720 PRINT_COMMA;
721 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200722 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200723 LEVEL_INC;
724 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
725 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200726 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200727 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200728 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200729 }
730 LEVEL_PRINTED;
731 if (!matching_node(iter, iter->next)) {
732 break;
733 }
734 }
735 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200736 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200737 LEVEL_PRINTED;
738
739 return LY_SUCCESS;
740}
741
742/**
743 * @brief Print opaq data node including its attributes.
744 *
745 * @param[in] ctx JSON printer context.
746 * @param[in] node Opaq node to print.
747 * @return LY_ERR value.
748 */
749static LY_ERR
750json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
751{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200752 uint8_t first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200753
754 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200755 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev;
756 const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next;
757 if (prev->next && matching_node((const struct lyd_node *)prev, (const struct lyd_node *)node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200758 first = 0;
759 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200760 if (next && matching_node((const struct lyd_node *)node, (const struct lyd_node *)next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200761 last = 0;
762 }
763 }
764
765 if (first) {
766 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0));
767
768 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200769 json_print_array_open(ctx, (struct lyd_node *)node);
Radek Krejci5536d282020-08-04 23:27:44 +0200770 LEVEL_INC;
771 }
772 }
773 if (node->child || (node->hint & LYD_NODE_OPAQ_ISLIST)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200774 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200775 LEVEL_PRINTED;
776 } else {
777 if (node->hint & LYD_VALUE_PARSE_ISEMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200778 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200779 } else if (node->hint & (LYD_VALUE_PARSE_ISBOOLEAN | LYD_VALUE_PARSE_ISNUMBER)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200780 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200781 } else {
782 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200783 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200784 }
785 LEVEL_PRINTED;
786
787 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200788 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200789
790 }
791 if (last && (node->hint & LYD_NODE_OPAQ_ISLIST)) {
792 json_print_array_close(ctx);
793 LEVEL_DEC;
794 LEVEL_PRINTED;
795 }
796
797 return LY_SUCCESS;
798}
799
800/**
801 * @brief Print all the types of data node including its metadata.
802 *
803 * @param[in] ctx JSON printer context.
804 * @param[in] node Data node to print.
805 * @return LY_ERR value.
806 */
807static LY_ERR
808json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
809{
810 if (!ly_should_print(node, ctx->options)) {
811 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
812 json_print_array_close(ctx);
813 }
814 return LY_SUCCESS;
815 }
816
817 if (!node->schema) {
818 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
819 } else {
820 switch (node->schema->nodetype) {
821 case LYS_RPC:
822 case LYS_ACTION:
823 case LYS_NOTIF:
824 case LYS_CONTAINER:
825 LY_CHECK_RET(json_print_container(ctx, node));
826 break;
827 case LYS_LEAF:
828 LY_CHECK_RET(json_print_leaf(ctx, node));
829 break;
830 case LYS_LEAFLIST:
831 case LYS_LIST:
832 LY_CHECK_RET(json_print_leaf_list(ctx, node));
833 break;
834 case LYS_ANYXML:
835 case LYS_ANYDATA:
836 LY_CHECK_RET(json_print_container(ctx, node));
837 break;
838 default:
839 LOGINT(node->schema->module->ctx);
840 return EXIT_FAILURE;
841 }
842 }
843
844 ctx->level_printed = ctx->level;
845
846 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
847 json_print_metadata_leaflist(ctx);
848 ctx->print_sibling_metadata = NULL;
849 }
850
851 return LY_SUCCESS;
852}
853
854LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200855json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200856{
857 const struct lyd_node *node;
858 struct jsonpr_ctx ctx = {0};
859 const char *delimiter = (options & LYD_PRINT_FORMAT) ? "\n" : "";
860
861 ctx.out = out;
862 ctx.level = 1;
863 ctx.level_printed = 0;
864 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200865 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200866
867 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200868 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200869
870 /* content */
871 LY_LIST_FOR(root, node) {
872 LY_CHECK_RET(json_print_node(&ctx, node));
873 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
874 break;
875 }
876 }
877
878 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200879 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200880
881 assert(!ctx.open.count);
882 ly_set_erase(&ctx.open, NULL);
883
884 ly_print_flush(out);
885 return LY_SUCCESS;
886}