blob: 839eee8996690a5d3adb2fd29a77b932090e5a63 [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;
92
Radek Krejci46180b52016-08-31 16:01:32 +020093 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
94 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default(leaf))) {
95 /* we have implicit OR explicit default node */
Radek Krejci1eefeb32016-04-15 16:01:46 +020096 /* get with-defaults module */
97 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL);
98 }
Radek Krejci94ca54b2015-07-08 15:48:47 +020099
Radek Krejci5a988152015-07-15 11:16:26 +0200100 if (!onlyvalue) {
Michal Vasko635bd452016-05-18 13:26:55 +0200101 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejci5a988152015-07-15 11:16:26 +0200102 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100103 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100104 ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200105 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100106 ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200107 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200108 }
Radek Krejcie4748472015-07-08 18:00:22 +0200109
Radek Krejcida61fb22015-10-30 11:10:03 +0100110 switch (leaf->value_type & LY_DATA_TYPE_MASK) {
Radek Krejcie4748472015-07-08 18:00:22 +0200111 case LY_TYPE_BINARY:
112 case LY_TYPE_STRING:
Radek Krejci2a27a042016-08-22 16:21:58 +0200113 case LY_TYPE_LEAFREF:
Radek Krejci3e3affe2015-07-09 15:38:40 +0200114 case LY_TYPE_BITS:
Radek Krejci5b315a92015-07-10 13:18:45 +0200115 case LY_TYPE_ENUM:
Radek Krejciac8aac62015-07-10 15:36:35 +0200116 case LY_TYPE_IDENT:
Radek Krejcic5090c32015-08-12 09:46:19 +0200117 case LY_TYPE_INST:
Michal Vasko44913842016-04-13 14:20:41 +0200118 json_print_string(out, leaf->value_str);
Radek Krejciac8aac62015-07-10 15:36:35 +0200119 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200120
121 case LY_TYPE_BOOL:
122 case LY_TYPE_DEC64:
123 case LY_TYPE_INT8:
124 case LY_TYPE_INT16:
125 case LY_TYPE_INT32:
126 case LY_TYPE_INT64:
127 case LY_TYPE_UINT8:
128 case LY_TYPE_UINT16:
129 case LY_TYPE_UINT32:
130 case LY_TYPE_UINT64:
Michal Vasko44913842016-04-13 14:20:41 +0200131 ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null");
Radek Krejcib1c12512015-08-11 11:22:04 +0200132 break;
133
Radek Krejcib1c12512015-08-11 11:22:04 +0200134 case LY_TYPE_EMPTY:
Radek Krejci76b07902015-10-09 09:11:25 +0200135 ly_print(out, "[null]");
Michal Vasko58110162015-07-15 15:50:16 +0200136 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200137
Radek Krejcie4748472015-07-08 18:00:22 +0200138 default:
Michal Vasko493bea72015-07-16 16:08:12 +0200139 /* error */
Radek Krejci76b07902015-10-09 09:11:25 +0200140 ly_print(out, "\"(!error!)\"");
Radek Krejcie4748472015-07-08 18:00:22 +0200141 }
142
Radek Krejci382e7f92016-01-12 17:15:47 +0100143 /* print attributes as sibling leafs */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200144 if (!onlyvalue && (node->attr || wdmod)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100145 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100146 ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
147 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100148 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100149 ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
150 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100151 }
Radek Krejci1eefeb32016-04-15 16:01:46 +0200152 json_print_attrs(out, level + 1, node, wdmod);
Radek Krejcida61fb22015-10-30 11:10:03 +0100153 ly_print(out, "%*s}", LEVEL, INDENT);
154 }
155
Radek Krejcib1c12512015-08-11 11:22:04 +0200156 return;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200157}
158
Michal Vaskoab8e4402015-07-17 12:54:28 +0200159static void
Radek Krejci46180b52016-08-31 16:01:32 +0200160json_print_container(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vasko98763c62015-07-17 13:47:00 +0200161{
162 const char *schema;
Michal Vasko98763c62015-07-17 13:47:00 +0200163
Michal Vasko635bd452016-05-18 13:26:55 +0200164 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200165 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100166 schema = lys_node_module(node->schema)->name;
Michal Vasko95068c42016-03-24 14:58:11 +0100167 ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200168 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100169 ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200170 }
Michal Vasko95068c42016-03-24 14:58:11 +0100171 if (level) {
172 level++;
173 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100174 if (node->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100175 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci1eefeb32016-04-15 16:01:46 +0200176 json_print_attrs(out, (level? level + 1 : level), node, NULL);
Michal Vasko95068c42016-03-24 14:58:11 +0100177 ly_print(out, "%*s}", LEVEL, INDENT);
178 if (node->child) {
179 ly_print(out, ",%s", (level ? "\n" : ""));
180 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100181 }
Radek Krejci46180b52016-08-31 16:01:32 +0200182 json_print_nodes(out, level, node->child, 1, 0, options);
Michal Vasko95068c42016-03-24 14:58:11 +0100183 if (level) {
184 level--;
185 }
Radek Krejci76b07902015-10-09 09:11:25 +0200186 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko98763c62015-07-17 13:47:00 +0200187}
188
189static void
Radek Krejci46180b52016-08-31 16:01:32 +0200190json_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 +0200191{
Radek Krejcida61fb22015-10-30 11:10:03 +0100192 const char *schema = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100193 const struct lyd_node *list = node;
Radek Krejcida61fb22015-10-30 11:10:03 +0100194 int flag_empty = 0, flag_attrs = 0;
Radek Krejci23238922015-10-27 17:13:34 +0100195
196 if (!list->child) {
197 /* empty, e.g. in case of filter */
198 flag_empty = 1;
199 }
Michal Vasko98763c62015-07-17 13:47:00 +0200200
Michal Vasko635bd452016-05-18 13:26:55 +0200201 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Michal Vasko98763c62015-07-17 13:47:00 +0200202 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100203 schema = lys_node_module(node->schema)->name;
Radek Krejci23238922015-10-27 17:13:34 +0100204 ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200205 } else {
Radek Krejci23238922015-10-27 17:13:34 +0100206 ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name);
Michal Vasko98763c62015-07-17 13:47:00 +0200207 }
208
Radek Krejci23238922015-10-27 17:13:34 +0100209 if (flag_empty) {
Michal Vasko95068c42016-03-24 14:58:11 +0100210 ly_print(out, "%snull", (level ? " " : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100211 return;
212 }
Michal Vasko95068c42016-03-24 14:58:11 +0100213 ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci23238922015-10-27 17:13:34 +0100214
Michal Vasko95068c42016-03-24 14:58:11 +0100215 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200216 ++level;
217 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200218
219 while (list) {
Michal Vasko98763c62015-07-17 13:47:00 +0200220 if (is_list) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200221 /* list print */
Michal Vasko95068c42016-03-24 14:58:11 +0100222 if (level) {
223 ++level;
224 }
225 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : ""));
226 if (level) {
227 ++level;
228 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100229 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100230 ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : ""));
Radek Krejci1eefeb32016-04-15 16:01:46 +0200231 json_print_attrs(out, level + 1, node, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100232 ly_print(out, "%*s}%s", LEVEL, INDENT, list->child ? ",\n" : "");
233 }
Radek Krejci46180b52016-08-31 16:01:32 +0200234 json_print_nodes(out, level, list->child, 1, 0, options);
Michal Vasko95068c42016-03-24 14:58:11 +0100235 if (level) {
236 --level;
237 }
Radek Krejci76b07902015-10-09 09:11:25 +0200238 ly_print(out, "%*s}", LEVEL, INDENT);
Michal Vasko95068c42016-03-24 14:58:11 +0100239 if (level) {
240 --level;
241 }
Michal Vasko98763c62015-07-17 13:47:00 +0200242 } else {
Radek Krejci27aaa732015-09-04 15:24:04 +0200243 /* leaf-list print */
Radek Krejci76b07902015-10-09 09:11:25 +0200244 ly_print(out, "%*s", LEVEL, INDENT);
Radek Krejci46180b52016-08-31 16:01:32 +0200245 json_print_leaf(out, level, list, 1, toplevel, options);
Radek Krejcida61fb22015-10-30 11:10:03 +0100246 if (list->attr) {
247 flag_attrs = 1;
248 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200249 }
250 for (list = list->next; list && list->schema != node->schema; list = list->next);
251 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100252 ly_print(out, ",%s", (level ? "\n" : ""));
Michal Vasko98763c62015-07-17 13:47:00 +0200253 }
254 }
Radek Krejci27aaa732015-09-04 15:24:04 +0200255
Michal Vasko95068c42016-03-24 14:58:11 +0100256 if (!is_list && level) {
Michal Vasko98763c62015-07-17 13:47:00 +0200257 --level;
258 }
259
Michal Vasko95068c42016-03-24 14:58:11 +0100260 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100261
262 /* attributes */
263 if (!is_list && flag_attrs) {
264 if (schema) {
Michal Vasko95068c42016-03-24 14:58:11 +0100265 ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name,
266 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100267 } else {
Michal Vasko95068c42016-03-24 14:58:11 +0100268 ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name,
269 (level ? " " : ""), (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100270 }
Michal Vasko95068c42016-03-24 14:58:11 +0100271 if (level) {
272 level++;
273 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100274 for (list = node; list; ) {
275 if (list->attr) {
Michal Vasko95068c42016-03-24 14:58:11 +0100276 ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : ""));
Radek Krejci1eefeb32016-04-15 16:01:46 +0200277 json_print_attrs(out, 0, list, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100278 ly_print(out, "%*s}", LEVEL, INDENT);
279 } else {
280 ly_print(out, "%*snull", LEVEL, INDENT);
281 }
282
283
284 for (list = list->next; list && list->schema != node->schema; list = list->next);
285 if (list) {
Michal Vasko95068c42016-03-24 14:58:11 +0100286 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejcida61fb22015-10-30 11:10:03 +0100287 }
288 }
Michal Vasko95068c42016-03-24 14:58:11 +0100289 if (level) {
290 level--;
291 }
292 ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT);
Radek Krejcida61fb22015-10-30 11:10:03 +0100293 }
Michal Vasko98763c62015-07-17 13:47:00 +0200294}
295
296static void
Radek Krejci46180b52016-08-31 16:01:32 +0200297json_print_anydata(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
Michal Vaskoab8e4402015-07-17 12:54:28 +0200298{
Radek Krejcida61fb22015-10-30 11:10:03 +0100299 const char *schema = NULL;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200300 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
Radek Krejci9a5daea2016-03-02 16:49:40 +0100301 char *xml;
Radek Krejcida61fb22015-10-30 11:10:03 +0100302
Michal Vasko635bd452016-05-18 13:26:55 +0200303 if (toplevel || !node->parent || nscmp(node, node->parent)) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100304 /* print "namespace" */
Michal Vasko6c629ac2016-02-15 14:08:23 +0100305 schema = lys_node_module(node->schema)->name;
Radek Krejci9a5daea2016-03-02 16:49:40 +0100306 ly_print(out, "%*s\"%s:%s\": ", LEVEL, INDENT, schema, node->schema->name);
Radek Krejcida61fb22015-10-30 11:10:03 +0100307 } else {
Radek Krejci9a5daea2016-03-02 16:49:40 +0100308 ly_print(out, "%*s\"%s\": ", LEVEL, INDENT, node->schema->name);
309 }
310
Radek Krejci45826012016-08-24 15:07:57 +0200311 if (!(void*)any->value.tree) {
312 /* no content */
313 ly_print(out, "[null]");
314 } else {
315 switch (any->value_type) {
316 case LYD_ANYDATA_CONSTSTRING:
317 case LYD_ANYDATA_STRING:
318 json_print_string(out, any->value.str);
319 break;
320 case LYD_ANYDATA_DATATREE:
Radek Krejci46180b52016-08-31 16:01:32 +0200321 json_print_nodes(out, level, any->value.tree, 1, 0, options);
Radek Krejci45826012016-08-24 15:07:57 +0200322 break;
323 case LYD_ANYDATA_XML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200324 lyxml_print_mem(&xml, any->value.xml, LYXML_PRINT_SIBLINGS);
Radek Krejci9a5daea2016-03-02 16:49:40 +0100325 json_print_string(out, xml);
326 free(xml);
Radek Krejci45826012016-08-24 15:07:57 +0200327 break;
Radek Krejci9a5daea2016-03-02 16:49:40 +0100328 }
Radek Krejcida61fb22015-10-30 11:10:03 +0100329 }
330
331 /* print attributes as sibling leaf */
332 if (node->attr) {
Radek Krejcida61fb22015-10-30 11:10:03 +0100333 if (schema) {
Radek Krejci88f29302015-10-30 15:42:33 +0100334 ly_print(out, ",\n%*s\"@%s:%s\": {\n", LEVEL, INDENT, schema, node->schema->name);
Radek Krejcida61fb22015-10-30 11:10:03 +0100335 } else {
Radek Krejci88f29302015-10-30 15:42:33 +0100336 ly_print(out, ",\n%*s\"@%s\": {\n", LEVEL, INDENT, node->schema->name);
Radek Krejcida61fb22015-10-30 11:10:03 +0100337 }
Radek Krejci1eefeb32016-04-15 16:01:46 +0200338 json_print_attrs(out, (level ? level + 1 : level), node, NULL);
Radek Krejcida61fb22015-10-30 11:10:03 +0100339 ly_print(out, "%*s}", LEVEL, INDENT);
340 }
Michal Vaskoab8e4402015-07-17 12:54:28 +0200341}
342
Radek Krejci5044be32016-01-18 17:05:51 +0100343static void
Radek Krejci46180b52016-08-31 16:01:32 +0200344json_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 +0200345{
Radek Krejci572f1222016-09-01 09:52:47 +0200346 const struct lyd_node *node, *iter;
Radek Krejci27aaa732015-09-04 15:24:04 +0200347
348 LY_TREE_FOR(root, node) {
Radek Krejci572f1222016-09-01 09:52:47 +0200349 if (!lyd_wd_toprint(node, options)) {
350 continue;
Radek Krejci46180b52016-08-31 16:01:32 +0200351 }
352
Radek Krejci27aaa732015-09-04 15:24:04 +0200353 switch (node->schema->nodetype) {
Radek Krejcifb54be42015-10-02 15:21:16 +0200354 case LYS_RPC:
355 case LYS_NOTIF:
Radek Krejci27aaa732015-09-04 15:24:04 +0200356 case LYS_CONTAINER:
357 if (node->prev->next) {
358 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100359 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200360 }
Radek Krejci46180b52016-08-31 16:01:32 +0200361 json_print_container(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200362 break;
363 case LYS_LEAF:
364 if (node->prev->next) {
365 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100366 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200367 }
Radek Krejci46180b52016-08-31 16:01:32 +0200368 json_print_leaf(out, level, node, 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200369 break;
370 case LYS_LEAFLIST:
371 case LYS_LIST:
372 /* is it already printed? */
Radek Krejci9ce61512015-10-26 14:42:32 +0100373 for (iter = node->prev; iter->next; iter = iter->prev) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200374 if (iter == node) {
375 continue;
376 }
377 if (iter->schema == node->schema) {
378 /* the list has alread some previous instance and therefore it is already printed */
379 break;
380 }
381 }
Michal Vasko6c563772015-10-15 10:49:30 +0200382 if (!iter->next) {
Radek Krejci27aaa732015-09-04 15:24:04 +0200383 if (node->prev->next) {
384 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100385 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200386 }
387
388 /* print the list/leaflist */
Radek Krejci46180b52016-08-31 16:01:32 +0200389 json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200390 }
391 break;
392 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200393 case LYS_ANYDATA:
Radek Krejci27aaa732015-09-04 15:24:04 +0200394 if (node->prev->next) {
395 /* print the previous comma */
Michal Vasko95068c42016-03-24 14:58:11 +0100396 ly_print(out, ",%s", (level ? "\n" : ""));
Radek Krejci27aaa732015-09-04 15:24:04 +0200397 }
Radek Krejci46180b52016-08-31 16:01:32 +0200398 json_print_anydata(out, level, node, toplevel, options);
Radek Krejci27aaa732015-09-04 15:24:04 +0200399 break;
400 default:
401 LOGINT;
402 break;
403 }
Radek Krejci5044be32016-01-18 17:05:51 +0100404
405 if (!withsiblings) {
406 break;
407 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200408 }
Michal Vasko95068c42016-03-24 14:58:11 +0100409 if (level) {
410 ly_print(out, "\n");
411 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200412}
413
Radek Krejcif3752b02015-10-02 15:31:34 +0200414int
Radek Krejci5044be32016-01-18 17:05:51 +0100415json_print_data(struct lyout *out, const struct lyd_node *root, int options)
Radek Krejci94ca54b2015-07-08 15:48:47 +0200416{
417 int level = 0;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200418
Michal Vasko95068c42016-03-24 14:58:11 +0100419 if (options & LYP_FORMAT) {
420 ++level;
421 }
422
Radek Krejci94ca54b2015-07-08 15:48:47 +0200423 /* start */
Michal Vasko95068c42016-03-24 14:58:11 +0100424 ly_print(out, "{%s", (level ? "\n" : ""));
Radek Krejci94ca54b2015-07-08 15:48:47 +0200425
426 /* content */
Radek Krejci46180b52016-08-31 16:01:32 +0200427 json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200428
429 /* end */
Michal Vasko95068c42016-03-24 14:58:11 +0100430 ly_print(out, "}%s", (level ? "\n" : ""));
431 ly_print_flush(out);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200432
433 return EXIT_SUCCESS;
434}