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 | |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 30 | static void 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++) { |
| 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 Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 66 | static void |
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; |
| 70 | |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 71 | 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 Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 75 | for (attr = node->attr; attr; attr = attr->next) { |
| 76 | if (attr->module != node->schema->module) { |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 77 | ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, attr->module->name, attr->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 78 | } else { |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 79 | ly_print(out, "%*s\"%s\":", LEVEL, INDENT, attr->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 80 | } |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 81 | json_print_string(out, attr->value ? attr->value : ""); |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 82 | ly_print(out, "%s%s", attr->next ? "," : "", (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | static void |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 87 | 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] | 88 | { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 89 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 90 | const char *schema = NULL; |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 91 | const struct lys_module *wdmod = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 92 | LY_DATA_TYPE datatype; |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 93 | |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 94 | 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 Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 97 | /* get with-defaults module */ |
| 98 | wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL); |
| 99 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 100 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 101 | if (!onlyvalue) { |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 102 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 103 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 104 | schema = lys_node_module(node->schema)->name; |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 105 | 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] | 106 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 107 | ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : "")); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 108 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 109 | } |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 110 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 111 | datatype = leaf->value_type & LY_DATA_TYPE_MASK; |
| 112 | contentprint: |
| 113 | switch (datatype) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 114 | case LY_TYPE_BINARY: |
| 115 | case LY_TYPE_STRING: |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 116 | case LY_TYPE_BITS: |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 117 | case LY_TYPE_ENUM: |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 118 | case LY_TYPE_IDENT: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 119 | case LY_TYPE_INST: |
Radek Krejci | c27114c | 2016-09-20 10:02:28 +0200 | [diff] [blame] | 120 | case LY_TYPE_INT64: |
| 121 | case LY_TYPE_UINT64: |
严军喜10079489 | 1758ff6 | 2016-10-25 10:14:47 +0800 | [diff] [blame] | 122 | case LY_TYPE_DEC64: |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 123 | json_print_string(out, leaf->value_str); |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 124 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 125 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 126 | case LY_TYPE_INT8: |
| 127 | case LY_TYPE_INT16: |
| 128 | case LY_TYPE_INT32: |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 129 | case LY_TYPE_UINT8: |
| 130 | case LY_TYPE_UINT16: |
| 131 | case LY_TYPE_UINT32: |
严军喜10079489 | 1758ff6 | 2016-10-25 10:14:47 +0800 | [diff] [blame] | 132 | case LY_TYPE_BOOL: |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 133 | ly_print(out, "%s", leaf->value_str[0] ? leaf->value_str : "null"); |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 134 | break; |
| 135 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 136 | case LY_TYPE_LEAFREF: |
| 137 | datatype = lyd_leaf_type(leaf); |
| 138 | goto contentprint; |
| 139 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 140 | case LY_TYPE_EMPTY: |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 141 | ly_print(out, "[null]"); |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 142 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 143 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 144 | default: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 145 | /* error */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 146 | ly_print(out, "\"(!error!)\""); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 149 | /* print attributes as sibling leafs */ |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 150 | if (!onlyvalue && (node->attr || wdmod)) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 151 | if (schema) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 152 | ly_print(out, ",%s%*s\"@%s:%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name, |
| 153 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 154 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 155 | ly_print(out, ",%s%*s\"@%s\":%s{%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name, |
| 156 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 157 | } |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 158 | json_print_attrs(out, level + 1, node, wdmod); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 159 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 160 | } |
| 161 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 162 | return; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 165 | static void |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 166 | 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] | 167 | { |
| 168 | const char *schema; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 169 | |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 170 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 171 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 172 | schema = lys_node_module(node->schema)->name; |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 173 | 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] | 174 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 175 | 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] | 176 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 177 | if (level) { |
| 178 | level++; |
| 179 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 180 | if (node->attr) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 181 | ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 182 | json_print_attrs(out, (level? level + 1 : level), node, NULL); |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 183 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 184 | if (node->child) { |
| 185 | ly_print(out, ",%s", (level ? "\n" : "")); |
| 186 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 187 | } |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 188 | json_print_nodes(out, level, node->child, 1, 0, options); |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 189 | if (level) { |
| 190 | level--; |
| 191 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 192 | ly_print(out, "%*s}", LEVEL, INDENT); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 196 | 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] | 197 | { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 198 | const char *schema = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 199 | const struct lyd_node *list = node; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 200 | int flag_empty = 0, flag_attrs = 0; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 201 | |
Radek Krejci | e877516 | 2016-09-14 13:08:58 +0200 | [diff] [blame] | 202 | if (is_list && !list->child) { |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 203 | /* empty, e.g. in case of filter */ |
| 204 | flag_empty = 1; |
| 205 | } |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 206 | |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 207 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 208 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 209 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 210 | ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 211 | } else { |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 212 | ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 215 | if (flag_empty) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 216 | ly_print(out, "%snull", (level ? " " : "")); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 217 | return; |
| 218 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 219 | ly_print(out, "%s[%s", (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 220 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 221 | if (!is_list && level) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 222 | ++level; |
| 223 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 224 | |
| 225 | while (list) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 226 | if (is_list) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 227 | /* list print */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 228 | if (level) { |
| 229 | ++level; |
| 230 | } |
| 231 | ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? "\n" : "")); |
| 232 | if (level) { |
| 233 | ++level; |
| 234 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 235 | if (list->attr) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 236 | ly_print(out, "%*s\"@\":%s{%s", LEVEL, INDENT, (level ? " " : ""), (level ? "\n" : "")); |
严军喜10079489 | 8b539cf | 2016-10-12 16:34:52 +0800 | [diff] [blame] | 237 | json_print_attrs(out, level + 1, list, NULL); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 238 | ly_print(out, "%*s}%s", LEVEL, INDENT, list->child ? ",\n" : ""); |
| 239 | } |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 240 | json_print_nodes(out, level, list->child, 1, 0, options); |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 241 | if (level) { |
| 242 | --level; |
| 243 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 244 | ly_print(out, "%*s}", LEVEL, INDENT); |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 245 | if (level) { |
| 246 | --level; |
| 247 | } |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 248 | } else { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 249 | /* leaf-list print */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 250 | ly_print(out, "%*s", LEVEL, INDENT); |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 251 | json_print_leaf(out, level, list, 1, toplevel, options); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 252 | if (list->attr) { |
| 253 | flag_attrs = 1; |
| 254 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 255 | } |
| 256 | for (list = list->next; list && list->schema != node->schema; list = list->next); |
| 257 | if (list) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 258 | ly_print(out, ",%s", (level ? "\n" : "")); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 259 | } |
| 260 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 261 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 262 | if (!is_list && level) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 263 | --level; |
| 264 | } |
| 265 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 266 | ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 267 | |
| 268 | /* attributes */ |
| 269 | if (!is_list && flag_attrs) { |
| 270 | if (schema) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 271 | ly_print(out, ",%s%*s\"@%s:%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, schema, node->schema->name, |
| 272 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 273 | } else { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 274 | ly_print(out, ",%s%*s\"@%s\":%s[%s", (level ? "\n" : ""), LEVEL, INDENT, node->schema->name, |
| 275 | (level ? " " : ""), (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 276 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 277 | if (level) { |
| 278 | level++; |
| 279 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 280 | for (list = node; list; ) { |
| 281 | if (list->attr) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 282 | ly_print(out, "%*s{%s", LEVEL, INDENT, (level ? " " : "")); |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 283 | json_print_attrs(out, 0, list, NULL); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 284 | 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 Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 292 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 293 | } |
| 294 | } |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 295 | if (level) { |
| 296 | level--; |
| 297 | } |
| 298 | ly_print(out, "%s%*s]", (level ? "\n" : ""), LEVEL, INDENT); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 299 | } |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 303 | 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] | 304 | { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 305 | const char *schema = NULL; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 306 | struct lyd_node_anydata *any = (struct lyd_node_anydata *)node; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 307 | |
Michal Vasko | 635bd45 | 2016-05-18 13:26:55 +0200 | [diff] [blame] | 308 | if (toplevel || !node->parent || nscmp(node, node->parent)) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 309 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 310 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 311 | 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] | 312 | } else { |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 313 | ly_print(out, "%*s\"%s\":%s{%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""), (level ? "\n" : "")); |
| 314 | } |
| 315 | if (level) { |
| 316 | level++; |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 319 | 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 Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 326 | } |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 327 | 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 Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* print attributes as sibling leaf */ |
| 335 | if (node->attr) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 336 | if (schema) { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 337 | ly_print(out, ",\n%*s\"@%s:%s\": {\n", LEVEL, INDENT, schema, node->schema->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 338 | } else { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 339 | ly_print(out, ",\n%*s\"@%s\": {\n", LEVEL, INDENT, node->schema->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 340 | } |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 341 | json_print_attrs(out, (level ? level + 1 : level), node, NULL); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 342 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 343 | } |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 344 | |
| 345 | |
| 346 | if (level) { |
| 347 | level--; |
| 348 | } |
| 349 | ly_print(out, "%*s}", LEVEL, INDENT); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 352 | static void |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 353 | 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] | 354 | { |
Radek Krejci | 572f122 | 2016-09-01 09:52:47 +0200 | [diff] [blame] | 355 | const struct lyd_node *node, *iter; |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 356 | |
| 357 | LY_TREE_FOR(root, node) { |
Radek Krejci | 572f122 | 2016-09-01 09:52:47 +0200 | [diff] [blame] | 358 | if (!lyd_wd_toprint(node, options)) { |
| 359 | continue; |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 362 | switch (node->schema->nodetype) { |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 363 | case LYS_RPC: |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 364 | case LYS_ACTION: |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 365 | case LYS_NOTIF: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 366 | case LYS_CONTAINER: |
| 367 | if (node->prev->next) { |
| 368 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 369 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 370 | } |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 371 | json_print_container(out, level, node, toplevel, options); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 372 | break; |
| 373 | case LYS_LEAF: |
| 374 | if (node->prev->next) { |
| 375 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 376 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 377 | } |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 378 | json_print_leaf(out, level, node, 0, toplevel, options); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 379 | break; |
| 380 | case LYS_LEAFLIST: |
| 381 | case LYS_LIST: |
| 382 | /* is it already printed? */ |
Radek Krejci | 9ce6151 | 2015-10-26 14:42:32 +0100 | [diff] [blame] | 383 | for (iter = node->prev; iter->next; iter = iter->prev) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 384 | 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 Vasko | 6c56377 | 2015-10-15 10:49:30 +0200 | [diff] [blame] | 392 | if (!iter->next) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 393 | if (node->prev->next) { |
| 394 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 395 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* print the list/leaflist */ |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 399 | 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] | 400 | } |
| 401 | break; |
| 402 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 403 | case LYS_ANYDATA: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 404 | if (node->prev->next) { |
| 405 | /* print the previous comma */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 406 | ly_print(out, ",%s", (level ? "\n" : "")); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 407 | } |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 408 | json_print_anydata(out, level, node, toplevel, options); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 409 | break; |
| 410 | default: |
| 411 | LOGINT; |
| 412 | break; |
| 413 | } |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 414 | |
| 415 | if (!withsiblings) { |
| 416 | break; |
| 417 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 418 | } |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 419 | if (root && level) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 420 | ly_print(out, "\n"); |
| 421 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Radek Krejci | f3752b0 | 2015-10-02 15:31:34 +0200 | [diff] [blame] | 424 | int |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 425 | 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] | 426 | { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 427 | const struct lyd_node *node, *next; |
| 428 | int level = 0, action_input = 0; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 429 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 430 | if (options & LYP_FORMAT) { |
| 431 | ++level; |
| 432 | } |
| 433 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 434 | 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 Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 457 | /* start */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 458 | ly_print(out, "{%s", (level ? "\n" : "")); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 459 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 460 | 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 Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 467 | /* content */ |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 468 | json_print_nodes(out, level, root, options & LYP_WITHSIBLINGS, 1, options); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 469 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 470 | if (action_input) { |
| 471 | if (level) { |
| 472 | --level; |
| 473 | } |
| 474 | ly_print(out, "%*s}%s", LEVEL, INDENT, (level ? "\n" : "")); |
| 475 | } |
| 476 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 477 | /* end */ |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 478 | ly_print(out, "}%s", (level ? "\n" : "")); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 479 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 480 | ly_print_flush(out); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 481 | return EXIT_SUCCESS; |
| 482 | } |