Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * 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 Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <assert.h> |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 19 | #include <inttypes.h> |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 20 | |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 21 | #include "common.h" |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 22 | #include "printer.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 23 | #include "tree_data.h" |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 24 | #include "resolve.h" |
| 25 | #include "tree_internal.h" |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 26 | |
| 27 | #define INDENT "" |
| 28 | #define LEVEL (level*2) |
| 29 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 30 | static int json_print_nodes(struct lyout *out, int level, const struct lyd_node *root, int withsiblings, int toplevel, |
| 31 | int options); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 32 | |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 33 | static int |
| 34 | json_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++) { |
zyinter2008 | 56c0f13 | 2016-11-17 10:59:23 +0800 | [diff] [blame] | 44 | if (text[i] >= 0 && text[i] < 0x20) { |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 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 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 66 | static int |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 67 | json_print_attrs(struct lyout *out, int level, const struct lyd_node *node, const struct lys_module *wdmod) |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 68 | { |
| 69 | struct lyd_attr *attr; |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 70 | size_t len; |
| 71 | char *p; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 72 | |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 73 | 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 Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 77 | for (attr = node->attr; attr; attr = attr->next) { |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 78 | if (!attr->annotation) { |
| 79 | /* skip exception for the NETCONF's attribute since JSON is not defined for NETCONF */ |
| 80 | continue; |
| 81 | } |
Radek Krejci | b1ef187 | 2017-02-23 14:33:34 +0100 | [diff] [blame] | 82 | if (lys_main_module(attr->annotation->module) != lys_main_module(node->schema->module)) { |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 83 | ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, attr->annotation->module->name, attr->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 84 | } else { |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 85 | ly_print(out, "%*s\"%s\":", LEVEL, INDENT, attr->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 86 | } |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 87 | /* 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 Vasko | a80d830 | 2017-03-02 10:52:02 +0100 | [diff] [blame] | 97 | json_print_string(out, attr->value_str); |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 98 | 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 Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 130 | return EXIT_FAILURE; |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 133 | ly_print(out, "%s%s", attr->next ? "," : "", (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 134 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 135 | |
| 136 | return EXIT_SUCCESS; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 139 | static int |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 140 | json_print_leaf(struct lyout *out, int level, const struct lyd_node *node, int onlyvalue, int toplevel, int options) |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 141 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 142 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node, *iter; |
Michal Vasko | 0bff322 | 2017-01-05 10:55:31 +0100 | [diff] [blame] | 143 | const struct lys_type *type; |
Radek Krejci | 7a2a562 | 2016-12-05 13:32:05 +0100 | [diff] [blame] | 144 | const char *schema = NULL, *p, *mod_name; |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 145 | const struct lys_module *wdmod = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 146 | LY_DATA_TYPE datatype; |
Radek Krejci | 7a2a562 | 2016-12-05 13:32:05 +0100 | [diff] [blame] | 147 | size_t len; |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 148 | |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 149 | 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 Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 152 | /* get with-defaults module */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 153 | wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1); |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 154 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 155 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 156 | if (!onlyvalue) { |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 157 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 158 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 159 | schema = lys_node_module(node->schema)->name; |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 160 | ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : "")); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 161 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 162 | ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : "")); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 163 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 164 | } |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 165 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 166 | datatype = leaf->value_type & LY_DATA_TYPE_MASK; |
| 167 | contentprint: |
| 168 | switch (datatype) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 169 | case LY_TYPE_BINARY: |
| 170 | case LY_TYPE_STRING: |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 171 | case LY_TYPE_BITS: |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 172 | case LY_TYPE_ENUM: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 173 | case LY_TYPE_INST: |
Radek Krejci | c27114c | 2016-09-20 10:02:28 +0200 | [diff] [blame] | 174 | case LY_TYPE_INT64: |
| 175 | case LY_TYPE_UINT64: |
严军喜10079489 | 1758ff6 | 2016-10-25 10:14:47 +0800 | [diff] [blame] | 176 | case LY_TYPE_DEC64: |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 177 | json_print_string(out, leaf->value_str); |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 178 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 179 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 180 | case LY_TYPE_INT8: |
| 181 | case LY_TYPE_INT16: |
| 182 | case LY_TYPE_INT32: |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 183 | case LY_TYPE_UINT8: |
| 184 | case LY_TYPE_UINT16: |
| 185 | case LY_TYPE_UINT32: |
严军喜10079489 | 1758ff6 | 2016-10-25 10:14:47 +0800 | [diff] [blame] | 186 | case LY_TYPE_BOOL: |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 187 | ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null"); |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 188 | break; |
| 189 | |
Radek Krejci | 7a2a562 | 2016-12-05 13:32:05 +0100 | [diff] [blame] | 190 | case LY_TYPE_IDENT: |
| 191 | p = strchr(leaf->value_str, ':'); |
| 192 | assert(p); |
| 193 | len = p - leaf->value_str; |
| 194 | mod_name = leaf->schema->module->name; |
| 195 | if (!strncmp(leaf->value_str, mod_name, len) && !mod_name[len]) { |
| 196 | /* do not print the prefix, it is the default prefix for this node */ |
| 197 | json_print_string(out, ++p); |
| 198 | } else { |
| 199 | json_print_string(out, leaf->value_str); |
| 200 | } |
| 201 | break; |
| 202 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 203 | case LY_TYPE_LEAFREF: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 204 | iter = (struct lyd_node_leaf_list *)leaf->value.leafref; |
| 205 | while (iter && (iter->value_type == LY_TYPE_LEAFREF)) { |
| 206 | iter = (struct lyd_node_leaf_list *)iter->value.leafref; |
| 207 | } |
| 208 | if (!iter) { |
Michal Vasko | 0bff322 | 2017-01-05 10:55:31 +0100 | [diff] [blame] | 209 | /* unresolved and invalid, but we can learn the correct type anyway */ |
| 210 | type = lyd_leaf_type((struct lyd_node_leaf_list *)leaf); |
| 211 | if (!type) { |
| 212 | /* error */ |
| 213 | ly_print(out, "\"(!error!)\""); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 214 | return EXIT_FAILURE; |
Michal Vasko | 0bff322 | 2017-01-05 10:55:31 +0100 | [diff] [blame] | 215 | } |
| 216 | datatype = type->base; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 217 | } else { |
| 218 | datatype = iter->value_type & LY_DATA_TYPE_MASK; |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 219 | } |
Michal Vasko | 0bff322 | 2017-01-05 10:55:31 +0100 | [diff] [blame] | 220 | goto contentprint; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 221 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 222 | case LY_TYPE_EMPTY: |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 223 | ly_print(out, "[null]"); |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 224 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 225 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 226 | default: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 227 | /* error */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 228 | ly_print(out, "\"(!error!)\""); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 229 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 232 | /* print attributes as sibling leafs */ |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 233 | if (!onlyvalue && (node->attr || wdmod)) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 234 | if (schema) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 235 | ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name, |
| 236 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 237 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 238 | ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name, |
| 239 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 240 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 241 | if (json_print_attrs(out, (level ? level + 1 : level), node, wdmod)) { |
| 242 | return EXIT_FAILURE; |
| 243 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 244 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 245 | } |
| 246 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 247 | return EXIT_SUCCESS; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 250 | static int |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 251 | json_print_container(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options) |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 252 | { |
| 253 | const char *schema; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 254 | |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 255 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 256 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 257 | schema = lys_node_module(node->schema)->name; |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 258 | ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : "")); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 259 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 260 | ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : "")); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 261 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 262 | if (level) { |
| 263 | level++; |
| 264 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 265 | if (node->attr) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 266 | ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : "")); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 267 | if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) { |
| 268 | return EXIT_FAILURE; |
| 269 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 270 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 271 | if (node->child) { |
| 272 | ly_print(out, ",%s", (level ? "\n" : "")); |
| 273 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 274 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 275 | if (json_print_nodes(out, level, node->child, 1, 0, options)) { |
| 276 | return EXIT_FAILURE; |
| 277 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 278 | if (level) { |
| 279 | level--; |
| 280 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 281 | ly_print(out, "%*s}", LEVEL, INDENT); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 282 | |
| 283 | return EXIT_SUCCESS; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 286 | static int |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 287 | json_print_leaf_list(struct lyout *out, int level, const struct lyd_node *node, int is_list, int toplevel, int options) |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 288 | { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 289 | const char *schema = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 290 | const struct lyd_node *list = node; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 291 | int flag_empty = 0, flag_attrs = 0; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 292 | |
Radek Krejci | e877516 | 2016-09-14 13:08:58 +0200 | [diff] [blame] | 293 | if (is_list && !list->child) { |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 294 | /* empty, e.g. in case of filter */ |
| 295 | flag_empty = 1; |
| 296 | } |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 297 | |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 298 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 299 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 300 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 301 | ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 302 | } else { |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 303 | ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 306 | if (flag_empty) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 307 | ly_print(out, "%snull", (level ? " " : "")); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 308 | return EXIT_SUCCESS; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 309 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 310 | ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 311 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 312 | if (!is_list && level) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 313 | ++level; |
| 314 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 315 | |
| 316 | while (list) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 317 | if (is_list) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 318 | /* list print */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 319 | if (level) { |
| 320 | ++level; |
| 321 | } |
| 322 | ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : "")); |
| 323 | if (level) { |
| 324 | ++level; |
| 325 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 326 | if (list->attr) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 327 | ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : "")); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 328 | if (json_print_attrs(out, (level ? level + 1 : level), list, NULL)) { |
| 329 | return EXIT_FAILURE; |
| 330 | } |
Michal Vasko | f6aa861 | 2017-03-02 10:52:44 +0100 | [diff] [blame] | 331 | if (list->child) { |
| 332 | ly_print(out, "%*s},%s", LEVEL, INDENT, (level ? "\n" : "")); |
| 333 | } else { |
| 334 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 335 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 336 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 337 | if (json_print_nodes(out, level, list->child, 1, 0, options)) { |
| 338 | return EXIT_FAILURE; |
| 339 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 340 | if (level) { |
| 341 | --level; |
| 342 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 343 | ly_print(out, "%*s}", LEVEL, INDENT); |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 344 | if (level) { |
| 345 | --level; |
| 346 | } |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 347 | } else { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 348 | /* leaf-list print */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 349 | ly_print(out, "%*s", LEVEL, INDENT); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 350 | if (json_print_leaf(out, level, list, 1, toplevel, options)) { |
| 351 | return EXIT_FAILURE; |
| 352 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 353 | if (list->attr) { |
| 354 | flag_attrs = 1; |
| 355 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 356 | } |
| 357 | for (list = list->next; list && list->schema != node->schema; list = list->next); |
| 358 | if (list) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 359 | ly_print(out, ",%s", (level ? "\n" : "")); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 360 | } |
| 361 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 362 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 363 | if (!is_list && level) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 364 | --level; |
| 365 | } |
| 366 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 367 | ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 368 | |
| 369 | /* attributes */ |
| 370 | if (!is_list && flag_attrs) { |
| 371 | if (schema) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 372 | ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name, |
| 373 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 374 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 375 | ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name, |
| 376 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 377 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 378 | if (level) { |
| 379 | level++; |
| 380 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 381 | for (list = node; list; ) { |
| 382 | if (list->attr) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 383 | ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : "")); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 384 | if (json_print_attrs(out, 0, list, NULL)) { |
| 385 | return EXIT_FAILURE; |
| 386 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 387 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 388 | } else { |
| 389 | ly_print(out, "%*snull", LEVEL, INDENT); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | for (list = list->next; list && list->schema != node->schema; list = list->next); |
| 394 | if (list) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 395 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 396 | } |
| 397 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 398 | if (level) { |
| 399 | level--; |
| 400 | } |
| 401 | ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 402 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 403 | |
| 404 | return EXIT_SUCCESS; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 407 | static int |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 408 | json_print_anyxml(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options) |
| 409 | { |
| 410 | struct lyd_node_anydata *any = (struct lyd_node_anydata *)node; |
| 411 | int isobject = 0; |
| 412 | char *buf; |
| 413 | const char *schema = NULL; |
| 414 | |
| 415 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
| 416 | /* print "namespace" */ |
| 417 | schema = lys_node_module(node->schema)->name; |
| 418 | ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : "")); |
| 419 | } else { |
| 420 | ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : "")); |
| 421 | } |
| 422 | if (level) { |
| 423 | level++; |
| 424 | } |
| 425 | |
| 426 | switch (any->value_type) { |
| 427 | case LYD_ANYDATA_DATATREE: |
| 428 | isobject = 1; |
| 429 | ly_print(out, level ? "{\n" : "{"); |
Radek Krejci | db2f58c | 2016-11-24 11:24:48 +0100 | [diff] [blame] | 430 | /* do not print any default values nor empty containers */ |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 431 | if (json_print_nodes(out, level, any->value.tree, 1, 0, LYP_WITHSIBLINGS | (options & ~LYP_NETCONF))) { |
| 432 | return EXIT_FAILURE; |
| 433 | } |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 434 | break; |
| 435 | case LYD_ANYDATA_JSON: |
| 436 | isobject = 1; |
| 437 | ly_print(out, level ? "{\n" : "{"); |
| 438 | if (any->value.str) { |
| 439 | ly_print(out, "%*s%s%s", LEVEL, INDENT, any->value.str, level ? "\n" : ""); |
| 440 | } |
| 441 | break; |
| 442 | case LYD_ANYDATA_XML: |
| 443 | lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0) |
| 444 | | LYXML_PRINT_SIBLINGS); |
| 445 | json_print_string(out, buf); |
| 446 | free(buf); |
| 447 | break; |
| 448 | case LYD_ANYDATA_CONSTSTRING: |
| 449 | case LYD_ANYDATA_SXML: |
| 450 | if (any->value.str) { |
| 451 | json_print_string(out, any->value.str); |
| 452 | } else { |
| 453 | ly_print(out, "\"\""); |
| 454 | } |
| 455 | break; |
| 456 | default: |
| 457 | /* other formats are not supported */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame^] | 458 | LOGWRN(node->schema->module->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type); |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | |
| 462 | if (level) { |
| 463 | level--; |
| 464 | } |
| 465 | if (isobject) { |
| 466 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 467 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 468 | |
| 469 | return EXIT_SUCCESS; |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 470 | } |
| 471 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 472 | static int |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 473 | json_print_anydata(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options) |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 474 | { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 475 | const char *schema = NULL; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 476 | struct lyd_node_anydata *any = (struct lyd_node_anydata *)node; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 477 | |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 478 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 479 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 480 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 481 | ly_print(out, "%*s\"%s:%s\":%s{%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 482 | } else { |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 483 | ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : "")); |
| 484 | } |
| 485 | if (level) { |
| 486 | level++; |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 489 | switch (any->value_type) { |
| 490 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | db2f58c | 2016-11-24 11:24:48 +0100 | [diff] [blame] | 491 | /* do not print any default values nor empty containers */ |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 492 | if (json_print_nodes(out, level, any->value.tree, 1, 0, LYP_WITHSIBLINGS | (options & LYP_FORMAT))) { |
| 493 | return EXIT_FAILURE; |
| 494 | } |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 495 | break; |
| 496 | case LYD_ANYDATA_JSON: |
| 497 | if (any->value.str) { |
Michal Vasko | f6aa861 | 2017-03-02 10:52:44 +0100 | [diff] [blame] | 498 | ly_print(out, "%*s%s%s", LEVEL, INDENT, any->value.str, (level ? "\n" : "")); |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 499 | } |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 500 | break; |
| 501 | default: |
| 502 | /* other formats are not supported */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame^] | 503 | LOGWRN(node->schema->module->ctx, "Unable to print anydata content (type %d) as JSON.", any->value_type); |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 504 | break; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* print attributes as sibling leaf */ |
| 508 | if (node->attr) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 509 | if (schema) { |
Michal Vasko | f6aa861 | 2017-03-02 10:52:44 +0100 | [diff] [blame] | 510 | ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name, |
| 511 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 512 | } else { |
Michal Vasko | f6aa861 | 2017-03-02 10:52:44 +0100 | [diff] [blame] | 513 | ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name, |
| 514 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 515 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 516 | if (json_print_attrs(out, (level ? level + 1 : level), node, NULL)) { |
| 517 | return EXIT_FAILURE; |
| 518 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 519 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 520 | } |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 521 | |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 522 | if (level) { |
| 523 | level--; |
| 524 | } |
| 525 | ly_print(out, "%*s}", LEVEL, INDENT); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 526 | |
| 527 | return EXIT_SUCCESS; |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 530 | static int |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 531 | json_print_nodes(struct lyout *out, int level, const struct lyd_node *root, int withsiblings, int toplevel, int options) |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 532 | { |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 533 | int ret = EXIT_SUCCESS; |
Radek Krejci | 572f122 | 2016-09-01 09:52:47 +0200 | [diff] [blame] | 534 | const struct lyd_node *node, *iter; |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 535 | |
| 536 | LY_TREE_FOR(root, node) { |
Radek Krejci | 572f122 | 2016-09-01 09:52:47 +0200 | [diff] [blame] | 537 | if (!lyd_wd_toprint(node, options)) { |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 538 | /* wd says do not print */ |
Radek Krejci | 572f122 | 2016-09-01 09:52:47 +0200 | [diff] [blame] | 539 | continue; |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 542 | switch (node->schema->nodetype) { |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 543 | case LYS_RPC: |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 544 | case LYS_ACTION: |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 545 | case LYS_NOTIF: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 546 | case LYS_CONTAINER: |
| 547 | if (node->prev->next) { |
| 548 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 549 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 550 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 551 | ret = json_print_container(out, level, node, toplevel, options); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 552 | break; |
| 553 | case LYS_LEAF: |
| 554 | if (node->prev->next) { |
| 555 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 556 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 557 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 558 | ret = json_print_leaf(out, level, node, 0, toplevel, options); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 559 | break; |
| 560 | case LYS_LEAFLIST: |
| 561 | case LYS_LIST: |
| 562 | /* is it already printed? */ |
Radek Krejci | 9ce6151 | 2015-10-26 14:42:32 +0100 | [diff] [blame] | 563 | for (iter = node->prev; iter->next; iter = iter->prev) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 564 | if (iter == node) { |
| 565 | continue; |
| 566 | } |
| 567 | if (iter->schema == node->schema) { |
| 568 | /* the list has alread some previous instance and therefore it is already printed */ |
| 569 | break; |
| 570 | } |
| 571 | } |
Michal Vasko | 6c56377 | 2015-10-15 10:49:30 +0200 | [diff] [blame] | 572 | if (!iter->next) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 573 | if (node->prev->next) { |
| 574 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 575 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* print the list/leaflist */ |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 579 | ret = json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0, toplevel, options); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 580 | } |
| 581 | break; |
| 582 | case LYS_ANYXML: |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 583 | if (node->prev->next) { |
| 584 | /* print the previous comma */ |
| 585 | ly_print(out, ",%s", (level ? "\n" : "")); |
| 586 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 587 | ret = json_print_anyxml(out, level, node, toplevel, options); |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 588 | break; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 589 | case LYS_ANYDATA: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 590 | if (node->prev->next) { |
| 591 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 592 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 593 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 594 | ret = json_print_anydata(out, level, node, toplevel, options); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 595 | break; |
| 596 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame^] | 597 | LOGINT(node->schema->module->ctx); |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 598 | ret = EXIT_FAILURE; |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 599 | break; |
| 600 | } |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 601 | |
| 602 | if (!withsiblings) { |
| 603 | break; |
| 604 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 605 | } |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 606 | if (root && level) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 607 | ly_print(out, "\n"); |
| 608 | } |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 609 | |
| 610 | return ret; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Radek Krejci | f3752b0 | 2015-10-02 15:31:34 +0200 | [diff] [blame] | 613 | int |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 614 | json_print_data(struct lyout *out, const struct lyd_node *root, int options) |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 615 | { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 616 | const struct lyd_node *node, *next; |
| 617 | int level = 0, action_input = 0; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 618 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 619 | if (options & LYP_FORMAT) { |
| 620 | ++level; |
| 621 | } |
| 622 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 623 | if (options & LYP_NETCONF) { |
| 624 | if (root->schema->nodetype != LYS_RPC) { |
| 625 | /* learn whether we are printing an action */ |
| 626 | LY_TREE_DFS_BEGIN(root, next, node) { |
| 627 | if (node->schema->nodetype == LYS_ACTION) { |
| 628 | break; |
| 629 | } |
| 630 | LY_TREE_DFS_END(root, next, node); |
| 631 | } |
| 632 | } else { |
| 633 | node = root; |
| 634 | } |
| 635 | |
| 636 | if (node && (node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 637 | if (node->child && (node->child->schema->parent->nodetype == LYS_OUTPUT)) { |
| 638 | /* skip the container */ |
| 639 | root = node->child; |
| 640 | } else if (node->schema->nodetype == LYS_ACTION) { |
| 641 | action_input = 1; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 646 | /* start */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 647 | ly_print(out, "{%s", (level ? "\n" : "")); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 648 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 649 | if (action_input) { |
| 650 | ly_print(out, "%*s\"yang:action\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : "")); |
| 651 | if (level) { |
| 652 | ++level; |
| 653 | } |
| 654 | } |
| 655 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 656 | /* content */ |
Michal Vasko | b3c31bd | 2017-07-17 10:01:48 +0200 | [diff] [blame] | 657 | if (json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options)) { |
| 658 | return EXIT_FAILURE; |
| 659 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 660 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 661 | if (action_input) { |
| 662 | if (level) { |
| 663 | --level; |
| 664 | } |
| 665 | ly_print(out, "%*s}%s", LEVEL, INDENT, (level ? "\n" : "")); |
| 666 | } |
| 667 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 668 | /* end */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 669 | ly_print(out, "}%s", (level ? "\n" : "")); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 670 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 671 | ly_print_flush(out); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 672 | return EXIT_SUCCESS; |
| 673 | } |