blob: dd054c42de5afae2236483e8992a857aaaea1482 [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
Radek Krejci1eefeb32016-04-15 16:01:46 +020074 if (wdmod) {
75 ly_print(out, "%*s\"%s:default\":\"true\"", LEVEL, INDENT, wdmod->name);
76 ly_print(out, "%s%s", node->attr ? "," : "", (level ? "\n" : ""));
77 }
Radek Krejcida61fb22015-10-30 11:10:03 +010078 for (attr = node->attr; attr; attr = attr->next) {
Radek Krejci532e5e92017-02-22 12:59:24 +010079 if (!attr->annotation) {
80 /* skip exception for the NETCONF's attribute since JSON is not defined for NETCONF */
81 continue;
82 }
Radek Krejcib1ef1872017-02-23 14:33:34 +010083 if (lys_main_module(attr->annotation->module) != lys_main_module(node->schema->module)) {
Radek Krejci532e5e92017-02-22 12:59:24 +010084 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, attr->annotation->module->name, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010085 } else {
Radek Krejci382e7f92016-01-12 17:15:47 +010086 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010087 }
Radek Krejcia571d942017-02-24 09:26:49 +010088 /* leafref is not supported */
Michal Vasko70bf8e52018-03-26 11:32:33 +020089 switch (attr->value_type) {
Radek Krejcia571d942017-02-24 09:26:49 +010090 case LY_TYPE_BINARY:
91 case LY_TYPE_STRING:
92 case LY_TYPE_BITS:
93 case LY_TYPE_ENUM:
94 case LY_TYPE_INST:
95 case LY_TYPE_INT64:
96 case LY_TYPE_UINT64:
97 case LY_TYPE_DEC64:
Michal Vaskoa80d8302017-03-02 10:52:02 +010098 json_print_string(out, attr->value_str);
Radek Krejcia571d942017-02-24 09:26:49 +010099 break;
100
101 case LY_TYPE_INT8:
102 case LY_TYPE_INT16:
103 case LY_TYPE_INT32:
104 case LY_TYPE_UINT8:
105 case LY_TYPE_UINT16:
106 case LY_TYPE_UINT32:
107 case LY_TYPE_BOOL:
108 ly_print(out, "%s", attr->value_str[0] ? attr->value_str : "null");
109 break;
110
111 case LY_TYPE_IDENT:
112 p = strchr(attr->value_str, ':');
113 assert(p);
114 len = p - attr->value_str;
115 if (!strncmp(attr->value_str, attr->annotation->module->name, len)
116 && !attr->annotation->module->name[len]) {
117 /* do not print the prefix, it is the default prefix for this node */
118 json_print_string(out, ++p);
119 } else {
120 json_print_string(out, attr->value_str);
121 }
122 break;
123
124 case LY_TYPE_EMPTY:
125 ly_print(out, "[null]");
126 break;
127
128 default:
129 /* error */
130 ly_print(out, "\"(!error!)\"");
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200131 return EXIT_FAILURE;
Radek Krejcia571d942017-02-24 09:26:49 +0100132 }
133
Michal Vasko95068c42016-03-24 14:58:11 +0100134 ly_print(out, "%s%s", attr->next ? "," : "", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100135 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200136
137 return EXIT_SUCCESS;
Radek Krejcida61fb22015-10-30 11:10:03 +0100138}
139
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200140static int
Radek Krejci46180b52016-08-31 16:01:32 +0200141json_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 +0200142{
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100143 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node, *iter;
Michal Vasko0bff3222017-01-05 10:55:31 +0100144 const struct lys_type *type;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100145 const char *schema = NULL, *p, *mod_name;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200146 const struct lys_module *wdmod = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200147 LY_DATA_TYPE datatype;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100148 size_t len;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200149
Radek Krejci46180b52016-08-31 16:01:32 +0200150 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
151 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default(leaf))) {
152 /* we have implicit OR explicit default node */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200153 /* get with-defaults module */
Radek Krejcidfb00d62017-09-06 09:39:35 +0200154 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1);
Radek Krejci1eefeb32016-04-15 16:01:46 +0200155 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200156
Radek Krejci5a988152015-07-15 11:16:26 +0200157 if (!onlyvalue) {
Michal Vasko635bd452016-05-18 13:26:55 +0200158 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejci5a988152015-07-15 11:16:26 +0200159 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100160 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100161 ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200162 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100163 ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200164 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200165 }
Radek Krejcie4748472015-07-08 18:00:22 +0200166
Michal Vasko70bf8e52018-03-26 11:32:33 +0200167 datatype = leaf->value_type;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200168contentprint:
169 switch (datatype) {
Radek Krejcie4748472015-07-08 18:00:22 +0200170 case LY_TYPE_BINARY:
171 case LY_TYPE_STRING:
Radek Krejci3e3affe2015-07-09 15:38:40 +0200172 case LY_TYPE_BITS:
Radek Krejci5b315a92015-07-10 13:18:45 +0200173 case LY_TYPE_ENUM:
Radek Krejcic5090c32015-08-12 09:46:19 +0200174 case LY_TYPE_INST:
Radek Krejcic27114c2016-09-20 10:02:28 +0200175 case LY_TYPE_INT64:
176 case LY_TYPE_UINT64:
Michal Vasko3db69682018-03-27 16:11:31 +0200177 case LY_TYPE_UNION:
严军喜100794891758ff62016-10-25 10:14:47 +0800178 case LY_TYPE_DEC64:
Michal Vasko44913842016-04-13 14:20:41 +0200179 json_print_string(out, leaf->value_str);
Radek Krejciac8aac62015-07-10 15:36:35 +0200180 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200181
Radek Krejcib1c12512015-08-11 11:22:04 +0200182 case LY_TYPE_INT8:
183 case LY_TYPE_INT16:
184 case LY_TYPE_INT32:
Radek Krejcib1c12512015-08-11 11:22:04 +0200185 case LY_TYPE_UINT8:
186 case LY_TYPE_UINT16:
187 case LY_TYPE_UINT32:
严军喜100794891758ff62016-10-25 10:14:47 +0800188 case LY_TYPE_BOOL:
Michal Vasko44913842016-04-13 14:20:41 +0200189 ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null");
Radek Krejcib1c12512015-08-11 11:22:04 +0200190 break;
191
Radek Krejci7a2a5622016-12-05 13:32:05 +0100192 case LY_TYPE_IDENT:
193 p = strchr(leaf->value_str, ':');
194 assert(p);
195 len = p - leaf->value_str;
196 mod_name = leaf->schema->module->name;
197 if (!strncmp(leaf->value_str, mod_name, len) && !mod_name[len]) {
198 /* do not print the prefix, it is the default prefix for this node */
199 json_print_string(out, ++p);
200 } else {
201 json_print_string(out, leaf->value_str);
202 }
203 break;
204
Radek Krejci9b6aad22016-09-20 15:55:51 +0200205 case LY_TYPE_LEAFREF:
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100206 iter = (struct lyd_node_leaf_list *)leaf->value.leafref;
207 while (iter && (iter->value_type == LY_TYPE_LEAFREF)) {
208 iter = (struct lyd_node_leaf_list *)iter->value.leafref;
209 }
210 if (!iter) {
Michal Vasko0bff3222017-01-05 10:55:31 +0100211 /* unresolved and invalid, but we can learn the correct type anyway */
212 type = lyd_leaf_type((struct lyd_node_leaf_list *)leaf);
213 if (!type) {
214 /* error */
215 ly_print(out, "\"(!error!)\"");
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200216 return EXIT_FAILURE;
Michal Vasko0bff3222017-01-05 10:55:31 +0100217 }
218 datatype = type->base;
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100219 } else {
Michal Vasko70bf8e52018-03-26 11:32:33 +0200220 datatype = iter->value_type;
Radek Krejci9ad23f42016-10-31 15:46:52 +0100221 }
Michal Vasko0bff3222017-01-05 10:55:31 +0100222 goto contentprint;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200223
Radek Krejcib1c12512015-08-11 11:22:04 +0200224 case LY_TYPE_EMPTY:
Radek Krejci76b07902015-10-09 09:11:25 +0200225 ly_print(out, "[null]");
Michal Vasko58110162015-07-15 15:50:16 +0200226 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200227
Radek Krejcie4748472015-07-08 18:00:22 +0200228 default:
Michal Vasko493bea72015-07-16 16:08:12 +0200229 /* error */
Radek Krejci76b07902015-10-09 09:11:25 +0200230 ly_print(out, "\"(!error!)\"");
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200231 return EXIT_FAILURE;
Radek Krejcie4748472015-07-08 18:00:22 +0200232 }
233
Radek Krejci382e7f92016-01-12 17:15:47 +0100234 /* print attributes as sibling leafs */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200235 if (!onlyvalue && (node->attr || wdmod)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100236 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100237 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
238 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100239 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100240 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
241 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100242 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200243 if (json_print_attrs(out, (level ? level + 1 : level), node, wdmod)) {
244 return EXIT_FAILURE;
245 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100246 ly_print(out, "%*s}", LEVEL, INDENT);
247 }
248
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200249 return EXIT_SUCCESS;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200250}
251
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200252static int
Radek Krejci46180b52016-08-31 16:01:32 +0200253json_print_container(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vasko98763c62015-07-17 13:47:00 +0200254{
255 const char *schema;
Michal Vasko98763c62015-07-17 13:47:00 +0200256
Michal Vasko635bd452016-05-18 13:26:55 +0200257 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200258 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100259 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100260 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200261 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100262 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200263 }
Michal Vasko95068c42016-03-24 14:58:11 +0100264 if (level) {
265 level++;
266 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100267 if (node->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100268 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200269 if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) {
270 return EXIT_FAILURE;
271 }
Michal Vasko95068c42016-03-24 14:58:11 +0100272 ly_print(out, "%*s}", LEVEL, INDENT);
273 if (node->child) {
274 ly_print(out, ",%s", (level ? "\n" : ""));
275 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100276 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200277 if (json_print_nodes(out, level, node->child, 1, 0, options)) {
278 return EXIT_FAILURE;
279 }
Michal Vasko95068c42016-03-24 14:58:11 +0100280 if (level) {
281 level--;
282 }
Radek Krejci76b07902015-10-09 09:11:25 +0200283 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200284
285 return EXIT_SUCCESS;
Michal Vasko98763c62015-07-17 13:47:00 +0200286}
287
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200288static int
Radek Krejci46180b52016-08-31 16:01:32 +0200289json_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 +0200290{
Radek Krejcida61fb22015-10-30 11:10:03 +0100291 const char *schema = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100292 const struct lyd_node *list = node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100293 int flag_empty = 0, flag_attrs = 0;
Radek Krejci23238922015-10-27 17:13:34 +0100294
Radek Krejcie8775162016-09-14 13:08:58 +0200295 if (is_list && !list->child) {
Radek Krejci23238922015-10-27 17:13:34 +0100296 /* empty, e.g. in case of filter */
297 flag_empty = 1;
298 }
Michal Vasko98763c62015-07-17 13:47:00 +0200299
Michal Vasko635bd452016-05-18 13:26:55 +0200300 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200301 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100302 schema = lys_node_module(node->schema)->name;
Radek Krejci23238922015-10-27 17:13:34 +0100303 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200304 } else {
Radek Krejci23238922015-10-27 17:13:34 +0100305 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200306 }
307
Radek Krejci23238922015-10-27 17:13:34 +0100308 if (flag_empty) {
Michal Vasko95068c42016-03-24 14:58:11 +0100309 ly_print(out, "%snull", (level ? " " : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200310 return EXIT_SUCCESS;
Radek Krejci23238922015-10-27 17:13:34 +0100311 }
Michal Vasko95068c42016-03-24 14:58:11 +0100312 ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100313
Michal Vasko95068c42016-03-24 14:58:11 +0100314 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200315 ++level;
316 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200317
318 while (list) {
Michal Vasko98763c62015-07-17 13:47:00 +0200319 if (is_list) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200320 /* list print */
Michal Vasko95068c42016-03-24 14:58:11 +0100321 if (level) {
322 ++level;
323 }
324 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : ""));
325 if (level) {
326 ++level;
327 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100328 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100329 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200330 if (json_print_attrs(out, (level ? level + 1 : level), list, NULL)) {
331 return EXIT_FAILURE;
332 }
Michal Vaskof6aa8612017-03-02 10:52:44 +0100333 if (list->child) {
334 ly_print(out, "%*s},%s", LEVEL, INDENT, (level ? "\n" : ""));
335 } else {
336 ly_print(out, "%*s}", LEVEL, INDENT);
337 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100338 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200339 if (json_print_nodes(out, level, list->child, 1, 0, options)) {
340 return EXIT_FAILURE;
341 }
Michal Vasko95068c42016-03-24 14:58:11 +0100342 if (level) {
343 --level;
344 }
Radek Krejci76b07902015-10-09 09:11:25 +0200345 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko95068c42016-03-24 14:58:11 +0100346 if (level) {
347 --level;
348 }
Michal Vasko98763c62015-07-17 13:47:00 +0200349 } else {
Radek Krejci27aaa732015-09-04 15:24:04 +0200350 /* leaf-list print */
Radek Krejci76b07902015-10-09 09:11:25 +0200351 ly_print(out, "%*s", LEVEL, INDENT);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200352 if (json_print_leaf(out, level, list, 1, toplevel, options)) {
353 return EXIT_FAILURE;
354 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100355 if (list->attr) {
356 flag_attrs = 1;
357 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200358 }
Alexandre Snarskiice16bcf2018-09-12 12:10:07 +0300359 if (toplevel && !(options & LYP_WITHSIBLINGS)) {
360 /* if initially called without LYP_WITHSIBLINGS do not print other list entries */
361 break;
362 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200363 for (list = list->next; list && list->schema != node->schema; list = list->next);
364 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100365 ly_print(out, ",%s", (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200366 }
367 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200368
Michal Vasko95068c42016-03-24 14:58:11 +0100369 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200370 --level;
371 }
372
Michal Vasko95068c42016-03-24 14:58:11 +0100373 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100374
375 /* attributes */
376 if (!is_list && flag_attrs) {
377 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100378 ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
379 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100380 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100381 ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
382 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100383 }
Michal Vasko95068c42016-03-24 14:58:11 +0100384 if (level) {
385 level++;
386 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100387 for (list = node; list; ) {
388 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100389 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200390 if (json_print_attrs(out, 0, list, NULL)) {
391 return EXIT_FAILURE;
392 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100393 ly_print(out, "%*s}", LEVEL, INDENT);
394 } else {
395 ly_print(out, "%*snull", LEVEL, INDENT);
396 }
397
398
399 for (list = list->next; list && list->schema != node->schema; list = list->next);
400 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100401 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100402 }
403 }
Michal Vasko95068c42016-03-24 14:58:11 +0100404 if (level) {
405 level--;
406 }
407 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100408 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200409
410 return EXIT_SUCCESS;
Michal Vasko98763c62015-07-17 13:47:00 +0200411}
412
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200413static int
Michal Vasko9696e4d2018-05-28 08:28:31 +0200414json_print_anydataxml(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Radek Krejcia7177672016-11-17 14:53:03 +0900415{
416 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
Michal Vasko9696e4d2018-05-28 08:28:31 +0200417 int is_object = 0;
Radek Krejcia7177672016-11-17 14:53:03 +0900418 char *buf;
419 const char *schema = NULL;
420
421 if (toplevel || !node->parent || nscmp(node, node->parent)) {
422 /* print "namespace" */
423 schema = lys_node_module(node->schema)->name;
Michal Vasko9696e4d2018-05-28 08:28:31 +0200424 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Radek Krejcia7177672016-11-17 14:53:03 +0900425 } else {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200426 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Radek Krejcia7177672016-11-17 14:53:03 +0900427 }
428 if (level) {
429 level++;
430 }
431
432 switch (any->value_type) {
433 case LYD_ANYDATA_DATATREE:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200434 is_object = 1;
435 ly_print(out, "%s{%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcidb2f58c2016-11-24 11:24:48 +0100436 /* do not print any default values nor empty containers */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200437 if (json_print_nodes(out, level, any->value.tree, 1, 0, LYP_WITHSIBLINGS | (options & ~LYP_NETCONF))) {
438 return EXIT_FAILURE;
439 }
Radek Krejcia7177672016-11-17 14:53:03 +0900440 break;
441 case LYD_ANYDATA_JSON:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200442 if (level) {
443 ly_print(out, "\n");
444 }
Radek Krejcia7177672016-11-17 14:53:03 +0900445 if (any->value.str) {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200446 ly_print(out, "%s", any->value.str);
447 }
Michal Vaskoe748db42018-08-17 10:30:32 +0200448 if (level && (!any->value.str || (any->value.str[strlen(any->value.str) - 1] != '\n'))) {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200449 /* do not print 2 newlines */
450 ly_print(out, "\n");
Radek Krejcia7177672016-11-17 14:53:03 +0900451 }
452 break;
453 case LYD_ANYDATA_XML:
454 lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0)
455 | LYXML_PRINT_SIBLINGS);
Michal Vasko9696e4d2018-05-28 08:28:31 +0200456 if (level) {
457 ly_print(out, " ");
458 }
Radek Krejcia7177672016-11-17 14:53:03 +0900459 json_print_string(out, buf);
460 free(buf);
461 break;
462 case LYD_ANYDATA_CONSTSTRING:
463 case LYD_ANYDATA_SXML:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200464 if (level) {
465 ly_print(out, " ");
466 }
Radek Krejcia7177672016-11-17 14:53:03 +0900467 if (any->value.str) {
468 json_print_string(out, any->value.str);
469 } else {
470 ly_print(out, "\"\"");
471 }
472 break;
Michal Vaskodcaf7222018-08-08 16:27:00 +0200473 case LYD_ANYDATA_STRING:
474 case LYD_ANYDATA_SXMLD:
475 case LYD_ANYDATA_JSOND:
476 case LYD_ANYDATA_LYBD:
477 case LYD_ANYDATA_LYB:
Radek Krejcia7177672016-11-17 14:53:03 +0900478 /* other formats are not supported */
Michal Vasko9696e4d2018-05-28 08:28:31 +0200479 json_print_string(out, "(error)");
Radek Krejci4ae82942016-09-19 16:41:06 +0200480 break;
Radek Krejcida61fb22015-10-30 11:10:03 +0100481 }
482
483 /* print attributes as sibling leaf */
484 if (node->attr) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100485 if (schema) {
Michal Vaskof6aa8612017-03-02 10:52:44 +0100486 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
487 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100488 } else {
Michal Vaskof6aa8612017-03-02 10:52:44 +0100489 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
490 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100491 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200492 if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) {
493 return EXIT_FAILURE;
494 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100495 ly_print(out, "%*s}", LEVEL, INDENT);
496 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200497
Radek Krejci4ae82942016-09-19 16:41:06 +0200498 if (level) {
499 level--;
500 }
Michal Vasko9696e4d2018-05-28 08:28:31 +0200501 if (is_object) {
502 ly_print(out, "%*s}", LEVEL, INDENT);
503 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200504
505 return EXIT_SUCCESS;
Michal Vaskoab8e4402015-07-17 12:54:28 +0200506}
507
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200508static int
Radek Krejci46180b52016-08-31 16:01:32 +0200509json_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 +0200510{
Radek Krejcic90c6512018-02-14 15:21:19 +0100511 int ret = EXIT_SUCCESS,comma_flag = 0;
Radek Krejci572f1222016-09-01 09:52:47 +0200512 const struct lyd_node *node, *iter;
Radek Krejci27aaa732015-09-04 15:24:04 +0200513
514 LY_TREE_FOR(root, node) {
Radek Krejci572f1222016-09-01 09:52:47 +0200515 if (!lyd_wd_toprint(node, options)) {
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200516 /* wd says do not print */
Radek Krejci572f1222016-09-01 09:52:47 +0200517 continue;
Radek Krejci46180b52016-08-31 16:01:32 +0200518 }
519
Radek Krejci27aaa732015-09-04 15:24:04 +0200520 switch (node->schema->nodetype) {
Radek Krejcifb54be42015-10-02 15:21:16 +0200521 case LYS_RPC:
Michal Vaskoafa7a642016-10-18 15:11:38 +0200522 case LYS_ACTION:
Radek Krejcifb54be42015-10-02 15:21:16 +0200523 case LYS_NOTIF:
Radek Krejci27aaa732015-09-04 15:24:04 +0200524 case LYS_CONTAINER:
Radek Krejcic90c6512018-02-14 15:21:19 +0100525 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200526 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100527 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200528 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200529 ret = json_print_container(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200530 break;
531 case LYS_LEAF:
Radek Krejcic90c6512018-02-14 15:21:19 +0100532 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200533 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100534 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200535 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200536 ret = json_print_leaf(out, level, node, 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200537 break;
538 case LYS_LEAFLIST:
539 case LYS_LIST:
Alexandre Snarskiice16bcf2018-09-12 12:10:07 +0300540 /* is it already printed? (root node is not) */
541 for (iter = node->prev; iter->next && node != root; iter = iter->prev) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200542 if (iter == node) {
543 continue;
544 }
545 if (iter->schema == node->schema) {
546 /* the list has alread some previous instance and therefore it is already printed */
547 break;
548 }
549 }
Alexandre Snarskiice16bcf2018-09-12 12:10:07 +0300550 if (!iter->next || node == root) {
Radek Krejcic90c6512018-02-14 15:21:19 +0100551 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200552 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100553 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200554 }
555
556 /* print the list/leaflist */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200557 ret = json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200558 }
559 break;
560 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200561 case LYS_ANYDATA:
Radek Krejcic90c6512018-02-14 15:21:19 +0100562 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200563 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100564 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200565 }
Michal Vasko9696e4d2018-05-28 08:28:31 +0200566 ret = json_print_anydataxml(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200567 break;
568 default:
Michal Vasko53b7da02018-02-13 15:28:42 +0100569 LOGINT(node->schema->module->ctx);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200570 ret = EXIT_FAILURE;
Radek Krejci27aaa732015-09-04 15:24:04 +0200571 break;
572 }
Radek Krejci5044be32016-01-18 17:05:51 +0100573
574 if (!withsiblings) {
575 break;
576 }
Radek Krejcic90c6512018-02-14 15:21:19 +0100577 comma_flag = 1;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200578 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200579 if (root && level) {
Michal Vasko95068c42016-03-24 14:58:11 +0100580 ly_print(out, "\n");
581 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200582
583 return ret;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200584}
585
Radek Krejcif3752b02015-10-02 15:31:34 +0200586int
Radek Krejci5044be32016-01-18 17:05:51 +0100587json_print_data(struct lyout *out, const struct lyd_node *root, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200588{
Michal Vaskoafa7a642016-10-18 15:11:38 +0200589 const struct lyd_node *node, *next;
590 int level = 0, action_input = 0;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200591
Michal Vasko95068c42016-03-24 14:58:11 +0100592 if (options & LYP_FORMAT) {
593 ++level;
594 }
595
Michal Vaskoafa7a642016-10-18 15:11:38 +0200596 if (options & LYP_NETCONF) {
597 if (root->schema->nodetype != LYS_RPC) {
598 /* learn whether we are printing an action */
599 LY_TREE_DFS_BEGIN(root, next, node) {
600 if (node->schema->nodetype == LYS_ACTION) {
601 break;
602 }
603 LY_TREE_DFS_END(root, next, node);
604 }
605 } else {
606 node = root;
607 }
608
609 if (node && (node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
610 if (node->child && (node->child->schema->parent->nodetype == LYS_OUTPUT)) {
611 /* skip the container */
612 root = node->child;
613 } else if (node->schema->nodetype == LYS_ACTION) {
614 action_input = 1;
615 }
616 }
617 }
618
Radek Krejci94ca54b2015-07-08 15:48:47 +0200619 /* start */
Michal Vasko95068c42016-03-24 14:58:11 +0100620 ly_print(out, "{%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200621
Michal Vaskoafa7a642016-10-18 15:11:38 +0200622 if (action_input) {
623 ly_print(out, "%*s\"yang:action\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
624 if (level) {
625 ++level;
626 }
627 }
628
Radek Krejci94ca54b2015-07-08 15:48:47 +0200629 /* content */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200630 if (json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options)) {
631 return EXIT_FAILURE;
632 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200633
Michal Vaskoafa7a642016-10-18 15:11:38 +0200634 if (action_input) {
635 if (level) {
636 --level;
637 }
638 ly_print(out, "%*s}%s", LEVEL, INDENT, (level ? "\n" : ""));
639 }
640
Radek Krejci94ca54b2015-07-08 15:48:47 +0200641 /* end */
Michal Vasko95068c42016-03-24 14:58:11 +0100642 ly_print(out, "}%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200643
Michal Vaskoafa7a642016-10-18 15:11:38 +0200644 ly_print_flush(out);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200645 return EXIT_SUCCESS;
646}