blob: d3d8e8028abb9b5d416b1b00fe87c9861a2f42f4 [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
Radek Krejci46180b52016-08-31 16:01:32 +020030static void 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
Radek Krejci94ca54b2015-07-08 15:48:47 +020066static void
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 */
88 switch (attr->value_type & LY_DATA_TYPE_MASK) {
89 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!)\"");
130 }
131
Michal Vasko95068c42016-03-24 14:58:11 +0100132 ly_print(out, "%s%s", attr->next ? "," : "", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100133 }
134}
135
136static void
Radek Krejci46180b52016-08-31 16:01:32 +0200137json_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 +0200138{
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100139 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node, *iter;
Michal Vasko0bff3222017-01-05 10:55:31 +0100140 const struct lys_type *type;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100141 const char *schema = NULL, *p, *mod_name;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200142 const struct lys_module *wdmod = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200143 LY_DATA_TYPE datatype;
Radek Krejci7a2a5622016-12-05 13:32:05 +0100144 size_t len;
Radek Krejci1eefeb32016-04-15 16:01:46 +0200145
Radek Krejci46180b52016-08-31 16:01:32 +0200146 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
147 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default(leaf))) {
148 /* we have implicit OR explicit default node */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200149 /* get with-defaults module */
150 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL);
151 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200152
Radek Krejci5a988152015-07-15 11:16:26 +0200153 if (!onlyvalue) {
Michal Vasko635bd452016-05-18 13:26:55 +0200154 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejci5a988152015-07-15 11:16:26 +0200155 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100156 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100157 ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200158 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100159 ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200160 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200161 }
Radek Krejcie4748472015-07-08 18:00:22 +0200162
Radek Krejci9b6aad22016-09-20 15:55:51 +0200163 datatype = leaf->value_type & LY_DATA_TYPE_MASK;
164contentprint:
165 switch (datatype) {
Radek Krejcie4748472015-07-08 18:00:22 +0200166 case LY_TYPE_BINARY:
167 case LY_TYPE_STRING:
Radek Krejci3e3affe2015-07-09 15:38:40 +0200168 case LY_TYPE_BITS:
Radek Krejci5b315a92015-07-10 13:18:45 +0200169 case LY_TYPE_ENUM:
Radek Krejcic5090c32015-08-12 09:46:19 +0200170 case LY_TYPE_INST:
Radek Krejcic27114c2016-09-20 10:02:28 +0200171 case LY_TYPE_INT64:
172 case LY_TYPE_UINT64:
严军喜100794891758ff62016-10-25 10:14:47 +0800173 case LY_TYPE_DEC64:
Michal Vasko44913842016-04-13 14:20:41 +0200174 json_print_string(out, leaf->value_str);
Radek Krejciac8aac62015-07-10 15:36:35 +0200175 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200176
Radek Krejcib1c12512015-08-11 11:22:04 +0200177 case LY_TYPE_INT8:
178 case LY_TYPE_INT16:
179 case LY_TYPE_INT32:
Radek Krejcib1c12512015-08-11 11:22:04 +0200180 case LY_TYPE_UINT8:
181 case LY_TYPE_UINT16:
182 case LY_TYPE_UINT32:
严军喜100794891758ff62016-10-25 10:14:47 +0800183 case LY_TYPE_BOOL:
Michal Vasko44913842016-04-13 14:20:41 +0200184 ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null");
Radek Krejcib1c12512015-08-11 11:22:04 +0200185 break;
186
Radek Krejci7a2a5622016-12-05 13:32:05 +0100187 case LY_TYPE_IDENT:
188 p = strchr(leaf->value_str, ':');
189 assert(p);
190 len = p - leaf->value_str;
191 mod_name = leaf->schema->module->name;
192 if (!strncmp(leaf->value_str, mod_name, len) && !mod_name[len]) {
193 /* do not print the prefix, it is the default prefix for this node */
194 json_print_string(out, ++p);
195 } else {
196 json_print_string(out, leaf->value_str);
197 }
198 break;
199
Radek Krejci9b6aad22016-09-20 15:55:51 +0200200 case LY_TYPE_LEAFREF:
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100201 iter = (struct lyd_node_leaf_list *)leaf->value.leafref;
202 while (iter && (iter->value_type == LY_TYPE_LEAFREF)) {
203 iter = (struct lyd_node_leaf_list *)iter->value.leafref;
204 }
205 if (!iter) {
Michal Vasko0bff3222017-01-05 10:55:31 +0100206 /* unresolved and invalid, but we can learn the correct type anyway */
207 type = lyd_leaf_type((struct lyd_node_leaf_list *)leaf);
208 if (!type) {
209 /* error */
210 ly_print(out, "\"(!error!)\"");
211 return;
212 }
213 datatype = type->base;
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100214 } else {
215 datatype = iter->value_type & LY_DATA_TYPE_MASK;
Radek Krejci9ad23f42016-10-31 15:46:52 +0100216 }
Michal Vasko0bff3222017-01-05 10:55:31 +0100217 goto contentprint;
Radek Krejci9b6aad22016-09-20 15:55:51 +0200218
Radek Krejcib1c12512015-08-11 11:22:04 +0200219 case LY_TYPE_EMPTY:
Radek Krejci76b07902015-10-09 09:11:25 +0200220 ly_print(out, "[null]");
Michal Vasko58110162015-07-15 15:50:16 +0200221 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200222
Radek Krejcie4748472015-07-08 18:00:22 +0200223 default:
Michal Vasko493bea72015-07-16 16:08:12 +0200224 /* error */
Radek Krejci76b07902015-10-09 09:11:25 +0200225 ly_print(out, "\"(!error!)\"");
Radek Krejcie4748472015-07-08 18:00:22 +0200226 }
227
Radek Krejci382e7f92016-01-12 17:15:47 +0100228 /* print attributes as sibling leafs */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200229 if (!onlyvalue && (node->attr || wdmod)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100230 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100231 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
232 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100233 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100234 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
235 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100236 }
Radek Krejci1eefeb32016-04-15 16:01:46 +0200237 json_print_attrs(out, level + 1, node, wdmod);
Radek Krejcida61fb22015-10-30 11:10:03 +0100238 ly_print(out, "%*s}", LEVEL, INDENT);
239 }
240
Radek Krejcib1c12512015-08-11 11:22:04 +0200241 return;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200242}
243
Michal Vaskoab8e4402015-07-17 12:54:28 +0200244static void
Radek Krejci46180b52016-08-31 16:01:32 +0200245json_print_container(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vasko98763c62015-07-17 13:47:00 +0200246{
247 const char *schema;
Michal Vasko98763c62015-07-17 13:47:00 +0200248
Michal Vasko635bd452016-05-18 13:26:55 +0200249 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200250 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100251 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100252 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200253 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100254 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200255 }
Michal Vasko95068c42016-03-24 14:58:11 +0100256 if (level) {
257 level++;
258 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100259 if (node->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100260 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci1eefeb32016-04-15 16:01:46 +0200261 json_print_attrs(out, (level? level + 1 : level), node, NULL);
Michal Vasko95068c42016-03-24 14:58:11 +0100262 ly_print(out, "%*s}", LEVEL, INDENT);
263 if (node->child) {
264 ly_print(out, ",%s", (level ? "\n" : ""));
265 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100266 }
Radek Krejci46180b52016-08-31 16:01:32 +0200267 json_print_nodes(out, level, node->child, 1, 0, options);
Michal Vasko95068c42016-03-24 14:58:11 +0100268 if (level) {
269 level--;
270 }
Radek Krejci76b07902015-10-09 09:11:25 +0200271 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko98763c62015-07-17 13:47:00 +0200272}
273
274static void
Radek Krejci46180b52016-08-31 16:01:32 +0200275json_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 +0200276{
Radek Krejcida61fb22015-10-30 11:10:03 +0100277 const char *schema = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100278 const struct lyd_node *list = node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100279 int flag_empty = 0, flag_attrs = 0;
Radek Krejci23238922015-10-27 17:13:34 +0100280
Radek Krejcie8775162016-09-14 13:08:58 +0200281 if (is_list && !list->child) {
Radek Krejci23238922015-10-27 17:13:34 +0100282 /* empty, e.g. in case of filter */
283 flag_empty = 1;
284 }
Michal Vasko98763c62015-07-17 13:47:00 +0200285
Michal Vasko635bd452016-05-18 13:26:55 +0200286 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200287 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100288 schema = lys_node_module(node->schema)->name;
Radek Krejci23238922015-10-27 17:13:34 +0100289 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200290 } else {
Radek Krejci23238922015-10-27 17:13:34 +0100291 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200292 }
293
Radek Krejci23238922015-10-27 17:13:34 +0100294 if (flag_empty) {
Michal Vasko95068c42016-03-24 14:58:11 +0100295 ly_print(out, "%snull", (level ? " " : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100296 return;
297 }
Michal Vasko95068c42016-03-24 14:58:11 +0100298 ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100299
Michal Vasko95068c42016-03-24 14:58:11 +0100300 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200301 ++level;
302 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200303
304 while (list) {
Michal Vasko98763c62015-07-17 13:47:00 +0200305 if (is_list) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200306 /* list print */
Michal Vasko95068c42016-03-24 14:58:11 +0100307 if (level) {
308 ++level;
309 }
310 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : ""));
311 if (level) {
312 ++level;
313 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100314 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100315 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
严军喜100794898b539cf2016-10-12 16:34:52 +0800316 json_print_attrs(out, level + 1, list, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100317 ly_print(out, "%*s}%s", LEVEL, INDENT, list->child ? ",\n" : "");
318 }
Radek Krejci46180b52016-08-31 16:01:32 +0200319 json_print_nodes(out, level, list->child, 1, 0, options);
Michal Vasko95068c42016-03-24 14:58:11 +0100320 if (level) {
321 --level;
322 }
Radek Krejci76b07902015-10-09 09:11:25 +0200323 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko95068c42016-03-24 14:58:11 +0100324 if (level) {
325 --level;
326 }
Michal Vasko98763c62015-07-17 13:47:00 +0200327 } else {
Radek Krejci27aaa732015-09-04 15:24:04 +0200328 /* leaf-list print */
Radek Krejci76b07902015-10-09 09:11:25 +0200329 ly_print(out, "%*s", LEVEL, INDENT);
Radek Krejci46180b52016-08-31 16:01:32 +0200330 json_print_leaf(out, level, list, 1, toplevel, options);
Radek Krejcida61fb22015-10-30 11:10:03 +0100331 if (list->attr) {
332 flag_attrs = 1;
333 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200334 }
335 for (list = list->next; list && list->schema != node->schema; list = list->next);
336 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100337 ly_print(out, ",%s", (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200338 }
339 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200340
Michal Vasko95068c42016-03-24 14:58:11 +0100341 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200342 --level;
343 }
344
Michal Vasko95068c42016-03-24 14:58:11 +0100345 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100346
347 /* attributes */
348 if (!is_list && flag_attrs) {
349 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100350 ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
351 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100352 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100353 ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
354 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100355 }
Michal Vasko95068c42016-03-24 14:58:11 +0100356 if (level) {
357 level++;
358 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100359 for (list = node; list; ) {
360 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100361 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : ""));
Radek Krejci1eefeb32016-04-15 16:01:46 +0200362 json_print_attrs(out, 0, list, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100363 ly_print(out, "%*s}", LEVEL, INDENT);
364 } else {
365 ly_print(out, "%*snull", LEVEL, INDENT);
366 }
367
368
369 for (list = list->next; list && list->schema != node->schema; list = list->next);
370 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100371 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100372 }
373 }
Michal Vasko95068c42016-03-24 14:58:11 +0100374 if (level) {
375 level--;
376 }
377 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100378 }
Michal Vasko98763c62015-07-17 13:47:00 +0200379}
380
381static void
Radek Krejcia7177672016-11-17 14:53:03 +0900382json_print_anyxml(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
383{
384 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
385 int isobject = 0;
386 char *buf;
387 const char *schema = NULL;
388
389 if (toplevel || !node->parent || nscmp(node, node->parent)) {
390 /* print "namespace" */
391 schema = lys_node_module(node->schema)->name;
392 ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
393 } else {
394 ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
395 }
396 if (level) {
397 level++;
398 }
399
400 switch (any->value_type) {
401 case LYD_ANYDATA_DATATREE:
402 isobject = 1;
403 ly_print(out, level ? "{\n" : "{");
Radek Krejcidb2f58c2016-11-24 11:24:48 +0100404 /* do not print any default values nor empty containers */
Michal Vasko82762502017-01-17 13:04:06 +0100405 json_print_nodes(out, level, any->value.tree, 1, 0, LYP_WITHSIBLINGS | (options & ~LYP_NETCONF));
Radek Krejcia7177672016-11-17 14:53:03 +0900406 break;
407 case LYD_ANYDATA_JSON:
408 isobject = 1;
409 ly_print(out, level ? "{\n" : "{");
410 if (any->value.str) {
411 ly_print(out, "%*s%s%s", LEVEL, INDENT, any->value.str, level ? "\n" : "");
412 }
413 break;
414 case LYD_ANYDATA_XML:
415 lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0)
416 | LYXML_PRINT_SIBLINGS);
417 json_print_string(out, buf);
418 free(buf);
419 break;
420 case LYD_ANYDATA_CONSTSTRING:
421 case LYD_ANYDATA_SXML:
422 if (any->value.str) {
423 json_print_string(out, any->value.str);
424 } else {
425 ly_print(out, "\"\"");
426 }
427 break;
428 default:
429 /* other formats are not supported */
430 LOGWRN("Unable to print anydata content (type %d) as JSON.", any->value_type);
431 break;
432 }
433
434 if (level) {
435 level--;
436 }
437 if (isobject) {
438 ly_print(out, "%*s}", LEVEL, INDENT);
439 }
440}
441
442static void
Radek Krejci46180b52016-08-31 16:01:32 +0200443json_print_anydata(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vaskoab8e4402015-07-17 12:54:28 +0200444{
Radek Krejcida61fb22015-10-30 11:10:03 +0100445 const char *schema = NULL;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200446 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100447
Michal Vasko635bd452016-05-18 13:26:55 +0200448 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100449 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100450 schema = lys_node_module(node->schema)->name;
Radek Krejci4ae82942016-09-19 16:41:06 +0200451 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100452 } else {
Radek Krejci4ae82942016-09-19 16:41:06 +0200453 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
454 }
455 if (level) {
456 level++;
Radek Krejci9a5daea2016-03-02 16:49:40 +0100457 }
458
Radek Krejci4ae82942016-09-19 16:41:06 +0200459 switch (any->value_type) {
460 case LYD_ANYDATA_DATATREE:
Radek Krejcidb2f58c2016-11-24 11:24:48 +0100461 /* do not print any default values nor empty containers */
462 json_print_nodes(out, level, any->value.tree, 1, 0, LYP_WITHSIBLINGS | (options & LYP_FORMAT));
Radek Krejci4ae82942016-09-19 16:41:06 +0200463 break;
464 case LYD_ANYDATA_JSON:
465 if (any->value.str) {
466 ly_print(out, "%*s%s\n", LEVEL, INDENT, any->value.str);
Radek Krejci9a5daea2016-03-02 16:49:40 +0100467 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200468 break;
469 default:
470 /* other formats are not supported */
471 LOGWRN("Unable to print anydata content (type %d) as JSON.", any->value_type);
472 break;
Radek Krejcida61fb22015-10-30 11:10:03 +0100473 }
474
475 /* print attributes as sibling leaf */
476 if (node->attr) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100477 if (schema) {
Radek Krejci88f29302015-10-30 15:42:33 +0100478 ly_print(out, ",\n%*s\"@%s:%s\": {\n", LEVEL, INDENT, schema, node->schema->name);
Radek Krejcida61fb22015-10-30 11:10:03 +0100479 } else {
Radek Krejci88f29302015-10-30 15:42:33 +0100480 ly_print(out, ",\n%*s\"@%s\": {\n", LEVEL, INDENT, node->schema->name);
Radek Krejcida61fb22015-10-30 11:10:03 +0100481 }
Radek Krejci1eefeb32016-04-15 16:01:46 +0200482 json_print_attrs(out, (level ? level + 1 : level), node, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100483 ly_print(out, "%*s}", LEVEL, INDENT);
484 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200485
486
487 if (level) {
488 level--;
489 }
490 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vaskoab8e4402015-07-17 12:54:28 +0200491}
492
Radek Krejci5044be32016-01-18 17:05:51 +0100493static void
Radek Krejci46180b52016-08-31 16:01:32 +0200494json_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 +0200495{
Radek Krejci572f1222016-09-01 09:52:47 +0200496 const struct lyd_node *node, *iter;
Radek Krejci27aaa732015-09-04 15:24:04 +0200497
498 LY_TREE_FOR(root, node) {
Radek Krejci572f1222016-09-01 09:52:47 +0200499 if (!lyd_wd_toprint(node, options)) {
500 continue;
Radek Krejci46180b52016-08-31 16:01:32 +0200501 }
502
Radek Krejci27aaa732015-09-04 15:24:04 +0200503 switch (node->schema->nodetype) {
Radek Krejcifb54be42015-10-02 15:21:16 +0200504 case LYS_RPC:
Michal Vaskoafa7a642016-10-18 15:11:38 +0200505 case LYS_ACTION:
Radek Krejcifb54be42015-10-02 15:21:16 +0200506 case LYS_NOTIF:
Radek Krejci27aaa732015-09-04 15:24:04 +0200507 case LYS_CONTAINER:
508 if (node->prev->next) {
509 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100510 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200511 }
Radek Krejci46180b52016-08-31 16:01:32 +0200512 json_print_container(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200513 break;
514 case LYS_LEAF:
515 if (node->prev->next) {
516 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100517 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200518 }
Radek Krejci46180b52016-08-31 16:01:32 +0200519 json_print_leaf(out, level, node, 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200520 break;
521 case LYS_LEAFLIST:
522 case LYS_LIST:
523 /* is it already printed? */
Radek Krejci9ce61512015-10-26 14:42:32 +0100524 for (iter = node->prev; iter->next; iter = iter->prev) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200525 if (iter == node) {
526 continue;
527 }
528 if (iter->schema == node->schema) {
529 /* the list has alread some previous instance and therefore it is already printed */
530 break;
531 }
532 }
Michal Vasko6c563772015-10-15 10:49:30 +0200533 if (!iter->next) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200534 if (node->prev->next) {
535 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100536 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200537 }
538
539 /* print the list/leaflist */
Radek Krejci46180b52016-08-31 16:01:32 +0200540 json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200541 }
542 break;
543 case LYS_ANYXML:
Radek Krejcia7177672016-11-17 14:53:03 +0900544 if (node->prev->next) {
545 /* print the previous comma */
546 ly_print(out, ",%s", (level ? "\n" : ""));
547 }
548 json_print_anyxml(out, level, node, toplevel, options);
549 break;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200550 case LYS_ANYDATA:
Radek Krejci27aaa732015-09-04 15:24:04 +0200551 if (node->prev->next) {
552 /* 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 }
Radek Krejci46180b52016-08-31 16:01:32 +0200555 json_print_anydata(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200556 break;
557 default:
558 LOGINT;
559 break;
560 }
Radek Krejci5044be32016-01-18 17:05:51 +0100561
562 if (!withsiblings) {
563 break;
564 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200565 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200566 if (root && level) {
Michal Vasko95068c42016-03-24 14:58:11 +0100567 ly_print(out, "\n");
568 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200569}
570
Radek Krejcif3752b02015-10-02 15:31:34 +0200571int
Radek Krejci5044be32016-01-18 17:05:51 +0100572json_print_data(struct lyout *out, const struct lyd_node *root, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200573{
Michal Vaskoafa7a642016-10-18 15:11:38 +0200574 const struct lyd_node *node, *next;
575 int level = 0, action_input = 0;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200576
Michal Vasko95068c42016-03-24 14:58:11 +0100577 if (options & LYP_FORMAT) {
578 ++level;
579 }
580
Michal Vaskoafa7a642016-10-18 15:11:38 +0200581 if (options & LYP_NETCONF) {
582 if (root->schema->nodetype != LYS_RPC) {
583 /* learn whether we are printing an action */
584 LY_TREE_DFS_BEGIN(root, next, node) {
585 if (node->schema->nodetype == LYS_ACTION) {
586 break;
587 }
588 LY_TREE_DFS_END(root, next, node);
589 }
590 } else {
591 node = root;
592 }
593
594 if (node && (node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
595 if (node->child && (node->child->schema->parent->nodetype == LYS_OUTPUT)) {
596 /* skip the container */
597 root = node->child;
598 } else if (node->schema->nodetype == LYS_ACTION) {
599 action_input = 1;
600 }
601 }
602 }
603
Radek Krejci94ca54b2015-07-08 15:48:47 +0200604 /* start */
Michal Vasko95068c42016-03-24 14:58:11 +0100605 ly_print(out, "{%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200606
Michal Vaskoafa7a642016-10-18 15:11:38 +0200607 if (action_input) {
608 ly_print(out, "%*s\"yang:action\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
609 if (level) {
610 ++level;
611 }
612 }
613
Radek Krejci94ca54b2015-07-08 15:48:47 +0200614 /* content */
Radek Krejci46180b52016-08-31 16:01:32 +0200615 json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200616
Michal Vaskoafa7a642016-10-18 15:11:38 +0200617 if (action_input) {
618 if (level) {
619 --level;
620 }
621 ly_print(out, "%*s}%s", LEVEL, INDENT, (level ? "\n" : ""));
622 }
623
Radek Krejci94ca54b2015-07-08 15:48:47 +0200624 /* end */
Michal Vasko95068c42016-03-24 14:58:11 +0100625 ly_print(out, "}%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200626
Michal Vaskoafa7a642016-10-18 15:11:38 +0200627 ly_print_flush(out);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200628 return EXIT_SUCCESS;
629}