blob: a3fe6fcf3a51e8f3c13ec9993727f1ba191b1c87 [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>
Radek Krejci47fab892020-11-05 17:02:41 +010016#include <stdint.h>
Radek Krejci5536d282020-08-04 23:27:44 +020017#include <stdlib.h>
Radek Krejci5536d282020-08-04 23:27:44 +020018
Radek Krejci13a57b62019-07-19 13:04:09 +020019#include "common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010020#include "context.h"
Radek Krejci5536d282020-08-04 23:27:44 +020021#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010022#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "out_internal.h"
Radek Krejci5536d282020-08-04 23:27:44 +020024#include "parser_data.h"
25#include "plugins_types.h"
26#include "printer_data.h"
27#include "printer_internal.h"
28#include "set.h"
29#include "tree.h"
30#include "tree_data.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020031#include "tree_schema.h"
32
33/**
Radek Krejci5536d282020-08-04 23:27:44 +020034 * @brief JSON printer context.
35 */
36struct jsonpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020037 struct ly_out *out; /**< output specification */
38 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
39 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
40 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020041
Radek Krejci1deb5be2020-08-26 16:43:36 +020042 uint16_t level_printed; /* level where some data were already printed */
Radek Krejci5536d282020-08-04 23:27:44 +020043 struct ly_set open; /* currently open array(s) */
44 const struct lyd_node *print_sibling_metadata;
45};
46
47/**
48 * @brief Mark that something was already written in the current level,
49 * used to decide if a comma is expected between the items
50 */
51#define LEVEL_PRINTED ctx->level_printed = ctx->level
52
53#define PRINT_COMMA \
54 if (ctx->level_printed >= ctx->level) {\
Michal Vasko5233e962020-08-14 14:26:20 +020055 ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\
Radek Krejci5536d282020-08-04 23:27:44 +020056 }
57
58static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node);
59
60/**
Radek Krejci5536d282020-08-04 23:27:44 +020061 * Compare 2 nodes, despite it is regular data node or an opaq node, and
62 * decide if they corresponds to the same schema node.
63 *
64 * TODO: rewrite lyd_compare_single and use it instead of this
65 *
66 * @return 1 - matching nodes, 0 - non-matching nodes
67 */
68static int
69matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
70{
71 assert(node1 || node2);
72
73 if (!node1 || !node2) {
74 return 0;
75 } else if (node1->schema != node2->schema) {
76 return 0;
77 }
78 if (!node1->schema) {
79 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020080 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
81 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +010082 if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) {
Radek Krejci5536d282020-08-04 23:27:44 +020083 return 0;
84 }
85 }
86
87 return 1;
88}
89
90/**
91 * @brief Open the JSON array ('[') for the specified @p node
92 *
93 * @param[in] ctx JSON printer context.
94 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020095 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020096 */
Michal Vaskob0099a92020-08-31 14:55:23 +020097static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +020098json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node)
99{
100 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +0200101 ly_print_(ctx->out, "[%s", (!node->schema || node->schema->nodetype != LYS_LEAFLIST) && DO_FORMAT ? "\n" : "");
Michal Vaskob0099a92020-08-31 14:55:23 +0200102 LY_CHECK_RET(ly_set_add(&ctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200103 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200104
105 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200106}
107
108/**
109 * @brief Get know if the array for the provided @p node is currently open.
110 *
111 * @param[in] ctx JSON printer context.
112 * @param[in] node Data node to check.
113 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
114 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
115 */
116static int
117is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node)
118{
Michal Vasko22df3f02020-08-24 13:29:22 +0200119 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 +0200120 return 1;
121 } else {
122 return 0;
123 }
124}
125
126/**
127 * @brief Close the most inner JSON array.
128 *
129 * @param[in] ctx JSON printer context.
130 */
131static void
132json_print_array_close(struct jsonpr_ctx *ctx)
133{
Michal Vasko22df3f02020-08-24 13:29:22 +0200134 const struct lysc_node *schema = ((const struct lyd_node *)ctx->open.objs[ctx->open.count - 1])->schema;
Radek Krejci5536d282020-08-04 23:27:44 +0200135
136 LEVEL_DEC;
137 ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL);
Michal Vasko69730152020-10-09 16:30:07 +0200138 if (schema && (schema->nodetype == LYS_LEAFLIST)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200139 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +0200140 ly_print_(ctx->out, "]");
Radek Krejci5536d282020-08-04 23:27:44 +0200141 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200142 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200143 }
144}
145
146/**
147 * @brief Get the node's module name to use as the @p node prefix in JSON.
148 * @param[in] node Node to process.
149 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
150 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
151 */
152static const char *
153node_prefix(const struct lyd_node *node)
154{
155 if (node->schema) {
156 return node->schema->module->name;
157 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200158 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200159 const struct lys_module *mod;
160
161 switch (onode->format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100162 case LY_PREF_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100163 return onode->name.module_name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100164 case LY_PREF_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100165 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200166 if (!mod) {
167 return NULL;
168 }
169 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100170 default:
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.
Michal Vaskoad92b672020-11-12 13:11:31 +0100286 * @param[in] name Name structure to provide name and prefix to print. If NULL, only "" name is printed.
Radek Krejci5536d282020-08-04 23:27:44 +0200287 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
288 * @return LY_ERR value.
289 */
290static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100291json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LY_PREFIX_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100292 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200293{
Michal Vaskoad92b672020-11-12 13:11:31 +0100294 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200295
296 PRINT_COMMA;
297
298 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100299 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200300 const struct lys_module *mod;
301
302 switch (format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100303 case LY_PREF_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100304 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200305 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100306 case LY_PREF_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100307 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, name->module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200308 if (mod) {
309 module_name = mod->name;
310 }
311 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100312 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200313 /* cannot be created */
314 LOGINT_RET(ctx->ctx);
315 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100316
317 name_str = name->name;
318 } else {
319 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200320 }
321
322 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200323 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100324 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200325 } else {
Michal Vaskoad92b672020-11-12 13:11:31 +0100326 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200327 }
328
329 return LY_SUCCESS;
330}
331
332/**
333 * @brief Print data value.
334 *
335 * @param[in] ctx JSON printer context.
336 * @param[in] val Data value to be printed.
337 * @return LY_ERR value.
338 */
339static LY_ERR
340json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val)
341{
Radek Krejci857189e2020-09-01 13:26:36 +0200342 ly_bool dynamic = 0;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200343 const char *value = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Radek Krejci5536d282020-08-04 23:27:44 +0200344
345 /* leafref is not supported */
346 switch (val->realtype->basetype) {
347 case LY_TYPE_BINARY:
348 case LY_TYPE_STRING:
349 case LY_TYPE_BITS:
350 case LY_TYPE_ENUM:
351 case LY_TYPE_INST:
352 case LY_TYPE_INT64:
353 case LY_TYPE_UINT64:
354 case LY_TYPE_DEC64:
355 case LY_TYPE_IDENT:
Michal Vaskoc5b59af2020-11-03 17:13:41 +0100356 case LY_TYPE_UNION:
Radek Krejci5536d282020-08-04 23:27:44 +0200357 json_print_string(ctx->out, value);
358 break;
359
360 case LY_TYPE_INT8:
361 case LY_TYPE_INT16:
362 case LY_TYPE_INT32:
363 case LY_TYPE_UINT8:
364 case LY_TYPE_UINT16:
365 case LY_TYPE_UINT32:
366 case LY_TYPE_BOOL:
Michal Vasko5233e962020-08-14 14:26:20 +0200367 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200368 break;
369
370 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200371 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200372 break;
373
374 default:
375 /* error */
376 LOGINT_RET(ctx->ctx);
377 }
378
379 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200380 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200381 }
382
383 return LY_SUCCESS;
384}
385
386/**
387 * @brief Print all the attributes of the opaq node.
388 *
389 * @param[in] ctx JSON printer context.
390 * @param[in] node Opaq node where the attributes are placed.
391 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
392 * @return LY_ERR value.
393 */
394static LY_ERR
395json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
396{
397 struct lyd_attr *attr;
398
399 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200400 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200401 LEVEL_PRINTED;
402 }
403
404 for (attr = node->attr; attr; attr = attr->next) {
405 PRINT_COMMA;
Michal Vasko9e685082021-01-29 14:49:09 +0100406 json_print_member2(ctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200407
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200408 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200409 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200410 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200411 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200412 } else {
413 json_print_string(ctx->out, attr->value);
414 }
415 LEVEL_PRINTED;
416 }
417
418 return LY_SUCCESS;
419}
420
421/**
422 * @brief Print all the metadata of the node.
423 *
424 * @param[in] ctx JSON printer context.
425 * @param[in] node Node where the metadata are placed.
426 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
427 * @return LY_ERR value.
428 */
429static LY_ERR
430json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
431{
432 struct lyd_meta *meta;
433
434 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200435 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200436 LEVEL_PRINTED;
437 }
438
439 for (meta = node->meta; meta; meta = meta->next) {
440 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200441 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200442 LY_CHECK_RET(json_print_value(ctx, &meta->value));
443 LEVEL_PRINTED;
444 }
445
446 return LY_SUCCESS;
447}
448
449/**
450 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
451 *
452 * @param[in] ctx JSON printer context.
453 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200454 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200455 * @return LY_ERR value.
456 */
457static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200458json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200459{
460 const struct lys_module *wdmod = NULL;
461
462 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
463 /* we have implicit OR explicit default node */
464 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200465 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200466 }
467
468 if (node->schema && node->meta) {
469 if (inner) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100470 LY_CHECK_RET(json_print_member2(ctx, NULL, LY_PREF_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200471 } else {
472 LY_CHECK_RET(json_print_member(ctx, node, 1));
473 }
Michal Vasko5233e962020-08-14 14:26:20 +0200474 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200475 LEVEL_INC;
476 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
477 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200478 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200479 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200480 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200481 if (inner) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100482 LY_CHECK_RET(json_print_member2(ctx, NULL, LY_PREF_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200483 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200484 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100485 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200486 }
Michal Vasko5233e962020-08-14 14:26:20 +0200487 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200488 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200489 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200490 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200491 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200492 LEVEL_PRINTED;
493 }
494
495 return LY_SUCCESS;
496}
497
498/**
499 * @brief Print leaf data node including its metadata.
500 *
501 * @param[in] ctx JSON printer context.
502 * @param[in] node Data node to print.
503 * @return LY_ERR value.
504 */
505static LY_ERR
506json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
507{
508 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vasko22df3f02020-08-24 13:29:22 +0200509 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200510 LEVEL_PRINTED;
511
512 /* print attributes as sibling */
513 json_print_attributes(ctx, node, 0);
514
515 return LY_SUCCESS;
516}
517
518/**
519 * @brief Print anydata data node including its metadata.
520 *
521 * @param[in] ctx JSON printer context.
522 * @param[in] any Anydata node to print.
523 * @return LY_ERR value.
524 */
525static LY_ERR
526json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
527{
528 LY_ERR ret = LY_SUCCESS;
529 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200530 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200531
532 if (!any->value.tree) {
533 /* no content */
534 return LY_SUCCESS;
535 }
536
537 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200538 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200539
540 /* turn logging off */
541 prev_lo = ly_log_options(0);
542
543 /* try to parse it into a data tree */
544 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
545 /* successfully parsed */
546 free(any->value.mem);
547 any->value.tree = iter;
548 any->value_type = LYD_ANYDATA_DATATREE;
549 }
550
551 /* turn loggin on again */
552 ly_log_options(prev_lo);
553 }
554
555 switch (any->value_type) {
556 case LYD_ANYDATA_DATATREE:
557 /* close opening tag and print data */
558 prev_opts = ctx->options;
559 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
560
561 LY_LIST_FOR(any->value.tree, iter) {
562 ret = json_print_node(ctx, iter);
563 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
564 }
565
566 ctx->options = prev_opts;
567 break;
568 case LYD_ANYDATA_JSON:
569 /* print without escaping special characters */
570 if (!any->value.str[0]) {
571 return LY_SUCCESS;
572 }
Michal Vasko5233e962020-08-14 14:26:20 +0200573 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200574 break;
575 case LYD_ANYDATA_STRING:
576 case LYD_ANYDATA_XML:
577 case LYD_ANYDATA_LYB:
578 /* JSON and LYB format is not supported */
579 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
580 return LY_SUCCESS;
581 }
582
583 return LY_SUCCESS;
584}
585
586/**
587 * @brief Print content of a single container/list data node including its metadata.
588 * The envelope specific to container and list are expected to be printed by the caller.
589 *
590 * @param[in] ctx JSON printer context.
591 * @param[in] node Data node to print.
592 * @return LY_ERR value.
593 */
594static LY_ERR
595json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
596{
597 struct lyd_node *child;
Radek Krejci857189e2020-09-01 13:26:36 +0200598 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200599
Michal Vasko630d9892020-12-08 17:11:08 +0100600 LY_LIST_FOR(lyd_child(node), child) {
601 if (ly_should_print(child, ctx->options)) {
602 break;
603 }
604 }
605 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200606 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200607 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200608 has_content = 1;
609 }
610
Michal Vasko69730152020-10-09 16:30:07 +0200611 if (!node->schema || (node->schema->nodetype != LYS_LIST)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200612 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200613 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200614 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200615 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Michal Vasko69730152020-10-09 16:30:07 +0200616 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200617 }
618 LEVEL_INC;
619
620 json_print_attributes(ctx, node, 1);
621
622 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
623 /* print children */
Michal Vasko630d9892020-12-08 17:11:08 +0100624 LY_LIST_FOR(lyd_child(node), child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200625 LY_CHECK_RET(json_print_node(ctx, child));
626 }
627 } else {
628 /* anydata */
629 json_print_anydata(ctx, (struct lyd_node_any *)node);
630 }
631
Radek Krejci5536d282020-08-04 23:27:44 +0200632 LEVEL_DEC;
633 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200634 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200635 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200636 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200637 }
638 LEVEL_PRINTED;
639
640 return LY_SUCCESS;
641}
642
643/**
644 * @brief Print container data node including its metadata.
645 *
646 * @param[in] ctx JSON printer context.
647 * @param[in] node Data node to print.
648 * @return LY_ERR value.
649 */
650static int
651json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
652{
653 LY_CHECK_RET(json_print_member(ctx, node, 0));
654 LY_CHECK_RET(json_print_inner(ctx, node));
655
656 return LY_SUCCESS;
657}
658
659/**
660 * @brief Print single leaf-list or list instance.
661 *
662 * In case of list, metadata are printed inside the list object. For the leaf-list,
663 * metadata are marked in the context for later printing after closing the array next to it using
664 * json_print_metadata_leaflist().
665 *
666 * @param[in] ctx JSON printer context.
667 * @param[in] node Data node to print.
668 * @return LY_ERR value.
669 */
670static LY_ERR
671json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
672{
673 if (!is_open_array(ctx, node)) {
674 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200675 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200676 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200677 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200678 }
679
680 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200681 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200682 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200683 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200684 LEVEL_PRINTED;
685 } else {
686 /* print list's content */
687 LY_CHECK_RET(json_print_inner(ctx, node));
688 }
689 } else {
690 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200691 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200692
693 if (node->meta && !ctx->print_sibling_metadata) {
694 ctx->print_sibling_metadata = node;
695 }
696 }
697
Michal Vasko69730152020-10-09 16:30:07 +0200698 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200699 json_print_array_close(ctx);
700 }
701
702 return LY_SUCCESS;
703}
704
705/**
706 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
707 * This function is supposed to be called when the leaf-list array is closed.
708 *
709 * @param[in] ctx JSON printer context.
710 * @return LY_ERR value.
711 */
712static LY_ERR
713json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
714{
715 const struct lyd_node *prev, *node, *iter;
716
717 if (!ctx->print_sibling_metadata) {
718 return LY_SUCCESS;
719 }
720
721 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
722 prev->next && matching_node(node, prev);
723 node = prev, prev = node->prev) {}
724
725 /* node is the first instance of the leaf-list */
726
727 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200728 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200729 LEVEL_INC;
730 LY_LIST_FOR(node, iter) {
731 PRINT_COMMA;
732 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200733 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200734 LEVEL_INC;
735 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
736 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200737 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200738 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200739 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200740 }
741 LEVEL_PRINTED;
742 if (!matching_node(iter, iter->next)) {
743 break;
744 }
745 }
746 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200747 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200748 LEVEL_PRINTED;
749
750 return LY_SUCCESS;
751}
752
753/**
754 * @brief Print opaq data node including its attributes.
755 *
756 * @param[in] ctx JSON printer context.
757 * @param[in] node Opaq node to print.
758 * @return LY_ERR value.
759 */
760static LY_ERR
761json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
762{
Radek Krejci857189e2020-09-01 13:26:36 +0200763 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200764
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200765 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100766 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200767 first = 0;
768 }
Michal Vasko9e685082021-01-29 14:49:09 +0100769 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200770 last = 0;
771 }
772 }
773
774 if (first) {
Michal Vasko9e685082021-01-29 14:49:09 +0100775 LY_CHECK_RET(json_print_member2(ctx, lyd_parent(&node->node), node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200776
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200777 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100778 LY_CHECK_RET(json_print_array_open(ctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200779 LEVEL_INC;
780 }
781 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200782 if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko9e685082021-01-29 14:49:09 +0100783 LY_CHECK_RET(json_print_inner(ctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200784 LEVEL_PRINTED;
785 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200786 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200787 ly_print_(ctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200788 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200789 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200790 } else {
791 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200792 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200793 }
794 LEVEL_PRINTED;
795
796 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200797 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200798
799 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200800 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200801 json_print_array_close(ctx);
802 LEVEL_DEC;
803 LEVEL_PRINTED;
804 }
805
806 return LY_SUCCESS;
807}
808
809/**
810 * @brief Print all the types of data node including its metadata.
811 *
812 * @param[in] ctx JSON printer context.
813 * @param[in] node Data node to print.
814 * @return LY_ERR value.
815 */
816static LY_ERR
817json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
818{
819 if (!ly_should_print(node, ctx->options)) {
Michal Vasko69730152020-10-09 16:30:07 +0200820 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200821 json_print_array_close(ctx);
822 }
823 return LY_SUCCESS;
824 }
825
826 if (!node->schema) {
827 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
828 } else {
829 switch (node->schema->nodetype) {
830 case LYS_RPC:
831 case LYS_ACTION:
832 case LYS_NOTIF:
833 case LYS_CONTAINER:
834 LY_CHECK_RET(json_print_container(ctx, node));
835 break;
836 case LYS_LEAF:
837 LY_CHECK_RET(json_print_leaf(ctx, node));
838 break;
839 case LYS_LEAFLIST:
840 case LYS_LIST:
841 LY_CHECK_RET(json_print_leaf_list(ctx, node));
842 break;
843 case LYS_ANYXML:
844 case LYS_ANYDATA:
845 LY_CHECK_RET(json_print_container(ctx, node));
846 break;
847 default:
848 LOGINT(node->schema->module->ctx);
849 return EXIT_FAILURE;
850 }
851 }
852
853 ctx->level_printed = ctx->level;
854
855 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
856 json_print_metadata_leaflist(ctx);
857 ctx->print_sibling_metadata = NULL;
858 }
859
860 return LY_SUCCESS;
861}
862
863LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200864json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200865{
866 const struct lyd_node *node;
867 struct jsonpr_ctx ctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200868 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200869
Radek Krejci2e874772020-08-28 16:36:33 +0200870 if (!root) {
871 ly_print_(out, "{}%s", delimiter);
872 ly_print_flush(out);
873 return LY_SUCCESS;
874 }
875
Radek Krejci5536d282020-08-04 23:27:44 +0200876 ctx.out = out;
877 ctx.level = 1;
878 ctx.level_printed = 0;
879 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200880 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200881
882 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200883 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200884
885 /* content */
886 LY_LIST_FOR(root, node) {
887 LY_CHECK_RET(json_print_node(&ctx, node));
888 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
889 break;
890 }
891 }
892
893 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200894 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200895
896 assert(!ctx.open.count);
897 ly_set_erase(&ctx.open, NULL);
898
899 ly_print_flush(out);
900 return LY_SUCCESS;
901}