blob: f597bbb0ecd1cfe498c5218458b972f6a61cf324 [file] [log] [blame]
Radek Krejci94ca54b2015-07-08 15:48:47 +02001/**
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 Krejci7511f402015-07-10 09:56:30 +020026#include <inttypes.h>
Radek Krejci94ca54b2015-07-08 15:48:47 +020027
Radek Krejci998a0b82015-08-17 13:14:36 +020028#include "common.h"
29#include "xml.h"
30#include "tree.h"
31#include "resolve.h"
32#include "tree_internal.h"
Radek Krejci94ca54b2015-07-08 15:48:47 +020033
34#define INDENT ""
35#define LEVEL (level*2)
36
37void json_print_node(FILE *f, int level, struct lyd_node *node);
38
39/* 0 - same, 1 - different */
40static int
41nscmp(struct lyd_node *node1, struct lyd_node *node2)
42{
Radek Krejcib8048692015-08-05 13:36:34 +020043 struct lys_module *m1, *m2;
Radek Krejci94ca54b2015-07-08 15:48:47 +020044
45 /* we have to cover submodules belonging to the same module */
46 if (node1->schema->module->type) {
Radek Krejcib8048692015-08-05 13:36:34 +020047 m1 = ((struct lys_submodule *)node1->schema->module)->belongsto;
Radek Krejci94ca54b2015-07-08 15:48:47 +020048 } else {
49 m1 = node1->schema->module;
50 }
51 if (node2->schema->module->type) {
Radek Krejcib8048692015-08-05 13:36:34 +020052 m2 = ((struct lys_submodule *)node2->schema->module)->belongsto;
Radek Krejci94ca54b2015-07-08 15:48:47 +020053 } 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
65static void
Radek Krejci5a988152015-07-15 11:16:26 +020066json_print_leaf(FILE *f, int level, struct lyd_node *node, int onlyvalue)
Radek Krejci94ca54b2015-07-08 15:48:47 +020067{
Radek Krejcie4748472015-07-08 18:00:22 +020068 struct lyd_node_leaf *leaf = (struct lyd_node_leaf *)node;
Michal Vasko07471a52015-07-16 11:18:48 +020069 LY_DATA_TYPE data_type;
Michal Vaskoc82396e2015-07-17 15:31:25 +020070 const char *schema;
Radek Krejci94ca54b2015-07-08 15:48:47 +020071
Radek Krejci5a988152015-07-15 11:16:26 +020072 if (!onlyvalue) {
73 if (!node->parent || nscmp(node, node->parent)) {
74 /* print "namespace" */
75 if (node->schema->module->type) {
76 /* submodule, get module */
Radek Krejcib8048692015-08-05 13:36:34 +020077 schema = ((struct lys_submodule *)node->schema->module)->belongsto->name;
Radek Krejci5a988152015-07-15 11:16:26 +020078 } else {
79 schema = node->schema->module->name;
80 }
81 fprintf(f, "%*s\"%s:%s\": ", LEVEL, INDENT, schema, node->schema->name);
Radek Krejci94ca54b2015-07-08 15:48:47 +020082 } else {
Radek Krejci5a988152015-07-15 11:16:26 +020083 fprintf(f, "%*s\"%s\": ", LEVEL, INDENT, node->schema->name);
Radek Krejci94ca54b2015-07-08 15:48:47 +020084 }
Radek Krejci94ca54b2015-07-08 15:48:47 +020085 }
Radek Krejcie4748472015-07-08 18:00:22 +020086
Radek Krejci25b9fd32015-08-10 15:06:07 +020087 data_type = leaf->value_type;
Michal Vasko07471a52015-07-16 11:18:48 +020088
Radek Krejcib1c12512015-08-11 11:22:04 +020089 switch (data_type & LY_DATA_TYPE_MASK) {
Radek Krejcie4748472015-07-08 18:00:22 +020090 case LY_TYPE_BINARY:
91 case LY_TYPE_STRING:
Radek Krejci3e3affe2015-07-09 15:38:40 +020092 case LY_TYPE_BITS:
Radek Krejci5b315a92015-07-10 13:18:45 +020093 case LY_TYPE_ENUM:
Radek Krejciac8aac62015-07-10 15:36:35 +020094 case LY_TYPE_IDENT:
Radek Krejcic5090c32015-08-12 09:46:19 +020095 case LY_TYPE_INST:
Radek Krejcib1c12512015-08-11 11:22:04 +020096 fprintf(f, "\"%s\"", leaf->value_str ? leaf->value_str : "");
Radek Krejciac8aac62015-07-10 15:36:35 +020097 break;
Radek Krejcib1c12512015-08-11 11:22:04 +020098
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 Krejci5a988152015-07-15 11:16:26 +0200112 case LY_TYPE_LEAFREF:
113 json_print_leaf(f, level, leaf->value.leafref, 1);
114 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200115
116 case LY_TYPE_EMPTY:
117 fprintf(f, "[null]");
Michal Vasko58110162015-07-15 15:50:16 +0200118 break;
Radek Krejcib1c12512015-08-11 11:22:04 +0200119
Radek Krejcie4748472015-07-08 18:00:22 +0200120 default:
Michal Vasko493bea72015-07-16 16:08:12 +0200121 /* error */
122 fprintf(f, "\"(!error!)\"");
Radek Krejcie4748472015-07-08 18:00:22 +0200123 }
124
Radek Krejci5a988152015-07-15 11:16:26 +0200125 if (!onlyvalue) {
Radek Krejci9566b092015-07-31 11:18:15 +0200126 fprintf(f, "%s\n", lyd_is_last(node) ? "" : ",");
Radek Krejci5a988152015-07-15 11:16:26 +0200127 }
Radek Krejcib1c12512015-08-11 11:22:04 +0200128 return;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200129}
130
Michal Vaskoab8e4402015-07-17 12:54:28 +0200131static void
Michal Vasko98763c62015-07-17 13:47:00 +0200132json_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 Krejcib8048692015-08-05 13:36:34 +0200141 schema = ((struct lys_submodule *)node->schema->module)->belongsto->name;
Michal Vasko98763c62015-07-17 13:47:00 +0200142 } 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 Krejci9566b092015-07-31 11:18:15 +0200152 fprintf(f, "%*s}%s\n", LEVEL, INDENT, lyd_is_last(node) ? "" : ",");
Michal Vasko98763c62015-07-17 13:47:00 +0200153}
154
155static void
156json_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
169static void
170json_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 Vasko98763c62015-07-17 13:47:00 +0200175
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 Krejcib8048692015-08-05 13:36:34 +0200185 schema = ((struct lys_submodule *)node->schema->module)->belongsto->name;
Michal Vasko98763c62015-07-17 13:47:00 +0200186 } 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 Krejci9566b092015-07-31 11:18:15 +0200212 fprintf(f, "%*s]%s\n", LEVEL, INDENT, lyd_is_last(node) ? "" : ",");
Michal Vasko98763c62015-07-17 13:47:00 +0200213}
214
215static void
Michal Vaskoab8e4402015-07-17 12:54:28 +0200216json_print_anyxml(FILE *f, int level, struct lyd_node *node)
217{
218 struct lyd_node_anyxml *axml = (struct lyd_node_anyxml *)node;
219
Radek Krejci9566b092015-07-31 11:18:15 +0200220 fprintf(f, "%*s\"%s\": [null]%s\n", LEVEL, INDENT, axml->value->name, lyd_is_last(node) ? "" : ",");
Michal Vaskoab8e4402015-07-17 12:54:28 +0200221}
222
Radek Krejci94ca54b2015-07-08 15:48:47 +0200223void
224json_print_node(FILE *f, int level, struct lyd_node *node)
225{
226 switch (node->schema->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +0200227 case LYS_CONTAINER:
Radek Krejci94ca54b2015-07-08 15:48:47 +0200228 json_print_container(f, level, node);
229 break;
Radek Krejci76512572015-08-04 09:47:08 +0200230 case LYS_LEAF:
Radek Krejci5a988152015-07-15 11:16:26 +0200231 json_print_leaf(f, level, node, 0);
Radek Krejci94ca54b2015-07-08 15:48:47 +0200232 break;
Radek Krejci76512572015-08-04 09:47:08 +0200233 case LYS_LEAFLIST:
Michal Vasko98763c62015-07-17 13:47:00 +0200234 json_print_leaf_list(f, level, node, 0);
235 break;
Radek Krejci76512572015-08-04 09:47:08 +0200236 case LYS_LIST:
Michal Vasko98763c62015-07-17 13:47:00 +0200237 json_print_leaf_list(f, level, node, 1);
Michal Vaskoab8e4402015-07-17 12:54:28 +0200238 break;
Radek Krejci76512572015-08-04 09:47:08 +0200239 case LYS_ANYXML:
Michal Vaskoab8e4402015-07-17 12:54:28 +0200240 json_print_anyxml(f, level, node);
241 break;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200242 default:
Michal Vasko0c888fd2015-08-11 15:54:08 +0200243 LOGINT;
Radek Krejci94ca54b2015-07-08 15:48:47 +0200244 break;
245 }
Radek Krejci94ca54b2015-07-08 15:48:47 +0200246}
247
Michal Vasko520d4732015-07-13 15:53:33 +0200248API int
Radek Krejci94ca54b2015-07-08 15:48:47 +0200249json_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}