blob: 10b8bdffbd07740a60badf2e9ef71de96e25fedb [file] [log] [blame]
Radek Krejci94ca54b2015-07-08 15:48:47 +02001/**
2 * @file printer/json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON printer for libyang data structure
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci94ca54b2015-07-08 15:48:47 +020013 */
14
15#include <stdlib.h>
16#include <stdio.h>
17#include <string.h>
18#include <assert.h>
Radek Krejci7511f402015-07-10 09:56:30 +020019#include <inttypes.h>
Radek Krejci94ca54b2015-07-08 15:48:47 +020020
Radek Krejci998a0b82015-08-17 13:14:36 +020021#include "common.h"
Radek Krejci76b07902015-10-09 09:11:25 +020022#include "printer.h"
Michal Vasko2d162e12015-09-24 14:33:29 +020023#include "tree_data.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020024#include "resolve.h"
25#include "tree_internal.h"
Radek Krejci94ca54b2015-07-08 15:48:47 +020026
27#define INDENT ""
28#define LEVEL (level*2)
29
Michal Vaskob3c31bd2017-07-17 10:01:48 +020030static int json_print_nodes(struct lyout *out, int level, const struct lyd_node *root, int withsiblings, int toplevel,
31 int options);
Radek Krejci94ca54b2015-07-08 15:48:47 +020032
Radek Krejci0e67ac42018-07-25 15:46:37 +020033int
Radek Krejci382e7f92016-01-12 17:15:47 +010034json_print_string(struct lyout *out, const char *text)
35{
36 unsigned int i, n;
37
38 if (!text) {
39 return 0;
40 }
41
42 ly_write(out, "\"", 1);
43 for (i = n = 0; text[i]; i++) {
Jan Kundrátf5cbb962018-08-24 14:17:35 +020044 const unsigned char ascii = text[i];
45 if (ascii < 0x20) {
Radek Krejci382e7f92016-01-12 17:15:47 +010046 /* control character */
Jan Kundrátf5cbb962018-08-24 14:17:35 +020047 n += ly_print(out, "\\u%.4X", ascii);
Radek Krejci382e7f92016-01-12 17:15:47 +010048 } else {
Jan Kundrátf5cbb962018-08-24 14:17:35 +020049 switch (ascii) {
Radek Krejci382e7f92016-01-12 17:15:47 +010050 case '"':
51 n += ly_print(out, "\\\"");
52 break;
53 case '\\':
54 n += ly_print(out, "\\\\");
55 break;
56 default:
57 ly_write(out, &text[i], 1);
58 n++;
59 }
60 }
61 }
62 ly_write(out, "\"", 1);
63
64 return n + 2;
65}
66
Michal Vaskob3c31bd2017-07-17 10:01:48 +020067static int
Radek Krejci1eefeb32016-04-15 16:01:46 +020068json_print_attrs(struct lyout *out, int level, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejcida61fb22015-10-30 11:10:03 +010069{
70 struct lyd_attr *attr;
Radek Krejcia571d942017-02-24 09:26:49 +010071 size_t len;
72 char *p;
Radek Krejcida61fb22015-10-30 11:10:03 +010073
Michal Vasko609d7082019-04-26 09:35:40 +020074 LY_PRINT_SET;
75
Radek Krejci1eefeb32016-04-15 16:01:46 +020076 if (wdmod) {
77 ly_print(out, "%*s\"%s:default\":\"true\"", LEVEL, INDENT, wdmod->name);
78 ly_print(out, "%s%s", node->attr ? "," : "", (level ? "\n" : ""));
79 }
Radek Krejcida61fb22015-10-30 11:10:03 +010080 for (attr = node->attr; attr; attr = attr->next) {
Radek Krejci532e5e92017-02-22 12:59:24 +010081 if (!attr->annotation) {
82 /* skip exception for the NETCONF's attribute since JSON is not defined for NETCONF */
83 continue;
84 }
Radek Krejcib1ef1872017-02-23 14:33:34 +010085 if (lys_main_module(attr->annotation->module) != lys_main_module(node->schema->module)) {
Radek Krejci532e5e92017-02-22 12:59:24 +010086 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, attr->annotation->module->name, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010087 } else {
Radek Krejci382e7f92016-01-12 17:15:47 +010088 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010089 }
Radek Krejcia571d942017-02-24 09:26:49 +010090 /* leafref is not supported */
Michal Vasko70bf8e52018-03-26 11:32:33 +020091 switch (attr->value_type) {
Radek Krejcia571d942017-02-24 09:26:49 +010092 case LY_TYPE_BINARY:
93 case LY_TYPE_STRING:
94 case LY_TYPE_BITS:
95 case LY_TYPE_ENUM:
96 case LY_TYPE_INST:
97 case LY_TYPE_INT64:
98 case LY_TYPE_UINT64:
99 case LY_TYPE_DEC64:
Michal Vaskoa80d8302017-03-02 10:52:02 +0100100 json_print_string(out, attr->value_str);
Radek Krejcia571d942017-02-24 09:26:49 +0100101 break;
102
103 case LY_TYPE_INT8:
104 case LY_TYPE_INT16:
105 case LY_TYPE_INT32:
106 case LY_TYPE_UINT8:
107 case LY_TYPE_UINT16:
108 case LY_TYPE_UINT32:
109 case LY_TYPE_BOOL:
110 ly_print(out, "%s", attr->value_str[0] ? attr->value_str : "null");
111 break;
112
113 case LY_TYPE_IDENT:
114 p = strchr(attr->value_str, ':');
115 assert(p);
116 len = p - attr->value_str;
117 if (!strncmp(attr->value_str, attr->annotation->module->name, len)
118 && !attr->annotation->module->name[len]) {
119 /* do not print the prefix, it is the default prefix for this node */
120 json_print_string(out, ++p);
121 } else {
122 json_print_string(out, attr->value_str);
123 }
124 break;
125
126 case LY_TYPE_EMPTY:
127 ly_print(out, "[null]");
128 break;
129
130 default:
131 /* error */
Michal Vasko609d7082019-04-26 09:35:40 +0200132 LOGINT(node->schema->module->ctx);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200133 return EXIT_FAILURE;
Radek Krejcia571d942017-02-24 09:26:49 +0100134 }
135
Michal Vasko95068c42016-03-24 14:58:11 +0100136 ly_print(out, "%s%s", attr->next ? "," : "", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100137 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200138
Michal Vasko609d7082019-04-26 09:35:40 +0200139 LY_PRINT_RET(node->schema->module->ctx);
Radek Krejcida61fb22015-10-30 11:10:03 +0100140}
141
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200142static int
Radek Krejci46180b52016-08-31 16:01:32 +0200143json_print_leaf(struct lyout *out, int level, const struct lyd_node *node, int onlyvalue, int toplevel, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200144{
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100145 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node, *iter;
Michal Vasko0bff3222017-01-05 10:55:31 +0100146 const struct lys_type *type;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100147 const char *schema = NULL, *p, *mod_name;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200148 const struct lys_module *wdmod = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200149 LY_DATA_TYPE datatype;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100150 size_t len;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200151
Michal Vasko609d7082019-04-26 09:35:40 +0200152 LY_PRINT_SET;
153
Radek Krejci46180b52016-08-31 16:01:32 +0200154 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
155 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default(leaf))) {
156 /* we have implicit OR explicit default node */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200157 /* get with-defaults module */
Radek Krejcidfb00d62017-09-06 09:39:35 +0200158 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1);
Radek Krejci1eefeb32016-04-15 16:01:46 +0200159 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200160
Radek Krejci5a988152015-07-15 11:16:26 +0200161 if (!onlyvalue) {
Michal Vasko635bd452016-05-18 13:26:55 +0200162 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejci5a988152015-07-15 11:16:26 +0200163 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100164 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100165 ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200166 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100167 ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200168 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200169 }
Radek Krejcie4748472015-07-08 18:00:22 +0200170
Michal Vasko70bf8e52018-03-26 11:32:33 +0200171 datatype = leaf->value_type;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200172contentprint:
173 switch (datatype) {
Radek Krejcie4748472015-07-08 18:00:22 +0200174 case LY_TYPE_BINARY:
175 case LY_TYPE_STRING:
Radek Krejci3e3affe2015-07-09 15:38:40 +0200176 case LY_TYPE_BITS:
Radek Krejci5b315a92015-07-10 13:18:45 +0200177 case LY_TYPE_ENUM:
Radek Krejcic5090c32015-08-12 09:46:19 +0200178 case LY_TYPE_INST:
Radek Krejcic27114c2016-09-20 10:02:28 +0200179 case LY_TYPE_INT64:
180 case LY_TYPE_UINT64:
Michal Vasko3db69682018-03-27 16:11:31 +0200181 case LY_TYPE_UNION:
严军喜100794891758ff62016-10-25 10:14:47 +0800182 case LY_TYPE_DEC64:
Michal Vasko44913842016-04-13 14:20:41 +0200183 json_print_string(out, leaf->value_str);
Radek Krejciac8aac62015-07-10 15:36:35 +0200184 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200185
Radek Krejcib1c12512015-08-11 11:22:04 +0200186 case LY_TYPE_INT8:
187 case LY_TYPE_INT16:
188 case LY_TYPE_INT32:
Radek Krejcib1c12512015-08-11 11:22:04 +0200189 case LY_TYPE_UINT8:
190 case LY_TYPE_UINT16:
191 case LY_TYPE_UINT32:
严军喜100794891758ff62016-10-25 10:14:47 +0800192 case LY_TYPE_BOOL:
Michal Vasko44913842016-04-13 14:20:41 +0200193 ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null");
Radek Krejcib1c12512015-08-11 11:22:04 +0200194 break;
195
Radek Krejci7a2a5622016-12-05 13:32:05 +0100196 case LY_TYPE_IDENT:
197 p = strchr(leaf->value_str, ':');
198 assert(p);
199 len = p - leaf->value_str;
200 mod_name = leaf->schema->module->name;
201 if (!strncmp(leaf->value_str, mod_name, len) && !mod_name[len]) {
202 /* do not print the prefix, it is the default prefix for this node */
203 json_print_string(out, ++p);
204 } else {
205 json_print_string(out, leaf->value_str);
206 }
207 break;
208
Radek Krejci9b6aad22016-09-20 15:55:51 +0200209 case LY_TYPE_LEAFREF:
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100210 iter = (struct lyd_node_leaf_list *)leaf->value.leafref;
211 while (iter && (iter->value_type == LY_TYPE_LEAFREF)) {
212 iter = (struct lyd_node_leaf_list *)iter->value.leafref;
213 }
214 if (!iter) {
Michal Vasko0bff3222017-01-05 10:55:31 +0100215 /* unresolved and invalid, but we can learn the correct type anyway */
216 type = lyd_leaf_type((struct lyd_node_leaf_list *)leaf);
217 if (!type) {
218 /* error */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200219 return EXIT_FAILURE;
Michal Vasko0bff3222017-01-05 10:55:31 +0100220 }
221 datatype = type->base;
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100222 } else {
Michal Vasko70bf8e52018-03-26 11:32:33 +0200223 datatype = iter->value_type;
Radek Krejci9ad23f42016-10-31 15:46:52 +0100224 }
Michal Vasko0bff3222017-01-05 10:55:31 +0100225 goto contentprint;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200226
Radek Krejcib1c12512015-08-11 11:22:04 +0200227 case LY_TYPE_EMPTY:
Radek Krejci76b07902015-10-09 09:11:25 +0200228 ly_print(out, "[null]");
Michal Vasko58110162015-07-15 15:50:16 +0200229 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200230
Fred Gand4219762021-01-27 15:30:18 +0800231 case LY_TYPE_UNKNOWN:
232 if (leaf->value_str[0] == '\0') {
233 ly_write(out, "\"\"", 2);
234 break;
235 }
236 datatype = ((struct lys_node_leaf *)leaf->schema)->type.base;
237 goto contentprint;
238
Radek Krejcie4748472015-07-08 18:00:22 +0200239 default:
Michal Vasko493bea72015-07-16 16:08:12 +0200240 /* error */
Michal Vasko609d7082019-04-26 09:35:40 +0200241 LOGINT(node->schema->module->ctx);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200242 return EXIT_FAILURE;
Radek Krejcie4748472015-07-08 18:00:22 +0200243 }
244
Radek Krejci382e7f92016-01-12 17:15:47 +0100245 /* print attributes as sibling leafs */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200246 if (!onlyvalue && (node->attr || wdmod)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100247 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100248 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
249 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100250 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100251 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
252 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100253 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200254 if (json_print_attrs(out, (level ? level + 1 : level), node, wdmod)) {
255 return EXIT_FAILURE;
256 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100257 ly_print(out, "%*s}", LEVEL, INDENT);
258 }
259
Michal Vasko609d7082019-04-26 09:35:40 +0200260 LY_PRINT_RET(node->schema->module->ctx);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200261}
262
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200263static int
Radek Krejci46180b52016-08-31 16:01:32 +0200264json_print_container(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vasko98763c62015-07-17 13:47:00 +0200265{
266 const char *schema;
Michal Vasko98763c62015-07-17 13:47:00 +0200267
Michal Vasko609d7082019-04-26 09:35:40 +0200268 LY_PRINT_SET;
269
Michal Vasko635bd452016-05-18 13:26:55 +0200270 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200271 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100272 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100273 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200274 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100275 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200276 }
Michal Vasko95068c42016-03-24 14:58:11 +0100277 if (level) {
278 level++;
279 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100280 if (node->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100281 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200282 if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) {
283 return EXIT_FAILURE;
284 }
Michal Vasko95068c42016-03-24 14:58:11 +0100285 ly_print(out, "%*s}", LEVEL, INDENT);
Fred Gand4219762021-01-27 15:30:18 +0800286 ly_print(out, "%s%s", (node->child? "," : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100287 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200288 if (json_print_nodes(out, level, node->child, 1, 0, options)) {
289 return EXIT_FAILURE;
290 }
Michal Vasko95068c42016-03-24 14:58:11 +0100291 if (level) {
292 level--;
293 }
Radek Krejci76b07902015-10-09 09:11:25 +0200294 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200295
Michal Vasko609d7082019-04-26 09:35:40 +0200296 LY_PRINT_RET(node->schema->module->ctx);
Michal Vasko98763c62015-07-17 13:47:00 +0200297}
298
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200299static int
Radek Krejci46180b52016-08-31 16:01:32 +0200300json_print_leaf_list(struct lyout *out, int level, const struct lyd_node *node, int is_list, int toplevel, int options)
Michal Vasko98763c62015-07-17 13:47:00 +0200301{
Radek Krejcida61fb22015-10-30 11:10:03 +0100302 const char *schema = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100303 const struct lyd_node *list = node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100304 int flag_empty = 0, flag_attrs = 0;
Radek Krejci23238922015-10-27 17:13:34 +0100305
Michal Vasko609d7082019-04-26 09:35:40 +0200306 LY_PRINT_SET;
307
Radek Krejcie8775162016-09-14 13:08:58 +0200308 if (is_list && !list->child) {
Radek Krejci23238922015-10-27 17:13:34 +0100309 /* empty, e.g. in case of filter */
310 flag_empty = 1;
311 }
Michal Vasko98763c62015-07-17 13:47:00 +0200312
Michal Vasko635bd452016-05-18 13:26:55 +0200313 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200314 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100315 schema = lys_node_module(node->schema)->name;
Radek Krejci23238922015-10-27 17:13:34 +0100316 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200317 } else {
Radek Krejci23238922015-10-27 17:13:34 +0100318 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200319 }
320
Radek Krejci23238922015-10-27 17:13:34 +0100321 if (flag_empty) {
Fred Gand4219762021-01-27 15:30:18 +0800322 ly_print(out, "%s[]", (level ? " " : ""));
Michal Vasko609d7082019-04-26 09:35:40 +0200323 goto finish;
Radek Krejci23238922015-10-27 17:13:34 +0100324 }
Michal Vasko95068c42016-03-24 14:58:11 +0100325 ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100326
Michal Vasko95068c42016-03-24 14:58:11 +0100327 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200328 ++level;
329 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200330
331 while (list) {
Michal Vasko98763c62015-07-17 13:47:00 +0200332 if (is_list) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200333 /* list print */
Michal Vasko95068c42016-03-24 14:58:11 +0100334 if (level) {
335 ++level;
336 }
337 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : ""));
338 if (level) {
339 ++level;
340 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100341 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100342 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200343 if (json_print_attrs(out, (level ? level + 1 : level), list, NULL)) {
344 return EXIT_FAILURE;
345 }
Michal Vaskof6aa8612017-03-02 10:52:44 +0100346 if (list->child) {
347 ly_print(out, "%*s},%s", LEVEL, INDENT, (level ? "\n" : ""));
348 } else {
349 ly_print(out, "%*s}", LEVEL, INDENT);
350 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100351 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200352 if (json_print_nodes(out, level, list->child, 1, 0, options)) {
353 return EXIT_FAILURE;
354 }
Michal Vasko95068c42016-03-24 14:58:11 +0100355 if (level) {
356 --level;
357 }
Radek Krejci76b07902015-10-09 09:11:25 +0200358 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko95068c42016-03-24 14:58:11 +0100359 if (level) {
360 --level;
361 }
Michal Vasko98763c62015-07-17 13:47:00 +0200362 } else {
Radek Krejci27aaa732015-09-04 15:24:04 +0200363 /* leaf-list print */
Radek Krejci76b07902015-10-09 09:11:25 +0200364 ly_print(out, "%*s", LEVEL, INDENT);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200365 if (json_print_leaf(out, level, list, 1, toplevel, options)) {
366 return EXIT_FAILURE;
367 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100368 if (list->attr) {
369 flag_attrs = 1;
370 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200371 }
Alexandre Snarskiice16bcf2018-09-12 12:10:07 +0300372 if (toplevel && !(options & LYP_WITHSIBLINGS)) {
373 /* if initially called without LYP_WITHSIBLINGS do not print other list entries */
374 break;
375 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200376 for (list = list->next; list && list->schema != node->schema; list = list->next);
377 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100378 ly_print(out, ",%s", (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200379 }
380 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200381
Michal Vasko95068c42016-03-24 14:58:11 +0100382 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200383 --level;
384 }
385
Michal Vasko95068c42016-03-24 14:58:11 +0100386 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100387
388 /* attributes */
389 if (!is_list && flag_attrs) {
390 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100391 ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
392 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100393 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100394 ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
395 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100396 }
Michal Vasko95068c42016-03-24 14:58:11 +0100397 if (level) {
398 level++;
399 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100400 for (list = node; list; ) {
401 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100402 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200403 if (json_print_attrs(out, 0, list, NULL)) {
404 return EXIT_FAILURE;
405 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100406 ly_print(out, "%*s}", LEVEL, INDENT);
407 } else {
408 ly_print(out, "%*snull", LEVEL, INDENT);
409 }
410
411
412 for (list = list->next; list && list->schema != node->schema; list = list->next);
413 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100414 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100415 }
416 }
Michal Vasko95068c42016-03-24 14:58:11 +0100417 if (level) {
418 level--;
419 }
420 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100421 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200422
Michal Vasko609d7082019-04-26 09:35:40 +0200423finish:
424 LY_PRINT_RET(node->schema->module->ctx);
Michal Vasko98763c62015-07-17 13:47:00 +0200425}
426
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200427static int
Michal Vasko9696e4d2018-05-28 08:28:31 +0200428json_print_anydataxml(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Radek Krejcia7177672016-11-17 14:53:03 +0900429{
430 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
Michal Vasko9696e4d2018-05-28 08:28:31 +0200431 int is_object = 0;
Radek Krejcia7177672016-11-17 14:53:03 +0900432 char *buf;
433 const char *schema = NULL;
434
Michal Vasko609d7082019-04-26 09:35:40 +0200435 LY_PRINT_SET;
436
Radek Krejcia7177672016-11-17 14:53:03 +0900437 if (toplevel || !node->parent || nscmp(node, node->parent)) {
438 /* print "namespace" */
439 schema = lys_node_module(node->schema)->name;
Michal Vasko9696e4d2018-05-28 08:28:31 +0200440 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Radek Krejcia7177672016-11-17 14:53:03 +0900441 } else {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200442 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Radek Krejcia7177672016-11-17 14:53:03 +0900443 }
444 if (level) {
445 level++;
446 }
447
448 switch (any->value_type) {
449 case LYD_ANYDATA_DATATREE:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200450 is_object = 1;
451 ly_print(out, "%s{%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcidb2f58c2016-11-24 11:24:48 +0100452 /* do not print any default values nor empty containers */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200453 if (json_print_nodes(out, level, any->value.tree, 1, 0, LYP_WITHSIBLINGS | (options & ~LYP_NETCONF))) {
454 return EXIT_FAILURE;
455 }
Radek Krejcia7177672016-11-17 14:53:03 +0900456 break;
457 case LYD_ANYDATA_JSON:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200458 if (level) {
459 ly_print(out, "\n");
460 }
Radek Krejcia7177672016-11-17 14:53:03 +0900461 if (any->value.str) {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200462 ly_print(out, "%s", any->value.str);
463 }
Michal Vaskoe748db42018-08-17 10:30:32 +0200464 if (level && (!any->value.str || (any->value.str[strlen(any->value.str) - 1] != '\n'))) {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200465 /* do not print 2 newlines */
466 ly_print(out, "\n");
Radek Krejcia7177672016-11-17 14:53:03 +0900467 }
468 break;
469 case LYD_ANYDATA_XML:
470 lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0)
471 | LYXML_PRINT_SIBLINGS);
Michal Vasko9696e4d2018-05-28 08:28:31 +0200472 if (level) {
473 ly_print(out, " ");
474 }
Radek Krejcia7177672016-11-17 14:53:03 +0900475 json_print_string(out, buf);
476 free(buf);
477 break;
478 case LYD_ANYDATA_CONSTSTRING:
479 case LYD_ANYDATA_SXML:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200480 if (level) {
481 ly_print(out, " ");
482 }
Radek Krejcia7177672016-11-17 14:53:03 +0900483 if (any->value.str) {
484 json_print_string(out, any->value.str);
485 } else {
486 ly_print(out, "\"\"");
487 }
488 break;
Michal Vaskodcaf7222018-08-08 16:27:00 +0200489 case LYD_ANYDATA_STRING:
490 case LYD_ANYDATA_SXMLD:
491 case LYD_ANYDATA_JSOND:
492 case LYD_ANYDATA_LYBD:
493 case LYD_ANYDATA_LYB:
Radek Krejcia7177672016-11-17 14:53:03 +0900494 /* other formats are not supported */
Michal Vasko609d7082019-04-26 09:35:40 +0200495 LOGINT(node->schema->module->ctx);
496 return EXIT_FAILURE;
Radek Krejcida61fb22015-10-30 11:10:03 +0100497 }
498
499 /* print attributes as sibling leaf */
Fred Gand4219762021-01-27 15:30:18 +0800500 switch(node->schema->nodetype) {
501 case LYS_ANYXML:
502 if (level) {
503 level--;
Radek Krejcida61fb22015-10-30 11:10:03 +0100504 }
Fred Gand4219762021-01-27 15:30:18 +0800505 if (is_object) {
506 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200507 }
Fred Gand4219762021-01-27 15:30:18 +0800508 if (node->attr) {
509 if (schema) {
510 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
511 (level ? " " : ""), (level ? "\n" : ""));
512 } else {
513 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
514 (level ? " " : ""), (level ? "\n" : ""));
515 }
516 if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) {
517 return EXIT_FAILURE;
518 }
519 ly_print(out, "%*s}", LEVEL, INDENT);
520 }
521 break;
522 case LYS_ANYDATA:
523 if (node->attr) {
524 ly_print(out, ",%s%*s\"@\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
525 if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) {
526 return EXIT_FAILURE;
527 }
528 ly_print(out, "%*s}", LEVEL, INDENT);
529 }
530 if (level) {
531 level--;
532 }
533 if (is_object) {
534 ly_print(out, "%s%*s}", (level ? "\n" : ""), LEVEL, INDENT);
535 }
536 break;
537 default:
538 LOGINT(node->schema->module->ctx);
539 return EXIT_FAILURE;
Michal Vasko9696e4d2018-05-28 08:28:31 +0200540 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200541
Michal Vasko609d7082019-04-26 09:35:40 +0200542 LY_PRINT_RET(node->schema->module->ctx);
Michal Vaskoab8e4402015-07-17 12:54:28 +0200543}
544
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200545static int
Radek Krejci46180b52016-08-31 16:01:32 +0200546json_print_nodes(struct lyout *out, int level, const struct lyd_node *root, int withsiblings, int toplevel, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200547{
Michal Vasko609d7082019-04-26 09:35:40 +0200548 int comma_flag = 0;
Radek Krejci572f1222016-09-01 09:52:47 +0200549 const struct lyd_node *node, *iter;
Radek Krejci27aaa732015-09-04 15:24:04 +0200550
Michal Vasko609d7082019-04-26 09:35:40 +0200551 LY_PRINT_SET;
552
Radek Krejci27aaa732015-09-04 15:24:04 +0200553 LY_TREE_FOR(root, node) {
Michal Vaskob5bf9bb2020-06-01 16:43:40 +0200554 if (lyd_node_should_print(node, options)) {
555 /* wd says to print */
556 switch (node->schema->nodetype) {
557 case LYS_RPC:
558 case LYS_ACTION:
559 case LYS_NOTIF:
560 case LYS_CONTAINER:
Radek Krejcic90c6512018-02-14 15:21:19 +0100561 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200562 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100563 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200564 }
Michal Vaskob5bf9bb2020-06-01 16:43:40 +0200565 if (json_print_container(out, level, node, toplevel, options)) {
Michal Vasko609d7082019-04-26 09:35:40 +0200566 return EXIT_FAILURE;
567 }
Michal Vaskob5bf9bb2020-06-01 16:43:40 +0200568 break;
569 case LYS_LEAF:
570 if (comma_flag) {
571 /* print the previous comma */
572 ly_print(out, ",%s", (level ? "\n" : ""));
573 }
574 if (json_print_leaf(out, level, node, 0, toplevel, options)) {
575 return EXIT_FAILURE;
576 }
577 break;
578 case LYS_LEAFLIST:
579 case LYS_LIST:
580 /* is it already printed? (root node is not) */
581 for (iter = node->prev; iter->next && node != root; iter = iter->prev) {
582 if (iter == node) {
583 continue;
584 }
585 if (iter->schema == node->schema) {
586 /* the list has alread some previous instance and therefore it is already printed */
587 break;
588 }
589 }
590 if (!iter->next || node == root) {
591 if (comma_flag) {
592 /* print the previous comma */
593 ly_print(out, ",%s", (level ? "\n" : ""));
594 }
595
596 /* print the list/leaflist */
597 if (json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0, toplevel, options)) {
598 return EXIT_FAILURE;
599 }
600 }
601 break;
602 case LYS_ANYXML:
603 case LYS_ANYDATA:
604 if (comma_flag) {
605 /* print the previous comma */
606 ly_print(out, ",%s", (level ? "\n" : ""));
607 }
608 if (json_print_anydataxml(out, level, node, toplevel, options)) {
609 return EXIT_FAILURE;
610 }
611 break;
612 default:
613 LOGINT(node->schema->module->ctx);
Michal Vasko609d7082019-04-26 09:35:40 +0200614 return EXIT_FAILURE;
615 }
Michal Vaskob5bf9bb2020-06-01 16:43:40 +0200616
617 comma_flag = 1;
Radek Krejci27aaa732015-09-04 15:24:04 +0200618 }
Radek Krejci5044be32016-01-18 17:05:51 +0100619
620 if (!withsiblings) {
621 break;
622 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200623 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200624 if (root && level) {
Michal Vasko95068c42016-03-24 14:58:11 +0100625 ly_print(out, "\n");
626 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200627
Michal Vasko609d7082019-04-26 09:35:40 +0200628 LY_PRINT_RET(root ? root->schema->module->ctx : NULL);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200629}
630
Radek Krejcif3752b02015-10-02 15:31:34 +0200631int
Radek Krejci5044be32016-01-18 17:05:51 +0100632json_print_data(struct lyout *out, const struct lyd_node *root, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200633{
Michal Vaskoafa7a642016-10-18 15:11:38 +0200634 const struct lyd_node *node, *next;
635 int level = 0, action_input = 0;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200636
Michal Vasko609d7082019-04-26 09:35:40 +0200637 LY_PRINT_SET;
638
Michal Vasko95068c42016-03-24 14:58:11 +0100639 if (options & LYP_FORMAT) {
640 ++level;
641 }
642
Michal Vaskoafa7a642016-10-18 15:11:38 +0200643 if (options & LYP_NETCONF) {
644 if (root->schema->nodetype != LYS_RPC) {
645 /* learn whether we are printing an action */
646 LY_TREE_DFS_BEGIN(root, next, node) {
647 if (node->schema->nodetype == LYS_ACTION) {
648 break;
649 }
650 LY_TREE_DFS_END(root, next, node);
651 }
652 } else {
653 node = root;
654 }
655
656 if (node && (node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
657 if (node->child && (node->child->schema->parent->nodetype == LYS_OUTPUT)) {
658 /* skip the container */
659 root = node->child;
660 } else if (node->schema->nodetype == LYS_ACTION) {
661 action_input = 1;
662 }
663 }
664 }
665
Radek Krejci94ca54b2015-07-08 15:48:47 +0200666 /* start */
Michal Vasko95068c42016-03-24 14:58:11 +0100667 ly_print(out, "{%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200668
Michal Vaskoafa7a642016-10-18 15:11:38 +0200669 if (action_input) {
670 ly_print(out, "%*s\"yang:action\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
671 if (level) {
672 ++level;
673 }
674 }
675
Radek Krejci94ca54b2015-07-08 15:48:47 +0200676 /* content */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200677 if (json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options)) {
678 return EXIT_FAILURE;
679 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200680
Michal Vaskoafa7a642016-10-18 15:11:38 +0200681 if (action_input) {
682 if (level) {
683 --level;
684 }
685 ly_print(out, "%*s}%s", LEVEL, INDENT, (level ? "\n" : ""));
686 }
687
Radek Krejci94ca54b2015-07-08 15:48:47 +0200688 /* end */
Michal Vasko95068c42016-03-24 14:58:11 +0100689 ly_print(out, "}%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200690
Michal Vaskoafa7a642016-10-18 15:11:38 +0200691 ly_print_flush(out);
Michal Vasko609d7082019-04-26 09:35:40 +0200692 LY_PRINT_RET(NULL);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200693}