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 |
| 11 | * |
| 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 | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 30 | static void json_print_nodes(struct lyout *out, int level, const struct lyd_node *root, int withsiblings); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 31 | |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 32 | static int |
| 33 | json_print_string(struct lyout *out, const char *text) |
| 34 | { |
| 35 | unsigned int i, n; |
| 36 | |
| 37 | if (!text) { |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | ly_write(out, "\"", 1); |
| 42 | for (i = n = 0; text[i]; i++) { |
| 43 | if (text[i] < 0x20) { |
| 44 | /* control character */ |
| 45 | n += ly_print(out, "\\u%.4X"); |
| 46 | } else { |
| 47 | switch (text[i]) { |
| 48 | case '"': |
| 49 | n += ly_print(out, "\\\""); |
| 50 | break; |
| 51 | case '\\': |
| 52 | n += ly_print(out, "\\\\"); |
| 53 | break; |
| 54 | default: |
| 55 | ly_write(out, &text[i], 1); |
| 56 | n++; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | ly_write(out, "\"", 1); |
| 61 | |
| 62 | return n + 2; |
| 63 | } |
| 64 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 65 | static void |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 66 | json_print_attrs(struct lyout *out, int level, const struct lyd_node *node) |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 67 | { |
| 68 | struct lyd_attr *attr; |
| 69 | |
| 70 | for (attr = node->attr; attr; attr = attr->next) { |
| 71 | if (attr->module != node->schema->module) { |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 72 | 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] | 73 | } else { |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 74 | ly_print(out, "%*s\"%s\":", LEVEL, INDENT, attr->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 75 | } |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 76 | json_print_string(out, attr->value ? attr->value : ""); |
| 77 | ly_print(out, "%s", attr->next ? ",\n" : "\n"); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | static void |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 82 | json_print_leaf(struct lyout *out, int level, const struct lyd_node *node, int onlyvalue) |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 83 | { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 84 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 85 | const char *schema = NULL; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 86 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 87 | if (!onlyvalue) { |
| 88 | if (!node->parent || nscmp(node, node->parent)) { |
| 89 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 90 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 91 | ly_print(out, "%*s\"%s:%s\": ", LEVEL, INDENT, schema, node->schema->name); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 92 | } else { |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 93 | ly_print(out, "%*s\"%s\": ", LEVEL, INDENT, node->schema->name); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 94 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 95 | } |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 96 | |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 97 | switch (leaf->value_type & LY_DATA_TYPE_MASK) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 98 | case LY_TYPE_BINARY: |
| 99 | case LY_TYPE_STRING: |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 100 | case LY_TYPE_BITS: |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 101 | case LY_TYPE_ENUM: |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 102 | case LY_TYPE_IDENT: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 103 | case LY_TYPE_INST: |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 104 | json_print_string(out, leaf->value_str ? leaf->value_str : ""); |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 105 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 106 | |
| 107 | case LY_TYPE_BOOL: |
| 108 | case LY_TYPE_DEC64: |
| 109 | case LY_TYPE_INT8: |
| 110 | case LY_TYPE_INT16: |
| 111 | case LY_TYPE_INT32: |
| 112 | case LY_TYPE_INT64: |
| 113 | case LY_TYPE_UINT8: |
| 114 | case LY_TYPE_UINT16: |
| 115 | case LY_TYPE_UINT32: |
| 116 | case LY_TYPE_UINT64: |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 117 | ly_print(out, "%s", leaf->value_str ? leaf->value_str : "null"); |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 118 | break; |
| 119 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 120 | case LY_TYPE_LEAFREF: |
Radek Krejci | 5ce04a4 | 2016-01-21 14:54:24 +0100 | [diff] [blame] | 121 | if (leaf->value.leafref) { |
| 122 | json_print_leaf(out, level, leaf->value.leafref, 1); |
| 123 | } else { |
| 124 | ly_print(out, ""); |
| 125 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 126 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 127 | |
| 128 | case LY_TYPE_EMPTY: |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 129 | ly_print(out, "[null]"); |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 130 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 131 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 132 | default: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 133 | /* error */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 134 | ly_print(out, "\"(!error!)\""); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Radek Krejci | 382e7f9 | 2016-01-12 17:15:47 +0100 | [diff] [blame] | 137 | /* print attributes as sibling leafs */ |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 138 | if (!onlyvalue && node->attr) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 139 | if (schema) { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 140 | 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] | 141 | } else { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 142 | ly_print(out, ",\n%*s\"@%s\": {\n", LEVEL, INDENT, node->schema->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 143 | } |
| 144 | json_print_attrs(out, level + 1, node); |
| 145 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 146 | } |
| 147 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 148 | return; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 151 | static void |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 152 | json_print_container(struct lyout *out, int level, const struct lyd_node *node) |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 153 | { |
| 154 | const char *schema; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 155 | |
| 156 | if (!node->parent || nscmp(node, node->parent)) { |
| 157 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 158 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 159 | ly_print(out, "%*s\"%s:%s\": {\n", LEVEL, INDENT, schema, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 160 | } else { |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 161 | ly_print(out, "%*s\"%s\": {\n", LEVEL, INDENT, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 162 | } |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 163 | level++; |
| 164 | if (node->attr) { |
| 165 | ly_print(out, "%*s\"@\": {\n", LEVEL, INDENT); |
| 166 | json_print_attrs(out, level + 1, node); |
| 167 | ly_print(out, "%*s}%s", LEVEL, INDENT, node->child ? ",\n" : ""); |
| 168 | } |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 169 | json_print_nodes(out, level, node->child, 1); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 170 | level--; |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 171 | ly_print(out, "%*s}", LEVEL, INDENT); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 175 | json_print_leaf_list(struct lyout *out, int level, const struct lyd_node *node, int is_list) |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 176 | { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 177 | const char *schema = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 178 | const struct lyd_node *list = node; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 179 | int flag_empty = 0, flag_attrs = 0; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 180 | |
| 181 | if (!list->child) { |
| 182 | /* empty, e.g. in case of filter */ |
| 183 | flag_empty = 1; |
| 184 | } |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 185 | |
| 186 | if (!node->parent || nscmp(node, node->parent)) { |
| 187 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 188 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 189 | ly_print(out, "%*s\"%s:%s\":", LEVEL, INDENT, schema, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 190 | } else { |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 191 | ly_print(out, "%*s\"%s\":", LEVEL, INDENT, node->schema->name); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 194 | if (flag_empty) { |
| 195 | ly_print(out, " null"); |
| 196 | return; |
| 197 | } |
| 198 | ly_print(out, " [\n"); |
| 199 | |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 200 | if (!is_list) { |
| 201 | ++level; |
| 202 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 203 | |
| 204 | while (list) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 205 | if (is_list) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 206 | /* list print */ |
| 207 | ++level; |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 208 | ly_print(out, "%*s{\n", LEVEL, INDENT); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 209 | ++level; |
| 210 | if (list->attr) { |
| 211 | ly_print(out, "%*s\"@\": {\n", LEVEL, INDENT); |
| 212 | json_print_attrs(out, level + 1, node); |
| 213 | ly_print(out, "%*s}%s", LEVEL, INDENT, list->child ? ",\n" : ""); |
| 214 | } |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 215 | json_print_nodes(out, level, list->child, 1); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 216 | --level; |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 217 | ly_print(out, "%*s}", LEVEL, INDENT); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 218 | --level; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 219 | } else { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 220 | /* leaf-list print */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 221 | ly_print(out, "%*s", LEVEL, INDENT); |
| 222 | json_print_leaf(out, level, list, 1); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 223 | if (list->attr) { |
| 224 | flag_attrs = 1; |
| 225 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 226 | } |
| 227 | for (list = list->next; list && list->schema != node->schema; list = list->next); |
| 228 | if (list) { |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 229 | ly_print(out, ",\n"); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 230 | } |
| 231 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 232 | |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 233 | if (!is_list) { |
| 234 | --level; |
| 235 | } |
| 236 | |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 237 | ly_print(out, "\n%*s]", LEVEL, INDENT); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 238 | |
| 239 | /* attributes */ |
| 240 | if (!is_list && flag_attrs) { |
| 241 | if (schema) { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 242 | 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] | 243 | } else { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 244 | ly_print(out, ",\n%*s\"@%s\": [\n", LEVEL, INDENT, node->schema->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 245 | } |
| 246 | level++; |
| 247 | for (list = node; list; ) { |
| 248 | if (list->attr) { |
| 249 | ly_print(out, "%*s{ ", LEVEL, INDENT); |
| 250 | json_print_attrs(out, 0, list); |
| 251 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 252 | } else { |
| 253 | ly_print(out, "%*snull", LEVEL, INDENT); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | for (list = list->next; list && list->schema != node->schema; list = list->next); |
| 258 | if (list) { |
| 259 | ly_print(out, ",\n"); |
| 260 | } |
| 261 | } |
| 262 | level--; |
| 263 | ly_print(out, "\n%*s]", LEVEL, INDENT); |
| 264 | } |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 268 | json_print_anyxml(struct lyout *out, int level, const struct lyd_node *node) |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 269 | { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 270 | const char *schema = NULL; |
| 271 | |
| 272 | if (!node->parent || nscmp(node, node->parent)) { |
| 273 | /* print "namespace" */ |
Michal Vasko | 6c629ac | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 274 | schema = lys_node_module(node->schema)->name; |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 275 | ly_print(out, "%*s\"%s:%s\": [null]", LEVEL, INDENT, schema, node->schema->name); |
| 276 | } else { |
| 277 | ly_print(out, "%*s\"%s\": [null]", LEVEL, INDENT, node->schema->name); |
| 278 | } |
| 279 | |
| 280 | /* print attributes as sibling leaf */ |
| 281 | if (node->attr) { |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 282 | if (schema) { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 283 | 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] | 284 | } else { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 285 | ly_print(out, ",\n%*s\"@%s\": {\n", LEVEL, INDENT, node->schema->name); |
Radek Krejci | da61fb2 | 2015-10-30 11:10:03 +0100 | [diff] [blame] | 286 | } |
| 287 | json_print_attrs(out, level + 1, node); |
| 288 | ly_print(out, "%*s}", LEVEL, INDENT); |
| 289 | } |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 292 | static void |
| 293 | json_print_nodes(struct lyout *out, int level, const struct lyd_node *root, int withsiblings) |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 294 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 295 | const struct lyd_node *node, *iter; |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 296 | |
| 297 | LY_TREE_FOR(root, node) { |
| 298 | switch (node->schema->nodetype) { |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 299 | case LYS_RPC: |
| 300 | case LYS_NOTIF: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 301 | case LYS_CONTAINER: |
| 302 | if (node->prev->next) { |
| 303 | /* print the previous comma */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 304 | ly_print(out, ",\n"); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 305 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 306 | json_print_container(out, level, node); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 307 | break; |
| 308 | case LYS_LEAF: |
| 309 | if (node->prev->next) { |
| 310 | /* print the previous comma */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 311 | ly_print(out, ",\n"); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 312 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 313 | json_print_leaf(out, level, node, 0); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 314 | break; |
| 315 | case LYS_LEAFLIST: |
| 316 | case LYS_LIST: |
| 317 | /* is it already printed? */ |
Radek Krejci | 9ce6151 | 2015-10-26 14:42:32 +0100 | [diff] [blame] | 318 | for (iter = node->prev; iter->next; iter = iter->prev) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 319 | if (iter == node) { |
| 320 | continue; |
| 321 | } |
| 322 | if (iter->schema == node->schema) { |
| 323 | /* the list has alread some previous instance and therefore it is already printed */ |
| 324 | break; |
| 325 | } |
| 326 | } |
Michal Vasko | 6c56377 | 2015-10-15 10:49:30 +0200 | [diff] [blame] | 327 | if (!iter->next) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 328 | if (node->prev->next) { |
| 329 | /* print the previous comma */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 330 | ly_print(out, ",\n"); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* print the list/leaflist */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 334 | json_print_leaf_list(out, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 335 | } |
| 336 | break; |
| 337 | case LYS_ANYXML: |
| 338 | if (node->prev->next) { |
| 339 | /* print the previous comma */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 340 | ly_print(out, ",\n"); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 341 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 342 | json_print_anyxml(out, level, node); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 343 | break; |
| 344 | default: |
| 345 | LOGINT; |
| 346 | break; |
| 347 | } |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 348 | |
| 349 | if (!withsiblings) { |
| 350 | break; |
| 351 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 352 | } |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 353 | ly_print(out, "\n"); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Radek Krejci | f3752b0 | 2015-10-02 15:31:34 +0200 | [diff] [blame] | 356 | int |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 357 | 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] | 358 | { |
| 359 | int level = 0; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 360 | |
| 361 | /* start */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 362 | ly_print(out, "{\n"); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 363 | |
| 364 | /* content */ |
Radek Krejci | 5044be3 | 2016-01-18 17:05:51 +0100 | [diff] [blame] | 365 | json_print_nodes(out, level + 1, root, options & LYP_WITHSIBLINGS); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 366 | |
| 367 | /* end */ |
Radek Krejci | 76b0790 | 2015-10-09 09:11:25 +0200 | [diff] [blame] | 368 | ly_print(out, "}\n"); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 369 | |
| 370 | return EXIT_SUCCESS; |
| 371 | } |