blob: aaab2108e53f4adb38e1efb1a994214f8554501d [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:
Michal Vaskoc5b59af2020-11-03 17:13:41 +0100354 case LY_TYPE_UNION:
Radek Krejci5536d282020-08-04 23:27:44 +0200355 json_print_string(ctx->out, value);
356 break;
357
358 case LY_TYPE_INT8:
359 case LY_TYPE_INT16:
360 case LY_TYPE_INT32:
361 case LY_TYPE_UINT8:
362 case LY_TYPE_UINT16:
363 case LY_TYPE_UINT32:
364 case LY_TYPE_BOOL:
Michal Vasko5233e962020-08-14 14:26:20 +0200365 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200366 break;
367
368 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200369 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200370 break;
371
372 default:
373 /* error */
374 LOGINT_RET(ctx->ctx);
375 }
376
377 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200378 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200379 }
380
381 return LY_SUCCESS;
382}
383
384/**
385 * @brief Print all the attributes of the opaq node.
386 *
387 * @param[in] ctx JSON printer context.
388 * @param[in] node Opaq node where the attributes are placed.
389 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
390 * @return LY_ERR value.
391 */
392static LY_ERR
393json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
394{
395 struct lyd_attr *attr;
396
397 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200398 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200399 LEVEL_PRINTED;
400 }
401
402 for (attr = node->attr; attr; attr = attr->next) {
403 PRINT_COMMA;
Michal Vasko22df3f02020-08-24 13:29:22 +0200404 json_print_member2(ctx, (struct lyd_node *)node, attr->format, &attr->prefix, attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200405
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200406 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200407 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200408 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200409 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200410 } else {
411 json_print_string(ctx->out, attr->value);
412 }
413 LEVEL_PRINTED;
414 }
415
416 return LY_SUCCESS;
417}
418
419/**
420 * @brief Print all the metadata of the node.
421 *
422 * @param[in] ctx JSON printer context.
423 * @param[in] node Node where the metadata are placed.
424 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
425 * @return LY_ERR value.
426 */
427static LY_ERR
428json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
429{
430 struct lyd_meta *meta;
431
432 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200433 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200434 LEVEL_PRINTED;
435 }
436
437 for (meta = node->meta; meta; meta = meta->next) {
438 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200439 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200440 LY_CHECK_RET(json_print_value(ctx, &meta->value));
441 LEVEL_PRINTED;
442 }
443
444 return LY_SUCCESS;
445}
446
447/**
448 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
449 *
450 * @param[in] ctx JSON printer context.
451 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200452 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200453 * @return LY_ERR value.
454 */
455static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200456json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200457{
458 const struct lys_module *wdmod = NULL;
459
460 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
461 /* we have implicit OR explicit default node */
462 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200463 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200464 }
465
466 if (node->schema && node->meta) {
467 if (inner) {
468 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
469 } else {
470 LY_CHECK_RET(json_print_member(ctx, node, 1));
471 }
Michal Vasko5233e962020-08-14 14:26:20 +0200472 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200473 LEVEL_INC;
474 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
475 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200476 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200477 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200478 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200479 if (inner) {
480 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
481 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200482 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vasko69730152020-10-09 16:30:07 +0200483 &((struct lyd_node_opaq *)node)->prefix, ((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200484 }
Michal Vasko5233e962020-08-14 14:26:20 +0200485 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200486 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200487 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200488 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200489 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200490 LEVEL_PRINTED;
491 }
492
493 return LY_SUCCESS;
494}
495
496/**
497 * @brief Print leaf data node including its metadata.
498 *
499 * @param[in] ctx JSON printer context.
500 * @param[in] node Data node to print.
501 * @return LY_ERR value.
502 */
503static LY_ERR
504json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
505{
506 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vasko22df3f02020-08-24 13:29:22 +0200507 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200508 LEVEL_PRINTED;
509
510 /* print attributes as sibling */
511 json_print_attributes(ctx, node, 0);
512
513 return LY_SUCCESS;
514}
515
516/**
517 * @brief Print anydata data node including its metadata.
518 *
519 * @param[in] ctx JSON printer context.
520 * @param[in] any Anydata node to print.
521 * @return LY_ERR value.
522 */
523static LY_ERR
524json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
525{
526 LY_ERR ret = LY_SUCCESS;
527 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200528 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200529
530 if (!any->value.tree) {
531 /* no content */
532 return LY_SUCCESS;
533 }
534
535 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200536 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200537
538 /* turn logging off */
539 prev_lo = ly_log_options(0);
540
541 /* try to parse it into a data tree */
542 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
543 /* successfully parsed */
544 free(any->value.mem);
545 any->value.tree = iter;
546 any->value_type = LYD_ANYDATA_DATATREE;
547 }
548
549 /* turn loggin on again */
550 ly_log_options(prev_lo);
551 }
552
553 switch (any->value_type) {
554 case LYD_ANYDATA_DATATREE:
555 /* close opening tag and print data */
556 prev_opts = ctx->options;
557 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
558
559 LY_LIST_FOR(any->value.tree, iter) {
560 ret = json_print_node(ctx, iter);
561 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
562 }
563
564 ctx->options = prev_opts;
565 break;
566 case LYD_ANYDATA_JSON:
567 /* print without escaping special characters */
568 if (!any->value.str[0]) {
569 return LY_SUCCESS;
570 }
Michal Vasko5233e962020-08-14 14:26:20 +0200571 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200572 break;
573 case LYD_ANYDATA_STRING:
574 case LYD_ANYDATA_XML:
575 case LYD_ANYDATA_LYB:
576 /* JSON and LYB format is not supported */
577 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
578 return LY_SUCCESS;
579 }
580
581 return LY_SUCCESS;
582}
583
584/**
585 * @brief Print content of a single container/list data node including its metadata.
586 * The envelope specific to container and list are expected to be printed by the caller.
587 *
588 * @param[in] ctx JSON printer context.
589 * @param[in] node Data node to print.
590 * @return LY_ERR value.
591 */
592static LY_ERR
593json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
594{
595 struct lyd_node *child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200596 struct lyd_node *children = lyd_child(node);
Radek Krejci857189e2020-09-01 13:26:36 +0200597 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200598
599 if (node->meta || children) {
600 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200601 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200602 has_content = 1;
603 }
604
Michal Vasko69730152020-10-09 16:30:07 +0200605 if (!node->schema || (node->schema->nodetype != LYS_LIST)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200606 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200607 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200608 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200609 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Michal Vasko69730152020-10-09 16:30:07 +0200610 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200611 }
612 LEVEL_INC;
613
614 json_print_attributes(ctx, node, 1);
615
616 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
617 /* print children */
618 LY_LIST_FOR(children, child) {
619 LY_CHECK_RET(json_print_node(ctx, child));
620 }
621 } else {
622 /* anydata */
623 json_print_anydata(ctx, (struct lyd_node_any *)node);
624 }
625
Radek Krejci5536d282020-08-04 23:27:44 +0200626 LEVEL_DEC;
627 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200628 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200629 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200630 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200631 }
632 LEVEL_PRINTED;
633
634 return LY_SUCCESS;
635}
636
637/**
638 * @brief Print container data node including its metadata.
639 *
640 * @param[in] ctx JSON printer context.
641 * @param[in] node Data node to print.
642 * @return LY_ERR value.
643 */
644static int
645json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
646{
647 LY_CHECK_RET(json_print_member(ctx, node, 0));
648 LY_CHECK_RET(json_print_inner(ctx, node));
649
650 return LY_SUCCESS;
651}
652
653/**
654 * @brief Print single leaf-list or list instance.
655 *
656 * In case of list, metadata are printed inside the list object. For the leaf-list,
657 * metadata are marked in the context for later printing after closing the array next to it using
658 * json_print_metadata_leaflist().
659 *
660 * @param[in] ctx JSON printer context.
661 * @param[in] node Data node to print.
662 * @return LY_ERR value.
663 */
664static LY_ERR
665json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
666{
667 if (!is_open_array(ctx, node)) {
668 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200669 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200670 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200671 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200672 }
673
674 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200675 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200676 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200677 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200678 LEVEL_PRINTED;
679 } else {
680 /* print list's content */
681 LY_CHECK_RET(json_print_inner(ctx, node));
682 }
683 } else {
684 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200685 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200686
687 if (node->meta && !ctx->print_sibling_metadata) {
688 ctx->print_sibling_metadata = node;
689 }
690 }
691
Michal Vasko69730152020-10-09 16:30:07 +0200692 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200693 json_print_array_close(ctx);
694 }
695
696 return LY_SUCCESS;
697}
698
699/**
700 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
701 * This function is supposed to be called when the leaf-list array is closed.
702 *
703 * @param[in] ctx JSON printer context.
704 * @return LY_ERR value.
705 */
706static LY_ERR
707json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
708{
709 const struct lyd_node *prev, *node, *iter;
710
711 if (!ctx->print_sibling_metadata) {
712 return LY_SUCCESS;
713 }
714
715 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
716 prev->next && matching_node(node, prev);
717 node = prev, prev = node->prev) {}
718
719 /* node is the first instance of the leaf-list */
720
721 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200722 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200723 LEVEL_INC;
724 LY_LIST_FOR(node, iter) {
725 PRINT_COMMA;
726 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200727 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200728 LEVEL_INC;
729 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
730 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200731 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200732 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200733 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200734 }
735 LEVEL_PRINTED;
736 if (!matching_node(iter, iter->next)) {
737 break;
738 }
739 }
740 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200741 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200742 LEVEL_PRINTED;
743
744 return LY_SUCCESS;
745}
746
747/**
748 * @brief Print opaq data node including its attributes.
749 *
750 * @param[in] ctx JSON printer context.
751 * @param[in] node Opaq node to print.
752 * @return LY_ERR value.
753 */
754static LY_ERR
755json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
756{
Radek Krejci857189e2020-09-01 13:26:36 +0200757 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200758
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200759 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200760 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev;
761 const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next;
762 if (prev->next && matching_node((const struct lyd_node *)prev, (const struct lyd_node *)node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200763 first = 0;
764 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200765 if (next && matching_node((const struct lyd_node *)node, (const struct lyd_node *)next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200766 last = 0;
767 }
768 }
769
770 if (first) {
771 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0));
772
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200773 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vaskob0099a92020-08-31 14:55:23 +0200774 LY_CHECK_RET(json_print_array_open(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200775 LEVEL_INC;
776 }
777 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200778 if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200779 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200780 LEVEL_PRINTED;
781 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200782 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200783 ly_print_(ctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200784 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200785 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200786 } else {
787 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200788 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200789 }
790 LEVEL_PRINTED;
791
792 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200793 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200794
795 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200796 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200797 json_print_array_close(ctx);
798 LEVEL_DEC;
799 LEVEL_PRINTED;
800 }
801
802 return LY_SUCCESS;
803}
804
805/**
806 * @brief Print all the types of data node including its metadata.
807 *
808 * @param[in] ctx JSON printer context.
809 * @param[in] node Data node to print.
810 * @return LY_ERR value.
811 */
812static LY_ERR
813json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
814{
815 if (!ly_should_print(node, ctx->options)) {
Michal Vasko69730152020-10-09 16:30:07 +0200816 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200817 json_print_array_close(ctx);
818 }
819 return LY_SUCCESS;
820 }
821
822 if (!node->schema) {
823 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
824 } else {
825 switch (node->schema->nodetype) {
826 case LYS_RPC:
827 case LYS_ACTION:
828 case LYS_NOTIF:
829 case LYS_CONTAINER:
830 LY_CHECK_RET(json_print_container(ctx, node));
831 break;
832 case LYS_LEAF:
833 LY_CHECK_RET(json_print_leaf(ctx, node));
834 break;
835 case LYS_LEAFLIST:
836 case LYS_LIST:
837 LY_CHECK_RET(json_print_leaf_list(ctx, node));
838 break;
839 case LYS_ANYXML:
840 case LYS_ANYDATA:
841 LY_CHECK_RET(json_print_container(ctx, node));
842 break;
843 default:
844 LOGINT(node->schema->module->ctx);
845 return EXIT_FAILURE;
846 }
847 }
848
849 ctx->level_printed = ctx->level;
850
851 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
852 json_print_metadata_leaflist(ctx);
853 ctx->print_sibling_metadata = NULL;
854 }
855
856 return LY_SUCCESS;
857}
858
859LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200860json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200861{
862 const struct lyd_node *node;
863 struct jsonpr_ctx ctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200864 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200865
Radek Krejci2e874772020-08-28 16:36:33 +0200866 if (!root) {
867 ly_print_(out, "{}%s", delimiter);
868 ly_print_flush(out);
869 return LY_SUCCESS;
870 }
871
Radek Krejci5536d282020-08-04 23:27:44 +0200872 ctx.out = out;
873 ctx.level = 1;
874 ctx.level_printed = 0;
875 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200876 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200877
878 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200879 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200880
881 /* content */
882 LY_LIST_FOR(root, node) {
883 LY_CHECK_RET(json_print_node(&ctx, node));
884 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
885 break;
886 }
887 }
888
889 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200890 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200891
892 assert(!ctx.open.count);
893 ly_set_erase(&ctx.open, NULL);
894
895 ly_print_flush(out);
896 return LY_SUCCESS;
897}