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