blob: 2e3ff483b6b4d1d4392b3e6c87cef9165d498007 [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 */
Michal Vaskob6b25302021-12-02 10:48:32 +010038 const struct lyd_node *root; /**< root node of the subtree being printed */
Radek Krejci1deb5be2020-08-26 16:43:36 +020039 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
40 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
41 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020042
Radek Krejci1deb5be2020-08-26 16:43:36 +020043 uint16_t level_printed; /* level where some data were already printed */
Michal Vaskob6b25302021-12-02 10:48:32 +010044 struct ly_set open; /* currently open array(s) */
Radek Krejci5536d282020-08-04 23:27:44 +020045 const struct lyd_node *print_sibling_metadata;
46};
47
48/**
49 * @brief Mark that something was already written in the current level,
50 * used to decide if a comma is expected between the items
51 */
Michal Vaskoc08b9d22022-02-10 15:48:39 +010052#define LEVEL_PRINTED pctx->level_printed = pctx->level
Radek Krejci5536d282020-08-04 23:27:44 +020053
54#define PRINT_COMMA \
Michal Vaskoc08b9d22022-02-10 15:48:39 +010055 if (pctx->level_printed >= pctx->level) { \
56 ly_print_(pctx->out, ",%s", (DO_FORMAT ? "\n" : "")); \
Radek Krejci5536d282020-08-04 23:27:44 +020057 }
58
Michal Vaskoc08b9d22022-02-10 15:48:39 +010059static LY_ERR json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node);
Radek Krejci5536d282020-08-04 23:27:44 +020060
61/**
Radek Krejci5536d282020-08-04 23:27:44 +020062 * Compare 2 nodes, despite it is regular data node or an opaq node, and
63 * decide if they corresponds to the same schema node.
64 *
65 * TODO: rewrite lyd_compare_single and use it instead of this
66 *
67 * @return 1 - matching nodes, 0 - non-matching nodes
68 */
69static int
70matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
71{
72 assert(node1 || node2);
73
74 if (!node1 || !node2) {
75 return 0;
76 } else if (node1->schema != node2->schema) {
77 return 0;
78 }
79 if (!node1->schema) {
80 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020081 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
82 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +010083 if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) {
Radek Krejci5536d282020-08-04 23:27:44 +020084 return 0;
85 }
86 }
87
88 return 1;
89}
90
91/**
92 * @brief Open the JSON array ('[') for the specified @p node
93 *
94 * @param[in] ctx JSON printer context.
95 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020096 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020097 */
Michal Vaskob0099a92020-08-31 14:55:23 +020098static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +010099json_print_array_open(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200100{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100101 ly_print_(pctx->out, "[%s", DO_FORMAT ? "\n" : "");
102 LY_CHECK_RET(ly_set_add(&pctx->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
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100117is_open_array(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200118{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100119 if (pctx->open.count && matching_node(node, pctx->open.dnodes[pctx->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
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100132json_print_array_close(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200133{
Radek Krejci5536d282020-08-04 23:27:44 +0200134 LEVEL_DEC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100135 ly_set_rm_index(&pctx->open, pctx->open.count - 1, NULL);
136 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200137}
138
139/**
140 * @brief Get the node's module name to use as the @p node prefix in JSON.
Radek Krejci31bc3f52021-04-26 11:09:58 +0200141 *
Radek Krejci5536d282020-08-04 23:27:44 +0200142 * @param[in] node Node to process.
143 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
144 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
145 */
146static const char *
147node_prefix(const struct lyd_node *node)
148{
149 if (node->schema) {
150 return node->schema->module->name;
151 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200152 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200153 const struct lys_module *mod;
154
155 switch (onode->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200156 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100157 return onode->name.module_name;
Radek Krejci8df109d2021-04-23 12:19:08 +0200158 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100159 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200160 if (!mod) {
161 return NULL;
162 }
163 return mod->name;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100164 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200165 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200166 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200167 }
168 }
169
170 return NULL;
171}
172
173/**
174 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
175 *
176 * Accepts both regulard a well as opaq nodes.
177 *
178 * @param[in] node1 The first node to compare.
179 * @param[in] node2 The second node to compare.
180 * @return 0 in case the nodes' modules are the same
181 * @return 1 in case the nodes belongs to different modules
182 */
183int
184json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
185{
186 assert(node1 || node2);
187
188 if (!node1 || !node2) {
189 return 1;
190 } else if (node1->schema && node2->schema) {
191 if (node1->schema->module == node2->schema->module) {
192 /* belongs to the same module */
193 return 0;
194 } else {
195 /* different modules */
196 return 1;
197 }
198 } else {
199 const char *pref1 = node_prefix(node1);
200 const char *pref2 = node_prefix(node2);
Michal Vasko69730152020-10-09 16:30:07 +0200201 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200202 return 0;
203 } else {
204 return 1;
205 }
206 }
207}
208
209/**
210 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
211 *
212 * @param[in] out The output handler.
213 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200214 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200215 */
Michal Vasko5233e962020-08-14 14:26:20 +0200216static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200217json_print_string(struct ly_out *out, const char *text)
218{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200219 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200220
221 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200222 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200223 }
224
Michal Vasko5233e962020-08-14 14:26:20 +0200225 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200226 for (i = n = 0; text[i]; i++) {
227 const unsigned char ascii = text[i];
228 if (ascii < 0x20) {
229 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200230 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200231 } else {
232 switch (ascii) {
233 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200234 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200235 break;
236 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200237 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200238 break;
239 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200240 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200241 n++;
242 }
243 }
244 }
Michal Vasko5233e962020-08-14 14:26:20 +0200245 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200246
Michal Vasko5233e962020-08-14 14:26:20 +0200247 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200248}
249
250/**
251 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
252 *
253 * @param[in] ctx JSON printer context.
254 * @param[in] node The data node being printed.
255 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
256 * @return LY_ERR value.
257 */
258static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100259json_print_member(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200260{
261 PRINT_COMMA;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100262 if ((LEVEL == 1) || json_nscmp(node, lyd_parent(node))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200263 /* print "namespace" */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100264 ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200265 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200266 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100267 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200268 }
269
270 return LY_SUCCESS;
271}
272
273/**
274 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
275 *
276 * @param[in] ctx JSON printer context.
277 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
278 * @param[in] format Format to decide how to process the @p prefix.
Michal Vaskoad92b672020-11-12 13:11:31 +0100279 * @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 +0200280 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
281 * @return LY_ERR value.
282 */
283static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100284json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VALUE_FORMAT format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100285 const struct ly_opaq_name *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200286{
Michal Vaskoad92b672020-11-12 13:11:31 +0100287 const char *module_name = NULL, *name_str;
Radek Krejci5536d282020-08-04 23:27:44 +0200288
289 PRINT_COMMA;
290
291 /* determine prefix string */
Michal Vaskoad92b672020-11-12 13:11:31 +0100292 if (name) {
Radek Krejci5536d282020-08-04 23:27:44 +0200293 const struct lys_module *mod;
294
295 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200296 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100297 module_name = name->module_name;
Radek Krejci5536d282020-08-04 23:27:44 +0200298 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200299 case LY_VALUE_XML:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100300 mod = ly_ctx_get_module_implemented_ns(pctx->ctx, name->module_ns);
Radek Krejci5536d282020-08-04 23:27:44 +0200301 if (mod) {
302 module_name = mod->name;
303 }
304 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100305 default:
Radek Krejci5536d282020-08-04 23:27:44 +0200306 /* cannot be created */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100307 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200308 }
Michal Vaskoad92b672020-11-12 13:11:31 +0100309
310 name_str = name->name;
311 } else {
312 name_str = "";
Radek Krejci5536d282020-08-04 23:27:44 +0200313 }
314
315 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200316 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100317 ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200318 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100319 ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200320 }
321
322 return LY_SUCCESS;
323}
324
325/**
326 * @brief Print data value.
327 *
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100328 * @param[in] pctx JSON printer context.
329 * @param[in] ctx Context used to print the value.
Radek Krejci5536d282020-08-04 23:27:44 +0200330 * @param[in] val Data value to be printed.
331 * @return LY_ERR value.
332 */
333static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100334json_print_value(struct jsonpr_ctx *pctx, const struct ly_ctx *ctx, const struct lyd_value *val)
Radek Krejci5536d282020-08-04 23:27:44 +0200335{
aPiecek0f6bf3e2021-08-25 15:47:49 +0200336 ly_bool dynamic;
Michal Vasko183b9112021-11-04 16:02:24 +0100337 LY_DATA_TYPE basetype;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100338 const char *value = val->realtype->plugin->print(ctx, val, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200339
Michal Vasko183b9112021-11-04 16:02:24 +0100340 basetype = val->realtype->basetype;
341
342print_val:
Radek Krejci5536d282020-08-04 23:27:44 +0200343 /* leafref is not supported */
Michal Vasko183b9112021-11-04 16:02:24 +0100344 switch (basetype) {
345 case LY_TYPE_UNION:
346 /* use the resolved type */
347 basetype = val->subvalue->value.realtype->basetype;
348 goto print_val;
349
Radek Krejci5536d282020-08-04 23:27:44 +0200350 case LY_TYPE_BINARY:
351 case LY_TYPE_STRING:
352 case LY_TYPE_BITS:
353 case LY_TYPE_ENUM:
354 case LY_TYPE_INST:
355 case LY_TYPE_INT64:
356 case LY_TYPE_UINT64:
357 case LY_TYPE_DEC64:
358 case LY_TYPE_IDENT:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100359 json_print_string(pctx->out, value);
Radek Krejci5536d282020-08-04 23:27:44 +0200360 break;
361
362 case LY_TYPE_INT8:
363 case LY_TYPE_INT16:
364 case LY_TYPE_INT32:
365 case LY_TYPE_UINT8:
366 case LY_TYPE_UINT16:
367 case LY_TYPE_UINT32:
368 case LY_TYPE_BOOL:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100369 ly_print_(pctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200370 break;
371
372 case LY_TYPE_EMPTY:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100373 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200374 break;
375
376 default:
377 /* error */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100378 LOGINT_RET(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200379 }
380
381 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200382 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200383 }
384
385 return LY_SUCCESS;
386}
387
388/**
389 * @brief Print all the attributes of the opaq node.
390 *
391 * @param[in] ctx JSON printer context.
392 * @param[in] node Opaq node where the attributes are placed.
393 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
394 * @return LY_ERR value.
395 */
396static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100397json_print_attribute(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200398{
399 struct lyd_attr *attr;
400
401 if (wdmod) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100402 ly_print_(pctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200403 LEVEL_PRINTED;
404 }
405
406 for (attr = node->attr; attr; attr = attr->next) {
407 PRINT_COMMA;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100408 json_print_member2(pctx, &node->node, attr->format, &attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200409
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200410 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100411 ly_print_(pctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200412 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100413 ly_print_(pctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200414 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100415 json_print_string(pctx->out, attr->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200416 }
417 LEVEL_PRINTED;
418 }
419
420 return LY_SUCCESS;
421}
422
423/**
424 * @brief Print all the metadata of the node.
425 *
426 * @param[in] ctx JSON printer context.
427 * @param[in] node Node where the metadata are placed.
428 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
429 * @return LY_ERR value.
430 */
431static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100432json_print_metadata(struct jsonpr_ctx *pctx, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejci5536d282020-08-04 23:27:44 +0200433{
434 struct lyd_meta *meta;
435
436 if (wdmod) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100437 ly_print_(pctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200438 LEVEL_PRINTED;
439 }
440
441 for (meta = node->meta; meta; meta = meta->next) {
442 PRINT_COMMA;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100443 ly_print_(pctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
444 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &meta->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200445 LEVEL_PRINTED;
446 }
447
448 return LY_SUCCESS;
449}
450
451/**
452 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
453 *
454 * @param[in] ctx JSON printer context.
455 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200456 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200457 * @return LY_ERR value.
458 */
459static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100460json_print_attributes(struct jsonpr_ctx *pctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200461{
462 const struct lys_module *wdmod = NULL;
463
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100464 if ((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200465 /* we have implicit OR explicit default node */
466 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200467 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200468 }
469
470 if (node->schema && node->meta) {
471 if (inner) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100472 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200473 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100474 LY_CHECK_RET(json_print_member(pctx, node, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200475 }
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100476 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200477 LEVEL_INC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100478 LY_CHECK_RET(json_print_metadata(pctx, node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200479 LEVEL_DEC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100480 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200481 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200482 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200483 if (inner) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100484 LY_CHECK_RET(json_print_member2(pctx, NULL, LY_VALUE_JSON, NULL, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200485 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100486 LY_CHECK_RET(json_print_member2(pctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vaskoad92b672020-11-12 13:11:31 +0100487 &((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200488 }
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100489 ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200490 LEVEL_INC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100491 LY_CHECK_RET(json_print_attribute(pctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200492 LEVEL_DEC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100493 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200494 LEVEL_PRINTED;
495 }
496
497 return LY_SUCCESS;
498}
499
500/**
501 * @brief Print leaf data node including its metadata.
502 *
503 * @param[in] ctx JSON printer context.
504 * @param[in] node Data node to print.
505 * @return LY_ERR value.
506 */
507static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100508json_print_leaf(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200509{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100510 LY_CHECK_RET(json_print_member(pctx, node, 0));
511 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200512 LEVEL_PRINTED;
513
514 /* print attributes as sibling */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100515 json_print_attributes(pctx, node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200516
517 return LY_SUCCESS;
518}
519
520/**
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100521 * @brief Print anydata/anyxml content.
Radek Krejci5536d282020-08-04 23:27:44 +0200522 *
523 * @param[in] ctx JSON printer context.
524 * @param[in] any Anydata node to print.
525 * @return LY_ERR value.
526 */
527static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100528json_print_any_content(struct jsonpr_ctx *pctx, struct lyd_node_any *any)
Radek Krejci5536d282020-08-04 23:27:44 +0200529{
530 LY_ERR ret = LY_SUCCESS;
531 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200532 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200533
Michal Vasko69abd362022-02-24 16:06:16 +0100534 assert(any->schema->nodetype & LYD_NODE_ANY);
Radek Krejci5536d282020-08-04 23:27:44 +0200535
536 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200537 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200538
539 /* turn logging off */
540 prev_lo = ly_log_options(0);
541
542 /* try to parse it into a data tree */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100543 if (lyd_parse_data_mem(pctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
Radek Krejci5536d282020-08-04 23:27:44 +0200544 /* successfully parsed */
545 free(any->value.mem);
546 any->value.tree = iter;
547 any->value_type = LYD_ANYDATA_DATATREE;
548 }
549
550 /* turn loggin on again */
551 ly_log_options(prev_lo);
552 }
553
554 switch (any->value_type) {
555 case LYD_ANYDATA_DATATREE:
Michal Vasko69abd362022-02-24 16:06:16 +0100556 /* print as an object */
557 ly_print_(pctx->out, "{%s", DO_FORMAT ? "\n" : "");
558 LEVEL_INC;
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100559
Radek Krejci5536d282020-08-04 23:27:44 +0200560 /* close opening tag and print data */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100561 prev_opts = pctx->options;
562 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci5536d282020-08-04 23:27:44 +0200563 LY_LIST_FOR(any->value.tree, iter) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100564 ret = json_print_node(pctx, iter);
Radek Krejci5536d282020-08-04 23:27:44 +0200565 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
566 }
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100567 pctx->options = prev_opts;
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100568
Michal Vasko69abd362022-02-24 16:06:16 +0100569 /* terminate the object */
570 if (DO_FORMAT) {
571 ly_print_(pctx->out, "\n%*s}", INDENT);
572 } else {
573 ly_print_(pctx->out, "}");
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100574 }
Michal Vasko69abd362022-02-24 16:06:16 +0100575 LEVEL_DEC;
Radek Krejci5536d282020-08-04 23:27:44 +0200576 break;
577 case LYD_ANYDATA_JSON:
Michal Vasko69abd362022-02-24 16:06:16 +0100578 if (!any->value.json) {
579 /* no content */
580 if (any->schema->nodetype == LYS_ANYXML) {
581 ly_print_(pctx->out, "null");
582 } else {
583 ly_print_(pctx->out, "{}");
584 }
585 } else {
586 /* print without escaping special characters */
587 ly_print_(pctx->out, "%s", any->value.json);
Radek Krejci5536d282020-08-04 23:27:44 +0200588 }
Radek Krejci5536d282020-08-04 23:27:44 +0200589 break;
590 case LYD_ANYDATA_STRING:
591 case LYD_ANYDATA_XML:
Michal Vasko69abd362022-02-24 16:06:16 +0100592 if (!any->value.str) {
593 /* no content */
594 if (any->schema->nodetype == LYS_ANYXML) {
595 ly_print_(pctx->out, "null");
596 } else {
597 ly_print_(pctx->out, "{}");
598 }
599 } else {
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100600 /* print as a string */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100601 ly_print_(pctx->out, "\"%s\"", any->value.str);
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100602 }
Michal Vasko69abd362022-02-24 16:06:16 +0100603 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200604 case LYD_ANYDATA_LYB:
Michal Vasko69abd362022-02-24 16:06:16 +0100605 /* LYB format is not supported */
606 LOGWRN(pctx->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type);
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100607 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200608 }
609
610 return LY_SUCCESS;
611}
612
613/**
Michal Vasko69abd362022-02-24 16:06:16 +0100614 * @brief Print content of a single container/list data node including its metadata.
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100615 * The envelope specific to nodes are expected to be printed by the caller.
Radek Krejci5536d282020-08-04 23:27:44 +0200616 *
617 * @param[in] ctx JSON printer context.
618 * @param[in] node Data node to print.
619 * @return LY_ERR value.
620 */
621static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100622json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200623{
624 struct lyd_node *child;
Radek Krejci857189e2020-09-01 13:26:36 +0200625 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200626
Michal Vasko630d9892020-12-08 17:11:08 +0100627 LY_LIST_FOR(lyd_child(node), child) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100628 if (ly_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100629 break;
630 }
631 }
632 if (node->meta || child) {
Radek Krejci5536d282020-08-04 23:27:44 +0200633 has_content = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200634 }
635
Michal Vasko1a85d332021-08-27 10:35:28 +0200636 if ((node->schema && (node->schema->nodetype == LYS_LIST)) ||
637 (!node->schema && (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST))) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100638 ly_print_(pctx->out, "%s%*s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ?
Michal Vasko1a85d332021-08-27 10:35:28 +0200639 (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : "");
640 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100641 ly_print_(pctx->out, "%s{%s", (is_open_array(pctx, node) && (pctx->level_printed >= pctx->level)) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200642 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200643 }
644 LEVEL_INC;
645
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100646 json_print_attributes(pctx, node, 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200647
Michal Vasko69abd362022-02-24 16:06:16 +0100648 /* print children */
649 LY_LIST_FOR(lyd_child(node), child) {
650 LY_CHECK_RET(json_print_node(pctx, child));
Radek Krejci5536d282020-08-04 23:27:44 +0200651 }
652
Radek Krejci5536d282020-08-04 23:27:44 +0200653 LEVEL_DEC;
654 if (DO_FORMAT && has_content) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100655 ly_print_(pctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200656 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100657 ly_print_(pctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200658 }
659 LEVEL_PRINTED;
660
661 return LY_SUCCESS;
662}
663
664/**
665 * @brief Print container data node including its metadata.
666 *
667 * @param[in] ctx JSON printer context.
668 * @param[in] node Data node to print.
669 * @return LY_ERR value.
670 */
671static int
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100672json_print_container(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200673{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100674 LY_CHECK_RET(json_print_member(pctx, node, 0));
675 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200676
677 return LY_SUCCESS;
678}
679
680/**
Michal Vasko69abd362022-02-24 16:06:16 +0100681 * @brief Print anydata/anyxml data node including its metadata.
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100682 *
683 * @param[in] ctx JSON printer context.
684 * @param[in] node Data node to print.
685 * @return LY_ERR value.
686 */
687static int
Michal Vasko69abd362022-02-24 16:06:16 +0100688json_print_any(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100689{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100690 LY_CHECK_RET(json_print_member(pctx, node, 0));
691 LY_CHECK_RET(json_print_any_content(pctx, (struct lyd_node_any *)node));
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100692 LEVEL_PRINTED;
693
694 /* print attributes as sibling */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100695 json_print_attributes(pctx, node, 0);
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100696
697 return LY_SUCCESS;
698}
699
700/**
Michal Vaskob6b25302021-12-02 10:48:32 +0100701 * @brief Check whether a node is the last printed instance of a (leaf-)list.
702 *
703 * @param[in] ctx JSON printer context.
704 * @param[in] node Last printed node.
705 * @return Whether it is the last printed instance or not.
706 */
707static ly_bool
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100708json_print_array_is_last_inst(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Michal Vaskob6b25302021-12-02 10:48:32 +0100709{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100710 if (!is_open_array(pctx, node)) {
Michal Vaskod3203372021-12-09 09:25:50 +0100711 /* no array open */
712 return 0;
713 }
714
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100715 if ((pctx->root == node) && !(pctx->options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vaskob6b25302021-12-02 10:48:32 +0100716 /* the only printed instance */
717 return 1;
718 }
719
Michal Vaskod3203372021-12-09 09:25:50 +0100720 if (!node->next || (node->next->schema != node->schema)) {
Michal Vaskob6b25302021-12-02 10:48:32 +0100721 /* last instance */
722 return 1;
723 }
724
725 return 0;
726}
727
728/**
Radek Krejci5536d282020-08-04 23:27:44 +0200729 * @brief Print single leaf-list or list instance.
730 *
731 * In case of list, metadata are printed inside the list object. For the leaf-list,
732 * metadata are marked in the context for later printing after closing the array next to it using
733 * json_print_metadata_leaflist().
734 *
735 * @param[in] ctx JSON printer context.
736 * @param[in] node Data node to print.
737 * @return LY_ERR value.
738 */
739static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100740json_print_leaf_list(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200741{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100742 if (!is_open_array(pctx, node)) {
743 LY_CHECK_RET(json_print_member(pctx, node, 0));
744 LY_CHECK_RET(json_print_array_open(pctx, node));
Radek Krejciee74a192021-04-09 09:55:34 +0200745 if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100746 ly_print_(pctx->out, "%*s", INDENT);
Radek Krejciee74a192021-04-09 09:55:34 +0200747 }
Radek Krejci5536d282020-08-04 23:27:44 +0200748 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100749 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200750 }
751
752 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200753 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200754 /* empty, e.g. in case of filter */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100755 ly_print_(pctx->out, "%s%snull", (pctx->level_printed >= pctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200756 LEVEL_PRINTED;
757 } else {
758 /* print list's content */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100759 LY_CHECK_RET(json_print_inner(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200760 }
761 } else {
762 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100763 LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200764
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100765 if (node->meta && !pctx->print_sibling_metadata) {
766 pctx->print_sibling_metadata = node;
Radek Krejci5536d282020-08-04 23:27:44 +0200767 }
768 }
769
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100770 if (json_print_array_is_last_inst(pctx, node)) {
771 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200772 }
773
774 return LY_SUCCESS;
775}
776
777/**
778 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
779 * This function is supposed to be called when the leaf-list array is closed.
780 *
781 * @param[in] ctx JSON printer context.
782 * @return LY_ERR value.
783 */
784static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100785json_print_metadata_leaflist(struct jsonpr_ctx *pctx)
Radek Krejci5536d282020-08-04 23:27:44 +0200786{
787 const struct lyd_node *prev, *node, *iter;
788
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100789 if (!pctx->print_sibling_metadata) {
Radek Krejci5536d282020-08-04 23:27:44 +0200790 return LY_SUCCESS;
791 }
792
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100793 for (node = pctx->print_sibling_metadata, prev = pctx->print_sibling_metadata->prev;
Radek Krejci5536d282020-08-04 23:27:44 +0200794 prev->next && matching_node(node, prev);
795 node = prev, prev = node->prev) {}
796
797 /* node is the first instance of the leaf-list */
798
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100799 LY_CHECK_RET(json_print_member(pctx, node, 1));
800 ly_print_(pctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200801 LEVEL_INC;
802 LY_LIST_FOR(node, iter) {
803 PRINT_COMMA;
804 if (iter->meta) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100805 ly_print_(pctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200806 LEVEL_INC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100807 LY_CHECK_RET(json_print_metadata(pctx, iter, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200808 LEVEL_DEC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100809 ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200810 } else {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100811 ly_print_(pctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200812 }
813 LEVEL_PRINTED;
814 if (!matching_node(iter, iter->next)) {
815 break;
816 }
817 }
818 LEVEL_DEC;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100819 ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200820 LEVEL_PRINTED;
821
822 return LY_SUCCESS;
823}
824
825/**
826 * @brief Print opaq data node including its attributes.
827 *
828 * @param[in] ctx JSON printer context.
829 * @param[in] node Opaq node to print.
830 * @return LY_ERR value.
831 */
832static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100833json_print_opaq(struct jsonpr_ctx *pctx, const struct lyd_node_opaq *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200834{
Radek Krejci857189e2020-09-01 13:26:36 +0200835 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200836
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200837 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100838 if (node->prev->next && matching_node(node->prev, &node->node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200839 first = 0;
840 }
Michal Vasko9e685082021-01-29 14:49:09 +0100841 if (node->next && matching_node(&node->node, node->next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200842 last = 0;
843 }
844 }
845
846 if (first) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100847 LY_CHECK_RET(json_print_member2(pctx, lyd_parent(&node->node), node->format, &node->name, 0));
Radek Krejci5536d282020-08-04 23:27:44 +0200848
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200849 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100850 LY_CHECK_RET(json_print_array_open(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200851 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200852 if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100853 ly_print_(pctx->out, "%*s", INDENT);
Michal Vasko1a85d332021-08-27 10:35:28 +0200854 }
855 } else if (node->hints & LYD_NODEHINT_LEAFLIST) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100856 ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200857 }
Michal Vasko1a85d332021-08-27 10:35:28 +0200858 if (node->child || (node->hints & LYD_NODEHINT_LIST)) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100859 LY_CHECK_RET(json_print_inner(pctx, &node->node));
Radek Krejci5536d282020-08-04 23:27:44 +0200860 LEVEL_PRINTED;
861 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200862 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100863 ly_print_(pctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200864 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100865 ly_print_(pctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200866 } else {
867 /* string */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100868 ly_print_(pctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200869 }
870 LEVEL_PRINTED;
871
872 /* attributes */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100873 json_print_attributes(pctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200874
875 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200876 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100877 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200878 LEVEL_PRINTED;
879 }
880
881 return LY_SUCCESS;
882}
883
884/**
885 * @brief Print all the types of data node including its metadata.
886 *
887 * @param[in] ctx JSON printer context.
888 * @param[in] node Data node to print.
889 * @return LY_ERR value.
890 */
891static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100892json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
Radek Krejci5536d282020-08-04 23:27:44 +0200893{
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100894 if (!ly_should_print(node, pctx->options)) {
895 if (json_print_array_is_last_inst(pctx, node)) {
896 json_print_array_close(pctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200897 }
898 return LY_SUCCESS;
899 }
900
901 if (!node->schema) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100902 LY_CHECK_RET(json_print_opaq(pctx, (const struct lyd_node_opaq *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200903 } else {
904 switch (node->schema->nodetype) {
905 case LYS_RPC:
906 case LYS_ACTION:
907 case LYS_NOTIF:
908 case LYS_CONTAINER:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100909 LY_CHECK_RET(json_print_container(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200910 break;
911 case LYS_LEAF:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100912 LY_CHECK_RET(json_print_leaf(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200913 break;
914 case LYS_LEAFLIST:
915 case LYS_LIST:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100916 LY_CHECK_RET(json_print_leaf_list(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200917 break;
Michal Vasko69abd362022-02-24 16:06:16 +0100918 case LYS_ANYDATA:
Radek Krejci5536d282020-08-04 23:27:44 +0200919 case LYS_ANYXML:
Michal Vasko69abd362022-02-24 16:06:16 +0100920 LY_CHECK_RET(json_print_any(pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200921 break;
922 default:
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100923 LOGINT(pctx->ctx);
Radek Krejci5536d282020-08-04 23:27:44 +0200924 return EXIT_FAILURE;
925 }
926 }
927
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100928 pctx->level_printed = pctx->level;
Radek Krejci5536d282020-08-04 23:27:44 +0200929
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100930 if (pctx->print_sibling_metadata && !matching_node(node->next, pctx->print_sibling_metadata)) {
931 json_print_metadata_leaflist(pctx);
932 pctx->print_sibling_metadata = NULL;
Radek Krejci5536d282020-08-04 23:27:44 +0200933 }
934
935 return LY_SUCCESS;
936}
937
938LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200939json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200940{
941 const struct lyd_node *node;
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100942 struct jsonpr_ctx pctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200943 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200944
Radek Krejci2e874772020-08-28 16:36:33 +0200945 if (!root) {
946 ly_print_(out, "{}%s", delimiter);
947 ly_print_flush(out);
948 return LY_SUCCESS;
949 }
950
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100951 pctx.out = out;
952 pctx.level = 1;
953 pctx.level_printed = 0;
954 pctx.options = options;
955 pctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200956
957 /* start */
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100958 ly_print_(pctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200959
960 /* content */
961 LY_LIST_FOR(root, node) {
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100962 pctx.root = node;
963 LY_CHECK_RET(json_print_node(&pctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200964 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
965 break;
966 }
967 }
968
969 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200970 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200971
Michal Vaskoc08b9d22022-02-10 15:48:39 +0100972 assert(!pctx.open.count);
973 ly_set_erase(&pctx.open, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200974
975 ly_print_flush(out);
976 return LY_SUCCESS;
977}