blob: f67d31fdbb16fcde39d10c90ae014ba2a5123ea0 [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"
Michal Vaskoafac7822020-10-20 14:22:26 +020021#include "out_internal.h"
Radek Krejci5536d282020-08-04 23:27:44 +020022#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 {
Radek Krejci1deb5be2020-08-26 16:43:36 +020036 struct ly_out *out; /**< output specification */
37 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
38 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
39 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020040
Radek Krejci1deb5be2020-08-26 16:43:36 +020041 uint16_t level_printed; /* level where some data were already printed */
Radek Krejci5536d282020-08-04 23:27:44 +020042 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) {\
Michal Vasko5233e962020-08-14 14:26:20 +020054 ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\
Radek Krejci5536d282020-08-04 23:27:44 +020055 }
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 */
Michal Vasko22df3f02020-08-24 13:29:22 +020079 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
80 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vasko69730152020-10-09 16:30:07 +020081 if ((onode1->name != onode2->name) || (onode1->prefix.id != onode2->prefix.id)) {
Radek Krejci5536d282020-08-04 23:27:44 +020082 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.
Michal Vaskob0099a92020-08-31 14:55:23 +020094 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020095 */
Michal Vaskob0099a92020-08-31 14:55:23 +020096static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +020097json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node)
98{
99 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +0200100 ly_print_(ctx->out, "[%s", (!node->schema || node->schema->nodetype != LYS_LEAFLIST) && DO_FORMAT ? "\n" : "");
Michal Vaskob0099a92020-08-31 14:55:23 +0200101 LY_CHECK_RET(ly_set_add(&ctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200102 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200103
104 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200105}
106
107/**
108 * @brief Get know if the array for the provided @p node is currently open.
109 *
110 * @param[in] ctx JSON printer context.
111 * @param[in] node Data node to check.
112 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
113 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
114 */
115static int
116is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node)
117{
Michal Vasko22df3f02020-08-24 13:29:22 +0200118 if (ctx->open.count && matching_node(node, (const struct lyd_node *)ctx->open.objs[ctx->open.count - 1])) {
Radek Krejci5536d282020-08-04 23:27:44 +0200119 return 1;
120 } else {
121 return 0;
122 }
123}
124
125/**
126 * @brief Close the most inner JSON array.
127 *
128 * @param[in] ctx JSON printer context.
129 */
130static void
131json_print_array_close(struct jsonpr_ctx *ctx)
132{
Michal Vasko22df3f02020-08-24 13:29:22 +0200133 const struct lysc_node *schema = ((const struct lyd_node *)ctx->open.objs[ctx->open.count - 1])->schema;
Radek Krejci5536d282020-08-04 23:27:44 +0200134
135 LEVEL_DEC;
136 ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL);
Michal Vasko69730152020-10-09 16:30:07 +0200137 if (schema && (schema->nodetype == LYS_LEAFLIST)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200138 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +0200139 ly_print_(ctx->out, "]");
Radek Krejci5536d282020-08-04 23:27:44 +0200140 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200141 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200142 }
143}
144
145/**
146 * @brief Get the node's module name to use as the @p node prefix in JSON.
147 * @param[in] node Node to process.
148 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
149 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
150 */
151static const char *
152node_prefix(const struct lyd_node *node)
153{
154 if (node->schema) {
155 return node->schema->module->name;
156 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200157 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200158 const struct lys_module *mod;
159
160 switch (onode->format) {
161 case LYD_JSON:
162 return onode->prefix.module_name;
163 case LYD_XML:
164 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->prefix.module_ns);
165 if (!mod) {
166 return NULL;
167 }
168 return mod->name;
Radek Krejci5536d282020-08-04 23:27:44 +0200169 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200170 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200171 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200172 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200173 }
174 }
175
176 return NULL;
177}
178
179/**
180 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
181 *
182 * Accepts both regulard a well as opaq nodes.
183 *
184 * @param[in] node1 The first node to compare.
185 * @param[in] node2 The second node to compare.
186 * @return 0 in case the nodes' modules are the same
187 * @return 1 in case the nodes belongs to different modules
188 */
189int
190json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
191{
192 assert(node1 || node2);
193
194 if (!node1 || !node2) {
195 return 1;
196 } else if (node1->schema && node2->schema) {
197 if (node1->schema->module == node2->schema->module) {
198 /* belongs to the same module */
199 return 0;
200 } else {
201 /* different modules */
202 return 1;
203 }
204 } else {
205 const char *pref1 = node_prefix(node1);
206 const char *pref2 = node_prefix(node2);
Michal Vasko69730152020-10-09 16:30:07 +0200207 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200208 return 0;
209 } else {
210 return 1;
211 }
212 }
213}
214
215/**
216 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
217 *
218 * @param[in] out The output handler.
219 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200220 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200221 */
Michal Vasko5233e962020-08-14 14:26:20 +0200222static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200223json_print_string(struct ly_out *out, const char *text)
224{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200225 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200226
227 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200228 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200229 }
230
Michal Vasko5233e962020-08-14 14:26:20 +0200231 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200232 for (i = n = 0; text[i]; i++) {
233 const unsigned char ascii = text[i];
234 if (ascii < 0x20) {
235 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200236 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200237 } else {
238 switch (ascii) {
239 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200240 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200241 break;
242 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200243 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200244 break;
245 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200246 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200247 n++;
248 }
249 }
250 }
Michal Vasko5233e962020-08-14 14:26:20 +0200251 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200252
Michal Vasko5233e962020-08-14 14:26:20 +0200253 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200254}
255
256/**
257 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
258 *
259 * @param[in] ctx JSON printer context.
260 * @param[in] node The data node being printed.
261 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
262 * @return LY_ERR value.
263 */
264static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200265json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200266{
267 PRINT_COMMA;
Michal Vasko69730152020-10-09 16:30:07 +0200268 if ((LEVEL == 1) || json_nscmp(node, (const struct lyd_node *)node->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200269 /* print "namespace" */
Michal Vasko5233e962020-08-14 14:26:20 +0200270 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200271 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200272 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200273 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200274 node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200275 }
276
277 return LY_SUCCESS;
278}
279
280/**
281 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
282 *
283 * @param[in] ctx JSON printer context.
284 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
285 * @param[in] format Format to decide how to process the @p prefix.
286 * @param[in] prefix Prefix structure to provide prefix string if prefix to print.
287 * @param[in] name Name of the memeber to print.
288 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
289 * @return LY_ERR value.
290 */
291static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200292json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LYD_FORMAT format,
Radek Krejci857189e2020-09-01 13:26:36 +0200293 const struct ly_prefix *prefix, const char *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200294{
295 const char *module_name = NULL;
296
297 PRINT_COMMA;
298
299 /* determine prefix string */
300 if (prefix) {
301 const struct lys_module *mod;
302
303 switch (format) {
304 case LYD_JSON:
305 module_name = prefix->module_name;
306 break;
307 case LYD_XML:
308 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, prefix->module_ns);
309 if (mod) {
310 module_name = mod->name;
311 }
312 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200313 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200314 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200315 /* cannot be created */
316 LOGINT_RET(ctx->ctx);
317 }
318 }
319
320 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200321 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vasko5233e962020-08-14 14:26:20 +0200322 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200323 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200324 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200325 }
326
327 return LY_SUCCESS;
328}
329
330/**
331 * @brief Print data value.
332 *
333 * @param[in] ctx JSON printer context.
334 * @param[in] val Data value to be printed.
335 * @return LY_ERR value.
336 */
337static LY_ERR
338json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val)
339{
Radek Krejci857189e2020-09-01 13:26:36 +0200340 ly_bool dynamic = 0;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200341 const char *value = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Radek Krejci5536d282020-08-04 23:27:44 +0200342
343 /* leafref is not supported */
344 switch (val->realtype->basetype) {
345 case LY_TYPE_BINARY:
346 case LY_TYPE_STRING:
347 case LY_TYPE_BITS:
348 case LY_TYPE_ENUM:
349 case LY_TYPE_INST:
350 case LY_TYPE_INT64:
351 case LY_TYPE_UINT64:
352 case LY_TYPE_DEC64:
353 case LY_TYPE_IDENT:
354 json_print_string(ctx->out, value);
355 break;
356
357 case LY_TYPE_INT8:
358 case LY_TYPE_INT16:
359 case LY_TYPE_INT32:
360 case LY_TYPE_UINT8:
361 case LY_TYPE_UINT16:
362 case LY_TYPE_UINT32:
363 case LY_TYPE_BOOL:
Michal Vasko5233e962020-08-14 14:26:20 +0200364 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200365 break;
366
367 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200368 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200369 break;
370
371 default:
372 /* error */
373 LOGINT_RET(ctx->ctx);
374 }
375
376 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200377 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200378 }
379
380 return LY_SUCCESS;
381}
382
383/**
384 * @brief Print all the attributes of the opaq node.
385 *
386 * @param[in] ctx JSON printer context.
387 * @param[in] node Opaq node where the attributes are placed.
388 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
389 * @return LY_ERR value.
390 */
391static LY_ERR
392json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
393{
394 struct lyd_attr *attr;
395
396 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200397 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200398 LEVEL_PRINTED;
399 }
400
401 for (attr = node->attr; attr; attr = attr->next) {
402 PRINT_COMMA;
Michal Vasko22df3f02020-08-24 13:29:22 +0200403 json_print_member2(ctx, (struct lyd_node *)node, attr->format, &attr->prefix, attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200404
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200405 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200406 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200407 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200408 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200409 } else {
410 json_print_string(ctx->out, attr->value);
411 }
412 LEVEL_PRINTED;
413 }
414
415 return LY_SUCCESS;
416}
417
418/**
419 * @brief Print all the metadata of the node.
420 *
421 * @param[in] ctx JSON printer context.
422 * @param[in] node Node where the metadata are placed.
423 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
424 * @return LY_ERR value.
425 */
426static LY_ERR
427json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
428{
429 struct lyd_meta *meta;
430
431 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200432 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200433 LEVEL_PRINTED;
434 }
435
436 for (meta = node->meta; meta; meta = meta->next) {
437 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200438 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200439 LY_CHECK_RET(json_print_value(ctx, &meta->value));
440 LEVEL_PRINTED;
441 }
442
443 return LY_SUCCESS;
444}
445
446/**
447 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
448 *
449 * @param[in] ctx JSON printer context.
450 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200451 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200452 * @return LY_ERR value.
453 */
454static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200455json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200456{
457 const struct lys_module *wdmod = NULL;
458
459 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
460 /* we have implicit OR explicit default node */
461 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200462 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200463 }
464
465 if (node->schema && node->meta) {
466 if (inner) {
467 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
468 } else {
469 LY_CHECK_RET(json_print_member(ctx, node, 1));
470 }
Michal Vasko5233e962020-08-14 14:26:20 +0200471 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200472 LEVEL_INC;
473 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
474 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200475 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200476 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200477 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200478 if (inner) {
479 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
480 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200481 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vasko69730152020-10-09 16:30:07 +0200482 &((struct lyd_node_opaq *)node)->prefix, ((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200483 }
Michal Vasko5233e962020-08-14 14:26:20 +0200484 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200485 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200486 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200487 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200488 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200489 LEVEL_PRINTED;
490 }
491
492 return LY_SUCCESS;
493}
494
495/**
496 * @brief Print leaf data node including its metadata.
497 *
498 * @param[in] ctx JSON printer context.
499 * @param[in] node Data node to print.
500 * @return LY_ERR value.
501 */
502static LY_ERR
503json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
504{
505 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vasko22df3f02020-08-24 13:29:22 +0200506 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200507 LEVEL_PRINTED;
508
509 /* print attributes as sibling */
510 json_print_attributes(ctx, node, 0);
511
512 return LY_SUCCESS;
513}
514
515/**
516 * @brief Print anydata data node including its metadata.
517 *
518 * @param[in] ctx JSON printer context.
519 * @param[in] any Anydata node to print.
520 * @return LY_ERR value.
521 */
522static LY_ERR
523json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
524{
525 LY_ERR ret = LY_SUCCESS;
526 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200527 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200528
529 if (!any->value.tree) {
530 /* no content */
531 return LY_SUCCESS;
532 }
533
534 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200535 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200536
537 /* turn logging off */
538 prev_lo = ly_log_options(0);
539
540 /* try to parse it into a data tree */
541 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
542 /* successfully parsed */
543 free(any->value.mem);
544 any->value.tree = iter;
545 any->value_type = LYD_ANYDATA_DATATREE;
546 }
547
548 /* turn loggin on again */
549 ly_log_options(prev_lo);
550 }
551
552 switch (any->value_type) {
553 case LYD_ANYDATA_DATATREE:
554 /* close opening tag and print data */
555 prev_opts = ctx->options;
556 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
557
558 LY_LIST_FOR(any->value.tree, iter) {
559 ret = json_print_node(ctx, iter);
560 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
561 }
562
563 ctx->options = prev_opts;
564 break;
565 case LYD_ANYDATA_JSON:
566 /* print without escaping special characters */
567 if (!any->value.str[0]) {
568 return LY_SUCCESS;
569 }
Michal Vasko5233e962020-08-14 14:26:20 +0200570 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200571 break;
572 case LYD_ANYDATA_STRING:
573 case LYD_ANYDATA_XML:
574 case LYD_ANYDATA_LYB:
575 /* JSON and LYB format is not supported */
576 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
577 return LY_SUCCESS;
578 }
579
580 return LY_SUCCESS;
581}
582
583/**
584 * @brief Print content of a single container/list data node including its metadata.
585 * The envelope specific to container and list are expected to be printed by the caller.
586 *
587 * @param[in] ctx JSON printer context.
588 * @param[in] node Data node to print.
589 * @return LY_ERR value.
590 */
591static LY_ERR
592json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
593{
594 struct lyd_node *child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200595 struct lyd_node *children = lyd_child(node);
Radek Krejci857189e2020-09-01 13:26:36 +0200596 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200597
598 if (node->meta || children) {
599 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200600 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200601 has_content = 1;
602 }
603
Michal Vasko69730152020-10-09 16:30:07 +0200604 if (!node->schema || (node->schema->nodetype != LYS_LIST)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200605 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200606 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200607 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200608 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Michal Vasko69730152020-10-09 16:30:07 +0200609 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200610 }
611 LEVEL_INC;
612
613 json_print_attributes(ctx, node, 1);
614
615 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
616 /* print children */
617 LY_LIST_FOR(children, child) {
618 LY_CHECK_RET(json_print_node(ctx, child));
619 }
620 } else {
621 /* anydata */
622 json_print_anydata(ctx, (struct lyd_node_any *)node);
623 }
624
Radek Krejci5536d282020-08-04 23:27:44 +0200625 LEVEL_DEC;
626 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200627 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200628 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200629 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200630 }
631 LEVEL_PRINTED;
632
633 return LY_SUCCESS;
634}
635
636/**
637 * @brief Print container data node including its metadata.
638 *
639 * @param[in] ctx JSON printer context.
640 * @param[in] node Data node to print.
641 * @return LY_ERR value.
642 */
643static int
644json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
645{
646 LY_CHECK_RET(json_print_member(ctx, node, 0));
647 LY_CHECK_RET(json_print_inner(ctx, node));
648
649 return LY_SUCCESS;
650}
651
652/**
653 * @brief Print single leaf-list or list instance.
654 *
655 * In case of list, metadata are printed inside the list object. For the leaf-list,
656 * metadata are marked in the context for later printing after closing the array next to it using
657 * json_print_metadata_leaflist().
658 *
659 * @param[in] ctx JSON printer context.
660 * @param[in] node Data node to print.
661 * @return LY_ERR value.
662 */
663static LY_ERR
664json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
665{
666 if (!is_open_array(ctx, node)) {
667 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200668 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200669 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200670 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200671 }
672
673 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200674 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200675 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200676 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200677 LEVEL_PRINTED;
678 } else {
679 /* print list's content */
680 LY_CHECK_RET(json_print_inner(ctx, node));
681 }
682 } else {
683 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200684 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200685
686 if (node->meta && !ctx->print_sibling_metadata) {
687 ctx->print_sibling_metadata = node;
688 }
689 }
690
Michal Vasko69730152020-10-09 16:30:07 +0200691 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200692 json_print_array_close(ctx);
693 }
694
695 return LY_SUCCESS;
696}
697
698/**
699 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
700 * This function is supposed to be called when the leaf-list array is closed.
701 *
702 * @param[in] ctx JSON printer context.
703 * @return LY_ERR value.
704 */
705static LY_ERR
706json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
707{
708 const struct lyd_node *prev, *node, *iter;
709
710 if (!ctx->print_sibling_metadata) {
711 return LY_SUCCESS;
712 }
713
714 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
715 prev->next && matching_node(node, prev);
716 node = prev, prev = node->prev) {}
717
718 /* node is the first instance of the leaf-list */
719
720 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200721 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200722 LEVEL_INC;
723 LY_LIST_FOR(node, iter) {
724 PRINT_COMMA;
725 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200726 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200727 LEVEL_INC;
728 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
729 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200730 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200731 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200732 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200733 }
734 LEVEL_PRINTED;
735 if (!matching_node(iter, iter->next)) {
736 break;
737 }
738 }
739 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200740 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200741 LEVEL_PRINTED;
742
743 return LY_SUCCESS;
744}
745
746/**
747 * @brief Print opaq data node including its attributes.
748 *
749 * @param[in] ctx JSON printer context.
750 * @param[in] node Opaq node to print.
751 * @return LY_ERR value.
752 */
753static LY_ERR
754json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
755{
Radek Krejci857189e2020-09-01 13:26:36 +0200756 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200757
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200758 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200759 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev;
760 const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next;
761 if (prev->next && matching_node((const struct lyd_node *)prev, (const struct lyd_node *)node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200762 first = 0;
763 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200764 if (next && matching_node((const struct lyd_node *)node, (const struct lyd_node *)next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200765 last = 0;
766 }
767 }
768
769 if (first) {
770 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0));
771
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200772 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vaskob0099a92020-08-31 14:55:23 +0200773 LY_CHECK_RET(json_print_array_open(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200774 LEVEL_INC;
775 }
776 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200777 if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200778 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200779 LEVEL_PRINTED;
780 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200781 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200782 ly_print_(ctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200783 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200784 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200785 } else {
786 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200787 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200788 }
789 LEVEL_PRINTED;
790
791 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200792 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200793
794 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200795 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200796 json_print_array_close(ctx);
797 LEVEL_DEC;
798 LEVEL_PRINTED;
799 }
800
801 return LY_SUCCESS;
802}
803
804/**
805 * @brief Print all the types of data node including its metadata.
806 *
807 * @param[in] ctx JSON printer context.
808 * @param[in] node Data node to print.
809 * @return LY_ERR value.
810 */
811static LY_ERR
812json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
813{
814 if (!ly_should_print(node, ctx->options)) {
Michal Vasko69730152020-10-09 16:30:07 +0200815 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200816 json_print_array_close(ctx);
817 }
818 return LY_SUCCESS;
819 }
820
821 if (!node->schema) {
822 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
823 } else {
824 switch (node->schema->nodetype) {
825 case LYS_RPC:
826 case LYS_ACTION:
827 case LYS_NOTIF:
828 case LYS_CONTAINER:
829 LY_CHECK_RET(json_print_container(ctx, node));
830 break;
831 case LYS_LEAF:
832 LY_CHECK_RET(json_print_leaf(ctx, node));
833 break;
834 case LYS_LEAFLIST:
835 case LYS_LIST:
836 LY_CHECK_RET(json_print_leaf_list(ctx, node));
837 break;
838 case LYS_ANYXML:
839 case LYS_ANYDATA:
840 LY_CHECK_RET(json_print_container(ctx, node));
841 break;
842 default:
843 LOGINT(node->schema->module->ctx);
844 return EXIT_FAILURE;
845 }
846 }
847
848 ctx->level_printed = ctx->level;
849
850 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
851 json_print_metadata_leaflist(ctx);
852 ctx->print_sibling_metadata = NULL;
853 }
854
855 return LY_SUCCESS;
856}
857
858LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200859json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200860{
861 const struct lyd_node *node;
862 struct jsonpr_ctx ctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200863 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200864
Radek Krejci2e874772020-08-28 16:36:33 +0200865 if (!root) {
866 ly_print_(out, "{}%s", delimiter);
867 ly_print_flush(out);
868 return LY_SUCCESS;
869 }
870
Radek Krejci5536d282020-08-04 23:27:44 +0200871 ctx.out = out;
872 ctx.level = 1;
873 ctx.level_printed = 0;
874 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200875 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200876
877 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200878 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200879
880 /* content */
881 LY_LIST_FOR(root, node) {
882 LY_CHECK_RET(json_print_node(&ctx, node));
883 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
884 break;
885 }
886 }
887
888 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200889 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200890
891 assert(!ctx.open.count);
892 ly_set_erase(&ctx.open, NULL);
893
894 ly_print_flush(out);
895 return LY_SUCCESS;
896}