Merge branch 'json_schema_printer' into devel
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6226656..41ff2cf 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -206,6 +206,7 @@
src/xpath.c
src/printer_yang.c
src/printer_yin.c
+ src/printer_json_schema.c
src/printer_xml.c
src/printer_tree.c
src/printer_info.c
diff --git a/src/common.c b/src/common.c
index eb2712f..4756101 100644
--- a/src/common.c
+++ b/src/common.c
@@ -737,9 +737,9 @@
assert(out_size == out_used);
return lydict_insert_zc(ctx, out);
}
- id = strpbrk_backwards(col - 1, "/ [\'\"\f\n\r\t\v", (col - in) - 1);
- if ((id[0] == '/') || (id[0] == ' ') || (id[0] == '[') || (id[0] == '\'') || (id[0] == '\"') || (id[0] == '\f') ||
- (id[0] == '\n') || (id[0] == '\r') || (id[0] == '\t') || (id[0] == '\v')) {
+ id = strpbrk_backwards(col - 1, " \f\n\r\t\v(", (col - in) - 1);
+ if ((id[0] == ' ') || (id[0] == '\f') || (id[0] == '\n') || (id[0] == '\r') ||
+ (id[0] == '\t') || (id[0] == '\v') || (id[0] == '(')) {
++id;
}
id_len = col - id;
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 323ccc9..0606c96 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -468,7 +468,6 @@
yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
{
char *exp, *value;
- struct lys_node *node;
exp = value = (char *) list->keys;
while ((value = strpbrk(value, " \t\n"))) {
@@ -483,8 +482,7 @@
list->keys = calloc(list->keys_size, sizeof *list->keys);
LY_CHECK_ERR_RETURN(!list->keys, LOGMEM(module->ctx), EXIT_FAILURE);
- for (node = list->parent; node && node->nodetype != LYS_GROUPING; node = lys_parent(node));
- if (!node && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
+ if (unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
diff --git a/src/parser_yin.c b/src/parser_yin.c
index bf9a058..4467a8f 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -5471,9 +5471,7 @@
}
if (list->keys_str) {
- /* check that we are not in grouping */
- for (node = parent; node && node->nodetype != LYS_GROUPING; node = lys_parent(node));
- if (!node && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
+ if (unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
goto error;
}
} /* else config false list without a key, key_str presence in case of config true is checked earlier */
diff --git a/src/printer.c b/src/printer.c
index 3cff70c..aa7115b 100644
--- a/src/printer.c
+++ b/src/printer.c
@@ -284,6 +284,7 @@
{
int count = 0, brackets_flag = *index_e;
uint8_t op;
+ struct lys_module *mod;
op = iff_getop(expr->expr, *index_e);
(*index_e)++;
@@ -298,6 +299,9 @@
count += ly_print(out, "%s:", lys_main_module(expr->features[*index_f]->module)->name);
} else if (prefix_kind == 2) {
count += ly_print(out, "%s:", lys_main_module(expr->features[*index_f]->module)->prefix);
+ } else if (prefix_kind == 3) {
+ mod = lys_main_module(expr->features[*index_f]->module);
+ count += ly_print(out, "%s%s%s:", mod->name, mod->rev_size ? "@" : "", mod->rev_size ? mod->rev[0].date : "");
}
}
count += ly_print(out, expr->features[*index_f]->name);
@@ -365,6 +369,9 @@
case LYS_OUT_INFO:
ret = info_print_model(out, module, target_node);
break;
+ case LYS_OUT_JSON:
+ ret = jsons_print_model(out, module, target_node);
+ break;
default:
LOGERR(module->ctx, LY_EINVAL, "Unknown output format.");
ret = EXIT_FAILURE;
@@ -454,6 +461,222 @@
return lys_print_(&out, module, format, target_node, line_length, options);
}
+int
+lys_print_target(struct lyout *out, const struct lys_module *module, const char *target_schema_path,
+ void (*clb_print_typedef)(struct lyout*, const struct lys_tpdf*, int*),
+ void (*clb_print_identity)(struct lyout*, const struct lys_ident*, int*),
+ void (*clb_print_feature)(struct lyout*, const struct lys_feature*, int*),
+ void (*clb_print_type)(struct lyout*, const struct lys_type*, int*),
+ void (*clb_print_grouping)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_container)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_choice)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_leaf)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_leaflist)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_list)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_anydata)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_case)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_notif)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_rpc)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_action)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_input)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_output)(struct lyout*, const struct lys_node*, int*))
+{
+ int rc, i, f = 1;
+ char *spec_target = NULL;
+ struct lys_node *target = NULL;
+ struct lys_tpdf *tpdf = NULL;
+ uint8_t tpdf_size = 0;
+
+ if ((target_schema_path[0] == '/') || !strncmp(target_schema_path, "type/", 5)) {
+ rc = resolve_absolute_schema_nodeid((target_schema_path[0] == '/' ? target_schema_path : target_schema_path + 4), module,
+ LYS_ANY & ~(LYS_USES | LYS_AUGMENT | LYS_GROUPING), (const struct lys_node **)&target);
+ if (rc || !target) {
+ LOGERR(module->ctx, LY_EINVAL, "Target %s could not be resolved.",
+ (target_schema_path[0] == '/' ? target_schema_path : target_schema_path + 4));
+ return EXIT_FAILURE;
+ }
+ } else if (!strncmp(target_schema_path, "grouping/", 9)) {
+ /* cut the data part off */
+ if ((spec_target = strchr(target_schema_path + 9, '/'))) {
+ /* HACK only temporary */
+ spec_target[0] = '\0';
+ ++spec_target;
+ }
+ rc = resolve_absolute_schema_nodeid(target_schema_path + 8, module, LYS_GROUPING, (const struct lys_node **)&target);
+ if (rc || !target) {
+ ly_print(out, "Grouping %s not found.\n", target_schema_path + 8);
+ return EXIT_FAILURE;
+ }
+ } else if (!strncmp(target_schema_path, "typedef/", 8)) {
+ if ((spec_target = strrchr(target_schema_path + 8, '/'))) {
+ /* schema node typedef */
+ /* HACK only temporary */
+ spec_target[0] = '\0';
+ ++spec_target;
+
+ rc = resolve_absolute_schema_nodeid(target_schema_path + 7, module,
+ LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC | LYS_ACTION,
+ (const struct lys_node **)&target);
+ if (rc || !target) {
+ /* perhaps it's in a grouping */
+ rc = resolve_absolute_schema_nodeid(target_schema_path + 7, module, LYS_GROUPING,
+ (const struct lys_node **)&target);
+ }
+ if (!rc && target) {
+ switch (target->nodetype) {
+ case LYS_CONTAINER:
+ tpdf = ((struct lys_node_container *)target)->tpdf;
+ tpdf_size = ((struct lys_node_container *)target)->tpdf_size;
+ break;
+ case LYS_LIST:
+ tpdf = ((struct lys_node_list *)target)->tpdf;
+ tpdf_size = ((struct lys_node_list *)target)->tpdf_size;
+ break;
+ case LYS_NOTIF:
+ tpdf = ((struct lys_node_notif *)target)->tpdf;
+ tpdf_size = ((struct lys_node_notif *)target)->tpdf_size;
+ break;
+ case LYS_RPC:
+ case LYS_ACTION:
+ tpdf = ((struct lys_node_rpc_action *)target)->tpdf;
+ tpdf_size = ((struct lys_node_rpc_action *)target)->tpdf_size;
+ break;
+ case LYS_GROUPING:
+ tpdf = ((struct lys_node_grp *)target)->tpdf;
+ tpdf_size = ((struct lys_node_grp *)target)->tpdf_size;
+ break;
+ default:
+ LOGINT(module->ctx);
+ return EXIT_FAILURE;
+ }
+ }
+ } else {
+ /* module typedef */
+ spec_target = (char *)target_schema_path + 8;
+ tpdf = module->tpdf;
+ tpdf_size = module->tpdf_size;
+ }
+
+ for (i = 0; i < tpdf_size; ++i) {
+ if (!strcmp(tpdf[i].name, spec_target)) {
+ clb_print_typedef(out, &tpdf[i], &f);
+ break;
+ }
+ }
+ /* HACK return previous hack */
+ --spec_target;
+ spec_target[0] = '/';
+
+ if (i == tpdf_size) {
+ ly_print(out, "Typedef %s not found.\n", target_schema_path);
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
+
+ } else if (!strncmp(target_schema_path, "identity/", 9)) {
+ target_schema_path += 9;
+ for (i = 0; i < (signed)module->ident_size; ++i) {
+ if (!strcmp(module->ident[i].name, target_schema_path)) {
+ break;
+ }
+ }
+ if (i == (signed)module->ident_size) {
+ ly_print(out, "Identity %s not found.\n", target_schema_path);
+ return EXIT_FAILURE;
+ }
+
+ clb_print_identity(out, &module->ident[i], &f);
+ return EXIT_SUCCESS;
+
+ } else if (!strncmp(target_schema_path, "feature/", 8)) {
+ target_schema_path += 8;
+ for (i = 0; i < module->features_size; ++i) {
+ if (!strcmp(module->features[i].name, target_schema_path)) {
+ break;
+ }
+ }
+ if (i == module->features_size) {
+ ly_print(out, "Feature %s not found.\n", target_schema_path);
+ return EXIT_FAILURE;
+ }
+
+ clb_print_feature(out, &module->features[i], &f);
+ return EXIT_SUCCESS;
+ } else {
+ ly_print(out, "Target could not be resolved.\n");
+ return EXIT_FAILURE;
+ }
+
+ if (!strncmp(target_schema_path, "type/", 5)) {
+ if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
+ LOGERR(module->ctx, LY_EINVAL, "Target is not a leaf or a leaf-list.");
+ return EXIT_FAILURE;
+ }
+ clb_print_type(out, &((struct lys_node_leaf *)target)->type, &f);
+ return EXIT_SUCCESS;
+ } else if (!strncmp(target_schema_path, "grouping/", 9) && !spec_target) {
+ clb_print_grouping(out, target, &f);
+ return EXIT_SUCCESS;
+ }
+
+ /* find the node in the grouping */
+ if (spec_target) {
+ rc = resolve_descendant_schema_nodeid(spec_target, target->child, LYS_NO_RPC_NOTIF_NODE,
+ 0, (const struct lys_node **)&target);
+ if (rc || !target) {
+ ly_print(out, "Grouping %s child \"%s\" not found.\n", target_schema_path + 9, spec_target);
+ return EXIT_FAILURE;
+ }
+ /* HACK return previous hack */
+ --spec_target;
+ spec_target[0] = '/';
+ }
+ switch (target->nodetype) {
+ case LYS_CONTAINER:
+ clb_print_container(out, target, &f);
+ break;
+ case LYS_CHOICE:
+ clb_print_choice(out, target, &f);
+ break;
+ case LYS_LEAF:
+ clb_print_leaf(out, target, &f);
+ break;
+ case LYS_LEAFLIST:
+ clb_print_leaflist(out, target, &f);
+ break;
+ case LYS_LIST:
+ clb_print_list(out, target, &f);
+ break;
+ case LYS_ANYXML:
+ case LYS_ANYDATA:
+ clb_print_anydata(out, target, &f);
+ break;
+ case LYS_CASE:
+ clb_print_case(out, target, &f);
+ break;
+ case LYS_NOTIF:
+ clb_print_notif(out, target, &f);
+ break;
+ case LYS_RPC:
+ clb_print_rpc(out, target, &f);
+ break;
+ case LYS_ACTION:
+ clb_print_action(out, target, &f);
+ break;
+ case LYS_INPUT:
+ clb_print_input(out, target, &f);
+ break;
+ case LYS_OUTPUT:
+ clb_print_output(out, target, &f);
+ break;
+ default:
+ ly_print(out, "Nodetype %s not supported.\n", strnodetype(target->nodetype));
+ break;
+ }
+
+ return EXIT_SUCCESS;
+}
+
static int
lyd_print_(struct lyout *out, const struct lyd_node *root, LYD_FORMAT format, int options)
{
diff --git a/src/printer.h b/src/printer.h
index 211681a..660b32b 100644
--- a/src/printer.h
+++ b/src/printer.h
@@ -71,19 +71,39 @@
int ly_write_skip(struct lyout *out, size_t count, size_t *position);
int ly_write_skipped(struct lyout *out, size_t position, const char *buf, size_t count);
-/* prefix_kind: 0 - print import prefixes for foreign features, 1 - print module names, 2 - print prefixes (tree printer) */
+/* prefix_kind: 0 - print import prefixes for foreign features, 1 - print module names, 2 - print prefixes (tree printer), 3 - print module names including revisions (JSONS printer) */
int ly_print_iffeature(struct lyout *out, const struct lys_module *module, struct lys_iffeature *expr, int prefix_kind);
int yang_print_model(struct lyout *out, const struct lys_module *module);
int yin_print_model(struct lyout *out, const struct lys_module *module);
int tree_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path, int line_length, int options);
int info_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path);
+int jsons_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path);
int json_print_data(struct lyout *out, const struct lyd_node *root, int options);
int xml_print_data(struct lyout *out, const struct lyd_node *root, int options);
int xml_print_node(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options);
int lyb_print_data(struct lyout *out, const struct lyd_node *root, int options);
+int lys_print_target(struct lyout *out, const struct lys_module *module, const char *target_schema_path,
+ void (*clb_print_typedef)(struct lyout*, const struct lys_tpdf*, int*),
+ void (*clb_print_identity)(struct lyout*, const struct lys_ident*, int*),
+ void (*clb_print_feature)(struct lyout*, const struct lys_feature*, int*),
+ void (*clb_print_type)(struct lyout*, const struct lys_type*, int*),
+ void (*clb_print_grouping)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_container)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_choice)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_leaf)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_leaflist)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_list)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_anydata)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_case)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_notif)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_rpc)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_action)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_input)(struct lyout*, const struct lys_node*, int*),
+ void (*clb_print_output)(struct lyout*, const struct lys_node*, int*));
+
/**
* get know if the node is supposed to be printed according to the specified with-default mode
* return 1 - print, 0 - do not print
diff --git a/src/printer_info.c b/src/printer_info.c
index 6f1187d..af34585 100644
--- a/src/printer_info.c
+++ b/src/printer_info.c
@@ -254,7 +254,7 @@
}
static void
-info_print_type_detail(struct lyout *out, const struct lys_type *type, int uni)
+info_print_type_detail_(struct lyout *out, const struct lys_type *type, int uni)
{
unsigned int i;
struct lys_type *orig;
@@ -386,7 +386,7 @@
if (!uni) {
for (i = 0; i < type->info.uni.count; ++i) {
- info_print_type_detail(out, &type->info.uni.types[i], 1);
+ info_print_type_detail_(out, &type->info.uni.types[i], 1);
}
}
break;
@@ -412,6 +412,12 @@
}
static void
+info_print_type_detail(struct lyout *out, const struct lys_type *type, int * UNUSED(first))
+{
+ return info_print_type_detail_(out, type, 0);
+}
+
+static void
info_print_list_constr(struct lyout *out, uint32_t min, uint32_t max)
{
ly_print(out, "%-*s%u..", INDENT_LEN, "Elements: ", min);
@@ -672,20 +678,20 @@
}
static void
-info_print_typedef_detail(struct lyout *outf, const struct lys_tpdf *tpdf)
+info_print_typedef_detail(struct lyout *outf, const struct lys_tpdf *tpdf, int * UNUSED(first))
{
ly_print(outf, "%-*s%s\n", INDENT_LEN, "Typedef: ", tpdf->name);
ly_print(outf, "%-*s%s\n", INDENT_LEN, "Module: ", tpdf->module->name);
info_print_text(outf, tpdf->dsc, "Desc: ");
info_print_text(outf, tpdf->ref, "Reference: ");
info_print_flags(outf, tpdf->flags, LYS_STATUS_MASK, 0);
- info_print_type_detail(outf, &tpdf->type, 0);
+ info_print_type_detail_(outf, &tpdf->type, 0);
info_print_text(outf, tpdf->units, "Units: ");
info_print_text(outf, tpdf->dflt, "Default: ");
}
static void
-info_print_ident_detail(struct lyout *out, const struct lys_ident *ident)
+info_print_ident_detail(struct lyout *out, const struct lys_ident *ident, int * UNUSED(first))
{
unsigned int i;
@@ -715,7 +721,7 @@
}
static void
-info_print_feature_detail(struct lyout *out, const struct lys_feature *feat)
+info_print_feature_detail(struct lyout *out, const struct lys_feature *feat, int * UNUSED(first))
{
ly_print(out, "%-*s%s\n", INDENT_LEN, "Feature: ", feat->name);
ly_print(out, "%-*s%s\n", INDENT_LEN, "Module: ", feat->module->name);
@@ -782,7 +788,7 @@
}
static void
-info_print_container(struct lyout *out, const struct lys_node *node)
+info_print_container(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_container *cont = (struct lys_node_container *)node;
@@ -801,7 +807,7 @@
}
static void
-info_print_choice(struct lyout *out, const struct lys_node *node)
+info_print_choice(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_choice *choice = (struct lys_node_choice *)node;
@@ -823,7 +829,7 @@
}
static void
-info_print_leaf(struct lyout *out, const struct lys_node *node)
+info_print_leaf(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
@@ -841,7 +847,7 @@
}
static void
-info_print_leaflist(struct lyout *out, const struct lys_node *node)
+info_print_leaflist(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
@@ -859,7 +865,7 @@
}
static void
-info_print_list(struct lyout *out, const struct lys_node *node)
+info_print_list(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_list *list = (struct lys_node_list *)node;
@@ -880,7 +886,7 @@
}
static void
-info_print_anydata(struct lyout *out, const struct lys_node *node)
+info_print_anydata(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_anydata *any = (struct lys_node_anydata *)node;
@@ -895,7 +901,7 @@
}
static void
-info_print_grouping(struct lyout *out, const struct lys_node *node)
+info_print_grouping(struct lyout *out, const struct lys_node *node, int * UNUSED(first))
{
struct lys_node_grp *group = (struct lys_node_grp *)node;
@@ -910,7 +916,7 @@
}
static void
-info_print_case(struct lyout *out, const struct lys_node *node)
+info_print_case(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_case *cas = (struct lys_node_case *)node;
@@ -926,7 +932,7 @@
}
static void
-info_print_input(struct lyout *out, const struct lys_node *node)
+info_print_input(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_inout *input = (struct lys_node_inout *)node;
@@ -940,7 +946,7 @@
}
static void
-info_print_output(struct lyout *out, const struct lys_node *node)
+info_print_output(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_inout *output = (struct lys_node_inout *)node;
@@ -954,7 +960,7 @@
}
static void
-info_print_notif(struct lyout *out, const struct lys_node *node)
+info_print_notif(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_notif *ntf = (struct lys_node_notif *)node;
@@ -971,7 +977,7 @@
}
static void
-info_print_rpc(struct lyout *out, const struct lys_node *node)
+info_print_rpc(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_rpc_action *rpc = (struct lys_node_rpc_action *)node;
@@ -987,7 +993,7 @@
}
static void
-info_print_action(struct lyout *out, const struct lys_node *node)
+info_print_action(struct lyout *out, const struct lys_node *node, int *UNUSED(first))
{
struct lys_node_rpc_action *act = (struct lys_node_rpc_action *)node;
@@ -1005,11 +1011,7 @@
int
info_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path)
{
- int i, rc;
- char *spec_target = NULL;
- struct lys_node *target = NULL;
- struct lys_tpdf *tpdf = NULL;
- uint8_t tpdf_size = 0;
+ int rc = EXIT_SUCCESS;
if (!target_schema_path) {
if (module->type == 0) {
@@ -1018,194 +1020,26 @@
info_print_submodule(out, (struct lys_submodule *)module);
}
} else {
- if ((target_schema_path[0] == '/') || !strncmp(target_schema_path, "type/", 5)) {
- rc = resolve_absolute_schema_nodeid((target_schema_path[0] == '/' ? target_schema_path : target_schema_path + 4), module,
- LYS_ANY & ~(LYS_USES | LYS_AUGMENT | LYS_GROUPING), (const struct lys_node **)&target);
- if (rc || !target) {
- ly_print(out, "Target %s could not be resolved.\n", (target_schema_path[0] == '/' ? target_schema_path : target_schema_path + 4));
- return EXIT_FAILURE;
- }
- } else if (!strncmp(target_schema_path, "grouping/", 9)) {
- /* cut the data part off */
- if ((spec_target = strchr(target_schema_path + 9, '/'))) {
- /* HACK only temporary */
- spec_target[0] = '\0';
- ++spec_target;
- }
- rc = resolve_absolute_schema_nodeid(target_schema_path + 8, module, LYS_GROUPING, (const struct lys_node **)&target);
- if (rc || !target) {
- ly_print(out, "Grouping %s not found.\n", target_schema_path + 8);
- return EXIT_FAILURE;
- }
- } else if (!strncmp(target_schema_path, "typedef/", 8)) {
- if ((spec_target = strrchr(target_schema_path + 8, '/'))) {
- /* schema node typedef */
- /* HACK only temporary */
- spec_target[0] = '\0';
- ++spec_target;
-
- rc = resolve_absolute_schema_nodeid(target_schema_path + 7, module,
- LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC | LYS_ACTION,
- (const struct lys_node **)&target);
- if (rc || !target) {
- /* perhaps it's in a grouping */
- rc = resolve_absolute_schema_nodeid(target_schema_path + 7, module, LYS_GROUPING,
- (const struct lys_node **)&target);
- }
- if (!rc && target) {
- switch (target->nodetype) {
- case LYS_CONTAINER:
- tpdf = ((struct lys_node_container *)target)->tpdf;
- tpdf_size = ((struct lys_node_container *)target)->tpdf_size;
- break;
- case LYS_LIST:
- tpdf = ((struct lys_node_list *)target)->tpdf;
- tpdf_size = ((struct lys_node_list *)target)->tpdf_size;
- break;
- case LYS_NOTIF:
- tpdf = ((struct lys_node_notif *)target)->tpdf;
- tpdf_size = ((struct lys_node_notif *)target)->tpdf_size;
- break;
- case LYS_RPC:
- case LYS_ACTION:
- tpdf = ((struct lys_node_rpc_action *)target)->tpdf;
- tpdf_size = ((struct lys_node_rpc_action *)target)->tpdf_size;
- break;
- case LYS_GROUPING:
- tpdf = ((struct lys_node_grp *)target)->tpdf;
- tpdf_size = ((struct lys_node_grp *)target)->tpdf_size;
- break;
- default:
- LOGINT(module->ctx);
- return EXIT_FAILURE;
- }
- }
- } else {
- /* module typedef */
- spec_target = (char *)target_schema_path + 8;
- tpdf = module->tpdf;
- tpdf_size = module->tpdf_size;
- }
-
- for (i = 0; i < tpdf_size; ++i) {
- if (!strcmp(tpdf[i].name, spec_target)) {
- info_print_typedef_detail(out, &tpdf[i]);
- break;
- }
- }
- /* HACK return previous hack */
- --spec_target;
- spec_target[0] = '/';
-
- if (i == tpdf_size) {
- ly_print(out, "Typedef %s not found.\n", target_schema_path);
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
-
- } else if (!strncmp(target_schema_path, "identity/", 9)) {
- target_schema_path += 9;
- for (i = 0; i < (signed)module->ident_size; ++i) {
- if (!strcmp(module->ident[i].name, target_schema_path)) {
- break;
- }
- }
- if (i == (signed)module->ident_size) {
- ly_print(out, "Identity %s not found.\n", target_schema_path);
- return EXIT_FAILURE;
- }
-
- info_print_ident_detail(out, &module->ident[i]);
- return EXIT_SUCCESS;
-
- } else if (!strncmp(target_schema_path, "feature/", 8)) {
- target_schema_path += 8;
- for (i = 0; i < module->features_size; ++i) {
- if (!strcmp(module->features[i].name, target_schema_path)) {
- break;
- }
- }
- if (i == module->features_size) {
- ly_print(out, "Feature %s not found.\n", target_schema_path);
- return EXIT_FAILURE;
- }
-
- info_print_feature_detail(out, &module->features[i]);
- return EXIT_SUCCESS;
- } else {
- ly_print(out, "Target could not be resolved.\n");
- return EXIT_FAILURE;
- }
-
- if (!strncmp(target_schema_path, "type/", 5)) {
- if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
- ly_print(out, "Target is not a leaf or a leaf-list.\n");
- return EXIT_FAILURE;
- }
- info_print_type_detail(out, &((struct lys_node_leaf *)target)->type, 0);
- return EXIT_SUCCESS;
- } else if (!strncmp(target_schema_path, "grouping/", 9) && !spec_target) {
- info_print_grouping(out, target);
- return EXIT_SUCCESS;
- }
-
- /* find the node in the grouping */
- if (spec_target) {
- rc = resolve_descendant_schema_nodeid(spec_target, target->child, LYS_NO_RPC_NOTIF_NODE,
- 0, (const struct lys_node **)&target);
- if (rc || !target) {
- ly_print(out, "Grouping %s child \"%s\" not found.\n", target_schema_path + 9, spec_target);
- return EXIT_FAILURE;
- }
- /* HACK return previous hack */
- --spec_target;
- spec_target[0] = '/';
- }
-
- switch (target->nodetype) {
- case LYS_CONTAINER:
- info_print_container(out, target);
- break;
- case LYS_CHOICE:
- info_print_choice(out, target);
- break;
- case LYS_LEAF:
- info_print_leaf(out, target);
- break;
- case LYS_LEAFLIST:
- info_print_leaflist(out, target);
- break;
- case LYS_LIST:
- info_print_list(out, target);
- break;
- case LYS_ANYXML:
- case LYS_ANYDATA:
- info_print_anydata(out, target);
- break;
- case LYS_CASE:
- info_print_case(out, target);
- break;
- case LYS_NOTIF:
- info_print_notif(out, target);
- break;
- case LYS_RPC:
- info_print_rpc(out, target);
- break;
- case LYS_ACTION:
- info_print_action(out, target);
- break;
- case LYS_INPUT:
- info_print_input(out, target);
- break;
- case LYS_OUTPUT:
- info_print_output(out, target);
- break;
- default:
- ly_print(out, "Nodetype %s not supported.\n", strnodetype(target->nodetype));
- break;
- }
+ rc = lys_print_target(out, module, target_schema_path,
+ info_print_typedef_detail,
+ info_print_ident_detail,
+ info_print_feature_detail,
+ info_print_type_detail,
+ info_print_grouping,
+ info_print_container,
+ info_print_choice,
+ info_print_leaf,
+ info_print_leaflist,
+ info_print_list,
+ info_print_anydata,
+ info_print_case,
+ info_print_notif,
+ info_print_rpc,
+ info_print_action,
+ info_print_input,
+ info_print_output);
}
ly_print_flush(out);
- return EXIT_SUCCESS;
+ return rc;
}
diff --git a/src/printer_json.c b/src/printer_json.c
index 5514c0c..6c77827 100644
--- a/src/printer_json.c
+++ b/src/printer_json.c
@@ -30,7 +30,7 @@
static int json_print_nodes(struct lyout *out, int level, const struct lyd_node *root, int withsiblings, int toplevel,
int options);
-static int
+int
json_print_string(struct lyout *out, const char *text)
{
unsigned int i, n;
@@ -43,7 +43,7 @@
for (i = n = 0; text[i]; i++) {
if (text[i] >= 0 && text[i] < 0x20) {
/* control character */
- n += ly_print(out, "\\u%.4X");
+ n += ly_print(out, "\\u%.4X", text[i]);
} else {
switch (text[i]) {
case '"':
diff --git a/src/printer_json_schema.c b/src/printer_json_schema.c
new file mode 100644
index 0000000..edfda40
--- /dev/null
+++ b/src/printer_json_schema.c
@@ -0,0 +1,1300 @@
+/**
+ * @file printer/info.c
+ * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @brief Schema JSON printer for libyang data model structure
+ *
+ * Copyright (c) 2018 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "common.h"
+#include "printer.h"
+#include "tree_schema.h"
+#include "resolve.h"
+
+#define INDENT_LEN 2
+
+static const char *
+jsons_nodetype_str(LYS_NODE value) {
+ switch (value) {
+ case LYS_CONTAINER: return "container";
+ case LYS_CHOICE: return "choice";
+ case LYS_LEAF: return "leaf";
+ case LYS_LEAFLIST: return "leaf-list";
+ case LYS_LIST: return "list";
+ case LYS_ANYXML: return "anyxml";
+ case LYS_CASE: return "case";
+ case LYS_NOTIF: return "notification";
+ case LYS_RPC: return "rpc";
+ case LYS_INPUT: return "input";
+ case LYS_OUTPUT: return "output";
+ case LYS_ACTION: return "action";
+ case LYS_ANYDATA: return "anydata";
+ default: return NULL;
+ }
+}
+
+/* shared with printer_json.c */
+int json_print_string(struct lyout *out, const char *text);
+
+static void jsons_print_data(struct lyout *out, const struct lys_module *mod, struct lys_node *data, int *first);
+static void jsons_print_notifs(struct lyout *out, struct lys_node *data, int *first);
+static void jsons_print_actions(struct lyout *out, struct lys_node *data, int *first);
+
+static void
+jsons_print_text(struct lyout *out, const char *label, const char *arg, const char *text, int closeit, int *first)
+{
+ if (!text) {
+ return;
+ }
+ ly_print(out, "%s\"%s\":{\"%s\":", (first && (*first)) ? "" : ",", label, arg);
+ json_print_string(out, text);
+ if (closeit) {
+ ly_print(out, "}");
+ }
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_object(struct lyout *out, const char *label, const char *arg, const char *val, int closeit, int *first)
+{
+ if (!val) {
+ return;
+ }
+
+ ly_print(out, "%s\"%s\":{\"%s\":\"%s\"%s", (first && (*first)) ? "" : ",", label, arg, val, closeit ? "}" : "");
+
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_status(struct lyout *out, uint16_t flags, int *first)
+{
+ const char *str;
+
+ if (flags & LYS_STATUS_MASK) {
+ if (flags & LYS_STATUS_OBSLT) {
+ str = "obsolete";
+ } else if (flags & LYS_STATUS_DEPRC) {
+ str = "deprecated";
+ } else {
+ str = "current";
+ }
+ jsons_print_object(out, "status", "value", str, 1, first);
+ }
+}
+
+static void
+jsons_print_config(struct lyout *out, uint16_t flags, int *first)
+{
+ const char *str = NULL;
+
+ if (flags & LYS_CONFIG_MASK) {
+ if (flags & LYS_CONFIG_R) {
+ str = "false";
+ } else if (flags & LYS_CONFIG_W) {
+ str = "true";
+ }
+ jsons_print_object(out, "config", "value", str, 1, first);
+ }
+}
+
+static void
+jsons_print_mand(struct lyout *out, uint16_t flags, int *first)
+{
+ const char *str;
+
+ if (flags & LYS_MAND_MASK) {
+ if (flags & LYS_MAND_TRUE) {
+ str = "true";
+ } else if (flags & LYS_MAND_FALSE) {
+ str = "false";
+ }
+ jsons_print_object(out, "mandatory", "value", str, 1, first);
+ }
+}
+
+static void
+jsons_print_ordering(struct lyout *out, uint16_t flags, int *first)
+{
+ if (flags & LYS_USERORDERED) {
+ jsons_print_object(out, "ordered-by", "value", "user", 1, first);
+ } else {
+ jsons_print_object(out, "ordered-by", "value", "system", 1, first);
+ }
+}
+
+static void
+jsons_print_iffeatures(struct lyout *out, const struct lys_module *module,
+ struct lys_iffeature *iff, uint8_t iff_size, int *first)
+{
+ int i;
+
+ if (!iff_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"if-features\":[", (first && (*first)) ? "" : ",");
+ for (i = 0; i < iff_size; ++i) {
+ ly_print(out, "%s\"", i ? "," : "");
+ ly_print_iffeature(out, module, &iff[i], 3);
+ ly_print(out, "\"");
+ }
+ ly_print(out, "]");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_when(struct lyout *out, const struct lys_when *when, int *first)
+{
+ if (!when) {
+ return;
+ }
+ jsons_print_text(out, "when", "condition", when->cond, 0, first);
+ jsons_print_text(out, "description", "text", when->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", when->ref, 1, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_typerestr(struct lyout *out, const struct lys_restr *restr, const char *label, int *first)
+{
+ int pattern = 0;
+
+ if (!restr) {
+ return;
+ }
+ if (restr->expr[0] == 0x06 || restr->expr[0] == 0x15) {
+ pattern = 1;
+ }
+
+ if (label) {
+ jsons_print_text(out, label, "value", pattern ? &restr->expr[1] : restr->expr, 0, first);
+ } else {
+ ly_print(out, "%s{\"%s\":", (first && (*first)) ? "" : ",", "value");
+ json_print_string(out, pattern ? &restr->expr[1] : restr->expr);
+ }
+ if (pattern && restr->expr[0] == 0x15) {
+ jsons_print_object(out, "modifier", "value", "invert-match", 1, NULL);
+ }
+ jsons_print_text(out, "description", "text", restr->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", restr->ref, 1, NULL);
+ jsons_print_object(out, "error-app-tag", "value", restr->eapptag, 1, NULL);
+ jsons_print_text(out, "error-message", "value", restr->emsg, 1, NULL);
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_musts(struct lyout *out, const struct lys_restr *must, uint8_t must_size, int *first)
+{
+ int i, f;
+
+ if (!must_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"musts\":[", (first && (*first)) ? "" : ",");
+ f = 1;
+ for (i = 0; i < must_size; ++i) {
+ jsons_print_typerestr(out, &must[i], NULL, &f);
+ }
+ ly_print(out, "]");
+}
+
+static void
+jsons_print_type_(struct lyout *out, const struct lys_type *type, int with_label, int *first)
+{
+ unsigned int i;
+ int f;
+ struct lys_module *mod;
+
+ if (!type) {
+ return;
+ }
+
+ if (with_label) {
+ ly_print(out, "%s\"type\":{", (first && (*first)) ? "" : ",");
+ } else {
+ ly_print(out, "%s{", (first && (*first)) ? "" : ",");
+ }
+
+ switch (type->base) {
+ case LY_TYPE_BINARY:
+ ly_print(out, "\"basetype\":\"binary\"");
+ jsons_print_typerestr(out, type->info.binary.length, "length", NULL);
+ break;
+ case LY_TYPE_BITS:
+ ly_print(out, "\"basetype\":\"bits\"");
+ ly_print(out, ",\"bits\":[");
+ for (i = 0; i < type->info.bits.count; ++i) {
+ ly_print(out, "%s{\"position\":\"%u\",\"name\":\"%s\"", i ? "," : "",
+ type->info.bits.bit[i].pos, type->info.bits.bit[i].name);
+ jsons_print_text(out, "description", "text", type->info.bits.bit[i].dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", type->info.bits.bit[i].ref, 1, NULL);
+ jsons_print_status(out, type->info.bits.bit[i].flags, NULL);
+ jsons_print_iffeatures(out, type->parent->module,
+ type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size, NULL);
+ ly_print(out, "}");
+ }
+ ly_print(out, "]");
+ break;
+ case LY_TYPE_BOOL:
+ ly_print(out, "\"basetype\":\"boolean\"");
+ break;
+ case LY_TYPE_DEC64:
+ ly_print(out, "\"basetype\":\"decimal64\"");
+ jsons_print_typerestr(out, type->info.dec64.range, "range", NULL);
+ ly_print(out, ",\"fraction-digits\":{\"value\":\"%u\"}", type->info.dec64.dig);
+ break;
+ case LY_TYPE_EMPTY:
+ ly_print(out, "\"basetype\":\"empty\"");
+ break;
+ case LY_TYPE_ENUM:
+ ly_print(out, "\"basetype\":\"enumeration\"");
+ ly_print(out, ",\"enums\":[");
+ for (i = 0; i < type->info.enums.count; ++i) {
+ ly_print(out, "%s{\"value\":\"%d\",\"name\":\"%s\"", i ? "," : "",
+ type->info.enums.enm[i].value, type->info.enums.enm[i].name);
+ jsons_print_text(out, "description", "text", type->info.enums.enm[i].dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", type->info.enums.enm[i].ref, 1, NULL);
+ jsons_print_status(out, type->info.enums.enm[i].flags, NULL);
+ jsons_print_iffeatures(out, type->parent->module,
+ type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size, NULL);
+ ly_print(out, "}");
+ }
+ ly_print(out, "]");
+ break;
+ case LY_TYPE_IDENT:
+ ly_print(out, "\"basetype\":\"identityref\"");
+ if (type->info.ident.count) {
+ ly_print(out, ",\"bases\":[");
+ for (i = 0; i < type->info.ident.count; ++i) {
+ mod = type->info.ident.ref[i]->module;
+ ly_print(out, "%s\"%s%s%s:%s\"", i ? "," : "",
+ mod->name, mod->rev_size ? "@" : "", mod->rev_size ? mod->rev[0].date : "",
+ type->info.ident.ref[i]->name);
+ }
+ ly_print(out, "]");
+ }
+ break;
+ case LY_TYPE_INST:
+ ly_print(out, "\"basetype\":\"instance-identifier\"");
+ if (type->info.inst.req) {
+ jsons_print_object(out, "require-instance", "value", type->info.inst.req == -1 ? "false" : "true", 1, NULL);
+ }
+ break;
+ case LY_TYPE_INT8:
+ ly_print(out, "\"basetype\":\"int8\"");
+ goto int_range;
+ case LY_TYPE_INT16:
+ ly_print(out, "\"basetype\":\"int16\"");
+ goto int_range;
+ case LY_TYPE_INT32:
+ ly_print(out, "\"basetype\":\"int32\"");
+ goto int_range;
+ case LY_TYPE_INT64:
+ ly_print(out, "\"basetype\":\"int64\"");
+ goto int_range;
+ case LY_TYPE_UINT8:
+ ly_print(out, "\"basetype\":\"uint8\"");
+ goto int_range;
+ case LY_TYPE_UINT16:
+ ly_print(out, "\"basetype\":\"uint16\"");
+ goto int_range;
+ case LY_TYPE_UINT32:
+ ly_print(out, "\"basetype\":\"uint32\"");
+ goto int_range;
+ case LY_TYPE_UINT64:
+ ly_print(out, "\"basetype\":\"uint64\"");
+
+int_range:
+ jsons_print_typerestr(out, type->info.num.range, "range", NULL);
+ break;
+ case LY_TYPE_LEAFREF:
+ ly_print(out, "\"basetype\":\"leafref\"");
+ jsons_print_text(out, "path", "value", type->info.lref.path, 1, NULL);
+ if (type->info.lref.req) {
+ jsons_print_object(out, "require-instance", "value", type->info.lref.req == -1 ? "false" : "true", 1, NULL);
+ }
+ break;
+ case LY_TYPE_STRING:
+ ly_print(out, "\"basetype\":\"string\"");
+ jsons_print_typerestr(out, type->info.str.length, "length", NULL);
+ if (type->info.str.pat_count) {
+ ly_print(out, ",\"patterns\":[");
+ f = 1;
+ for (i = 0; i < type->info.str.pat_count; ++i) {
+ jsons_print_typerestr(out, &type->info.str.patterns[i], NULL, &f);
+ }
+ ly_print(out, "]");
+ }
+
+ break;
+ case LY_TYPE_UNION:
+ ly_print(out, "\"basetype\":\"union\"");
+ ly_print(out, ",\"types\":[");
+ f = 1;
+ for (i = 0; i < type->info.uni.count; ++i) {
+ jsons_print_type_(out, &type->info.uni.types[i], 0, &f);
+ }
+ ly_print(out, "]");
+ break;
+ default:
+ /* unused outside libyang, we never should be here */
+ LOGINT(type->parent->module->ctx);
+ break;
+ }
+
+ if (type->der) {
+ ly_print(out, ",\"derived-from\":");
+ if (type->der->module) {
+ mod = type->der->module;
+ ly_print(out, "\"%s%s%s:%s\"",
+ mod->name, mod->rev_size ? "@" : "", mod->rev_size ? mod->rev[0].date : "",
+ type->der->name);
+ } else {
+ ly_print(out, "\"%s\"", type->der->name);
+ }
+ }
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_type(struct lyout *out, const struct lys_type *type, int *first)
+{
+ return jsons_print_type_(out, type, 1, first);
+}
+
+static void
+jsons_print_typedef(struct lyout *out, const struct lys_tpdf *tpdf, int *first)
+{
+ int f;
+
+ ly_print(out, "%s\"%s\":{", (first && (*first)) ? "" : ",", tpdf->name);
+ f = 1;
+ jsons_print_type(out, &tpdf->type, &f);
+ jsons_print_text(out, "description", "text", tpdf->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", tpdf->ref, 1, NULL);
+ jsons_print_status(out, tpdf->flags, NULL);
+ jsons_print_object(out, "units", "name", tpdf->units, 1, NULL);
+ jsons_print_object(out, "default", "value", tpdf->dflt, 1, NULL);
+ ly_print(out, "}");
+
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_typedefs(struct lyout *out, const struct lys_tpdf *tpdf, uint8_t tpdf_size, int *first)
+{
+ int i;
+
+ if (!tpdf_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"typedefs\":[", (first && (*first)) ? "" : ",");
+ for (i = 0; i < tpdf_size; ++i) {
+ ly_print(out, "%s\"%s\"", i ? "," : "", tpdf[i].name);
+ }
+ ly_print(out, "]");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_min(struct lyout *out, uint32_t min, int *first)
+{
+ ly_print(out, "%s\"min-elements\":{\"value\":%u}", (first && (*first)) ? "" : ",", min);
+}
+
+static void
+jsons_print_max(struct lyout *out, uint32_t max, int *first)
+{
+ ly_print(out, "%s\"max-elements\":{\"value\":%u}", (first && (*first)) ? "" : ",", max);
+}
+
+static void
+jsons_print_uniques(struct lyout *out, const struct lys_unique *unique, uint8_t unique_size, int *first)
+{
+ int i, j;
+
+ if (!unique_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"uniques\":[", (first && (*first)) ? "" : ",");
+ for (i = 0; i < unique_size; ++i) {
+ ly_print(out, "%s[", i ? "," : "");
+ for (j = 0; j < unique[i].expr_size; ++j) {
+ ly_print(out, "%s\"%s\"", j ? "," : "", unique[i].expr[j]);
+ }
+ ly_print(out, "]");
+ }
+ ly_print(out, "]");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_defaults(struct lyout *out, const char **dflts, uint8_t dflts_size, int *first)
+{
+ int i;
+
+ if (!dflts_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"defaults\":[", (first && (*first)) ? "" : ",");
+ for (i = 0; i < dflts_size; ++i) {
+ ly_print(out, "%s\"%s\"", i ? "," : "", dflts[i]);
+ }
+ ly_print(out, "]");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_revisions(struct lyout *out, const struct lys_revision *rev, uint8_t rev_size, int *first)
+{
+ int i, f;
+
+ if (!rev_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"revision\":{", (first && (*first)) ? "" : ",");
+ for (i = 0; i < rev_size; ++i) {
+ ly_print(out, "%s\"%s\":{", i ? "," : "", rev[i].date);
+ f = 1;
+ jsons_print_text(out, "description", "text", rev[i].dsc, 1, &f);
+ jsons_print_text(out, "reference", "text", rev[i].ref, 1, &f);
+ ly_print(out, "}");
+ }
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_imports_(struct lyout *out, const struct lys_submodule *submodule,
+ const struct lys_import *imp, uint8_t imp_size, char **label)
+{
+ int i, j = 1, f;
+ char *str;
+
+ if (imp_size && (*label)) {
+ ly_print(out, *label);
+ free(*label);
+ (*label) = NULL;
+ j = 0;
+ }
+ for (i = 0; i < imp_size; ++i) {
+ ly_print(out, "%s\"%s%s%s\":{", i + j ? "," : "", imp[i].module->name,
+ imp[i].rev[0] ? "@" : "", imp[i].rev);
+ f = 1;
+ jsons_print_object(out, "prefix", "value", imp[i].prefix, 1, &f);
+ jsons_print_text(out, "description", "text", imp[i].dsc, 1, &f);
+ jsons_print_text(out, "reference", "text", imp[i].ref, 1, &f);
+ if (submodule) {
+ ly_print(out, ",\"from-submodule\":\"%s%s%s\"", submodule->name,
+ submodule->rev_size ? "@" : "", submodule->rev_size ? submodule->rev[0].date : "");
+ }
+ asprintf(&str, "%s%s%s", imp[i].module->name, imp[i].module->rev_size ? "@" : "", imp[i].module->rev_size ? imp[i].module->rev[0].date : "");
+ jsons_print_text(out, "resolves-to", "module", str, 1, &f);
+ free(str);
+ ly_print(out, "}");
+ }
+}
+
+static void
+jsons_print_imports(struct lyout *out, const struct lys_module *mod, int *first)
+{
+ char *str;
+
+ if (!mod->imp_size && !mod->inc_size) {
+ return;
+ }
+
+ asprintf(&str, "%s\"import\":{", (first && (*first)) ? "" : ",");
+ jsons_print_imports_(out, NULL, mod->imp, mod->imp_size, &str);
+ /* FIXME key duplication in case multiple submodules imports the same module,
+ * but the question is if it is needed to print imports even from submodules -
+ * similar code should be then added even for typedefs or identities
+ for (i = 0; i < mod->inc_size; ++i) {
+ jsons_print_imports_(out, mod->inc[i].submodule, mod->inc[i].submodule->imp, mod->inc[i].submodule->imp_size, &str);
+ }
+ */
+ if (str) {
+ free(str);
+ } else {
+ ly_print(out, "}");
+ }
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_includes(struct lyout *out, const struct lys_include *inc, uint8_t inc_size, int *first)
+{
+ int i, f;
+
+ if (!inc_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"include\":{", (first && (*first)) ? "" : ",");
+ for (i = 0; i < inc_size; ++i) {
+ ly_print(out, "%s\"%s%s%s\":{", i ? "," : "", inc[i].submodule->name,
+ inc[i].rev[0] ? "@" : "", inc[i].rev);
+ f = 1;
+ jsons_print_text(out, "description", "text", inc[i].dsc, 1, &f);
+ jsons_print_text(out, "reference", "text", inc[i].ref, 1, &f);
+ ly_print(out, "}");
+ }
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_augment(struct lyout *out, const struct lys_node_augment *aug, uint8_t aug_size, int *first)
+{
+ int i, f;
+
+ if (!aug_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"augment\":{", (first && (*first)) ? "" : ",");
+ for (i = 0; i < aug_size; ++i) {
+ ly_print(out, "%s\"%s\":{", i ? "," : "", aug[i].target_name);
+ f = 1;
+ jsons_print_text(out, "description", "text", aug[i].dsc, 1, &f);
+ jsons_print_text(out, "reference", "text", aug[i].ref, 1, &f);
+ jsons_print_status(out, aug[i].flags, &f);
+ jsons_print_iffeatures(out, aug[i].module, aug[i].iffeature, aug[i].iffeature_size, &f);
+ jsons_print_when(out, aug[i].when, &f);
+ jsons_print_data(out, aug->module, aug->child, &f);
+ jsons_print_actions(out, aug->child, &f);
+ jsons_print_notifs(out, aug->child, &f);
+ ly_print(out, "}");
+ }
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+
+ if (first) {
+ ly_print(out, "\n");
+ }
+}
+
+static void
+jsons_print_deviation(struct lyout *out, const struct lys_deviation *dev, uint8_t dev_size, int *first)
+{
+ int i, j, f, f2;
+
+ if (!dev_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"deviations\":{", (first && (*first)) ? "" : ",");
+ for (i = 0; i < dev_size; ++i) {
+ ly_print(out, "%s\"%s\":{", i ? "," : "", dev[i].target_name);
+ f = 1;
+ jsons_print_text(out, "description", "text", dev[i].dsc, 1, &f);
+ jsons_print_text(out, "reference", "text", dev[i].ref, 1, &f);
+ if (dev[i].deviate_size) {
+ ly_print(out, "%s\"deviates\":[", f ? "" : ",");
+ f = 0;
+ f2 = 1;
+ for (j = 0; j < dev[i].deviate_size; ++j) {
+ ly_print(out, "%s{", j ? "" : ",");
+ jsons_print_config(out, dev[i].deviate[j].flags, &f2);
+ jsons_print_defaults(out, dev[i].deviate[j].dflt, dev[i].deviate[j].dflt_size, &f2);
+ jsons_print_mand(out, dev[i].deviate[j].flags, &f2);
+ if (dev[i].deviate[j].min_set) {
+ ly_print(out, "%s\"min-elements\":{\"value\":%u}", f2 ? "" : ",", dev[i].deviate[j].min);
+ f2 = 0;
+ }
+ if (dev[i].deviate[j].max_set) {
+ ly_print(out, "%s\"max-elements\":{\"value\":%u}", f2 ? "" : ",", dev[i].deviate[j].max);
+ f2 = 0;
+ }
+ jsons_print_musts(out, dev[i].deviate[j].must, dev[i].deviate[j].must_size, &f2);
+ jsons_print_type(out, dev[i].deviate[j].type, &f2);
+ jsons_print_uniques(out, dev[i].deviate[j].unique, dev[i].deviate[j].unique_size, &f2);
+ jsons_print_text(out, "units", "name", dev[i].deviate[j].units, 1, &f2);
+ ly_print(out, "}");
+ }
+ ly_print(out, "]");
+ }
+ ly_print(out, "}");
+ }
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_identity(struct lyout *out, const struct lys_ident *ident, int *first)
+{
+ int f = 1, j;
+ struct lys_module *mod;
+
+ ly_print(out, "%s\"%s\":{", (first && (*first)) ? "" : ",", ident->name);
+ if (ident->base_size) {
+ ly_print(out, "\"bases\":[");
+ f = 0;
+ for (j = 0; j < ident->base_size; ++j) {
+ mod = ident->base[j]->module;
+ ly_print(out, "%s\"%s%s%s:%s\"", j ? "," : "",
+ mod->name, mod->rev_size ? "@" : "", mod->rev_size ? mod->rev[0].date : "",
+ ident->base[j]->name);
+ }
+ ly_print(out, "]");
+ }
+ jsons_print_text(out, "description", "text", ident->dsc, 1, &f);
+ jsons_print_text(out, "reference", "text", ident->ref, 1, &f);
+ jsons_print_status(out, ident->flags, &f);
+ jsons_print_iffeatures(out, ident->module, ident->iffeature, ident->iffeature_size, &f);
+
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_identities(struct lyout *out, const struct lys_ident *ident, uint16_t ident_size, int *first)
+{
+ int i;
+
+ if (!ident_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"identities\":[", (first && (*first)) ? "" : ",");
+ for (i = 0; i < ident_size; ++i) {
+ ly_print(out, "%s\"%s\"", i ? "," : "", ident[i].name);
+ }
+ ly_print(out, "]");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_feature(struct lyout *out, const struct lys_feature *feat, int *first)
+{
+ int f = 1;
+ unsigned int j;
+
+ ly_print(out, "%s\"%s\":{", (first && (*first)) ? "" : ",", feat->name);
+ jsons_print_text(out, "description", "text", feat->dsc, 1, &f);
+ jsons_print_text(out, "reference", "text", feat->ref, 1, &f);
+ jsons_print_status(out, feat->flags, &f);
+ jsons_print_iffeatures(out, feat->module, feat->iffeature, feat->iffeature_size, &f);
+ if (feat->depfeatures && feat->depfeatures->number) {
+ ly_print(out, "%s\"depending-features\":[", f ? "" : ",");
+ for (j = 0; j < feat->depfeatures->number; ++j) {
+ ly_print(out, "%s\"%s\"", j ? "," : "", ((struct lys_feature*)(feat->depfeatures->set.g[j]))->name);
+ }
+ ly_print(out, "]");
+ }
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_features(struct lyout *out, const struct lys_feature *feat, uint8_t feat_size, int *first)
+{
+ int i;
+
+ if (!feat_size) {
+ return;
+ }
+
+ ly_print(out, "%s\"features\":[", (first && (*first)) ? "" : ",");
+ for (i = 0; i < feat_size; ++i) {
+ ly_print(out, "%s\"%s\"", i ? "," : "", feat[i].name);
+ }
+ ly_print(out, "]");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_data_(struct lyout *out, const struct lys_module *mod, struct lys_node *data, int *first)
+{
+ struct lys_node *node;
+ int mask = LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE | LYS_USES | LYS_ANYDATA;
+
+ LY_TREE_FOR(data, node) {
+ if (!(node->nodetype & mask)) {
+ continue;
+ }
+ if (node->nodetype & (LYS_USES)) {
+ jsons_print_data_(out, mod, node->child, first);
+ } else if (lys_main_module(mod) == lys_main_module(node->module)) {
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ ly_print(out, "}");
+ } else {
+ ly_print(out, "\"%s:%s\":{\"nodetype\":\"%s\"}", lys_main_module(node->module)->name, node->name,
+ jsons_nodetype_str(node->nodetype));
+ (*first) = 0;
+ }
+ }
+}
+
+static void
+jsons_print_data(struct lyout *out, const struct lys_module *mod, struct lys_node *data, int *first)
+{
+ int f;
+
+ ly_print(out, "%s\"data\":{", (first && (*first)) ? "" : ",");
+ f = 1;
+ jsons_print_data_(out, mod, data, &f);
+ ly_print(out, "}");
+ if (first) {
+ (*first) = 0;
+ }
+}
+
+static void
+jsons_print_nodes_uses_(struct lyout *out, struct lys_node *data, const char *label, int mask, int *top_first, int *first)
+{
+ struct lys_node *node;
+ LY_TREE_FOR(data, node) {
+ if (!(node->nodetype & mask)) {
+ continue;
+ }
+ if (node->nodetype & (LYS_USES)) {
+ jsons_print_nodes_uses_(out, node->child, label, mask, top_first, first);
+ } else {
+ if (*first) {
+ ly_print(out, "%s\"%s\":[", (top_first && (*top_first)) ? "" : ",", label);
+ }
+ ly_print(out, "%s\"%s\"", (first && (*first)) ? "" : ",", node->name);
+ (*first) = 0;
+ }
+ }
+}
+
+static void
+jsons_print_nodes_(struct lyout *out, struct lys_node *data, const char *label, int mask, int *first)
+{
+ int f = 1;
+ struct lys_node *node;
+
+ LY_TREE_FOR(data, node) {
+ if (!(node->nodetype & mask)) {
+ continue;
+ }
+ if (node->nodetype & (LYS_USES)) {
+ jsons_print_nodes_uses_(out, node->child, label, mask, first, &f);
+ } else {
+ if (f) {
+ ly_print(out, "%s\"%s\":[", (first && (*first)) ? "" : ",", label);
+ }
+ ly_print(out, "%s\"%s\"", f ? "" : ",", node->name);
+ f = 0;
+ }
+ }
+ if (!f) {
+ ly_print(out, "]");
+ if (first) {
+ (*first) = 0;
+ }
+ }
+}
+
+static void
+jsons_print_groupings(struct lyout *out, struct lys_node *data, int *first)
+{
+ jsons_print_nodes_(out, data, "groupings", LYS_GROUPING, first);
+}
+
+static void
+jsons_print_rpcs(struct lyout *out, struct lys_node *data, int *first)
+{
+ jsons_print_nodes_(out, data, "rpcs", LYS_RPC, first);
+}
+
+static void
+jsons_print_actions(struct lyout *out, struct lys_node *data, int *first)
+{
+ jsons_print_nodes_(out, data, "actions", LYS_ACTION, first);
+}
+
+static void
+jsons_print_notifs(struct lyout *out, struct lys_node *data, int *first)
+{
+ jsons_print_nodes_(out, data, "notifications", LYS_NOTIF, first);
+}
+
+static void
+jsons_print_module(struct lyout *out, const struct lys_module *module)
+{
+ ly_print(out, "{\"%s\":{", module->name);
+ ly_print(out, "\"namespace\":\"%s\"", module->ns);
+ ly_print(out, ",\"prefix\":\"%s\"", module->prefix);
+ jsons_print_text(out, "description", "text", module->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", module->ref, 1, NULL);
+ jsons_print_text(out, "organization", "text", module->org, 1, NULL);
+ jsons_print_text(out, "contact", "text", module->contact, 1, NULL);
+ jsons_print_object(out, "yang-version", "value", (module->version == LYS_VERSION_1_1) ? "1.1" : "1.0", 1, NULL);
+ /* TODO deviated-by */
+
+ jsons_print_revisions(out, module->rev, module->rev_size, NULL);
+ jsons_print_includes(out, module->inc, module->inc_size, NULL);
+ jsons_print_imports(out, module, NULL);
+ jsons_print_typedefs(out, module->tpdf, module->tpdf_size, NULL);
+ jsons_print_identities(out, module->ident, module->ident_size, NULL);
+ jsons_print_features(out, module->features, module->features_size, NULL);
+ jsons_print_augment(out, module->augment, module->augment_size, NULL);
+ jsons_print_deviation(out, module->deviation, module->deviation_size, NULL);
+
+ jsons_print_groupings(out, module->data, NULL);
+ jsons_print_data(out, module, module->data, NULL);
+ jsons_print_rpcs(out, module->data, NULL);
+ jsons_print_notifs(out, module->data, NULL);
+
+ /* close the module */
+ ly_print(out, "}}");
+}
+
+static void
+jsons_print_submodule(struct lyout *out, const struct lys_submodule *module)
+{
+ ly_print(out, "{\"%s\":{", module->name);
+ ly_print(out, "\"belongs-to\":\"%s\"", module->belongsto->name);
+ jsons_print_text(out, "description", "text", module->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", module->ref, 1, NULL);
+ jsons_print_text(out, "organization", "text", module->org, 1, NULL);
+ jsons_print_text(out, "contact", "text", module->contact, 1, NULL);
+ jsons_print_object(out, "yang-version", "value", (module->version == LYS_VERSION_1_1) ? "1.1" : "1.0", 1, NULL);
+ /* TODO deviated-by */
+
+ jsons_print_revisions(out, module->rev, module->rev_size, NULL);
+ jsons_print_includes(out, module->inc, module->inc_size, NULL);
+ jsons_print_imports(out, (struct lys_module *)module, NULL);
+ jsons_print_typedefs(out, module->tpdf, module->tpdf_size, NULL);
+ jsons_print_identities(out, module->ident, module->ident_size, NULL);
+ jsons_print_features(out, module->features, module->features_size, NULL);
+ jsons_print_augment(out, module->augment, module->augment_size, NULL);
+ jsons_print_deviation(out, module->deviation, module->deviation_size, NULL);
+
+ /* close the module */
+ ly_print(out, "}}");
+}
+
+static void
+jsons_print_container(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_container *cont = (struct lys_node_container *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", cont->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", cont->ref, 1, NULL);
+ jsons_print_config(out, cont->flags, NULL);
+ jsons_print_status(out, cont->flags, NULL);
+ jsons_print_text(out, "presence", "value", cont->presence, 1, NULL);
+ jsons_print_iffeatures(out, cont->module, cont->iffeature, cont->iffeature_size, NULL);
+ jsons_print_when(out, cont->when, NULL);
+ jsons_print_musts(out, cont->must, cont->must_size, NULL);
+ jsons_print_typedefs(out, cont->tpdf, cont->tpdf_size, NULL);
+
+ jsons_print_groupings(out, cont->child, NULL);
+ jsons_print_data(out, cont->module, cont->child, NULL);
+ jsons_print_actions(out, cont->child, NULL);
+ jsons_print_notifs(out, cont->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_choice(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_choice *choice = (struct lys_node_choice *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", choice->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", choice->ref, 1, NULL);
+ jsons_print_config(out, choice->flags, NULL);
+ jsons_print_status(out, choice->flags, NULL);
+ jsons_print_mand(out, choice->flags, NULL);
+ if (choice->dflt) {
+ jsons_print_defaults(out, &choice->dflt->name, 1, NULL);
+ }
+ jsons_print_iffeatures(out, choice->module, choice->iffeature, choice->iffeature_size, NULL);
+ jsons_print_when(out, choice->when, NULL);
+
+ jsons_print_data(out, choice->module, choice->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_leaf(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", leaf->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", leaf->ref, 1, NULL);
+ jsons_print_status(out, leaf->flags, NULL);
+ jsons_print_config(out, leaf->flags, NULL);
+ jsons_print_mand(out, leaf->flags, NULL);
+ jsons_print_type(out, &leaf->type, NULL);
+ jsons_print_text(out, "units", "name", leaf->units, 1, NULL);
+ if (leaf->dflt) {
+ jsons_print_defaults(out, &leaf->dflt, 1, NULL);
+ }
+ jsons_print_iffeatures(out, leaf->module, leaf->iffeature, leaf->iffeature_size, NULL);
+ jsons_print_when(out, leaf->when, NULL);
+ jsons_print_musts(out, leaf->must, leaf->must_size, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_leaflist(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", llist->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", llist->ref, 1, NULL);
+ jsons_print_status(out, llist->flags, NULL);
+ jsons_print_config(out, llist->flags, NULL);
+ jsons_print_ordering(out, llist->flags, NULL);
+ jsons_print_type(out, &llist->type, NULL);
+ jsons_print_text(out, "units", "name", llist->units, 1, NULL);
+ jsons_print_defaults(out, llist->dflt, llist->dflt_size, NULL);
+ if (llist->min) {
+ jsons_print_min(out, llist->min, NULL);
+ }
+ if (llist->max) {
+ jsons_print_max(out, llist->max, NULL);
+ }
+ jsons_print_iffeatures(out, llist->module, llist->iffeature, llist->iffeature_size, NULL);
+ jsons_print_when(out, llist->when, NULL);
+ jsons_print_musts(out, llist->must, llist->must_size, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_list(struct lyout *out, const struct lys_node *node, int *first)
+{
+ uint8_t i;
+ struct lys_node_list *list = (struct lys_node_list *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", list->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", list->ref, 1, NULL);
+ jsons_print_status(out, list->flags, NULL);
+ jsons_print_config(out, list->flags, NULL);
+ jsons_print_ordering(out, list->flags, NULL);
+ if (list->min) {
+ jsons_print_min(out, list->min, NULL);
+ }
+ if (list->max) {
+ jsons_print_max(out, list->max, NULL);
+ }
+ jsons_print_iffeatures(out, list->module, list->iffeature, list->iffeature_size, NULL);
+ jsons_print_when(out, list->when, NULL);
+ jsons_print_musts(out, list->must, list->must_size, NULL);
+ ly_print(out, ",\"keys\":[");
+ for (i = 0; i < list->keys_size; ++i) {
+ ly_print(out, "%s\"%s\"", i ? "," : "", list->keys[i]->name);
+ }
+ ly_print(out, "]");
+ jsons_print_uniques(out, list->unique, list->unique_size, NULL);
+ jsons_print_typedefs(out, list->tpdf, list->tpdf_size, NULL);
+
+ jsons_print_groupings(out, list->child, NULL);
+ jsons_print_data(out, list->module, list->child, NULL);
+ jsons_print_actions(out, list->child, NULL);
+ jsons_print_notifs(out, list->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_anydata(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_anydata *any = (struct lys_node_anydata *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", any->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", any->ref, 1, NULL);
+ jsons_print_config(out, any->flags, NULL);
+ jsons_print_status(out, any->flags, NULL);
+ jsons_print_mand(out, any->flags, NULL);
+ jsons_print_iffeatures(out, any->module, any->iffeature, any->iffeature_size, NULL);
+ jsons_print_when(out, any->when, NULL);
+ jsons_print_musts(out, any->must, any->must_size, NULL);
+
+ /* TODO print content */
+
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_grouping(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_grp *group = (struct lys_node_grp *)node;
+
+ jsons_print_object(out, node->name, "module", lys_main_module(node->module)->name, 0, first);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", group->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", group->ref, 1, NULL);
+ jsons_print_status(out, group->flags, NULL);
+ jsons_print_typedefs(out, group->tpdf, group->tpdf_size, NULL);
+
+ jsons_print_groupings(out, group->child, NULL);
+ jsons_print_data(out, group->module, group->child, NULL);
+ jsons_print_actions(out, group->child, NULL);
+ jsons_print_notifs(out, group->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_case(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_case *cas = (struct lys_node_case *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", cas->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", cas->ref, 1, NULL);
+ jsons_print_config(out, cas->flags, NULL);
+ jsons_print_status(out, cas->flags, NULL);
+ jsons_print_iffeatures(out, cas->module, cas->iffeature, cas->iffeature_size, NULL);
+ jsons_print_when(out, cas->when, NULL);
+
+ jsons_print_data(out, cas->module, cas->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_input(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_inout *input = (struct lys_node_inout *)node;
+
+ jsons_print_object(out, "input", "module", lys_main_module(node->module)->name, 0, first);
+ jsons_print_typedefs(out, input->tpdf, input->tpdf_size, NULL);
+ jsons_print_musts(out, input->must, input->must_size, NULL);
+ jsons_print_groupings(out, input->child, NULL);
+ jsons_print_data(out, input->module, input->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_output(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_inout *output = (struct lys_node_inout *)node;
+
+ jsons_print_object(out, "output", "module", lys_main_module(node->module)->name, 0, first);
+ jsons_print_typedefs(out, output->tpdf, output->tpdf_size, NULL);
+ jsons_print_musts(out, output->must, output->must_size, NULL);
+ jsons_print_groupings(out, output->child, NULL);
+ jsons_print_data(out, output->module, output->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_notif(struct lyout *out, const struct lys_node *node, int *first)
+{
+ struct lys_node_notif *ntf = (struct lys_node_notif *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", ntf->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", ntf->ref, 1, NULL);
+ jsons_print_status(out, ntf->flags, NULL);
+ jsons_print_iffeatures(out, ntf->module, ntf->iffeature, ntf->iffeature_size, NULL);
+ jsons_print_typedefs(out, ntf->tpdf, ntf->tpdf_size, NULL);
+ jsons_print_musts(out, ntf->must, ntf->must_size, NULL);
+
+ jsons_print_groupings(out, ntf->child, NULL);
+ jsons_print_data(out, ntf->module, ntf->child, NULL);
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_rpc(struct lyout *out, const struct lys_node *node, int *first)
+{
+ const struct lys_node *child;
+ struct lys_node_rpc_action *rpc = (struct lys_node_rpc_action *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", rpc->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", rpc->ref, 1, NULL);
+ jsons_print_status(out, rpc->flags, NULL);
+ jsons_print_iffeatures(out, rpc->module, rpc->iffeature, rpc->iffeature_size, NULL);
+ jsons_print_typedefs(out, rpc->tpdf, rpc->tpdf_size, NULL);
+ jsons_print_groupings(out, rpc->child, NULL);
+
+ LY_TREE_FOR(node->child, child) {
+ if (!(child->nodetype & LYS_INPUT)) {
+ continue;
+ }
+ jsons_print_input(out, child, NULL);
+ break;
+ }
+
+ LY_TREE_FOR(node->child, child) {
+ if (!(child->nodetype & LYS_OUTPUT)) {
+ continue;
+ }
+ jsons_print_output(out, child, NULL);
+ break;
+ }
+ ly_print(out, "}");
+}
+
+static void
+jsons_print_action(struct lyout *out, const struct lys_node *node, int *first)
+{
+ const struct lys_node *child;
+ struct lys_node_rpc_action *act = (struct lys_node_rpc_action *)node;
+
+ jsons_print_object(out, node->name, "nodetype", jsons_nodetype_str(node->nodetype), 0, first);
+ ly_print(out, ",\"module\":\"%s\"", lys_main_module(node->module)->name);
+ if (node->module->type) {
+ ly_print(out, ",\"included-from\":\"%s\"", node->module->name);
+ }
+ jsons_print_text(out, "description", "text", act->dsc, 1, NULL);
+ jsons_print_text(out, "reference", "text", act->ref, 1, NULL);
+ jsons_print_status(out, act->flags, NULL);
+ jsons_print_iffeatures(out, act->module, act->iffeature, act->iffeature_size, NULL);
+ jsons_print_typedefs(out, act->tpdf, act->tpdf_size, NULL);
+ jsons_print_groupings(out, act->child, NULL);
+
+ LY_TREE_FOR(node->child, child) {
+ if (!(child->nodetype & LYS_INPUT)) {
+ continue;
+ }
+ jsons_print_input(out, child, NULL);
+ break;
+ }
+
+ LY_TREE_FOR(node->child, child) {
+ if (!(child->nodetype & LYS_OUTPUT)) {
+ continue;
+ }
+ jsons_print_output(out, child, NULL);
+ break;
+ }
+ ly_print(out, "}");
+}
+
+int
+jsons_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path)
+{
+ int rc = EXIT_SUCCESS;
+
+ if (!target_schema_path) {
+ if (module->type == 0) {
+ jsons_print_module(out, module);
+ } else {
+ jsons_print_submodule(out, (struct lys_submodule *)module);
+ }
+ } else {
+ ly_print(out, "{");
+ rc = lys_print_target(out, module, target_schema_path,
+ jsons_print_typedef,
+ jsons_print_identity,
+ jsons_print_feature,
+ jsons_print_type,
+ jsons_print_grouping,
+ jsons_print_container,
+ jsons_print_choice,
+ jsons_print_leaf,
+ jsons_print_leaflist,
+ jsons_print_list,
+ jsons_print_anydata,
+ jsons_print_case,
+ jsons_print_notif,
+ jsons_print_rpc,
+ jsons_print_action,
+ jsons_print_input,
+ jsons_print_output);
+ ly_print(out, "}");
+ }
+ ly_print_flush(out);
+
+ return rc;
+}
diff --git a/src/printer_yin.c b/src/printer_yin.c
index f6591ea..aee51e9 100644
--- a/src/printer_yin.c
+++ b/src/printer_yin.c
@@ -1646,7 +1646,7 @@
ly_print(out, ">\n");
level++;
- if (module->version || lys_ext_iter(module->ext, module->ext_size, 0, LYEXT_SUBSTMT_VERSION) != -1) {
+ if (module->version) {
yin_print_substmt(out, level, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1",
module, module->ext, module->ext_size);
}
diff --git a/src/resolve.c b/src/resolve.c
index d2b35ed..e482611 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -6064,11 +6064,6 @@
len = strlen(keys_str);
}
- if (list->keys[i]) {
- /* skip already resolved keys */
- goto next;
- }
-
rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
(const struct lys_node **)&list->keys[i]);
if (rc) {
@@ -6101,7 +6096,6 @@
free(s);
}
-next:
/* prepare for next iteration */
while (value && isspace(value[0])) {
value++;
diff --git a/src/tree_schema.c b/src/tree_schema.c
index 1505c58..436b377 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -3308,10 +3308,7 @@
list->keys_size = list_orig->keys_size;
if (!shallow) {
- /* the keys are going to be resolved only if the list is instantiated in data tree, not just
- * in another grouping */
- for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
- if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
+ if (unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
goto error;
}
} else {
diff --git a/src/tree_schema.h b/src/tree_schema.h
index 11100fc..f6771ed 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -257,6 +257,7 @@
LYS_OUT_YIN = 2, /**< YIN schema output format */
LYS_OUT_TREE, /**< Tree schema output format, for more information see the [printers](@ref howtoschemasprinters) page */
LYS_OUT_INFO, /**< Info schema output format, for more information see the [printers](@ref howtoschemasprinters) page */
+ LYS_OUT_JSON, /**< JSON schema output format, reflecting YIN format with conversion of attributes to object's members */
} LYS_OUTFORMAT;
/**
diff --git a/swig/cpp/src/Tree_Schema.cpp b/swig/cpp/src/Tree_Schema.cpp
index 2167411..4af77c7 100644
--- a/swig/cpp/src/Tree_Schema.cpp
+++ b/swig/cpp/src/Tree_Schema.cpp
@@ -61,6 +61,20 @@
free(strp);
return s_strp;
}
+std::string Module::print_mem(LYS_OUTFORMAT format, const char *target, int options) {
+ char *strp = nullptr;
+ int rc = 0;
+
+ rc = lys_print_mem(&strp, module, format, target, 0, options);
+ if (rc) {
+ check_libyang_error(module->ctx);
+ return nullptr;
+ }
+
+ std::string s_strp = strp;
+ free(strp);
+ return s_strp;
+}
Submodule::~Submodule() {};
S_Revision Submodule::rev() LY_NEW(submodule, rev, Revision);
std::vector<S_Deviation> *Submodule::deviation() LY_NEW_LIST(submodule, deviation, deviation_size, Deviation);
diff --git a/swig/cpp/src/Tree_Schema.hpp b/swig/cpp/src/Tree_Schema.hpp
index 4b02a8c..b4b1c75 100644
--- a/swig/cpp/src/Tree_Schema.hpp
+++ b/swig/cpp/src/Tree_Schema.hpp
@@ -111,6 +111,7 @@
S_Schema_Node data() LY_NEW(module, data, Schema_Node);
std::vector<S_Schema_Node> *data_instantiables(int options);
std::string print_mem(LYS_OUTFORMAT format, int options);
+ std::string print_mem(LYS_OUTFORMAT format, const char *target, int options);
friend Context;
friend Data_Node;
diff --git a/tests/api/test_tree_schema.c b/tests/api/test_tree_schema.c
index 16a8eea..5125ebf 100644
--- a/tests/api/test_tree_schema.c
+++ b/tests/api/test_tree_schema.c
@@ -307,6 +307,24 @@
Enabled: no\n\
If-feats: \n";
+char *result_jsons = "{\"a\":{"
+ "\"namespace\":\"urn:a\","
+ "\"prefix\":\"a_mod\","
+ "\"yang-version\":{\"value\":\"1.0\"},"
+ "\"revision\":{\"2015-01-01\":{\"description\":{\"text\":\"version 1\"},\"reference\":{\"text\":\"RFC XXXX\"}}},"
+ "\"include\":{\"asub\":{},\"atop\":{}},"
+ "\"features\":[\"foo\"],"
+ "\"augment\":{\"/x\":{"
+ "\"if-features\":[\"bar\"],"
+ "\"data\":{\"bar-y\":{\"nodetype\":\"container\"}}}},"
+ "\"groupings\":[\"gg\"],"
+ "\"data\":{\"top\":{\"nodetype\":\"container\",\"included-from\":\"atop\"},"
+ "\"x\":{\"nodetype\":\"container\"}},"
+ "\"rpcs\":[\"bar-rpc\",\"foo-rpc\"],"
+ "\"notifications\":[\"bar-notif\",\"fox-notif\"]}}";
+
+char *result_jsons_grouping = "{\"gg\":{\"module\":\"a\",\"data\":{\"bar-gggg\":{\"nodetype\":\"leaf\"}}}}";
+
static int
setup_f(void **state)
{
@@ -800,6 +818,38 @@
}
static void
+test_lys_print_mem_jsons(void **state)
+{
+ (void) state; /* unused */
+ const struct lys_module *module;
+ LYS_INFORMAT yang_format = LYS_IN_YIN;
+ const char *target = "grouping/gg";
+ char *result = NULL;
+ int rc;
+
+ module = lys_parse_mem(ctx, lys_module_a, yang_format);
+ if (!module) {
+ fail();
+ }
+
+ rc = lys_print_mem(&result, module, LYS_OUT_JSON, NULL, 0, 0);
+ if (rc) {
+ fail();
+ }
+
+ assert_string_equal(result_jsons, result);
+ free(result);
+
+ rc = lys_print_mem(&result, module, LYS_OUT_JSON, target, 0, 0);
+ if (rc) {
+ fail();
+ }
+
+ assert_string_equal(result_jsons_grouping, result);
+ free(result);
+}
+
+static void
test_lys_print_fd_tree(void **state)
{
(void) state; /* unused */
@@ -1001,6 +1051,80 @@
}
static void
+test_lys_print_fd_jsons(void **state)
+{
+ (void) state; /* unused */
+ const struct lys_module *module;
+ LYS_INFORMAT yang_format = LYS_IN_YIN;
+ struct stat sb;
+ char *target = "grouping/gg";
+ char file_name1[20];
+ char file_name2[20];
+ char *result;
+ int rc;
+ int fd1 = -1, fd2 = -1;
+
+ module = lys_parse_mem(ctx, lys_module_a, yang_format);
+ if (!module) {
+ goto error;
+ }
+
+ memset(file_name1, 0, sizeof(file_name1));
+ memset(file_name2, 0, sizeof(file_name2));
+ strncpy(file_name1, TMP_TEMPLATE, sizeof(file_name1));
+ strncpy(file_name2, TMP_TEMPLATE, sizeof(file_name2));
+
+ fd1 = mkstemp(file_name1);
+ fd2 = mkstemp(file_name2);
+ if (fd1 < 1 || fd2 < 1) {
+ goto error;
+ }
+
+ /* module */
+ rc = lys_print_fd(fd1, module, LYS_OUT_JSON, NULL, 0, 0);
+ if (rc) {
+ goto error;
+ }
+
+ if (fstat(fd1, &sb) == -1 || !S_ISREG(sb.st_mode)) {
+ goto error;
+ }
+
+ result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
+ assert_string_equal(result_jsons, result);
+
+ /* grouping */
+ rc = lys_print_fd(fd2, module, LYS_OUT_JSON, target, 0, 0);
+ if (rc) {
+ goto error;
+ }
+
+ if (fstat(fd2, &sb) == -1 || !S_ISREG(sb.st_mode)) {
+ goto error;
+ }
+
+ result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
+ assert_string_equal(result_jsons_grouping, result);
+
+ close(fd1);
+ close(fd2);
+ unlink(file_name1);
+ unlink(file_name2);
+
+ return;
+error:
+ if (fd1 > 0) {
+ close(fd1);
+ unlink(file_name1);
+ }
+ if (fd2 > 0) {
+ close(fd2);
+ unlink(file_name2);
+ }
+ fail();
+}
+
+static void
test_lys_print_file_tree(void **state)
{
(void) state; /* unused */
@@ -1250,6 +1374,99 @@
}
static void
+test_lys_print_file_jsons(void **state)
+{
+ (void) state; /* unused */
+ const struct lys_module *module;
+ LYS_INFORMAT yang_format = LYS_IN_YIN;
+ struct stat sb;
+ char *target = "grouping/gg";
+ char file_name1[20];
+ char file_name2[20];
+ char *result;
+ FILE *f1 = NULL, *f2 = NULL;
+ int rc;
+ int fd1 = -1, fd2 = -1;
+
+ module = lys_parse_mem(ctx, lys_module_a, yang_format);
+ if (!module) {
+ goto error;
+ }
+
+ memset(file_name1, 0, sizeof(file_name1));
+ memset(file_name2, 0, sizeof(file_name2));
+ strncpy(file_name1, TMP_TEMPLATE, sizeof(file_name1));
+ strncpy(file_name2, TMP_TEMPLATE, sizeof(file_name2));
+
+ fd1 = mkstemp(file_name1);
+ fd2 = mkstemp(file_name2);
+ if (fd1 < 1 || fd2 < 1) {
+ goto error;
+ }
+ close(fd1);
+ close(fd2);
+
+ f1 = (fopen(file_name1,"r+"));
+ f2 = (fopen(file_name2,"r+"));
+ if (!f1 || !f2) {
+ goto error;
+ }
+
+ /* module */
+ rc = lys_print_file(f1, module, LYS_OUT_JSON, NULL, 0, 0);
+ if (rc) {
+ goto error;
+ }
+
+ fclose(f1); f1 = NULL;
+
+ fd1 = open(file_name1, O_RDONLY);
+ if (fstat(fd1, &sb) == -1 || !S_ISREG(sb.st_mode)) {
+ goto error;
+ }
+
+ result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
+ assert_string_equal(result_jsons, result);
+
+ /* grouping */
+ rc = lys_print_file(f2, module, LYS_OUT_JSON, target, 0, 0);
+ if (rc) {
+ goto error;
+ }
+
+ fclose(f2); f2 = NULL;
+
+ fd2 = open(file_name2, O_RDONLY);
+ if (fstat(fd2, &sb) == -1 || !S_ISREG(sb.st_mode)) {
+ goto error;
+ }
+
+ result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
+ assert_string_equal(result_jsons_grouping, result);
+
+ close(fd1);
+ close(fd2);
+ unlink(file_name1);
+ unlink(file_name2);
+
+ return;
+error:
+ if (f1)
+ fclose(f1);
+ if (fd1 > 0) {
+ unlink(file_name1);
+ close(fd1);
+ }
+ if (f2)
+ fclose(f2);
+ if (fd2 > 0) {
+ unlink(file_name2);
+ close(fd2);
+ }
+ fail();
+}
+
+static void
test_lys_find_path(void **state)
{
(void) state; /* unused */
@@ -1327,14 +1544,17 @@
cmocka_unit_test_setup_teardown(test_lys_print_mem_yang, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_mem_yin, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_mem_info, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_lys_print_mem_jsons, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_fd_tree, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_fd_yang, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_fd_yin, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_fd_info, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_lys_print_fd_jsons, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_file_tree, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_file_yin, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_file_yang, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_print_file_info, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_lys_print_file_jsons, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_find_path, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_lys_path, setup_f, teardown_f),
};
diff --git a/tools/lint/commands.c b/tools/lint/commands.c
index 67414ee..696c88c 100644
--- a/tools/lint/commands.c
+++ b/tools/lint/commands.c
@@ -52,7 +52,7 @@
void
cmd_print_help(void)
{
- printf("print [-f (yang | yin | tree [<tree-options>] | info [-t <info-path>])] [-o <output-file>]"
+ printf("print [-f (yang | yin | tree [<tree-options>] | info [-P <info-path>]) | jsons] [-o <output-file>]"
" <model-name>[@<revision>]\n");
printf("\n");
printf("\ttree-options:\t--tree-print-groupings\t(print top-level groupings in a separate section)\n");
@@ -64,7 +64,7 @@
printf("\n");
printf("\tinfo-path:\t<schema-path> | typedef[<schema-path>]/<typedef-name> |\n");
printf("\t \t| identity/<identity-name> | feature/<feature-name> |\n");
- printf("\t \t| grouping/<grouping-name>(<schema-path>) |\n");
+ printf("\t \t| grouping[<schema-path>]/<grouping-name> |\n");
printf("\t \t| type/<schema-path-leaf-or-leaflist>\n");
printf("\n");
printf("\tschema-path:\t( /<module-name>:<node-identifier> )+\n");
@@ -330,6 +330,8 @@
tree_opts |= LYS_OUTOPT_TREE_RFC;
} else if (!strcmp(optarg, "info")) {
format = LYS_OUT_INFO;
+ } else if (!strcmp(optarg, "jsons")) {
+ format = LYS_OUT_JSON;
} else {
fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
goto cleanup;
@@ -407,6 +409,9 @@
}
ret = lys_print_file(output, module, format, target_path, tree_ll, tree_opts);
+ if (format == LYS_OUT_JSON) {
+ fputs("\n", output);
+ }
cleanup:
free(*argv);
diff --git a/tools/lint/main_ni.c b/tools/lint/main_ni.c
index 82d447c..bd992d8 100644
--- a/tools/lint/main_ni.c
+++ b/tools/lint/main_ni.c
@@ -36,7 +36,7 @@
help(int shortout)
{
fprintf(stdout, "Usage:\n");
- fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc }] <file>...\n");
+ fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc | jsons}] <file>...\n");
fprintf(stdout, " Validates the YANG module in <file>, and all its dependencies.\n\n");
fprintf(stdout, " yanglint [options] [-f { xml | json }] <schema>... <file>...\n");
fprintf(stdout, " Validates the YANG modeled data in <file> according to the <schema>.\n\n");
@@ -65,7 +65,7 @@
" has no effect for the auto, rpc, rpcreply and notif TYPEs.\n\n"
" -f FORMAT, --format=FORMAT\n"
" Convert to FORMAT. Supported formats: \n"
- " tree, yin, yang for schemas,\n"
+ " yang, yin, tree and jsons (JSON) for schemas,\n"
" xml, json for data.\n"
" -a, --auto Modify the xml output by adding envelopes for autodetection.\n\n"
" -i, --allimplemented Make all the imported modules implemented.\n\n"
@@ -338,6 +338,9 @@
} else if (!strcasecmp(optarg, "yang")) {
outformat_s = LYS_OUT_YANG;
outformat_d = 0;
+ } else if (!strcasecmp(optarg, "jsons")) {
+ outformat_s = LYS_OUT_JSON;
+ outformat_d = 0;
} else if (!strcasecmp(optarg, "xml")) {
outformat_s = 0;
outformat_d = LYD_XML;
@@ -691,12 +694,26 @@
/* convert (print) to FORMAT */
if (outformat_s) {
+ if (outformat_s == LYS_OUT_JSON && mods->number > 1) {
+ fputs("[", out);
+ }
for (u = 0; u < mods->number; u++) {
if (u) {
- fputs("\n", out);
+ if (outformat_s == LYS_OUT_JSON) {
+ fputs(",\n", out);
+ } else {
+ fputs("\n", out);
+ }
}
lys_print_file(out, (struct lys_module *)mods->set.g[u], outformat_s, outtarget_s, outline_length_s, outoptions_s);
}
+ if (outformat_s == LYS_OUT_JSON) {
+ if (mods->number > 1) {
+ fputs("]\n", out);
+ } else if (mods->number == 1) {
+ fputs("\n", out);
+ }
+ }
} else if (data) {
ly_errno = 0;