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