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 | |
| 28 | #include "../common.h" |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 29 | #include "../xml.h" |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 30 | #include "../tree.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 31 | #include "../resolve.h" |
Michal Vasko | c82396e | 2015-07-17 15:31:25 +0200 | [diff] [blame] | 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 | |
| 37 | void json_print_node(FILE *f, int level, struct lyd_node *node); |
| 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 | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 125 | if (!onlyvalue) { |
Radek Krejci | 9566b09 | 2015-07-31 11:18:15 +0200 | [diff] [blame] | 126 | fprintf(f, "%s\n", lyd_is_last(node) ? "" : ","); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 127 | } |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 128 | return; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 131 | static void |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 132 | json_print_container(FILE *f, int level, struct lyd_node *node) |
| 133 | { |
| 134 | const char *schema; |
| 135 | struct lyd_node *child; |
| 136 | |
| 137 | if (!node->parent || nscmp(node, node->parent)) { |
| 138 | /* print "namespace" */ |
| 139 | if (node->schema->module->type) { |
| 140 | /* submodule, get module */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 141 | schema = ((struct lys_submodule *)node->schema->module)->belongsto->name; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 142 | } else { |
| 143 | schema = node->schema->module->name; |
| 144 | } |
| 145 | fprintf(f, "%*s\"%s:%s\": {\n", LEVEL, INDENT, schema, node->schema->name); |
| 146 | } else { |
| 147 | fprintf(f, "%*s\"%s\": {\n", LEVEL, INDENT, node->schema->name); |
| 148 | } |
| 149 | LY_TREE_FOR(node->child, child) { |
| 150 | json_print_node(f, level + 1, child); |
| 151 | } |
Radek Krejci | 9566b09 | 2015-07-31 11:18:15 +0200 | [diff] [blame] | 152 | fprintf(f, "%*s}%s\n", LEVEL, INDENT, lyd_is_last(node) ? "" : ","); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void |
| 156 | json_print_list_internal(FILE *f, int level, struct lyd_node_list *list) |
| 157 | { |
| 158 | struct lyd_node *child; |
| 159 | |
| 160 | fprintf(f, "%*s{\n", LEVEL, INDENT); |
| 161 | |
| 162 | LY_TREE_FOR(list->child, child) { |
| 163 | json_print_node(f, level + 1, child); |
| 164 | } |
| 165 | |
| 166 | fprintf(f, "%*s}%s\n", LEVEL, INDENT, (list->lnext ? "," : "")); |
| 167 | } |
| 168 | |
| 169 | static void |
| 170 | json_print_leaf_list(FILE *f, int level, struct lyd_node *node, int is_list) |
| 171 | { |
| 172 | const char *schema; |
| 173 | struct lyd_node_list *list = (struct lyd_node_list *)node; |
| 174 | struct lyd_node_leaflist *llist = (struct lyd_node_leaflist *)node; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 175 | |
| 176 | if ((is_list && list->lprev) || (!is_list && llist->lprev)) { |
| 177 | /* this list is already printed */ |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (!node->parent || nscmp(node, node->parent)) { |
| 182 | /* print "namespace" */ |
| 183 | if (node->schema->module->type) { |
| 184 | /* submodule, get module */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 185 | schema = ((struct lys_submodule *)node->schema->module)->belongsto->name; |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 186 | } else { |
| 187 | schema = node->schema->module->name; |
| 188 | } |
| 189 | fprintf(f, "%*s\"%s:%s\": [\n", LEVEL, INDENT, schema, node->schema->name); |
| 190 | } else { |
| 191 | fprintf(f, "%*s\"%s\": [\n", LEVEL, INDENT, node->schema->name); |
| 192 | } |
| 193 | |
| 194 | if (!is_list) { |
| 195 | ++level; |
| 196 | } |
| 197 | while ((is_list && list) || (!is_list && llist)) { |
| 198 | if (is_list) { |
| 199 | json_print_list_internal(f, level + 1, list); |
| 200 | list = list->lnext; |
| 201 | } else { |
| 202 | fprintf(f, "%*s", LEVEL, INDENT); |
| 203 | json_print_leaf(f, level, (struct lyd_node *)llist, 1); |
| 204 | fprintf(f, "%s\n", (llist->lnext ? "," : "")); |
| 205 | llist = llist->lnext; |
| 206 | } |
| 207 | } |
| 208 | if (!is_list) { |
| 209 | --level; |
| 210 | } |
| 211 | |
Radek Krejci | 9566b09 | 2015-07-31 11:18:15 +0200 | [diff] [blame] | 212 | fprintf(f, "%*s]%s\n", LEVEL, INDENT, lyd_is_last(node) ? "" : ","); |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 216 | json_print_anyxml(FILE *f, int level, struct lyd_node *node) |
| 217 | { |
| 218 | struct lyd_node_anyxml *axml = (struct lyd_node_anyxml *)node; |
| 219 | |
Radek Krejci | 9566b09 | 2015-07-31 11:18:15 +0200 | [diff] [blame] | 220 | fprintf(f, "%*s\"%s\": [null]%s\n", LEVEL, INDENT, axml->value->name, lyd_is_last(node) ? "" : ","); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 223 | void |
| 224 | json_print_node(FILE *f, int level, struct lyd_node *node) |
| 225 | { |
| 226 | switch (node->schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 227 | case LYS_CONTAINER: |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 228 | json_print_container(f, level, node); |
| 229 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 230 | case LYS_LEAF: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 231 | json_print_leaf(f, level, node, 0); |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 232 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 233 | case LYS_LEAFLIST: |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 234 | json_print_leaf_list(f, level, node, 0); |
| 235 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 236 | case LYS_LIST: |
Michal Vasko | 98763c6 | 2015-07-17 13:47:00 +0200 | [diff] [blame] | 237 | json_print_leaf_list(f, level, node, 1); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 238 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 239 | case LYS_ANYXML: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 240 | json_print_anyxml(f, level, node); |
| 241 | break; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 242 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 243 | LOGINT; |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 244 | break; |
| 245 | } |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 248 | API int |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 249 | json_print_data(FILE *f, struct lyd_node *root) |
| 250 | { |
| 251 | int level = 0; |
| 252 | struct lyd_node *node; |
| 253 | |
| 254 | /* start */ |
| 255 | fprintf(f, "{\n"); |
| 256 | |
| 257 | /* content */ |
| 258 | LY_TREE_FOR(root, node) { |
| 259 | json_print_node(f, level + 1, node); |
| 260 | } |
| 261 | |
| 262 | /* end */ |
| 263 | fprintf(f, "}\n"); |
| 264 | |
| 265 | return EXIT_SUCCESS; |
| 266 | } |