blob: 0c10df9f9cb0d2e66cc145aeabd8672bd44acd62 [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 Vaskoad92b672020-11-12 13:11:31 +0100406 json_print_member2(ctx, (struct lyd_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 Krejcia1c1e542020-09-29 16:06:52 +0200598 struct lyd_node *children = lyd_child(node);
Radek Krejci857189e2020-09-01 13:26:36 +0200599 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200600
601 if (node->meta || children) {
602 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200603 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200604 has_content = 1;
605 }
606
Michal Vasko69730152020-10-09 16:30:07 +0200607 if (!node->schema || (node->schema->nodetype != LYS_LIST)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200608 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200609 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200610 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200611 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Michal Vasko69730152020-10-09 16:30:07 +0200612 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200613 }
614 LEVEL_INC;
615
616 json_print_attributes(ctx, node, 1);
617
618 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
619 /* print children */
620 LY_LIST_FOR(children, child) {
621 LY_CHECK_RET(json_print_node(ctx, child));
622 }
623 } else {
624 /* anydata */
625 json_print_anydata(ctx, (struct lyd_node_any *)node);
626 }
627
Radek Krejci5536d282020-08-04 23:27:44 +0200628 LEVEL_DEC;
629 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200630 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200631 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200632 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200633 }
634 LEVEL_PRINTED;
635
636 return LY_SUCCESS;
637}
638
639/**
640 * @brief Print container data node including its metadata.
641 *
642 * @param[in] ctx JSON printer context.
643 * @param[in] node Data node to print.
644 * @return LY_ERR value.
645 */
646static int
647json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
648{
649 LY_CHECK_RET(json_print_member(ctx, node, 0));
650 LY_CHECK_RET(json_print_inner(ctx, node));
651
652 return LY_SUCCESS;
653}
654
655/**
656 * @brief Print single leaf-list or list instance.
657 *
658 * In case of list, metadata are printed inside the list object. For the leaf-list,
659 * metadata are marked in the context for later printing after closing the array next to it using
660 * json_print_metadata_leaflist().
661 *
662 * @param[in] ctx JSON printer context.
663 * @param[in] node Data node to print.
664 * @return LY_ERR value.
665 */
666static LY_ERR
667json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
668{
669 if (!is_open_array(ctx, node)) {
670 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200671 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200672 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200673 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200674 }
675
676 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200677 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200678 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200679 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200680 LEVEL_PRINTED;
681 } else {
682 /* print list's content */
683 LY_CHECK_RET(json_print_inner(ctx, node));
684 }
685 } else {
686 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200687 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200688
689 if (node->meta && !ctx->print_sibling_metadata) {
690 ctx->print_sibling_metadata = node;
691 }
692 }
693
Michal Vasko69730152020-10-09 16:30:07 +0200694 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200695 json_print_array_close(ctx);
696 }
697
698 return LY_SUCCESS;
699}
700
701/**
702 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
703 * This function is supposed to be called when the leaf-list array is closed.
704 *
705 * @param[in] ctx JSON printer context.
706 * @return LY_ERR value.
707 */
708static LY_ERR
709json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
710{
711 const struct lyd_node *prev, *node, *iter;
712
713 if (!ctx->print_sibling_metadata) {
714 return LY_SUCCESS;
715 }
716
717 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
718 prev->next && matching_node(node, prev);
719 node = prev, prev = node->prev) {}
720
721 /* node is the first instance of the leaf-list */
722
723 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200724 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200725 LEVEL_INC;
726 LY_LIST_FOR(node, iter) {
727 PRINT_COMMA;
728 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200729 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200730 LEVEL_INC;
731 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
732 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200733 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200734 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200735 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200736 }
737 LEVEL_PRINTED;
738 if (!matching_node(iter, iter->next)) {
739 break;
740 }
741 }
742 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200743 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200744 LEVEL_PRINTED;
745
746 return LY_SUCCESS;
747}
748
749/**
750 * @brief Print opaq data node including its attributes.
751 *
752 * @param[in] ctx JSON printer context.
753 * @param[in] node Opaq node to print.
754 * @return LY_ERR value.
755 */
756static LY_ERR
757json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
758{
Radek Krejci857189e2020-09-01 13:26:36 +0200759 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200760
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200761 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200762 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev;
763 const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next;
764 if (prev->next && matching_node((const struct lyd_node *)prev, (const struct lyd_node *)node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200765 first = 0;
766 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200767 if (next && matching_node((const struct lyd_node *)node, (const struct lyd_node *)next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200768 last = 0;
769 }
770 }
771
772 if (first) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100773 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200774
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200775 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vaskob0099a92020-08-31 14:55:23 +0200776 LY_CHECK_RET(json_print_array_open(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200777 LEVEL_INC;
778 }
779 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200780 if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200781 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200782 LEVEL_PRINTED;
783 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200784 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200785 ly_print_(ctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200786 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200787 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200788 } else {
789 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200790 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200791 }
792 LEVEL_PRINTED;
793
794 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200795 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200796
797 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200798 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200799 json_print_array_close(ctx);
800 LEVEL_DEC;
801 LEVEL_PRINTED;
802 }
803
804 return LY_SUCCESS;
805}
806
807/**
808 * @brief Print all the types of data node including its metadata.
809 *
810 * @param[in] ctx JSON printer context.
811 * @param[in] node Data node to print.
812 * @return LY_ERR value.
813 */
814static LY_ERR
815json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
816{
817 if (!ly_should_print(node, ctx->options)) {
Michal Vasko69730152020-10-09 16:30:07 +0200818 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200819 json_print_array_close(ctx);
820 }
821 return LY_SUCCESS;
822 }
823
824 if (!node->schema) {
825 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
826 } else {
827 switch (node->schema->nodetype) {
828 case LYS_RPC:
829 case LYS_ACTION:
830 case LYS_NOTIF:
831 case LYS_CONTAINER:
832 LY_CHECK_RET(json_print_container(ctx, node));
833 break;
834 case LYS_LEAF:
835 LY_CHECK_RET(json_print_leaf(ctx, node));
836 break;
837 case LYS_LEAFLIST:
838 case LYS_LIST:
839 LY_CHECK_RET(json_print_leaf_list(ctx, node));
840 break;
841 case LYS_ANYXML:
842 case LYS_ANYDATA:
843 LY_CHECK_RET(json_print_container(ctx, node));
844 break;
845 default:
846 LOGINT(node->schema->module->ctx);
847 return EXIT_FAILURE;
848 }
849 }
850
851 ctx->level_printed = ctx->level;
852
853 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
854 json_print_metadata_leaflist(ctx);
855 ctx->print_sibling_metadata = NULL;
856 }
857
858 return LY_SUCCESS;
859}
860
861LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200862json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200863{
864 const struct lyd_node *node;
865 struct jsonpr_ctx ctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200866 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200867
Radek Krejci2e874772020-08-28 16:36:33 +0200868 if (!root) {
869 ly_print_(out, "{}%s", delimiter);
870 ly_print_flush(out);
871 return LY_SUCCESS;
872 }
873
Radek Krejci5536d282020-08-04 23:27:44 +0200874 ctx.out = out;
875 ctx.level = 1;
876 ctx.level_printed = 0;
877 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200878 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200879
880 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200881 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200882
883 /* content */
884 LY_LIST_FOR(root, node) {
885 LY_CHECK_RET(json_print_node(&ctx, node));
886 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
887 break;
888 }
889 }
890
891 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200892 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200893
894 assert(!ctx.open.count);
895 ly_set_erase(&ctx.open, NULL);
896
897 ly_print_flush(out);
898 return LY_SUCCESS;
899}