blob: 3abb3e8a373011c687d0f80fd6b971d7b5b6a562 [file] [log] [blame]
Radek Krejci13a57b62019-07-19 13:04:09 +02001/**
Michal Vasko90932a92020-02-12 14:33:03 +01002 * @file printer_json.c
Radek Krejci13a57b62019-07-19 13:04:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01004 * @brief JSON printer for libyang data structure
Radek Krejci13a57b62019-07-19 13:04:09 +02005 *
Radek Krejci5536d282020-08-04 23:27:44 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejci13a57b62019-07-19 13:04:09 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci5536d282020-08-04 23:27:44 +020015#include <assert.h>
16#include <stdlib.h>
17#include <string.h>
18
Radek Krejci13a57b62019-07-19 13:04:09 +020019#include "common.h"
Radek Krejci5536d282020-08-04 23:27:44 +020020#include "log.h"
21#include "parser_data.h"
22#include "plugins_types.h"
23#include "printer_data.h"
24#include "printer_internal.h"
25#include "set.h"
26#include "tree.h"
27#include "tree_data.h"
28#include "tree_data_internal.h"
Radek Krejci13a57b62019-07-19 13:04:09 +020029#include "tree_schema.h"
30
31/**
Radek Krejci5536d282020-08-04 23:27:44 +020032 * @brief JSON printer context.
33 */
34struct jsonpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020035 struct ly_out *out; /**< output specification */
36 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
37 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
38 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci5536d282020-08-04 23:27:44 +020039
Radek Krejci1deb5be2020-08-26 16:43:36 +020040 uint16_t level_printed; /* level where some data were already printed */
Radek Krejci5536d282020-08-04 23:27:44 +020041 struct ly_set open; /* currently open array(s) */
42 const struct lyd_node *print_sibling_metadata;
43};
44
45/**
46 * @brief Mark that something was already written in the current level,
47 * used to decide if a comma is expected between the items
48 */
49#define LEVEL_PRINTED ctx->level_printed = ctx->level
50
51#define PRINT_COMMA \
52 if (ctx->level_printed >= ctx->level) {\
Michal Vasko5233e962020-08-14 14:26:20 +020053 ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\
Radek Krejci5536d282020-08-04 23:27:44 +020054 }
55
56static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node);
57
58/**
Radek Krejci5536d282020-08-04 23:27:44 +020059 * Compare 2 nodes, despite it is regular data node or an opaq node, and
60 * decide if they corresponds to the same schema node.
61 *
62 * TODO: rewrite lyd_compare_single and use it instead of this
63 *
64 * @return 1 - matching nodes, 0 - non-matching nodes
65 */
66static int
67matching_node(const struct lyd_node *node1, const struct lyd_node *node2)
68{
69 assert(node1 || node2);
70
71 if (!node1 || !node2) {
72 return 0;
73 } else if (node1->schema != node2->schema) {
74 return 0;
75 }
76 if (!node1->schema) {
77 /* compare node names */
Michal Vasko22df3f02020-08-24 13:29:22 +020078 struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1;
79 struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2;
Radek Krejci5536d282020-08-04 23:27:44 +020080 if (onode1->name != onode2->name || onode1->prefix.id != onode2->prefix.id) {
81 return 0;
82 }
83 }
84
85 return 1;
86}
87
88/**
89 * @brief Open the JSON array ('[') for the specified @p node
90 *
91 * @param[in] ctx JSON printer context.
92 * @param[in] node First node of the array.
Michal Vaskob0099a92020-08-31 14:55:23 +020093 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +020094 */
Michal Vaskob0099a92020-08-31 14:55:23 +020095static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +020096json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node)
97{
98 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +020099 ly_print_(ctx->out, "[%s", (!node->schema || node->schema->nodetype != LYS_LEAFLIST) && DO_FORMAT ? "\n" : "");
Michal Vaskob0099a92020-08-31 14:55:23 +0200100 LY_CHECK_RET(ly_set_add(&ctx->open, (void *)node, 0, NULL));
Radek Krejci5536d282020-08-04 23:27:44 +0200101 LEVEL_INC;
Michal Vaskob0099a92020-08-31 14:55:23 +0200102
103 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200104}
105
106/**
107 * @brief Get know if the array for the provided @p node is currently open.
108 *
109 * @param[in] ctx JSON printer context.
110 * @param[in] node Data node to check.
111 * @return 1 in case the printer is currently in the array belonging to the provided @p node.
112 * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array).
113 */
114static int
115is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node)
116{
Michal Vasko22df3f02020-08-24 13:29:22 +0200117 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 +0200118 return 1;
119 } else {
120 return 0;
121 }
122}
123
124/**
125 * @brief Close the most inner JSON array.
126 *
127 * @param[in] ctx JSON printer context.
128 */
129static void
130json_print_array_close(struct jsonpr_ctx *ctx)
131{
Michal Vasko22df3f02020-08-24 13:29:22 +0200132 const struct lysc_node *schema = ((const struct lyd_node *)ctx->open.objs[ctx->open.count - 1])->schema;
Radek Krejci5536d282020-08-04 23:27:44 +0200133
134 LEVEL_DEC;
135 ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL);
136 if (schema && schema->nodetype == LYS_LEAFLIST) {
137 /* leaf-list's content is always printed on a single line */
Michal Vasko5233e962020-08-14 14:26:20 +0200138 ly_print_(ctx->out, "]");
Radek Krejci5536d282020-08-04 23:27:44 +0200139 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200140 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200141 }
142}
143
144/**
145 * @brief Get the node's module name to use as the @p node prefix in JSON.
146 * @param[in] node Node to process.
147 * @return The name of the module where the @p node belongs, it can be NULL in case the module name
148 * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context).
149 */
150static const char *
151node_prefix(const struct lyd_node *node)
152{
153 if (node->schema) {
154 return node->schema->module->name;
155 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200156 struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node;
Radek Krejci5536d282020-08-04 23:27:44 +0200157 const struct lys_module *mod;
158
159 switch (onode->format) {
160 case LYD_JSON:
161 return onode->prefix.module_name;
162 case LYD_XML:
163 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->prefix.module_ns);
164 if (!mod) {
165 return NULL;
166 }
167 return mod->name;
Radek Krejci5536d282020-08-04 23:27:44 +0200168 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200169 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200170 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200171 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200172 }
173 }
174
175 return NULL;
176}
177
178/**
179 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
180 *
181 * Accepts both regulard a well as opaq nodes.
182 *
183 * @param[in] node1 The first node to compare.
184 * @param[in] node2 The second node to compare.
185 * @return 0 in case the nodes' modules are the same
186 * @return 1 in case the nodes belongs to different modules
187 */
188int
189json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
190{
191 assert(node1 || node2);
192
193 if (!node1 || !node2) {
194 return 1;
195 } else if (node1->schema && node2->schema) {
196 if (node1->schema->module == node2->schema->module) {
197 /* belongs to the same module */
198 return 0;
199 } else {
200 /* different modules */
201 return 1;
202 }
203 } else {
204 const char *pref1 = node_prefix(node1);
205 const char *pref2 = node_prefix(node2);
206 if ((pref1 && pref2) && pref1 == pref2) {
207 return 0;
208 } else {
209 return 1;
210 }
211 }
212}
213
214/**
215 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
216 *
217 * @param[in] out The output handler.
218 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200219 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200220 */
Michal Vasko5233e962020-08-14 14:26:20 +0200221static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200222json_print_string(struct ly_out *out, const char *text)
223{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200224 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200225
226 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200227 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200228 }
229
Michal Vasko5233e962020-08-14 14:26:20 +0200230 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200231 for (i = n = 0; text[i]; i++) {
232 const unsigned char ascii = text[i];
233 if (ascii < 0x20) {
234 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200235 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200236 } else {
237 switch (ascii) {
238 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200239 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200240 break;
241 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200242 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200243 break;
244 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200245 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200246 n++;
247 }
248 }
249 }
Michal Vasko5233e962020-08-14 14:26:20 +0200250 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200251
Michal Vasko5233e962020-08-14 14:26:20 +0200252 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200253}
254
255/**
256 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
257 *
258 * @param[in] ctx JSON printer context.
259 * @param[in] node The data node being printed.
260 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
261 * @return LY_ERR value.
262 */
263static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200264json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, uint8_t is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200265{
266 PRINT_COMMA;
Michal Vasko22df3f02020-08-24 13:29:22 +0200267 if (LEVEL == 1 || json_nscmp(node, (const struct lyd_node *)node->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200268 /* print "namespace" */
Michal Vasko5233e962020-08-14 14:26:20 +0200269 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Radek Krejci5536d282020-08-04 23:27:44 +0200270 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
271 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200272 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "",
Radek Krejci5536d282020-08-04 23:27:44 +0200273 node->schema->name, DO_FORMAT ? " " : "");
274 }
275
276 return LY_SUCCESS;
277}
278
279/**
280 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
281 *
282 * @param[in] ctx JSON printer context.
283 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
284 * @param[in] format Format to decide how to process the @p prefix.
285 * @param[in] prefix Prefix structure to provide prefix string if prefix to print.
286 * @param[in] name Name of the memeber to print.
287 * @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
Radek Krejci1deb5be2020-08-26 16:43:36 +0200291json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LYD_FORMAT format,
292 const struct ly_prefix *prefix, const char *name, uint8_t is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200293{
294 const char *module_name = NULL;
295
296 PRINT_COMMA;
297
298 /* determine prefix string */
299 if (prefix) {
300 const struct lys_module *mod;
301
302 switch (format) {
303 case LYD_JSON:
304 module_name = prefix->module_name;
305 break;
306 case LYD_XML:
307 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, prefix->module_ns);
308 if (mod) {
309 module_name = mod->name;
310 }
311 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200312 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200313 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200314 /* cannot be created */
315 LOGINT_RET(ctx->ctx);
316 }
317 }
318
319 /* print the member */
320 if (module_name && (!parent || node_prefix(parent) != module_name)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200321 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200322 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200323 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200324 }
325
326 return LY_SUCCESS;
327}
328
329/**
330 * @brief Print data value.
331 *
332 * @param[in] ctx JSON printer context.
333 * @param[in] val Data value to be printed.
334 * @return LY_ERR value.
335 */
336static LY_ERR
337json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val)
338{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200339 uint8_t dynamic = 0;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200340 const char *value = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Radek Krejci5536d282020-08-04 23:27:44 +0200341
342 /* leafref is not supported */
343 switch (val->realtype->basetype) {
344 case LY_TYPE_BINARY:
345 case LY_TYPE_STRING:
346 case LY_TYPE_BITS:
347 case LY_TYPE_ENUM:
348 case LY_TYPE_INST:
349 case LY_TYPE_INT64:
350 case LY_TYPE_UINT64:
351 case LY_TYPE_DEC64:
352 case LY_TYPE_IDENT:
353 json_print_string(ctx->out, value);
354 break;
355
356 case LY_TYPE_INT8:
357 case LY_TYPE_INT16:
358 case LY_TYPE_INT32:
359 case LY_TYPE_UINT8:
360 case LY_TYPE_UINT16:
361 case LY_TYPE_UINT32:
362 case LY_TYPE_BOOL:
Michal Vasko5233e962020-08-14 14:26:20 +0200363 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200364 break;
365
366 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200367 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200368 break;
369
370 default:
371 /* error */
372 LOGINT_RET(ctx->ctx);
373 }
374
375 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200376 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200377 }
378
379 return LY_SUCCESS;
380}
381
382/**
383 * @brief Print all the attributes of the opaq node.
384 *
385 * @param[in] ctx JSON printer context.
386 * @param[in] node Opaq node where the attributes are placed.
387 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
388 * @return LY_ERR value.
389 */
390static LY_ERR
391json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
392{
393 struct lyd_attr *attr;
394
395 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200396 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200397 LEVEL_PRINTED;
398 }
399
400 for (attr = node->attr; attr; attr = attr->next) {
401 PRINT_COMMA;
Michal Vasko22df3f02020-08-24 13:29:22 +0200402 json_print_member2(ctx, (struct lyd_node *)node, attr->format, &attr->prefix, attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200403
404 if (attr->hint & (LYD_NODE_OPAQ_ISBOOLEAN | LYD_NODE_OPAQ_ISNUMBER)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200405 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200406 } else if (attr->hint & LYD_NODE_OPAQ_ISEMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200407 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200408 } else {
409 json_print_string(ctx->out, attr->value);
410 }
411 LEVEL_PRINTED;
412 }
413
414 return LY_SUCCESS;
415}
416
417/**
418 * @brief Print all the metadata of the node.
419 *
420 * @param[in] ctx JSON printer context.
421 * @param[in] node Node where the metadata are placed.
422 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
423 * @return LY_ERR value.
424 */
425static LY_ERR
426json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
427{
428 struct lyd_meta *meta;
429
430 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200431 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200432 LEVEL_PRINTED;
433 }
434
435 for (meta = node->meta; meta; meta = meta->next) {
436 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200437 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200438 LY_CHECK_RET(json_print_value(ctx, &meta->value));
439 LEVEL_PRINTED;
440 }
441
442 return LY_SUCCESS;
443}
444
445/**
446 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
447 *
448 * @param[in] ctx JSON printer context.
449 * @param[in] node Data node where the attributes/metadata are placed.
450 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
451 * @return LY_ERR value.
452 */
453static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200454json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, uint8_t inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200455{
456 const struct lys_module *wdmod = NULL;
457
458 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
459 /* we have implicit OR explicit default node */
460 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200461 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200462 }
463
464 if (node->schema && node->meta) {
465 if (inner) {
466 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
467 } else {
468 LY_CHECK_RET(json_print_member(ctx, node, 1));
469 }
Michal Vasko5233e962020-08-14 14:26:20 +0200470 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200471 LEVEL_INC;
472 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
473 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200474 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200475 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200476 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200477 if (inner) {
478 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
479 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200480 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format,
481 &((struct lyd_node_opaq *)node)->prefix, ((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200482 }
Michal Vasko5233e962020-08-14 14:26:20 +0200483 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200484 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200485 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200486 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200487 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200488 LEVEL_PRINTED;
489 }
490
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Print leaf data node including its metadata.
496 *
497 * @param[in] ctx JSON printer context.
498 * @param[in] node Data node to print.
499 * @return LY_ERR value.
500 */
501static LY_ERR
502json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
503{
504 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vasko22df3f02020-08-24 13:29:22 +0200505 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200506 LEVEL_PRINTED;
507
508 /* print attributes as sibling */
509 json_print_attributes(ctx, node, 0);
510
511 return LY_SUCCESS;
512}
513
514/**
515 * @brief Print anydata data node including its metadata.
516 *
517 * @param[in] ctx JSON printer context.
518 * @param[in] any Anydata node to print.
519 * @return LY_ERR value.
520 */
521static LY_ERR
522json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
523{
524 LY_ERR ret = LY_SUCCESS;
525 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200526 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200527
528 if (!any->value.tree) {
529 /* no content */
530 return LY_SUCCESS;
531 }
532
533 if (any->value_type == LYD_ANYDATA_LYB) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200534 uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT;
Radek Krejci5536d282020-08-04 23:27:44 +0200535
536 /* turn logging off */
537 prev_lo = ly_log_options(0);
538
539 /* try to parse it into a data tree */
540 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
541 /* successfully parsed */
542 free(any->value.mem);
543 any->value.tree = iter;
544 any->value_type = LYD_ANYDATA_DATATREE;
545 }
546
547 /* turn loggin on again */
548 ly_log_options(prev_lo);
549 }
550
551 switch (any->value_type) {
552 case LYD_ANYDATA_DATATREE:
553 /* close opening tag and print data */
554 prev_opts = ctx->options;
555 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
556
557 LY_LIST_FOR(any->value.tree, iter) {
558 ret = json_print_node(ctx, iter);
559 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
560 }
561
562 ctx->options = prev_opts;
563 break;
564 case LYD_ANYDATA_JSON:
565 /* print without escaping special characters */
566 if (!any->value.str[0]) {
567 return LY_SUCCESS;
568 }
Michal Vasko5233e962020-08-14 14:26:20 +0200569 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200570 break;
571 case LYD_ANYDATA_STRING:
572 case LYD_ANYDATA_XML:
573 case LYD_ANYDATA_LYB:
574 /* JSON and LYB format is not supported */
575 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
576 return LY_SUCCESS;
577 }
578
579 return LY_SUCCESS;
580}
581
582/**
583 * @brief Print content of a single container/list data node including its metadata.
584 * The envelope specific to container and list are expected to be printed by the caller.
585 *
586 * @param[in] ctx JSON printer context.
587 * @param[in] node Data node to print.
588 * @return LY_ERR value.
589 */
590static LY_ERR
591json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
592{
593 struct lyd_node *child;
594 struct lyd_node *children = lyd_node_children(node, 0);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200595 uint8_t has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200596
597 if (node->meta || children) {
598 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200599 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200600 has_content = 1;
601 }
602
603 if (!node->schema || node->schema->nodetype != LYS_LIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200604 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200605 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200606 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200607 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci5536d282020-08-04 23:27:44 +0200608 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
609 }
610 LEVEL_INC;
611
612 json_print_attributes(ctx, node, 1);
613
614 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
615 /* print children */
616 LY_LIST_FOR(children, child) {
617 LY_CHECK_RET(json_print_node(ctx, child));
618 }
619 } else {
620 /* anydata */
621 json_print_anydata(ctx, (struct lyd_node_any *)node);
622 }
623
Radek Krejci5536d282020-08-04 23:27:44 +0200624 LEVEL_DEC;
625 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200626 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200627 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200628 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200629 }
630 LEVEL_PRINTED;
631
632 return LY_SUCCESS;
633}
634
635/**
636 * @brief Print container data node including its metadata.
637 *
638 * @param[in] ctx JSON printer context.
639 * @param[in] node Data node to print.
640 * @return LY_ERR value.
641 */
642static int
643json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
644{
645 LY_CHECK_RET(json_print_member(ctx, node, 0));
646 LY_CHECK_RET(json_print_inner(ctx, node));
647
648 return LY_SUCCESS;
649}
650
651/**
652 * @brief Print single leaf-list or list instance.
653 *
654 * In case of list, metadata are printed inside the list object. For the leaf-list,
655 * metadata are marked in the context for later printing after closing the array next to it using
656 * json_print_metadata_leaflist().
657 *
658 * @param[in] ctx JSON printer context.
659 * @param[in] node Data node to print.
660 * @return LY_ERR value.
661 */
662static LY_ERR
663json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
664{
665 if (!is_open_array(ctx, node)) {
666 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200667 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200668 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200669 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200670 }
671
672 if (node->schema->nodetype == LYS_LIST) {
673 if (!lyd_node_children(node, 0)) {
674 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200675 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200676 LEVEL_PRINTED;
677 } else {
678 /* print list's content */
679 LY_CHECK_RET(json_print_inner(ctx, node));
680 }
681 } else {
682 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200683 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200684
685 if (node->meta && !ctx->print_sibling_metadata) {
686 ctx->print_sibling_metadata = node;
687 }
688 }
689
690 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
691 json_print_array_close(ctx);
692 }
693
694 return LY_SUCCESS;
695}
696
697/**
698 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
699 * This function is supposed to be called when the leaf-list array is closed.
700 *
701 * @param[in] ctx JSON printer context.
702 * @return LY_ERR value.
703 */
704static LY_ERR
705json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
706{
707 const struct lyd_node *prev, *node, *iter;
708
709 if (!ctx->print_sibling_metadata) {
710 return LY_SUCCESS;
711 }
712
713 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
714 prev->next && matching_node(node, prev);
715 node = prev, prev = node->prev) {}
716
717 /* node is the first instance of the leaf-list */
718
719 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200720 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200721 LEVEL_INC;
722 LY_LIST_FOR(node, iter) {
723 PRINT_COMMA;
724 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200725 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200726 LEVEL_INC;
727 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
728 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200729 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200730 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200731 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200732 }
733 LEVEL_PRINTED;
734 if (!matching_node(iter, iter->next)) {
735 break;
736 }
737 }
738 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200739 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200740 LEVEL_PRINTED;
741
742 return LY_SUCCESS;
743}
744
745/**
746 * @brief Print opaq data node including its attributes.
747 *
748 * @param[in] ctx JSON printer context.
749 * @param[in] node Opaq node to print.
750 * @return LY_ERR value.
751 */
752static LY_ERR
753json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
754{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200755 uint8_t first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200756
757 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200758 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev;
759 const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next;
760 if (prev->next && matching_node((const struct lyd_node *)prev, (const struct lyd_node *)node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200761 first = 0;
762 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200763 if (next && matching_node((const struct lyd_node *)node, (const struct lyd_node *)next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200764 last = 0;
765 }
766 }
767
768 if (first) {
769 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0));
770
771 if (node->hint & LYD_NODE_OPAQ_ISLIST) {
Michal Vaskob0099a92020-08-31 14:55:23 +0200772 LY_CHECK_RET(json_print_array_open(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200773 LEVEL_INC;
774 }
775 }
776 if (node->child || (node->hint & LYD_NODE_OPAQ_ISLIST)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200777 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200778 LEVEL_PRINTED;
779 } else {
780 if (node->hint & LYD_VALUE_PARSE_ISEMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200781 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200782 } else if (node->hint & (LYD_VALUE_PARSE_ISBOOLEAN | LYD_VALUE_PARSE_ISNUMBER)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200783 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200784 } else {
785 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200786 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200787 }
788 LEVEL_PRINTED;
789
790 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200791 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200792
793 }
794 if (last && (node->hint & LYD_NODE_OPAQ_ISLIST)) {
795 json_print_array_close(ctx);
796 LEVEL_DEC;
797 LEVEL_PRINTED;
798 }
799
800 return LY_SUCCESS;
801}
802
803/**
804 * @brief Print all the types of data node including its metadata.
805 *
806 * @param[in] ctx JSON printer context.
807 * @param[in] node Data node to print.
808 * @return LY_ERR value.
809 */
810static LY_ERR
811json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
812{
813 if (!ly_should_print(node, ctx->options)) {
814 if (is_open_array(ctx, node) && (!node->next || node->next->schema != node->schema)) {
815 json_print_array_close(ctx);
816 }
817 return LY_SUCCESS;
818 }
819
820 if (!node->schema) {
821 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
822 } else {
823 switch (node->schema->nodetype) {
824 case LYS_RPC:
825 case LYS_ACTION:
826 case LYS_NOTIF:
827 case LYS_CONTAINER:
828 LY_CHECK_RET(json_print_container(ctx, node));
829 break;
830 case LYS_LEAF:
831 LY_CHECK_RET(json_print_leaf(ctx, node));
832 break;
833 case LYS_LEAFLIST:
834 case LYS_LIST:
835 LY_CHECK_RET(json_print_leaf_list(ctx, node));
836 break;
837 case LYS_ANYXML:
838 case LYS_ANYDATA:
839 LY_CHECK_RET(json_print_container(ctx, node));
840 break;
841 default:
842 LOGINT(node->schema->module->ctx);
843 return EXIT_FAILURE;
844 }
845 }
846
847 ctx->level_printed = ctx->level;
848
849 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
850 json_print_metadata_leaflist(ctx);
851 ctx->print_sibling_metadata = NULL;
852 }
853
854 return LY_SUCCESS;
855}
856
857LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200858json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200859{
860 const struct lyd_node *node;
861 struct jsonpr_ctx ctx = {0};
862 const char *delimiter = (options & LYD_PRINT_FORMAT) ? "\n" : "";
863
Radek Krejci2e874772020-08-28 16:36:33 +0200864 if (!root) {
865 ly_print_(out, "{}%s", delimiter);
866 ly_print_flush(out);
867 return LY_SUCCESS;
868 }
869
Radek Krejci5536d282020-08-04 23:27:44 +0200870 ctx.out = out;
871 ctx.level = 1;
872 ctx.level_printed = 0;
873 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200874 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200875
876 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200877 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200878
879 /* content */
880 LY_LIST_FOR(root, node) {
881 LY_CHECK_RET(json_print_node(&ctx, node));
882 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
883 break;
884 }
885 }
886
887 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200888 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200889
890 assert(!ctx.open.count);
891 ly_set_erase(&ctx.open, NULL);
892
893 ly_print_flush(out);
894 return LY_SUCCESS;
895}