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