blob: 01c87d0f31724107dba25ed8af9eb6c6835b3dba [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++) {
44 if (text[i] < 0x20) {
45 /* 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;
70
Radek Krejci1eefeb32016-04-15 16:01:46 +020071 if (wdmod) {
72 ly_print(out, "%*s\"%s:default\":\"true\"", LEVEL, INDENT, wdmod->name);
73 ly_print(out, "%s%s", node->attr ? "," : "", (level ? "\n" : ""));
74 }
Radek Krejcida61fb22015-10-30 11:10:03 +010075 for (attr = node->attr; attr; attr = attr->next) {
76 if (attr->module != node->schema->module) {
Radek Krejci382e7f92016-01-12 17:15:47 +010077 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, attr->module->name, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010078 } else {
Radek Krejci382e7f92016-01-12 17:15:47 +010079 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, attr->name);
Radek Krejcida61fb22015-10-30 11:10:03 +010080 }
Radek Krejci382e7f92016-01-12 17:15:47 +010081 json_print_string(out, attr->value ? attr->value : "");
Michal Vasko95068c42016-03-24 14:58:11 +010082 ly_print(out, "%s%s", attr->next ? "," : "", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +010083 }
84}
85
86static void
Radek Krejci46180b52016-08-31 16:01:32 +020087json_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 +020088{
Michal Vasko4c183312015-09-25 10:41:47 +020089 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node;
Radek Krejcida61fb22015-10-30 11:10:03 +010090 const char *schema = NULL;
Radek Krejci1eefeb32016-04-15 16:01:46 +020091 const struct lys_module *wdmod = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +020092 LY_DATA_TYPE datatype;
Radek Krejci1eefeb32016-04-15 16:01:46 +020093
Radek Krejci46180b52016-08-31 16:01:32 +020094 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
95 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default(leaf))) {
96 /* we have implicit OR explicit default node */
Radek Krejci1eefeb32016-04-15 16:01:46 +020097 /* get with-defaults module */
98 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL);
99 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200100
Radek Krejci5a988152015-07-15 11:16:26 +0200101 if (!onlyvalue) {
Michal Vasko635bd452016-05-18 13:26:55 +0200102 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejci5a988152015-07-15 11:16:26 +0200103 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100104 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100105 ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200106 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100107 ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200108 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200109 }
Radek Krejcie4748472015-07-08 18:00:22 +0200110
Radek Krejci9b6aad22016-09-20 15:55:51 +0200111 datatype = leaf->value_type & LY_DATA_TYPE_MASK;
112contentprint:
113 switch (datatype) {
Radek Krejcie4748472015-07-08 18:00:22 +0200114 case LY_TYPE_BINARY:
115 case LY_TYPE_STRING:
Radek Krejci3e3affe2015-07-09 15:38:40 +0200116 case LY_TYPE_BITS:
Radek Krejci5b315a92015-07-10 13:18:45 +0200117 case LY_TYPE_ENUM:
Radek Krejciac8aac62015-07-10 15:36:35 +0200118 case LY_TYPE_IDENT:
Radek Krejcic5090c32015-08-12 09:46:19 +0200119 case LY_TYPE_INST:
Radek Krejcic27114c2016-09-20 10:02:28 +0200120 case LY_TYPE_INT64:
121 case LY_TYPE_UINT64:
严军喜100794891758ff62016-10-25 10:14:47 +0800122 case LY_TYPE_DEC64:
Michal Vasko44913842016-04-13 14:20:41 +0200123 json_print_string(out, leaf->value_str);
Radek Krejciac8aac62015-07-10 15:36:35 +0200124 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200125
Radek Krejcib1c12512015-08-11 11:22:04 +0200126 case LY_TYPE_INT8:
127 case LY_TYPE_INT16:
128 case LY_TYPE_INT32:
Radek Krejcib1c12512015-08-11 11:22:04 +0200129 case LY_TYPE_UINT8:
130 case LY_TYPE_UINT16:
131 case LY_TYPE_UINT32:
严军喜100794891758ff62016-10-25 10:14:47 +0800132 case LY_TYPE_BOOL:
Michal Vasko44913842016-04-13 14:20:41 +0200133 ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null");
Radek Krejcib1c12512015-08-11 11:22:04 +0200134 break;
135
Radek Krejci9b6aad22016-09-20 15:55:51 +0200136 case LY_TYPE_LEAFREF:
137 datatype = lyd_leaf_type(leaf);
138 goto contentprint;
139
Radek Krejcib1c12512015-08-11 11:22:04 +0200140 case LY_TYPE_EMPTY:
Radek Krejci76b07902015-10-09 09:11:25 +0200141 ly_print(out, "[null]");
Michal Vasko58110162015-07-15 15:50:16 +0200142 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200143
Radek Krejcie4748472015-07-08 18:00:22 +0200144 default:
Michal Vasko493bea72015-07-16 16:08:12 +0200145 /* error */
Radek Krejci76b07902015-10-09 09:11:25 +0200146 ly_print(out, "\"(!error!)\"");
Radek Krejcie4748472015-07-08 18:00:22 +0200147 }
148
Radek Krejci382e7f92016-01-12 17:15:47 +0100149 /* print attributes as sibling leafs */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200150 if (!onlyvalue && (node->attr || wdmod)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100151 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100152 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
153 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100154 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100155 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
156 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100157 }
Radek Krejci1eefeb32016-04-15 16:01:46 +0200158 json_print_attrs(out, level + 1, node, wdmod);
Radek Krejcida61fb22015-10-30 11:10:03 +0100159 ly_print(out, "%*s}", LEVEL, INDENT);
160 }
161
Radek Krejcib1c12512015-08-11 11:22:04 +0200162 return;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200163}
164
Michal Vaskoab8e4402015-07-17 12:54:28 +0200165static void
Radek Krejci46180b52016-08-31 16:01:32 +0200166json_print_container(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vasko98763c62015-07-17 13:47:00 +0200167{
168 const char *schema;
Michal Vasko98763c62015-07-17 13:47:00 +0200169
Michal Vasko635bd452016-05-18 13:26:55 +0200170 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200171 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100172 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100173 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200174 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100175 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200176 }
Michal Vasko95068c42016-03-24 14:58:11 +0100177 if (level) {
178 level++;
179 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100180 if (node->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100181 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci1eefeb32016-04-15 16:01:46 +0200182 json_print_attrs(out, (level? level + 1 : level), node, NULL);
Michal Vasko95068c42016-03-24 14:58:11 +0100183 ly_print(out, "%*s}", LEVEL, INDENT);
184 if (node->child) {
185 ly_print(out, ",%s", (level ? "\n" : ""));
186 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100187 }
Radek Krejci46180b52016-08-31 16:01:32 +0200188 json_print_nodes(out, level, node->child, 1, 0, options);
Michal Vasko95068c42016-03-24 14:58:11 +0100189 if (level) {
190 level--;
191 }
Radek Krejci76b07902015-10-09 09:11:25 +0200192 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko98763c62015-07-17 13:47:00 +0200193}
194
195static void
Radek Krejci46180b52016-08-31 16:01:32 +0200196json_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 +0200197{
Radek Krejcida61fb22015-10-30 11:10:03 +0100198 const char *schema = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100199 const struct lyd_node *list = node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100200 int flag_empty = 0, flag_attrs = 0;
Radek Krejci23238922015-10-27 17:13:34 +0100201
Radek Krejcie8775162016-09-14 13:08:58 +0200202 if (is_list && !list->child) {
Radek Krejci23238922015-10-27 17:13:34 +0100203 /* empty, e.g. in case of filter */
204 flag_empty = 1;
205 }
Michal Vasko98763c62015-07-17 13:47:00 +0200206
Michal Vasko635bd452016-05-18 13:26:55 +0200207 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200208 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100209 schema = lys_node_module(node->schema)->name;
Radek Krejci23238922015-10-27 17:13:34 +0100210 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200211 } else {
Radek Krejci23238922015-10-27 17:13:34 +0100212 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200213 }
214
Radek Krejci23238922015-10-27 17:13:34 +0100215 if (flag_empty) {
Michal Vasko95068c42016-03-24 14:58:11 +0100216 ly_print(out, "%snull", (level ? " " : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100217 return;
218 }
Michal Vasko95068c42016-03-24 14:58:11 +0100219 ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100220
Michal Vasko95068c42016-03-24 14:58:11 +0100221 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200222 ++level;
223 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200224
225 while (list) {
Michal Vasko98763c62015-07-17 13:47:00 +0200226 if (is_list) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200227 /* list print */
Michal Vasko95068c42016-03-24 14:58:11 +0100228 if (level) {
229 ++level;
230 }
231 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : ""));
232 if (level) {
233 ++level;
234 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100235 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100236 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
严军喜100794898b539cf2016-10-12 16:34:52 +0800237 json_print_attrs(out, level + 1, list, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100238 ly_print(out, "%*s}%s", LEVEL, INDENT, list->child ? ",\n" : "");
239 }
Radek Krejci46180b52016-08-31 16:01:32 +0200240 json_print_nodes(out, level, list->child, 1, 0, options);
Michal Vasko95068c42016-03-24 14:58:11 +0100241 if (level) {
242 --level;
243 }
Radek Krejci76b07902015-10-09 09:11:25 +0200244 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko95068c42016-03-24 14:58:11 +0100245 if (level) {
246 --level;
247 }
Michal Vasko98763c62015-07-17 13:47:00 +0200248 } else {
Radek Krejci27aaa732015-09-04 15:24:04 +0200249 /* leaf-list print */
Radek Krejci76b07902015-10-09 09:11:25 +0200250 ly_print(out, "%*s", LEVEL, INDENT);
Radek Krejci46180b52016-08-31 16:01:32 +0200251 json_print_leaf(out, level, list, 1, toplevel, options);
Radek Krejcida61fb22015-10-30 11:10:03 +0100252 if (list->attr) {
253 flag_attrs = 1;
254 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200255 }
256 for (list = list->next; list && list->schema != node->schema; list = list->next);
257 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100258 ly_print(out, ",%s", (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200259 }
260 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200261
Michal Vasko95068c42016-03-24 14:58:11 +0100262 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200263 --level;
264 }
265
Michal Vasko95068c42016-03-24 14:58:11 +0100266 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100267
268 /* attributes */
269 if (!is_list && flag_attrs) {
270 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100271 ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
272 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100273 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100274 ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
275 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100276 }
Michal Vasko95068c42016-03-24 14:58:11 +0100277 if (level) {
278 level++;
279 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100280 for (list = node; list; ) {
281 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100282 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : ""));
Radek Krejci1eefeb32016-04-15 16:01:46 +0200283 json_print_attrs(out, 0, list, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100284 ly_print(out, "%*s}", LEVEL, INDENT);
285 } else {
286 ly_print(out, "%*snull", LEVEL, INDENT);
287 }
288
289
290 for (list = list->next; list && list->schema != node->schema; list = list->next);
291 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100292 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100293 }
294 }
Michal Vasko95068c42016-03-24 14:58:11 +0100295 if (level) {
296 level--;
297 }
298 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100299 }
Michal Vasko98763c62015-07-17 13:47:00 +0200300}
301
302static void
Radek Krejci46180b52016-08-31 16:01:32 +0200303json_print_anydata(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vaskoab8e4402015-07-17 12:54:28 +0200304{
Radek Krejcida61fb22015-10-30 11:10:03 +0100305 const char *schema = NULL;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200306 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100307
Michal Vasko635bd452016-05-18 13:26:55 +0200308 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100309 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100310 schema = lys_node_module(node->schema)->name;
Radek Krejci4ae82942016-09-19 16:41:06 +0200311 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100312 } else {
Radek Krejci4ae82942016-09-19 16:41:06 +0200313 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
314 }
315 if (level) {
316 level++;
Radek Krejci9a5daea2016-03-02 16:49:40 +0100317 }
318
Radek Krejci4ae82942016-09-19 16:41:06 +0200319 switch (any->value_type) {
320 case LYD_ANYDATA_DATATREE:
321 json_print_nodes(out, level, any->value.tree, 1, 0, options);
322 break;
323 case LYD_ANYDATA_JSON:
324 if (any->value.str) {
325 ly_print(out, "%*s%s\n", LEVEL, INDENT, any->value.str);
Radek Krejci9a5daea2016-03-02 16:49:40 +0100326 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200327 break;
328 default:
329 /* other formats are not supported */
330 LOGWRN("Unable to print anydata content (type %d) as JSON.", any->value_type);
331 break;
Radek Krejcida61fb22015-10-30 11:10:03 +0100332 }
333
334 /* print attributes as sibling leaf */
335 if (node->attr) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100336 if (schema) {
Radek Krejci88f29302015-10-30 15:42:33 +0100337 ly_print(out, ",\n%*s\"@%s:%s\": {\n", LEVEL, INDENT, schema, node->schema->name);
Radek Krejcida61fb22015-10-30 11:10:03 +0100338 } else {
Radek Krejci88f29302015-10-30 15:42:33 +0100339 ly_print(out, ",\n%*s\"@%s\": {\n", LEVEL, INDENT, node->schema->name);
Radek Krejcida61fb22015-10-30 11:10:03 +0100340 }
Radek Krejci1eefeb32016-04-15 16:01:46 +0200341 json_print_attrs(out, (level ? level + 1 : level), node, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100342 ly_print(out, "%*s}", LEVEL, INDENT);
343 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200344
345
346 if (level) {
347 level--;
348 }
349 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vaskoab8e4402015-07-17 12:54:28 +0200350}
351
Radek Krejci5044be32016-01-18 17:05:51 +0100352static void
Radek Krejci46180b52016-08-31 16:01:32 +0200353json_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 +0200354{
Radek Krejci572f1222016-09-01 09:52:47 +0200355 const struct lyd_node *node, *iter;
Radek Krejci27aaa732015-09-04 15:24:04 +0200356
357 LY_TREE_FOR(root, node) {
Radek Krejci572f1222016-09-01 09:52:47 +0200358 if (!lyd_wd_toprint(node, options)) {
359 continue;
Radek Krejci46180b52016-08-31 16:01:32 +0200360 }
361
Radek Krejci27aaa732015-09-04 15:24:04 +0200362 switch (node->schema->nodetype) {
Radek Krejcifb54be42015-10-02 15:21:16 +0200363 case LYS_RPC:
Michal Vaskoafa7a642016-10-18 15:11:38 +0200364 case LYS_ACTION:
Radek Krejcifb54be42015-10-02 15:21:16 +0200365 case LYS_NOTIF:
Radek Krejci27aaa732015-09-04 15:24:04 +0200366 case LYS_CONTAINER:
367 if (node->prev->next) {
368 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100369 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200370 }
Radek Krejci46180b52016-08-31 16:01:32 +0200371 json_print_container(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200372 break;
373 case LYS_LEAF:
374 if (node->prev->next) {
375 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100376 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200377 }
Radek Krejci46180b52016-08-31 16:01:32 +0200378 json_print_leaf(out, level, node, 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200379 break;
380 case LYS_LEAFLIST:
381 case LYS_LIST:
382 /* is it already printed? */
Radek Krejci9ce61512015-10-26 14:42:32 +0100383 for (iter = node->prev; iter->next; iter = iter->prev) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200384 if (iter == node) {
385 continue;
386 }
387 if (iter->schema == node->schema) {
388 /* the list has alread some previous instance and therefore it is already printed */
389 break;
390 }
391 }
Michal Vasko6c563772015-10-15 10:49:30 +0200392 if (!iter->next) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200393 if (node->prev->next) {
394 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100395 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200396 }
397
398 /* print the list/leaflist */
Radek Krejci46180b52016-08-31 16:01:32 +0200399 json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200400 }
401 break;
402 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200403 case LYS_ANYDATA:
Radek Krejci27aaa732015-09-04 15:24:04 +0200404 if (node->prev->next) {
405 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100406 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200407 }
Radek Krejci46180b52016-08-31 16:01:32 +0200408 json_print_anydata(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200409 break;
410 default:
411 LOGINT;
412 break;
413 }
Radek Krejci5044be32016-01-18 17:05:51 +0100414
415 if (!withsiblings) {
416 break;
417 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200418 }
Radek Krejci4ae82942016-09-19 16:41:06 +0200419 if (root && level) {
Michal Vasko95068c42016-03-24 14:58:11 +0100420 ly_print(out, "\n");
421 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200422}
423
Radek Krejcif3752b02015-10-02 15:31:34 +0200424int
Radek Krejci5044be32016-01-18 17:05:51 +0100425json_print_data(struct lyout *out, const struct lyd_node *root, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200426{
Michal Vaskoafa7a642016-10-18 15:11:38 +0200427 const struct lyd_node *node, *next;
428 int level = 0, action_input = 0;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200429
Michal Vasko95068c42016-03-24 14:58:11 +0100430 if (options & LYP_FORMAT) {
431 ++level;
432 }
433
Michal Vaskoafa7a642016-10-18 15:11:38 +0200434 if (options & LYP_NETCONF) {
435 if (root->schema->nodetype != LYS_RPC) {
436 /* learn whether we are printing an action */
437 LY_TREE_DFS_BEGIN(root, next, node) {
438 if (node->schema->nodetype == LYS_ACTION) {
439 break;
440 }
441 LY_TREE_DFS_END(root, next, node);
442 }
443 } else {
444 node = root;
445 }
446
447 if (node && (node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
448 if (node->child && (node->child->schema->parent->nodetype == LYS_OUTPUT)) {
449 /* skip the container */
450 root = node->child;
451 } else if (node->schema->nodetype == LYS_ACTION) {
452 action_input = 1;
453 }
454 }
455 }
456
Radek Krejci94ca54b2015-07-08 15:48:47 +0200457 /* start */
Michal Vasko95068c42016-03-24 14:58:11 +0100458 ly_print(out, "{%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200459
Michal Vaskoafa7a642016-10-18 15:11:38 +0200460 if (action_input) {
461 ly_print(out, "%*s\"yang:action\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
462 if (level) {
463 ++level;
464 }
465 }
466
Radek Krejci94ca54b2015-07-08 15:48:47 +0200467 /* content */
Radek Krejci46180b52016-08-31 16:01:32 +0200468 json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200469
Michal Vaskoafa7a642016-10-18 15:11:38 +0200470 if (action_input) {
471 if (level) {
472 --level;
473 }
474 ly_print(out, "%*s}%s", LEVEL, INDENT, (level ? "\n" : ""));
475 }
476
Radek Krejci94ca54b2015-07-08 15:48:47 +0200477 /* end */
Michal Vasko95068c42016-03-24 14:58:11 +0100478 ly_print(out, "}%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200479
Michal Vaskoafa7a642016-10-18 15:11:38 +0200480 ly_print_flush(out);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200481 return EXIT_SUCCESS;
482}