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" |
| 29 | #include "xml.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 | |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 37 | void json_print_nodes(FILE *f, int level, struct lyd_node *root); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 38 | |
| 39 | /* 0 - same, 1 - different */ |
| 40 | static int |
| 41 | nscmp(struct lyd_node *node1, struct lyd_node *node2) |
| 42 | { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 43 | struct lys_module *m1, *m2; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 44 | |
| 45 | /* we have to cover submodules belonging to the same module */ |
| 46 | if (node1->schema->module->type) { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 47 | m1 = ((struct lys_submodule *)node1->schema->module)->belongsto; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 48 | } else { |
| 49 | m1 = node1->schema->module; |
| 50 | } |
| 51 | if (node2->schema->module->type) { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 52 | m2 = ((struct lys_submodule *)node2->schema->module)->belongsto; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 53 | } else { |
| 54 | m2 = node2->schema->module; |
| 55 | } |
| 56 | if (m1 == m2) { |
| 57 | /* belongs to the same module */ |
| 58 | return 0; |
| 59 | } else { |
| 60 | /* different modules */ |
| 61 | return 1; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static void |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 66 | json_print_leaf(FILE *f, int level, struct lyd_node *node, int onlyvalue) |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 67 | { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 68 | struct lyd_node_leaf *leaf = (struct lyd_node_leaf *)node; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 69 | LY_DATA_TYPE data_type; |
Michal Vasko | c82396e | 2015-07-17 15:31:25 +0200 | [diff] [blame] | 70 | const char *schema; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 71 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 72 | if (!onlyvalue) { |
| 73 | if (!node->parent || nscmp(node, node->parent)) { |
| 74 | /* print "namespace" */ |
| 75 | if (node->schema->module->type) { |
| 76 | /* submodule, get module */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 77 | schema = ((struct lys_submodule *)node->schema->module)->belongsto->name; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 78 | } else { |
| 79 | schema = node->schema->module->name; |
| 80 | } |
| 81 | fprintf(f, "%*s\"%s:%s\": ", LEVEL, INDENT, schema, node->schema->name); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 82 | } else { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 83 | fprintf(f, "%*s\"%s\": ", LEVEL, INDENT, node->schema->name); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 84 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 85 | } |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 86 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 87 | data_type = leaf->value_type; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 88 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 89 | switch (data_type & LY_DATA_TYPE_MASK) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 90 | case LY_TYPE_BINARY: |
| 91 | case LY_TYPE_STRING: |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 92 | case LY_TYPE_BITS: |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 93 | case LY_TYPE_ENUM: |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 94 | case LY_TYPE_IDENT: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 95 | case LY_TYPE_INST: |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 96 | fprintf(f, "\"%s\"", leaf->value_str ? leaf->value_str : ""); |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 97 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 98 | |
| 99 | case LY_TYPE_BOOL: |
| 100 | case LY_TYPE_DEC64: |
| 101 | case LY_TYPE_INT8: |
| 102 | case LY_TYPE_INT16: |
| 103 | case LY_TYPE_INT32: |
| 104 | case LY_TYPE_INT64: |
| 105 | case LY_TYPE_UINT8: |
| 106 | case LY_TYPE_UINT16: |
| 107 | case LY_TYPE_UINT32: |
| 108 | case LY_TYPE_UINT64: |
| 109 | fprintf(f, "%s", leaf->value_str ? leaf->value_str : ""); |
| 110 | break; |
| 111 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 112 | case LY_TYPE_LEAFREF: |
| 113 | json_print_leaf(f, level, leaf->value.leafref, 1); |
| 114 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 115 | |
| 116 | case LY_TYPE_EMPTY: |
| 117 | fprintf(f, "[null]"); |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 118 | break; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 119 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 120 | default: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 121 | /* error */ |
| 122 | fprintf(f, "\"(!error!)\""); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 125 | return; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 128 | static void |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 129 | json_print_container(FILE *f, int level, struct lyd_node *node) |
| 130 | { |
| 131 | const char *schema; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 132 | |
| 133 | if (!node->parent || nscmp(node, node->parent)) { |
| 134 | /* print "namespace" */ |
| 135 | if (node->schema->module->type) { |
| 136 | /* submodule, get module */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 137 | schema = ((struct lys_submodule *)node->schema->module)->belongsto->name; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 138 | } else { |
| 139 | schema = node->schema->module->name; |
| 140 | } |
| 141 | fprintf(f, "%*s\"%s:%s\": {\n", LEVEL, INDENT, schema, node->schema->name); |
| 142 | } else { |
| 143 | fprintf(f, "%*s\"%s\": {\n", LEVEL, INDENT, node->schema->name); |
| 144 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 145 | json_print_nodes(f, level + 1, node->child); |
| 146 | fprintf(f, "%*s}", LEVEL, INDENT); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void |
| 150 | json_print_leaf_list(FILE *f, int level, struct lyd_node *node, int is_list) |
| 151 | { |
| 152 | const char *schema; |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 153 | struct lyd_node *list = node; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 154 | |
| 155 | if (!node->parent || nscmp(node, node->parent)) { |
| 156 | /* print "namespace" */ |
| 157 | if (node->schema->module->type) { |
| 158 | /* submodule, get module */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 159 | schema = ((struct lys_submodule *)node->schema->module)->belongsto->name; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 160 | } else { |
| 161 | schema = node->schema->module->name; |
| 162 | } |
| 163 | fprintf(f, "%*s\"%s:%s\": [\n", LEVEL, INDENT, schema, node->schema->name); |
| 164 | } else { |
| 165 | fprintf(f, "%*s\"%s\": [\n", LEVEL, INDENT, node->schema->name); |
| 166 | } |
| 167 | |
| 168 | if (!is_list) { |
| 169 | ++level; |
| 170 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 171 | |
| 172 | while (list) { |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 173 | if (is_list) { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 174 | /* list print */ |
| 175 | ++level; |
| 176 | fprintf(f, "%*s{\n", LEVEL, INDENT); |
| 177 | json_print_nodes(f, level + 1, list->child); |
| 178 | fprintf(f, "%*s}", LEVEL, INDENT); |
| 179 | --level; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 180 | } else { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 181 | /* leaf-list print */ |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 182 | fprintf(f, "%*s", LEVEL, INDENT); |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 183 | json_print_leaf(f, level, list, 1); |
| 184 | } |
| 185 | for (list = list->next; list && list->schema != node->schema; list = list->next); |
| 186 | if (list) { |
| 187 | fprintf(f, ",\n"); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 188 | } |
| 189 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 190 | |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 191 | if (!is_list) { |
| 192 | --level; |
| 193 | } |
| 194 | |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 195 | fprintf(f, "\n%*s]", LEVEL, INDENT); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 199 | json_print_anyxml(FILE *f, int level, struct lyd_node *node) |
| 200 | { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 201 | fprintf(f, "%*s\"%s\": [null]", LEVEL, INDENT, node->schema->name); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 204 | void |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 205 | json_print_nodes(FILE *f, int level, struct lyd_node *root) |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 206 | { |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 207 | struct lyd_node *node, *iter; |
| 208 | |
| 209 | LY_TREE_FOR(root, node) { |
| 210 | switch (node->schema->nodetype) { |
| 211 | case LYS_CONTAINER: |
| 212 | if (node->prev->next) { |
| 213 | /* print the previous comma */ |
| 214 | fprintf(f, ",\n"); |
| 215 | } |
| 216 | json_print_container(f, level, node); |
| 217 | break; |
| 218 | case LYS_LEAF: |
| 219 | if (node->prev->next) { |
| 220 | /* print the previous comma */ |
| 221 | fprintf(f, ",\n"); |
| 222 | } |
| 223 | json_print_leaf(f, level, node, 0); |
| 224 | break; |
| 225 | case LYS_LEAFLIST: |
| 226 | case LYS_LIST: |
| 227 | /* is it already printed? */ |
| 228 | for (iter = node; iter->prev->next; iter = iter->prev) { |
| 229 | if (iter == node) { |
| 230 | continue; |
| 231 | } |
| 232 | if (iter->schema == node->schema) { |
| 233 | /* the list has alread some previous instance and therefore it is already printed */ |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | if (!iter->prev->next) { |
| 238 | if (node->prev->next) { |
| 239 | /* print the previous comma */ |
| 240 | fprintf(f, ",\n"); |
| 241 | } |
| 242 | |
| 243 | /* print the list/leaflist */ |
| 244 | json_print_leaf_list(f, level, node, node->schema->nodetype == LYS_LIST ? 1 : 0); |
| 245 | } |
| 246 | break; |
| 247 | case LYS_ANYXML: |
| 248 | if (node->prev->next) { |
| 249 | /* print the previous comma */ |
| 250 | fprintf(f, ",\n"); |
| 251 | } |
| 252 | json_print_anyxml(f, level, node); |
| 253 | break; |
| 254 | default: |
| 255 | LOGINT; |
| 256 | break; |
| 257 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 258 | } |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 259 | fprintf(f, "\n"); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 262 | API int |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 263 | json_print_data(FILE *f, struct lyd_node *root) |
| 264 | { |
| 265 | int level = 0; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 266 | |
| 267 | /* start */ |
| 268 | fprintf(f, "{\n"); |
| 269 | |
| 270 | /* content */ |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 271 | json_print_nodes(f, level + 1, root); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 272 | |
| 273 | /* end */ |
| 274 | fprintf(f, "}\n"); |
| 275 | |
| 276 | return EXIT_SUCCESS; |
| 277 | } |