blob: 5514c0ceaed80ed3c5d14ffb3a7df3e6f61e3857 [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 Krejci382e7f92016-01-12 17:15:47 +010033static int
34json_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++) {
zyinter200856c0f132016-11-17 10:59:23 +080044 if (text[i] >= 0 && text[i] < 0x20) {
Radek Krejci382e7f92016-01-12 17:15:47 +010045 /* control character */
46 n += ly_print(out, "\\u%.4X");
47 } else {
48 switch (text[i]) {
49 case '"':
50 n += ly_print(out, "\\\"");
51 break;
52 case '\\':
53 n += ly_print(out, "\\\\");
54 break;
55 default:
56 ly_write(out, &text[i], 1);
57 n++;
58 }
59 }
60 }
61 ly_write(out, "\"", 1);
62
63 return n + 2;
64}
65
Michal Vaskob3c31bd2017-07-17 10:01:48 +020066static int
Radek Krejci1eefeb32016-04-15 16:01:46 +020067json_print_attrs(struct lyout *out, int level, const struct lyd_node *node, const struct lys_module *wdmod)
Radek Krejcida61fb22015-10-30 11:10:03 +010068{
69 struct lyd_attr *attr;
Radek Krejcia571d942017-02-24 09:26:49 +010070 size_t len;
71 char *p;
Radek Krejcida61fb22015-10-30 11:10:03 +010072
Radek Krejci1eefeb32016-04-15 16:01:46 +020073 if (wdmod) {
74 ly_print(out, "%*s\"%s:default\":\"true\"", LEVEL, INDENT, wdmod->name);
75 ly_print(out, "%s%s", node->attr ? "," : "", (level ? "\n" : ""));
76 }
Radek Krejcida61fb22015-10-30 11:10:03 +010077 for (attr = node->attr; attr; attr = attr->next) {
Radek Krejci532e5e92017-02-22 12:59:24 +010078 if (!attr->annotation) {
79 /* skip exception for the NETCONF's attribute since JSON is not defined for NETCONF */
80 continue;
81 }
Radek Krejcib1ef1872017-02-23 14:33:34 +010082 if (lys_main_module(attr->annotation->module) != lys_main_module(node->schema->module)) {
Radek Krejci532e5e92017-02-22 12:59:24 +010083 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, attr->annotation->module->name, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010084 } else {
Radek Krejci382e7f92016-01-12 17:15:47 +010085 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010086 }
Radek Krejcia571d942017-02-24 09:26:49 +010087 /* leafref is not supported */
Michal Vasko70bf8e52018-03-26 11:32:33 +020088 switch (attr->value_type) {
Radek Krejcia571d942017-02-24 09:26:49 +010089 case LY_TYPE_BINARY:
90 case LY_TYPE_STRING:
91 case LY_TYPE_BITS:
92 case LY_TYPE_ENUM:
93 case LY_TYPE_INST:
94 case LY_TYPE_INT64:
95 case LY_TYPE_UINT64:
96 case LY_TYPE_DEC64:
Michal Vaskoa80d8302017-03-02 10:52:02 +010097 json_print_string(out, attr->value_str);
Radek Krejcia571d942017-02-24 09:26:49 +010098 break;
99
100 case LY_TYPE_INT8:
101 case LY_TYPE_INT16:
102 case LY_TYPE_INT32:
103 case LY_TYPE_UINT8:
104 case LY_TYPE_UINT16:
105 case LY_TYPE_UINT32:
106 case LY_TYPE_BOOL:
107 ly_print(out, "%s", attr->value_str[0] ? attr->value_str : "null");
108 break;
109
110 case LY_TYPE_IDENT:
111 p = strchr(attr->value_str, ':');
112 assert(p);
113 len = p - attr->value_str;
114 if (!strncmp(attr->value_str, attr->annotation->module->name, len)
115 && !attr->annotation->module->name[len]) {
116 /* do not print the prefix, it is the default prefix for this node */
117 json_print_string(out, ++p);
118 } else {
119 json_print_string(out, attr->value_str);
120 }
121 break;
122
123 case LY_TYPE_EMPTY:
124 ly_print(out, "[null]");
125 break;
126
127 default:
128 /* error */
129 ly_print(out, "\"(!error!)\"");
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200130 return EXIT_FAILURE;
Radek Krejcia571d942017-02-24 09:26:49 +0100131 }
132
Michal Vasko95068c42016-03-24 14:58:11 +0100133 ly_print(out, "%s%s", attr->next ? "," : "", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100134 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200135
136 return EXIT_SUCCESS;
Radek Krejcida61fb22015-10-30 11:10:03 +0100137}
138
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200139static int
Radek Krejci46180b52016-08-31 16:01:32 +0200140json_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 +0200141{
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100142 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node, *iter;
Michal Vasko0bff3222017-01-05 10:55:31 +0100143 const struct lys_type *type;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100144 const char *schema = NULL, *p, *mod_name;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200145 const struct lys_module *wdmod = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200146 LY_DATA_TYPE datatype;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100147 size_t len;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200148
Radek Krejci46180b52016-08-31 16:01:32 +0200149 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
150 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default(leaf))) {
151 /* we have implicit OR explicit default node */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200152 /* get with-defaults module */
Radek Krejcidfb00d62017-09-06 09:39:35 +0200153 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1);
Radek Krejci1eefeb32016-04-15 16:01:46 +0200154 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200155
Radek Krejci5a988152015-07-15 11:16:26 +0200156 if (!onlyvalue) {
Michal Vasko635bd452016-05-18 13:26:55 +0200157 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejci5a988152015-07-15 11:16:26 +0200158 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100159 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100160 ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200161 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100162 ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200163 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200164 }
Radek Krejcie4748472015-07-08 18:00:22 +0200165
Michal Vasko70bf8e52018-03-26 11:32:33 +0200166 datatype = leaf->value_type;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200167contentprint:
168 switch (datatype) {
Radek Krejcie4748472015-07-08 18:00:22 +0200169 case LY_TYPE_BINARY:
170 case LY_TYPE_STRING:
Radek Krejci3e3affe2015-07-09 15:38:40 +0200171 case LY_TYPE_BITS:
Radek Krejci5b315a92015-07-10 13:18:45 +0200172 case LY_TYPE_ENUM:
Radek Krejcic5090c32015-08-12 09:46:19 +0200173 case LY_TYPE_INST:
Radek Krejcic27114c2016-09-20 10:02:28 +0200174 case LY_TYPE_INT64:
175 case LY_TYPE_UINT64:
Michal Vasko3db69682018-03-27 16:11:31 +0200176 case LY_TYPE_UNION:
严军喜100794891758ff62016-10-25 10:14:47 +0800177 case LY_TYPE_DEC64:
Michal Vasko44913842016-04-13 14:20:41 +0200178 json_print_string(out, leaf->value_str);
Radek Krejciac8aac62015-07-10 15:36:35 +0200179 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200180
Radek Krejcib1c12512015-08-11 11:22:04 +0200181 case LY_TYPE_INT8:
182 case LY_TYPE_INT16:
183 case LY_TYPE_INT32:
Radek Krejcib1c12512015-08-11 11:22:04 +0200184 case LY_TYPE_UINT8:
185 case LY_TYPE_UINT16:
186 case LY_TYPE_UINT32:
严军喜100794891758ff62016-10-25 10:14:47 +0800187 case LY_TYPE_BOOL:
Michal Vasko44913842016-04-13 14:20:41 +0200188 ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null");
Radek Krejcib1c12512015-08-11 11:22:04 +0200189 break;
190
Radek Krejci7a2a5622016-12-05 13:32:05 +0100191 case LY_TYPE_IDENT:
192 p = strchr(leaf->value_str, ':');
193 assert(p);
194 len = p - leaf->value_str;
195 mod_name = leaf->schema->module->name;
196 if (!strncmp(leaf->value_str, mod_name, len) && !mod_name[len]) {
197 /* do not print the prefix, it is the default prefix for this node */
198 json_print_string(out, ++p);
199 } else {
200 json_print_string(out, leaf->value_str);
201 }
202 break;
203
Radek Krejci9b6aad22016-09-20 15:55:51 +0200204 case LY_TYPE_LEAFREF:
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100205 iter = (struct lyd_node_leaf_list *)leaf->value.leafref;
206 while (iter && (iter->value_type == LY_TYPE_LEAFREF)) {
207 iter = (struct lyd_node_leaf_list *)iter->value.leafref;
208 }
209 if (!iter) {
Michal Vasko0bff3222017-01-05 10:55:31 +0100210 /* unresolved and invalid, but we can learn the correct type anyway */
211 type = lyd_leaf_type((struct lyd_node_leaf_list *)leaf);
212 if (!type) {
213 /* error */
214 ly_print(out, "\"(!error!)\"");
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200215 return EXIT_FAILURE;
Michal Vasko0bff3222017-01-05 10:55:31 +0100216 }
217 datatype = type->base;
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100218 } else {
Michal Vasko70bf8e52018-03-26 11:32:33 +0200219 datatype = iter->value_type;
Radek Krejci9ad23f42016-10-31 15:46:52 +0100220 }
Michal Vasko0bff3222017-01-05 10:55:31 +0100221 goto contentprint;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200222
Radek Krejcib1c12512015-08-11 11:22:04 +0200223 case LY_TYPE_EMPTY:
Radek Krejci76b07902015-10-09 09:11:25 +0200224 ly_print(out, "[null]");
Michal Vasko58110162015-07-15 15:50:16 +0200225 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200226
Radek Krejcie4748472015-07-08 18:00:22 +0200227 default:
Michal Vasko493bea72015-07-16 16:08:12 +0200228 /* error */
Radek Krejci76b07902015-10-09 09:11:25 +0200229 ly_print(out, "\"(!error!)\"");
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200230 return EXIT_FAILURE;
Radek Krejcie4748472015-07-08 18:00:22 +0200231 }
232
Radek Krejci382e7f92016-01-12 17:15:47 +0100233 /* print attributes as sibling leafs */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200234 if (!onlyvalue && (node->attr || wdmod)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100235 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100236 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
237 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100238 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100239 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
240 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100241 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200242 if (json_print_attrs(out, (level ? level + 1 : level), node, wdmod)) {
243 return EXIT_FAILURE;
244 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100245 ly_print(out, "%*s}", LEVEL, INDENT);
246 }
247
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200248 return EXIT_SUCCESS;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200249}
250
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200251static int
Radek Krejci46180b52016-08-31 16:01:32 +0200252json_print_container(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vasko98763c62015-07-17 13:47:00 +0200253{
254 const char *schema;
Michal Vasko98763c62015-07-17 13:47:00 +0200255
Michal Vasko635bd452016-05-18 13:26:55 +0200256 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200257 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100258 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100259 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200260 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100261 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200262 }
Michal Vasko95068c42016-03-24 14:58:11 +0100263 if (level) {
264 level++;
265 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100266 if (node->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100267 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200268 if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) {
269 return EXIT_FAILURE;
270 }
Michal Vasko95068c42016-03-24 14:58:11 +0100271 ly_print(out, "%*s}", LEVEL, INDENT);
272 if (node->child) {
273 ly_print(out, ",%s", (level ? "\n" : ""));
274 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100275 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200276 if (json_print_nodes(out, level, node->child, 1, 0, options)) {
277 return EXIT_FAILURE;
278 }
Michal Vasko95068c42016-03-24 14:58:11 +0100279 if (level) {
280 level--;
281 }
Radek Krejci76b07902015-10-09 09:11:25 +0200282 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200283
284 return EXIT_SUCCESS;
Michal Vasko98763c62015-07-17 13:47:00 +0200285}
286
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200287static int
Radek Krejci46180b52016-08-31 16:01:32 +0200288json_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 +0200289{
Radek Krejcida61fb22015-10-30 11:10:03 +0100290 const char *schema = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100291 const struct lyd_node *list = node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100292 int flag_empty = 0, flag_attrs = 0;
Radek Krejci23238922015-10-27 17:13:34 +0100293
Radek Krejcie8775162016-09-14 13:08:58 +0200294 if (is_list && !list->child) {
Radek Krejci23238922015-10-27 17:13:34 +0100295 /* empty, e.g. in case of filter */
296 flag_empty = 1;
297 }
Michal Vasko98763c62015-07-17 13:47:00 +0200298
Michal Vasko635bd452016-05-18 13:26:55 +0200299 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200300 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100301 schema = lys_node_module(node->schema)->name;
Radek Krejci23238922015-10-27 17:13:34 +0100302 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200303 } else {
Radek Krejci23238922015-10-27 17:13:34 +0100304 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200305 }
306
Radek Krejci23238922015-10-27 17:13:34 +0100307 if (flag_empty) {
Michal Vasko95068c42016-03-24 14:58:11 +0100308 ly_print(out, "%snull", (level ? " " : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200309 return EXIT_SUCCESS;
Radek Krejci23238922015-10-27 17:13:34 +0100310 }
Michal Vasko95068c42016-03-24 14:58:11 +0100311 ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100312
Michal Vasko95068c42016-03-24 14:58:11 +0100313 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200314 ++level;
315 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200316
317 while (list) {
Michal Vasko98763c62015-07-17 13:47:00 +0200318 if (is_list) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200319 /* list print */
Michal Vasko95068c42016-03-24 14:58:11 +0100320 if (level) {
321 ++level;
322 }
323 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : ""));
324 if (level) {
325 ++level;
326 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100327 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100328 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200329 if (json_print_attrs(out, (level ? level + 1 : level), list, NULL)) {
330 return EXIT_FAILURE;
331 }
Michal Vaskof6aa8612017-03-02 10:52:44 +0100332 if (list->child) {
333 ly_print(out, "%*s},%s", LEVEL, INDENT, (level ? "\n" : ""));
334 } else {
335 ly_print(out, "%*s}", LEVEL, INDENT);
336 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100337 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200338 if (json_print_nodes(out, level, list->child, 1, 0, options)) {
339 return EXIT_FAILURE;
340 }
Michal Vasko95068c42016-03-24 14:58:11 +0100341 if (level) {
342 --level;
343 }
Radek Krejci76b07902015-10-09 09:11:25 +0200344 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko95068c42016-03-24 14:58:11 +0100345 if (level) {
346 --level;
347 }
Michal Vasko98763c62015-07-17 13:47:00 +0200348 } else {
Radek Krejci27aaa732015-09-04 15:24:04 +0200349 /* leaf-list print */
Radek Krejci76b07902015-10-09 09:11:25 +0200350 ly_print(out, "%*s", LEVEL, INDENT);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200351 if (json_print_leaf(out, level, list, 1, toplevel, options)) {
352 return EXIT_FAILURE;
353 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100354 if (list->attr) {
355 flag_attrs = 1;
356 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200357 }
358 for (list = list->next; list && list->schema != node->schema; list = list->next);
359 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100360 ly_print(out, ",%s", (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200361 }
362 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200363
Michal Vasko95068c42016-03-24 14:58:11 +0100364 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200365 --level;
366 }
367
Michal Vasko95068c42016-03-24 14:58:11 +0100368 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100369
370 /* attributes */
371 if (!is_list && flag_attrs) {
372 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100373 ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
374 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100375 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100376 ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
377 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100378 }
Michal Vasko95068c42016-03-24 14:58:11 +0100379 if (level) {
380 level++;
381 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100382 for (list = node; list; ) {
383 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100384 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : ""));
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200385 if (json_print_attrs(out, 0, list, NULL)) {
386 return EXIT_FAILURE;
387 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100388 ly_print(out, "%*s}", LEVEL, INDENT);
389 } else {
390 ly_print(out, "%*snull", LEVEL, INDENT);
391 }
392
393
394 for (list = list->next; list && list->schema != node->schema; list = list->next);
395 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100396 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100397 }
398 }
Michal Vasko95068c42016-03-24 14:58:11 +0100399 if (level) {
400 level--;
401 }
402 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100403 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200404
405 return EXIT_SUCCESS;
Michal Vasko98763c62015-07-17 13:47:00 +0200406}
407
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200408static int
Michal Vasko9696e4d2018-05-28 08:28:31 +0200409json_print_anydataxml(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Radek Krejcia7177672016-11-17 14:53:03 +0900410{
411 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
Michal Vasko9696e4d2018-05-28 08:28:31 +0200412 int is_object = 0;
Radek Krejcia7177672016-11-17 14:53:03 +0900413 char *buf;
414 const char *schema = NULL;
415
416 if (toplevel || !node->parent || nscmp(node, node->parent)) {
417 /* print "namespace" */
418 schema = lys_node_module(node->schema)->name;
Michal Vasko9696e4d2018-05-28 08:28:31 +0200419 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Radek Krejcia7177672016-11-17 14:53:03 +0900420 } else {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200421 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Radek Krejcia7177672016-11-17 14:53:03 +0900422 }
423 if (level) {
424 level++;
425 }
426
427 switch (any->value_type) {
428 case LYD_ANYDATA_DATATREE:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200429 is_object = 1;
430 ly_print(out, "%s{%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcidb2f58c2016-11-24 11:24:48 +0100431 /* do not print any default values nor empty containers */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200432 if (json_print_nodes(out, level, any->value.tree, 1, 0, LYP_WITHSIBLINGS | (options & ~LYP_NETCONF))) {
433 return EXIT_FAILURE;
434 }
Radek Krejcia7177672016-11-17 14:53:03 +0900435 break;
436 case LYD_ANYDATA_JSON:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200437 if (level) {
438 ly_print(out, "\n");
439 }
Radek Krejcia7177672016-11-17 14:53:03 +0900440 if (any->value.str) {
Michal Vasko9696e4d2018-05-28 08:28:31 +0200441 ly_print(out, "%s", any->value.str);
442 }
443 if (level && (any->value.str[strlen(any->value.str) - 1] != '\n')) {
444 /* do not print 2 newlines */
445 ly_print(out, "\n");
Radek Krejcia7177672016-11-17 14:53:03 +0900446 }
447 break;
448 case LYD_ANYDATA_XML:
449 lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0)
450 | LYXML_PRINT_SIBLINGS);
Michal Vasko9696e4d2018-05-28 08:28:31 +0200451 if (level) {
452 ly_print(out, " ");
453 }
Radek Krejcia7177672016-11-17 14:53:03 +0900454 json_print_string(out, buf);
455 free(buf);
456 break;
457 case LYD_ANYDATA_CONSTSTRING:
458 case LYD_ANYDATA_SXML:
Michal Vasko9696e4d2018-05-28 08:28:31 +0200459 if (level) {
460 ly_print(out, " ");
461 }
Radek Krejcia7177672016-11-17 14:53:03 +0900462 if (any->value.str) {
463 json_print_string(out, any->value.str);
464 } else {
465 ly_print(out, "\"\"");
466 }
467 break;
Michal Vaskodcaf7222018-08-08 16:27:00 +0200468 case LYD_ANYDATA_STRING:
469 case LYD_ANYDATA_SXMLD:
470 case LYD_ANYDATA_JSOND:
471 case LYD_ANYDATA_LYBD:
472 case LYD_ANYDATA_LYB:
Radek Krejcia7177672016-11-17 14:53:03 +0900473 /* other formats are not supported */
Michal Vasko9696e4d2018-05-28 08:28:31 +0200474 json_print_string(out, "(error)");
Radek Krejci4ae82942016-09-19 16:41:06 +0200475 break;
Radek Krejcida61fb22015-10-30 11:10:03 +0100476 }
477
478 /* print attributes as sibling leaf */
479 if (node->attr) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100480 if (schema) {
Michal Vaskof6aa8612017-03-02 10:52:44 +0100481 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
482 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100483 } else {
Michal Vaskof6aa8612017-03-02 10:52:44 +0100484 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
485 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100486 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200487 if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) {
488 return EXIT_FAILURE;
489 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100490 ly_print(out, "%*s}", LEVEL, INDENT);
491 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200492
Radek Krejci4ae82942016-09-19 16:41:06 +0200493 if (level) {
494 level--;
495 }
Michal Vasko9696e4d2018-05-28 08:28:31 +0200496 if (is_object) {
497 ly_print(out, "%*s}", LEVEL, INDENT);
498 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200499
500 return EXIT_SUCCESS;
Michal Vaskoab8e4402015-07-17 12:54:28 +0200501}
502
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200503static int
Radek Krejci46180b52016-08-31 16:01:32 +0200504json_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 +0200505{
Radek Krejcic90c6512018-02-14 15:21:19 +0100506 int ret = EXIT_SUCCESS,comma_flag = 0;
Radek Krejci572f1222016-09-01 09:52:47 +0200507 const struct lyd_node *node, *iter;
Radek Krejci27aaa732015-09-04 15:24:04 +0200508
509 LY_TREE_FOR(root, node) {
Radek Krejci572f1222016-09-01 09:52:47 +0200510 if (!lyd_wd_toprint(node, options)) {
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200511 /* wd says do not print */
Radek Krejci572f1222016-09-01 09:52:47 +0200512 continue;
Radek Krejci46180b52016-08-31 16:01:32 +0200513 }
514
Radek Krejci27aaa732015-09-04 15:24:04 +0200515 switch (node->schema->nodetype) {
Radek Krejcifb54be42015-10-02 15:21:16 +0200516 case LYS_RPC:
Michal Vaskoafa7a642016-10-18 15:11:38 +0200517 case LYS_ACTION:
Radek Krejcifb54be42015-10-02 15:21:16 +0200518 case LYS_NOTIF:
Radek Krejci27aaa732015-09-04 15:24:04 +0200519 case LYS_CONTAINER:
Radek Krejcic90c6512018-02-14 15:21:19 +0100520 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200521 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100522 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200523 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200524 ret = json_print_container(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200525 break;
526 case LYS_LEAF:
Radek Krejcic90c6512018-02-14 15:21:19 +0100527 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200528 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100529 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200530 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200531 ret = json_print_leaf(out, level, node, 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200532 break;
533 case LYS_LEAFLIST:
534 case LYS_LIST:
535 /* is it already printed? */
Radek Krejci9ce61512015-10-26 14:42:32 +0100536 for (iter = node->prev; iter->next; iter = iter->prev) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200537 if (iter == node) {
538 continue;
539 }
540 if (iter->schema == node->schema) {
541 /* the list has alread some previous instance and therefore it is already printed */
542 break;
543 }
544 }
Michal Vasko6c563772015-10-15 10:49:30 +0200545 if (!iter->next) {
Radek Krejcic90c6512018-02-14 15:21:19 +0100546 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200547 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100548 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200549 }
550
551 /* print the list/leaflist */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200552 ret = json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200553 }
554 break;
555 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200556 case LYS_ANYDATA:
Radek Krejcic90c6512018-02-14 15:21:19 +0100557 if (comma_flag) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200558 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100559 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200560 }
Michal Vasko9696e4d2018-05-28 08:28:31 +0200561 ret = json_print_anydataxml(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200562 break;
563 default:
Michal Vasko53b7da02018-02-13 15:28:42 +0100564 LOGINT(node->schema->module->ctx);
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200565 ret = EXIT_FAILURE;
Radek Krejci27aaa732015-09-04 15:24:04 +0200566 break;
567 }
Radek Krejci5044be32016-01-18 17:05:51 +0100568
569 if (!withsiblings) {
570 break;
571 }
Radek Krejcic90c6512018-02-14 15:21:19 +0100572 comma_flag = 1;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200573 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200574 if (root && level) {
Michal Vasko95068c42016-03-24 14:58:11 +0100575 ly_print(out, "\n");
576 }
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200577
578 return ret;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200579}
580
Radek Krejcif3752b02015-10-02 15:31:34 +0200581int
Radek Krejci5044be32016-01-18 17:05:51 +0100582json_print_data(struct lyout *out, const struct lyd_node *root, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200583{
Michal Vaskoafa7a642016-10-18 15:11:38 +0200584 const struct lyd_node *node, *next;
585 int level = 0, action_input = 0;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200586
Michal Vasko95068c42016-03-24 14:58:11 +0100587 if (options & LYP_FORMAT) {
588 ++level;
589 }
590
Michal Vaskoafa7a642016-10-18 15:11:38 +0200591 if (options & LYP_NETCONF) {
592 if (root->schema->nodetype != LYS_RPC) {
593 /* learn whether we are printing an action */
594 LY_TREE_DFS_BEGIN(root, next, node) {
595 if (node->schema->nodetype == LYS_ACTION) {
596 break;
597 }
598 LY_TREE_DFS_END(root, next, node);
599 }
600 } else {
601 node = root;
602 }
603
604 if (node && (node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
605 if (node->child && (node->child->schema->parent->nodetype == LYS_OUTPUT)) {
606 /* skip the container */
607 root = node->child;
608 } else if (node->schema->nodetype == LYS_ACTION) {
609 action_input = 1;
610 }
611 }
612 }
613
Radek Krejci94ca54b2015-07-08 15:48:47 +0200614 /* start */
Michal Vasko95068c42016-03-24 14:58:11 +0100615 ly_print(out, "{%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200616
Michal Vaskoafa7a642016-10-18 15:11:38 +0200617 if (action_input) {
618 ly_print(out, "%*s\"yang:action\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
619 if (level) {
620 ++level;
621 }
622 }
623
Radek Krejci94ca54b2015-07-08 15:48:47 +0200624 /* content */
Michal Vaskob3c31bd2017-07-17 10:01:48 +0200625 if (json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options)) {
626 return EXIT_FAILURE;
627 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200628
Michal Vaskoafa7a642016-10-18 15:11:38 +0200629 if (action_input) {
630 if (level) {
631 --level;
632 }
633 ly_print(out, "%*s}%s", LEVEL, INDENT, (level ? "\n" : ""));
634 }
635
Radek Krejci94ca54b2015-07-08 15:48:47 +0200636 /* end */
Michal Vasko95068c42016-03-24 14:58:11 +0100637 ly_print(out, "}%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200638
Michal Vaskoafa7a642016-10-18 15:11:38 +0200639 ly_print_flush(out);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200640 return EXIT_SUCCESS;
641}