blob: cffe955e7dc6fb74c7d3d42b6924fa2a9e5236f2 [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 Vasko69730152020-10-09 16:30:07 +020082 if ((onode1->name != onode2->name) || (onode1->prefix.id != onode2->prefix.id)) {
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) {
162 case LYD_JSON:
163 return onode->prefix.module_name;
164 case LYD_XML:
165 mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->prefix.module_ns);
166 if (!mod) {
167 return NULL;
168 }
169 return mod->name;
Radek Krejci5536d282020-08-04 23:27:44 +0200170 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200171 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200172 /* cannot be created */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200173 LOGINT(LYD_CTX(node));
Radek Krejci5536d282020-08-04 23:27:44 +0200174 }
175 }
176
177 return NULL;
178}
179
180/**
181 * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace)
182 *
183 * Accepts both regulard a well as opaq nodes.
184 *
185 * @param[in] node1 The first node to compare.
186 * @param[in] node2 The second node to compare.
187 * @return 0 in case the nodes' modules are the same
188 * @return 1 in case the nodes belongs to different modules
189 */
190int
191json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
192{
193 assert(node1 || node2);
194
195 if (!node1 || !node2) {
196 return 1;
197 } else if (node1->schema && node2->schema) {
198 if (node1->schema->module == node2->schema->module) {
199 /* belongs to the same module */
200 return 0;
201 } else {
202 /* different modules */
203 return 1;
204 }
205 } else {
206 const char *pref1 = node_prefix(node1);
207 const char *pref2 = node_prefix(node2);
Michal Vasko69730152020-10-09 16:30:07 +0200208 if ((pref1 && pref2) && (pref1 == pref2)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200209 return 0;
210 } else {
211 return 1;
212 }
213 }
214}
215
216/**
217 * @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
218 *
219 * @param[in] out The output handler.
220 * @param[in] text The string to print.
Michal Vasko5233e962020-08-14 14:26:20 +0200221 * @return LY_ERR value.
Radek Krejci5536d282020-08-04 23:27:44 +0200222 */
Michal Vasko5233e962020-08-14 14:26:20 +0200223static LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +0200224json_print_string(struct ly_out *out, const char *text)
225{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200226 uint64_t i, n;
Radek Krejci5536d282020-08-04 23:27:44 +0200227
228 if (!text) {
Michal Vasko5233e962020-08-14 14:26:20 +0200229 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200230 }
231
Michal Vasko5233e962020-08-14 14:26:20 +0200232 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200233 for (i = n = 0; text[i]; i++) {
234 const unsigned char ascii = text[i];
235 if (ascii < 0x20) {
236 /* control character */
Michal Vasko5233e962020-08-14 14:26:20 +0200237 ly_print_(out, "\\u%.4X", ascii);
Radek Krejci5536d282020-08-04 23:27:44 +0200238 } else {
239 switch (ascii) {
240 case '"':
Michal Vasko5233e962020-08-14 14:26:20 +0200241 ly_print_(out, "\\\"");
Radek Krejci5536d282020-08-04 23:27:44 +0200242 break;
243 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200244 ly_print_(out, "\\\\");
Radek Krejci5536d282020-08-04 23:27:44 +0200245 break;
246 default:
Michal Vasko5233e962020-08-14 14:26:20 +0200247 ly_write_(out, &text[i], 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200248 n++;
249 }
250 }
251 }
Michal Vasko5233e962020-08-14 14:26:20 +0200252 ly_write_(out, "\"", 1);
Radek Krejci5536d282020-08-04 23:27:44 +0200253
Michal Vasko5233e962020-08-14 14:26:20 +0200254 return LY_SUCCESS;
Radek Krejci5536d282020-08-04 23:27:44 +0200255}
256
257/**
258 * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed.
259 *
260 * @param[in] ctx JSON printer context.
261 * @param[in] node The data node being printed.
262 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
263 * @return LY_ERR value.
264 */
265static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200266json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200267{
268 PRINT_COMMA;
Michal Vasko69730152020-10-09 16:30:07 +0200269 if ((LEVEL == 1) || json_nscmp(node, (const struct lyd_node *)node->parent)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200270 /* print "namespace" */
Michal Vasko5233e962020-08-14 14:26:20 +0200271 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200272 node_prefix(node), node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200273 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200274 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200275 node->schema->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200276 }
277
278 return LY_SUCCESS;
279}
280
281/**
282 * @brief More generic alternative to json_print_member() to print some special cases of the member names.
283 *
284 * @param[in] ctx JSON printer context.
285 * @param[in] parent Parent node to compare modules deciding if the prefix is printed.
286 * @param[in] format Format to decide how to process the @p prefix.
287 * @param[in] prefix Prefix structure to provide prefix string if prefix to print.
288 * @param[in] name Name of the memeber to print.
289 * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier.
290 * @return LY_ERR value.
291 */
292static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200293json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LYD_FORMAT format,
Radek Krejci857189e2020-09-01 13:26:36 +0200294 const struct ly_prefix *prefix, const char *name, ly_bool is_attr)
Radek Krejci5536d282020-08-04 23:27:44 +0200295{
296 const char *module_name = NULL;
297
298 PRINT_COMMA;
299
300 /* determine prefix string */
301 if (prefix) {
302 const struct lys_module *mod;
303
304 switch (format) {
305 case LYD_JSON:
306 module_name = prefix->module_name;
307 break;
308 case LYD_XML:
309 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, prefix->module_ns);
310 if (mod) {
311 module_name = mod->name;
312 }
313 break;
Radek Krejci5536d282020-08-04 23:27:44 +0200314 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200315 case LYD_UNKNOWN:
Radek Krejci5536d282020-08-04 23:27:44 +0200316 /* cannot be created */
317 LOGINT_RET(ctx->ctx);
318 }
319 }
320
321 /* print the member */
Michal Vasko69730152020-10-09 16:30:07 +0200322 if (module_name && (!parent || (node_prefix(parent) != module_name))) {
Michal Vasko5233e962020-08-14 14:26:20 +0200323 ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200324 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200325 ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200326 }
327
328 return LY_SUCCESS;
329}
330
331/**
332 * @brief Print data value.
333 *
334 * @param[in] ctx JSON printer context.
335 * @param[in] val Data value to be printed.
336 * @return LY_ERR value.
337 */
338static LY_ERR
339json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val)
340{
Radek Krejci857189e2020-09-01 13:26:36 +0200341 ly_bool dynamic = 0;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200342 const char *value = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Radek Krejci5536d282020-08-04 23:27:44 +0200343
344 /* leafref is not supported */
345 switch (val->realtype->basetype) {
346 case LY_TYPE_BINARY:
347 case LY_TYPE_STRING:
348 case LY_TYPE_BITS:
349 case LY_TYPE_ENUM:
350 case LY_TYPE_INST:
351 case LY_TYPE_INT64:
352 case LY_TYPE_UINT64:
353 case LY_TYPE_DEC64:
354 case LY_TYPE_IDENT:
Michal Vaskoc5b59af2020-11-03 17:13:41 +0100355 case LY_TYPE_UNION:
Radek Krejci5536d282020-08-04 23:27:44 +0200356 json_print_string(ctx->out, value);
357 break;
358
359 case LY_TYPE_INT8:
360 case LY_TYPE_INT16:
361 case LY_TYPE_INT32:
362 case LY_TYPE_UINT8:
363 case LY_TYPE_UINT16:
364 case LY_TYPE_UINT32:
365 case LY_TYPE_BOOL:
Michal Vasko5233e962020-08-14 14:26:20 +0200366 ly_print_(ctx->out, "%s", value[0] ? value : "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200367 break;
368
369 case LY_TYPE_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +0200370 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200371 break;
372
373 default:
374 /* error */
375 LOGINT_RET(ctx->ctx);
376 }
377
378 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200379 free((char *)value);
Radek Krejci5536d282020-08-04 23:27:44 +0200380 }
381
382 return LY_SUCCESS;
383}
384
385/**
386 * @brief Print all the attributes of the opaq node.
387 *
388 * @param[in] ctx JSON printer context.
389 * @param[in] node Opaq node where the attributes are placed.
390 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
391 * @return LY_ERR value.
392 */
393static LY_ERR
394json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod)
395{
396 struct lyd_attr *attr;
397
398 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200399 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200400 LEVEL_PRINTED;
401 }
402
403 for (attr = node->attr; attr; attr = attr->next) {
404 PRINT_COMMA;
Michal Vasko22df3f02020-08-24 13:29:22 +0200405 json_print_member2(ctx, (struct lyd_node *)node, attr->format, &attr->prefix, attr->name, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200406
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200407 if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200408 ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200409 } else if (attr->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200410 ly_print_(ctx->out, "[null]");
Radek Krejci5536d282020-08-04 23:27:44 +0200411 } else {
412 json_print_string(ctx->out, attr->value);
413 }
414 LEVEL_PRINTED;
415 }
416
417 return LY_SUCCESS;
418}
419
420/**
421 * @brief Print all the metadata of the node.
422 *
423 * @param[in] ctx JSON printer context.
424 * @param[in] node Node where the metadata are placed.
425 * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed.
426 * @return LY_ERR value.
427 */
428static LY_ERR
429json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod)
430{
431 struct lyd_meta *meta;
432
433 if (wdmod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200434 ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name);
Radek Krejci5536d282020-08-04 23:27:44 +0200435 LEVEL_PRINTED;
436 }
437
438 for (meta = node->meta; meta; meta = meta->next) {
439 PRINT_COMMA;
Michal Vasko5233e962020-08-14 14:26:20 +0200440 ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200441 LY_CHECK_RET(json_print_value(ctx, &meta->value));
442 LEVEL_PRINTED;
443 }
444
445 return LY_SUCCESS;
446}
447
448/**
449 * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes.
450 *
451 * @param[in] ctx JSON printer context.
452 * @param[in] node Data node where the attributes/metadata are placed.
Radek Krejci857189e2020-09-01 13:26:36 +0200453 * @param[in] inner Flag if the @p node is an inner node in the tree.
Radek Krejci5536d282020-08-04 23:27:44 +0200454 * @return LY_ERR value.
455 */
456static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200457json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner)
Radek Krejci5536d282020-08-04 23:27:44 +0200458{
459 const struct lys_module *wdmod = NULL;
460
461 if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) {
462 /* we have implicit OR explicit default node */
463 /* get with-defaults module */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200464 wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults");
Radek Krejci5536d282020-08-04 23:27:44 +0200465 }
466
467 if (node->schema && node->meta) {
468 if (inner) {
469 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
470 } else {
471 LY_CHECK_RET(json_print_member(ctx, node, 1));
472 }
Michal Vasko5233e962020-08-14 14:26:20 +0200473 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200474 LEVEL_INC;
475 LY_CHECK_RET(json_print_metadata(ctx, node, wdmod));
476 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200477 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200478 LEVEL_PRINTED;
Michal Vasko22df3f02020-08-24 13:29:22 +0200479 } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) {
Radek Krejci5536d282020-08-04 23:27:44 +0200480 if (inner) {
481 LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1));
482 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200483 LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format,
Michal Vasko69730152020-10-09 16:30:07 +0200484 &((struct lyd_node_opaq *)node)->prefix, ((struct lyd_node_opaq *)node)->name, 1));
Radek Krejci5536d282020-08-04 23:27:44 +0200485 }
Michal Vasko5233e962020-08-14 14:26:20 +0200486 ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200487 LEVEL_INC;
Michal Vasko22df3f02020-08-24 13:29:22 +0200488 LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod));
Radek Krejci5536d282020-08-04 23:27:44 +0200489 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200490 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200491 LEVEL_PRINTED;
492 }
493
494 return LY_SUCCESS;
495}
496
497/**
498 * @brief Print leaf data node including its metadata.
499 *
500 * @param[in] ctx JSON printer context.
501 * @param[in] node Data node to print.
502 * @return LY_ERR value.
503 */
504static LY_ERR
505json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node)
506{
507 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vasko22df3f02020-08-24 13:29:22 +0200508 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200509 LEVEL_PRINTED;
510
511 /* print attributes as sibling */
512 json_print_attributes(ctx, node, 0);
513
514 return LY_SUCCESS;
515}
516
517/**
518 * @brief Print anydata data node including its metadata.
519 *
520 * @param[in] ctx JSON printer context.
521 * @param[in] any Anydata node to print.
522 * @return LY_ERR value.
523 */
524static LY_ERR
525json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
526{
527 LY_ERR ret = LY_SUCCESS;
528 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200529 uint32_t prev_opts, prev_lo;
Radek Krejci5536d282020-08-04 23:27:44 +0200530
531 if (!any->value.tree) {
532 /* no content */
533 return LY_SUCCESS;
534 }
535
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 */
543 if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) {
544 /* 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:
556 /* close opening tag and print data */
557 prev_opts = ctx->options;
558 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
559
560 LY_LIST_FOR(any->value.tree, iter) {
561 ret = json_print_node(ctx, iter);
562 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
563 }
564
565 ctx->options = prev_opts;
566 break;
567 case LYD_ANYDATA_JSON:
568 /* print without escaping special characters */
569 if (!any->value.str[0]) {
570 return LY_SUCCESS;
571 }
Michal Vasko5233e962020-08-14 14:26:20 +0200572 ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
Radek Krejci5536d282020-08-04 23:27:44 +0200573 break;
574 case LYD_ANYDATA_STRING:
575 case LYD_ANYDATA_XML:
576 case LYD_ANYDATA_LYB:
577 /* JSON and LYB format is not supported */
578 LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
579 return LY_SUCCESS;
580 }
581
582 return LY_SUCCESS;
583}
584
585/**
586 * @brief Print content of a single container/list data node including its metadata.
587 * The envelope specific to container and list are expected to be printed by the caller.
588 *
589 * @param[in] ctx JSON printer context.
590 * @param[in] node Data node to print.
591 * @return LY_ERR value.
592 */
593static LY_ERR
594json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node)
595{
596 struct lyd_node *child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200597 struct lyd_node *children = lyd_child(node);
Radek Krejci857189e2020-09-01 13:26:36 +0200598 ly_bool has_content = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200599
600 if (node->meta || children) {
601 has_content = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200602 } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) {
Radek Krejci5536d282020-08-04 23:27:44 +0200603 has_content = 1;
604 }
605
Michal Vasko69730152020-10-09 16:30:07 +0200606 if (!node->schema || (node->schema->nodetype != LYS_LIST)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200607 ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Radek Krejci0f969882020-08-21 16:56:47 +0200608 (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200609 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200610 ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "",
Michal Vasko69730152020-10-09 16:30:07 +0200611 INDENT, (DO_FORMAT && has_content) ? "\n" : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200612 }
613 LEVEL_INC;
614
615 json_print_attributes(ctx, node, 1);
616
617 if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) {
618 /* print children */
619 LY_LIST_FOR(children, child) {
620 LY_CHECK_RET(json_print_node(ctx, child));
621 }
622 } else {
623 /* anydata */
624 json_print_anydata(ctx, (struct lyd_node_any *)node);
625 }
626
Radek Krejci5536d282020-08-04 23:27:44 +0200627 LEVEL_DEC;
628 if (DO_FORMAT && has_content) {
Michal Vasko5233e962020-08-14 14:26:20 +0200629 ly_print_(ctx->out, "\n%*s}", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200630 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200631 ly_print_(ctx->out, "}");
Radek Krejci5536d282020-08-04 23:27:44 +0200632 }
633 LEVEL_PRINTED;
634
635 return LY_SUCCESS;
636}
637
638/**
639 * @brief Print container data node including its metadata.
640 *
641 * @param[in] ctx JSON printer context.
642 * @param[in] node Data node to print.
643 * @return LY_ERR value.
644 */
645static int
646json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node)
647{
648 LY_CHECK_RET(json_print_member(ctx, node, 0));
649 LY_CHECK_RET(json_print_inner(ctx, node));
650
651 return LY_SUCCESS;
652}
653
654/**
655 * @brief Print single leaf-list or list instance.
656 *
657 * In case of list, metadata are printed inside the list object. For the leaf-list,
658 * metadata are marked in the context for later printing after closing the array next to it using
659 * json_print_metadata_leaflist().
660 *
661 * @param[in] ctx JSON printer context.
662 * @param[in] node Data node to print.
663 * @return LY_ERR value.
664 */
665static LY_ERR
666json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node)
667{
668 if (!is_open_array(ctx, node)) {
669 LY_CHECK_RET(json_print_member(ctx, node, 0));
Michal Vaskob0099a92020-08-31 14:55:23 +0200670 LY_CHECK_RET(json_print_array_open(ctx, node));
Radek Krejci5536d282020-08-04 23:27:44 +0200671 } else if (node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko5233e962020-08-14 14:26:20 +0200672 ly_print_(ctx->out, ",");
Radek Krejci5536d282020-08-04 23:27:44 +0200673 }
674
675 if (node->schema->nodetype == LYS_LIST) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200676 if (!lyd_child(node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200677 /* empty, e.g. in case of filter */
Michal Vasko5233e962020-08-14 14:26:20 +0200678 ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : "");
Radek Krejci5536d282020-08-04 23:27:44 +0200679 LEVEL_PRINTED;
680 } else {
681 /* print list's content */
682 LY_CHECK_RET(json_print_inner(ctx, node));
683 }
684 } else {
685 assert(node->schema->nodetype == LYS_LEAFLIST);
Michal Vasko22df3f02020-08-24 13:29:22 +0200686 LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value));
Radek Krejci5536d282020-08-04 23:27:44 +0200687
688 if (node->meta && !ctx->print_sibling_metadata) {
689 ctx->print_sibling_metadata = node;
690 }
691 }
692
Michal Vasko69730152020-10-09 16:30:07 +0200693 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200694 json_print_array_close(ctx);
695 }
696
697 return LY_SUCCESS;
698}
699
700/**
701 * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list().
702 * This function is supposed to be called when the leaf-list array is closed.
703 *
704 * @param[in] ctx JSON printer context.
705 * @return LY_ERR value.
706 */
707static LY_ERR
708json_print_metadata_leaflist(struct jsonpr_ctx *ctx)
709{
710 const struct lyd_node *prev, *node, *iter;
711
712 if (!ctx->print_sibling_metadata) {
713 return LY_SUCCESS;
714 }
715
716 for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev;
717 prev->next && matching_node(node, prev);
718 node = prev, prev = node->prev) {}
719
720 /* node is the first instance of the leaf-list */
721
722 LY_CHECK_RET(json_print_member(ctx, node, 1));
Michal Vasko5233e962020-08-14 14:26:20 +0200723 ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : ""));
Radek Krejci5536d282020-08-04 23:27:44 +0200724 LEVEL_INC;
725 LY_LIST_FOR(node, iter) {
726 PRINT_COMMA;
727 if (iter->meta) {
Michal Vasko5233e962020-08-14 14:26:20 +0200728 ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{");
Radek Krejci5536d282020-08-04 23:27:44 +0200729 LEVEL_INC;
730 LY_CHECK_RET(json_print_metadata(ctx, iter, NULL));
731 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200732 ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200733 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200734 ly_print_(ctx->out, "null");
Radek Krejci5536d282020-08-04 23:27:44 +0200735 }
736 LEVEL_PRINTED;
737 if (!matching_node(iter, iter->next)) {
738 break;
739 }
740 }
741 LEVEL_DEC;
Michal Vasko5233e962020-08-14 14:26:20 +0200742 ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT);
Radek Krejci5536d282020-08-04 23:27:44 +0200743 LEVEL_PRINTED;
744
745 return LY_SUCCESS;
746}
747
748/**
749 * @brief Print opaq data node including its attributes.
750 *
751 * @param[in] ctx JSON printer context.
752 * @param[in] node Opaq node to print.
753 * @return LY_ERR value.
754 */
755static LY_ERR
756json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node)
757{
Radek Krejci857189e2020-09-01 13:26:36 +0200758 ly_bool first = 1, last = 1;
Radek Krejci5536d282020-08-04 23:27:44 +0200759
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200760 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200761 const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev;
762 const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next;
763 if (prev->next && matching_node((const struct lyd_node *)prev, (const struct lyd_node *)node)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200764 first = 0;
765 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200766 if (next && matching_node((const struct lyd_node *)node, (const struct lyd_node *)next)) {
Radek Krejci5536d282020-08-04 23:27:44 +0200767 last = 0;
768 }
769 }
770
771 if (first) {
772 LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0));
773
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200774 if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
Michal Vaskob0099a92020-08-31 14:55:23 +0200775 LY_CHECK_RET(json_print_array_open(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200776 LEVEL_INC;
777 }
778 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200779 if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200780 LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node));
Radek Krejci5536d282020-08-04 23:27:44 +0200781 LEVEL_PRINTED;
782 } else {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200783 if (node->hints & LYD_VALHINT_EMPTY) {
Michal Vasko5233e962020-08-14 14:26:20 +0200784 ly_print_(ctx->out, "[null]");
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200785 } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200786 ly_print_(ctx->out, "%s", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200787 } else {
788 /* string */
Michal Vasko5233e962020-08-14 14:26:20 +0200789 ly_print_(ctx->out, "\"%s\"", node->value);
Radek Krejci5536d282020-08-04 23:27:44 +0200790 }
791 LEVEL_PRINTED;
792
793 /* attributes */
Michal Vasko22df3f02020-08-24 13:29:22 +0200794 json_print_attributes(ctx, (const struct lyd_node *)node, 0);
Radek Krejci5536d282020-08-04 23:27:44 +0200795
796 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200797 if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200798 json_print_array_close(ctx);
799 LEVEL_DEC;
800 LEVEL_PRINTED;
801 }
802
803 return LY_SUCCESS;
804}
805
806/**
807 * @brief Print all the types of data node including its metadata.
808 *
809 * @param[in] ctx JSON printer context.
810 * @param[in] node Data node to print.
811 * @return LY_ERR value.
812 */
813static LY_ERR
814json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node)
815{
816 if (!ly_should_print(node, ctx->options)) {
Michal Vasko69730152020-10-09 16:30:07 +0200817 if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) {
Radek Krejci5536d282020-08-04 23:27:44 +0200818 json_print_array_close(ctx);
819 }
820 return LY_SUCCESS;
821 }
822
823 if (!node->schema) {
824 LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node));
825 } else {
826 switch (node->schema->nodetype) {
827 case LYS_RPC:
828 case LYS_ACTION:
829 case LYS_NOTIF:
830 case LYS_CONTAINER:
831 LY_CHECK_RET(json_print_container(ctx, node));
832 break;
833 case LYS_LEAF:
834 LY_CHECK_RET(json_print_leaf(ctx, node));
835 break;
836 case LYS_LEAFLIST:
837 case LYS_LIST:
838 LY_CHECK_RET(json_print_leaf_list(ctx, node));
839 break;
840 case LYS_ANYXML:
841 case LYS_ANYDATA:
842 LY_CHECK_RET(json_print_container(ctx, node));
843 break;
844 default:
845 LOGINT(node->schema->module->ctx);
846 return EXIT_FAILURE;
847 }
848 }
849
850 ctx->level_printed = ctx->level;
851
852 if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) {
853 json_print_metadata_leaflist(ctx);
854 ctx->print_sibling_metadata = NULL;
855 }
856
857 return LY_SUCCESS;
858}
859
860LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200861json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejci5536d282020-08-04 23:27:44 +0200862{
863 const struct lyd_node *node;
864 struct jsonpr_ctx ctx = {0};
Radek Krejci52f65552020-09-01 17:03:35 +0200865 const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n";
Radek Krejci5536d282020-08-04 23:27:44 +0200866
Radek Krejci2e874772020-08-28 16:36:33 +0200867 if (!root) {
868 ly_print_(out, "{}%s", delimiter);
869 ly_print_flush(out);
870 return LY_SUCCESS;
871 }
872
Radek Krejci5536d282020-08-04 23:27:44 +0200873 ctx.out = out;
874 ctx.level = 1;
875 ctx.level_printed = 0;
876 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200877 ctx.ctx = LYD_CTX(root);
Radek Krejci5536d282020-08-04 23:27:44 +0200878
879 /* start */
Michal Vasko5233e962020-08-14 14:26:20 +0200880 ly_print_(ctx.out, "{%s", delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200881
882 /* content */
883 LY_LIST_FOR(root, node) {
884 LY_CHECK_RET(json_print_node(&ctx, node));
885 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
886 break;
887 }
888 }
889
890 /* end */
Michal Vasko5233e962020-08-14 14:26:20 +0200891 ly_print_(out, "%s}%s", delimiter, delimiter);
Radek Krejci5536d282020-08-04 23:27:44 +0200892
893 assert(!ctx.open.count);
894 ly_set_erase(&ctx.open, NULL);
895
896 ly_print_flush(out);
897 return LY_SUCCESS;
898}