Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2 | * @file printer_json.c |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4 | * @brief JSON printer for libyang data structure |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 5 | * |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 16 | #include <stdint.h> |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 19 | #include "common.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 20 | #include "context.h" |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 21 | #include "log.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 22 | #include "out.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 23 | #include "out_internal.h" |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 24 | #include "parser_data.h" |
| 25 | #include "plugins_types.h" |
| 26 | #include "printer_data.h" |
| 27 | #include "printer_internal.h" |
| 28 | #include "set.h" |
| 29 | #include "tree.h" |
| 30 | #include "tree_data.h" |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 31 | #include "tree_schema.h" |
| 32 | |
| 33 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 34 | * @brief JSON printer context. |
| 35 | */ |
| 36 | struct jsonpr_ctx { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 37 | struct ly_out *out; /**< output specification */ |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 38 | const struct lyd_node *root; /**< root node of the subtree being printed */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 39 | uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
| 40 | uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */ |
| 41 | const struct ly_ctx *ctx; /**< libyang context */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 42 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 43 | uint16_t level_printed; /* level where some data were already printed */ |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 44 | struct ly_set open; /* currently open array(s) */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 45 | const struct lyd_node *print_sibling_metadata; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * @brief Mark that something was already written in the current level, |
| 50 | * used to decide if a comma is expected between the items |
| 51 | */ |
| 52 | #define LEVEL_PRINTED ctx->level_printed = ctx->level |
| 53 | |
| 54 | #define PRINT_COMMA \ |
| 55 | if (ctx->level_printed >= ctx->level) {\ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 56 | ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node); |
| 60 | |
| 61 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 62 | * Compare 2 nodes, despite it is regular data node or an opaq node, and |
| 63 | * decide if they corresponds to the same schema node. |
| 64 | * |
| 65 | * TODO: rewrite lyd_compare_single and use it instead of this |
| 66 | * |
| 67 | * @return 1 - matching nodes, 0 - non-matching nodes |
| 68 | */ |
| 69 | static int |
| 70 | matching_node(const struct lyd_node *node1, const struct lyd_node *node2) |
| 71 | { |
| 72 | assert(node1 || node2); |
| 73 | |
| 74 | if (!node1 || !node2) { |
| 75 | return 0; |
| 76 | } else if (node1->schema != node2->schema) { |
| 77 | return 0; |
| 78 | } |
| 79 | if (!node1->schema) { |
| 80 | /* compare node names */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 81 | struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1; |
| 82 | struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 83 | if ((onode1->name.name != onode2->name.name) || (onode1->name.prefix != onode2->name.prefix)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @brief Open the JSON array ('[') for the specified @p node |
| 93 | * |
| 94 | * @param[in] ctx JSON printer context. |
| 95 | * @param[in] node First node of the array. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 96 | * @return LY_ERR value. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 97 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 98 | static LY_ERR |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 99 | json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 100 | { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 101 | ly_print_(ctx->out, "[%s", DO_FORMAT ? "\n" : ""); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 102 | LY_CHECK_RET(ly_set_add(&ctx->open, (void *)node, 0, NULL)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 103 | LEVEL_INC; |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 104 | |
| 105 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @brief Get know if the array for the provided @p node is currently open. |
| 110 | * |
| 111 | * @param[in] ctx JSON printer context. |
| 112 | * @param[in] node Data node to check. |
| 113 | * @return 1 in case the printer is currently in the array belonging to the provided @p node. |
| 114 | * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array). |
| 115 | */ |
| 116 | static int |
| 117 | is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 118 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 119 | if (ctx->open.count && matching_node(node, (const struct lyd_node *)ctx->open.objs[ctx->open.count - 1])) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 120 | return 1; |
| 121 | } else { |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @brief Close the most inner JSON array. |
| 128 | * |
| 129 | * @param[in] ctx JSON printer context. |
| 130 | */ |
| 131 | static void |
| 132 | json_print_array_close(struct jsonpr_ctx *ctx) |
| 133 | { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 134 | LEVEL_DEC; |
| 135 | ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL); |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 136 | ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @brief Get the node's module name to use as the @p node prefix in JSON. |
Radek Krejci | 31bc3f5 | 2021-04-26 11:09:58 +0200 | [diff] [blame] | 141 | * |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 142 | * @param[in] node Node to process. |
| 143 | * @return The name of the module where the @p node belongs, it can be NULL in case the module name |
| 144 | * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context). |
| 145 | */ |
| 146 | static const char * |
| 147 | node_prefix(const struct lyd_node *node) |
| 148 | { |
| 149 | if (node->schema) { |
| 150 | return node->schema->module->name; |
| 151 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 152 | struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 153 | const struct lys_module *mod; |
| 154 | |
| 155 | switch (onode->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 156 | case LY_VALUE_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 157 | return onode->name.module_name; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 158 | case LY_VALUE_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 159 | mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->name.module_ns); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 160 | if (!mod) { |
| 161 | return NULL; |
| 162 | } |
| 163 | return mod->name; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 164 | default: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 165 | /* cannot be created */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 166 | LOGINT(LYD_CTX(node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace) |
| 175 | * |
| 176 | * Accepts both regulard a well as opaq nodes. |
| 177 | * |
| 178 | * @param[in] node1 The first node to compare. |
| 179 | * @param[in] node2 The second node to compare. |
| 180 | * @return 0 in case the nodes' modules are the same |
| 181 | * @return 1 in case the nodes belongs to different modules |
| 182 | */ |
| 183 | int |
| 184 | json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2) |
| 185 | { |
| 186 | assert(node1 || node2); |
| 187 | |
| 188 | if (!node1 || !node2) { |
| 189 | return 1; |
| 190 | } else if (node1->schema && node2->schema) { |
| 191 | if (node1->schema->module == node2->schema->module) { |
| 192 | /* belongs to the same module */ |
| 193 | return 0; |
| 194 | } else { |
| 195 | /* different modules */ |
| 196 | return 1; |
| 197 | } |
| 198 | } else { |
| 199 | const char *pref1 = node_prefix(node1); |
| 200 | const char *pref2 = node_prefix(node2); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 201 | if ((pref1 && pref2) && (pref1 == pref2)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 202 | return 0; |
| 203 | } else { |
| 204 | return 1; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @brief Print the @p text as JSON string - encode special characters and add quotation around the string. |
| 211 | * |
| 212 | * @param[in] out The output handler. |
| 213 | * @param[in] text The string to print. |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 214 | * @return LY_ERR value. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 215 | */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 216 | static LY_ERR |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 217 | json_print_string(struct ly_out *out, const char *text) |
| 218 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 219 | uint64_t i, n; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 220 | |
| 221 | if (!text) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 222 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 225 | ly_write_(out, "\"", 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 226 | for (i = n = 0; text[i]; i++) { |
| 227 | const unsigned char ascii = text[i]; |
| 228 | if (ascii < 0x20) { |
| 229 | /* control character */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 230 | ly_print_(out, "\\u%.4X", ascii); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 231 | } else { |
| 232 | switch (ascii) { |
| 233 | case '"': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 234 | ly_print_(out, "\\\""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 235 | break; |
| 236 | case '\\': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 237 | ly_print_(out, "\\\\"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 238 | break; |
| 239 | default: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 240 | ly_write_(out, &text[i], 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 241 | n++; |
| 242 | } |
| 243 | } |
| 244 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 245 | ly_write_(out, "\"", 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 246 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 247 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed. |
| 252 | * |
| 253 | * @param[in] ctx JSON printer context. |
| 254 | * @param[in] node The data node being printed. |
| 255 | * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier. |
| 256 | * @return LY_ERR value. |
| 257 | */ |
| 258 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 259 | json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool is_attr) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 260 | { |
| 261 | PRINT_COMMA; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 262 | if ((LEVEL == 1) || json_nscmp(node, (const struct lyd_node *)node->parent)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 263 | /* print "namespace" */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 264 | ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 265 | node_prefix(node), node->schema->name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 266 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 267 | ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 268 | node->schema->name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return LY_SUCCESS; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @brief More generic alternative to json_print_member() to print some special cases of the member names. |
| 276 | * |
| 277 | * @param[in] ctx JSON printer context. |
| 278 | * @param[in] parent Parent node to compare modules deciding if the prefix is printed. |
| 279 | * @param[in] format Format to decide how to process the @p prefix. |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 280 | * @param[in] name Name structure to provide name and prefix to print. If NULL, only "" name is printed. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 281 | * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier. |
| 282 | * @return LY_ERR value. |
| 283 | */ |
| 284 | static LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 285 | json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LY_VALUE_FORMAT format, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 286 | const struct ly_opaq_name *name, ly_bool is_attr) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 287 | { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 288 | const char *module_name = NULL, *name_str; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 289 | |
| 290 | PRINT_COMMA; |
| 291 | |
| 292 | /* determine prefix string */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 293 | if (name) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 294 | const struct lys_module *mod; |
| 295 | |
| 296 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 297 | case LY_VALUE_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 298 | module_name = name->module_name; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 299 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 300 | case LY_VALUE_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 301 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, name->module_ns); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 302 | if (mod) { |
| 303 | module_name = mod->name; |
| 304 | } |
| 305 | break; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 306 | default: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 307 | /* cannot be created */ |
| 308 | LOGINT_RET(ctx->ctx); |
| 309 | } |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 310 | |
| 311 | name_str = name->name; |
| 312 | } else { |
| 313 | name_str = ""; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* print the member */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 317 | if (module_name && (!parent || (node_prefix(parent) != module_name))) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 318 | ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name_str, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 319 | } else { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 320 | ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | return LY_SUCCESS; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * @brief Print data value. |
| 328 | * |
| 329 | * @param[in] ctx JSON printer context. |
| 330 | * @param[in] val Data value to be printed. |
| 331 | * @return LY_ERR value. |
| 332 | */ |
| 333 | static LY_ERR |
| 334 | json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val) |
| 335 | { |
aPiecek | 0f6bf3e | 2021-08-25 15:47:49 +0200 | [diff] [blame] | 336 | ly_bool dynamic; |
Michal Vasko | 183b911 | 2021-11-04 16:02:24 +0100 | [diff] [blame] | 337 | LY_DATA_TYPE basetype; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 338 | const char *value = val->realtype->plugin->print(ctx->ctx, val, LY_VALUE_JSON, NULL, &dynamic, NULL); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 339 | |
Michal Vasko | 183b911 | 2021-11-04 16:02:24 +0100 | [diff] [blame] | 340 | basetype = val->realtype->basetype; |
| 341 | |
| 342 | print_val: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 343 | /* leafref is not supported */ |
Michal Vasko | 183b911 | 2021-11-04 16:02:24 +0100 | [diff] [blame] | 344 | switch (basetype) { |
| 345 | case LY_TYPE_UNION: |
| 346 | /* use the resolved type */ |
| 347 | basetype = val->subvalue->value.realtype->basetype; |
| 348 | goto print_val; |
| 349 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 350 | case LY_TYPE_BINARY: |
| 351 | case LY_TYPE_STRING: |
| 352 | case LY_TYPE_BITS: |
| 353 | case LY_TYPE_ENUM: |
| 354 | case LY_TYPE_INST: |
| 355 | case LY_TYPE_INT64: |
| 356 | case LY_TYPE_UINT64: |
| 357 | case LY_TYPE_DEC64: |
| 358 | case LY_TYPE_IDENT: |
| 359 | json_print_string(ctx->out, value); |
| 360 | break; |
| 361 | |
| 362 | case LY_TYPE_INT8: |
| 363 | case LY_TYPE_INT16: |
| 364 | case LY_TYPE_INT32: |
| 365 | case LY_TYPE_UINT8: |
| 366 | case LY_TYPE_UINT16: |
| 367 | case LY_TYPE_UINT32: |
| 368 | case LY_TYPE_BOOL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 369 | ly_print_(ctx->out, "%s", value[0] ? value : "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 370 | break; |
| 371 | |
| 372 | case LY_TYPE_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 373 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 374 | break; |
| 375 | |
| 376 | default: |
| 377 | /* error */ |
| 378 | LOGINT_RET(ctx->ctx); |
| 379 | } |
| 380 | |
| 381 | if (dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 382 | free((char *)value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return LY_SUCCESS; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * @brief Print all the attributes of the opaq node. |
| 390 | * |
| 391 | * @param[in] ctx JSON printer context. |
| 392 | * @param[in] node Opaq node where the attributes are placed. |
| 393 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 394 | * @return LY_ERR value. |
| 395 | */ |
| 396 | static LY_ERR |
| 397 | json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod) |
| 398 | { |
| 399 | struct lyd_attr *attr; |
| 400 | |
| 401 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 402 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 403 | LEVEL_PRINTED; |
| 404 | } |
| 405 | |
| 406 | for (attr = node->attr; attr; attr = attr->next) { |
| 407 | PRINT_COMMA; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 408 | json_print_member2(ctx, &node->node, attr->format, &attr->name, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 409 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 410 | if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 411 | ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 412 | } else if (attr->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 413 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 414 | } else { |
| 415 | json_print_string(ctx->out, attr->value); |
| 416 | } |
| 417 | LEVEL_PRINTED; |
| 418 | } |
| 419 | |
| 420 | return LY_SUCCESS; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @brief Print all the metadata of the node. |
| 425 | * |
| 426 | * @param[in] ctx JSON printer context. |
| 427 | * @param[in] node Node where the metadata are placed. |
| 428 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 429 | * @return LY_ERR value. |
| 430 | */ |
| 431 | static LY_ERR |
| 432 | json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod) |
| 433 | { |
| 434 | struct lyd_meta *meta; |
| 435 | |
| 436 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 437 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 438 | LEVEL_PRINTED; |
| 439 | } |
| 440 | |
| 441 | for (meta = node->meta; meta; meta = meta->next) { |
| 442 | PRINT_COMMA; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 443 | ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 444 | LY_CHECK_RET(json_print_value(ctx, &meta->value)); |
| 445 | LEVEL_PRINTED; |
| 446 | } |
| 447 | |
| 448 | return LY_SUCCESS; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes. |
| 453 | * |
| 454 | * @param[in] ctx JSON printer context. |
| 455 | * @param[in] node Data node where the attributes/metadata are placed. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 456 | * @param[in] inner Flag if the @p node is an inner node in the tree. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 457 | * @return LY_ERR value. |
| 458 | */ |
| 459 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 460 | json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 461 | { |
| 462 | const struct lys_module *wdmod = NULL; |
| 463 | |
| 464 | if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) { |
| 465 | /* we have implicit OR explicit default node */ |
| 466 | /* get with-defaults module */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 467 | wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | if (node->schema && node->meta) { |
| 471 | if (inner) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 472 | LY_CHECK_RET(json_print_member2(ctx, NULL, LY_VALUE_JSON, NULL, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 473 | } else { |
| 474 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
| 475 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 476 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 477 | LEVEL_INC; |
| 478 | LY_CHECK_RET(json_print_metadata(ctx, node, wdmod)); |
| 479 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 480 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 481 | LEVEL_PRINTED; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 482 | } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 483 | if (inner) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 484 | LY_CHECK_RET(json_print_member2(ctx, NULL, LY_VALUE_JSON, NULL, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 485 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 486 | LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 487 | &((struct lyd_node_opaq *)node)->name, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 488 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 489 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 490 | LEVEL_INC; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 491 | LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 492 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 493 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 494 | LEVEL_PRINTED; |
| 495 | } |
| 496 | |
| 497 | return LY_SUCCESS; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * @brief Print leaf data node including its metadata. |
| 502 | * |
| 503 | * @param[in] ctx JSON printer context. |
| 504 | * @param[in] node Data node to print. |
| 505 | * @return LY_ERR value. |
| 506 | */ |
| 507 | static LY_ERR |
| 508 | json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 509 | { |
| 510 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 511 | LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 512 | LEVEL_PRINTED; |
| 513 | |
| 514 | /* print attributes as sibling */ |
| 515 | json_print_attributes(ctx, node, 0); |
| 516 | |
| 517 | return LY_SUCCESS; |
| 518 | } |
| 519 | |
| 520 | /** |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 521 | * @brief Print anydata/anyxml content. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 522 | * |
| 523 | * @param[in] ctx JSON printer context. |
| 524 | * @param[in] any Anydata node to print. |
| 525 | * @return LY_ERR value. |
| 526 | */ |
| 527 | static LY_ERR |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 528 | json_print_any_content(struct jsonpr_ctx *ctx, struct lyd_node_any *any) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 529 | { |
| 530 | LY_ERR ret = LY_SUCCESS; |
| 531 | struct lyd_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 532 | uint32_t prev_opts, prev_lo; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 533 | |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 534 | /* anyxml - printed as name/value pair; |
| 535 | * anydata - printed as an object */ |
| 536 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 537 | if (!any->value.tree) { |
| 538 | /* no content */ |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 539 | if (any->schema->nodetype == LYS_ANYXML) { |
| 540 | ly_print_(ctx->out, "null"); |
| 541 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 542 | return LY_SUCCESS; |
| 543 | } |
| 544 | |
| 545 | if (any->value_type == LYD_ANYDATA_LYB) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 546 | uint32_t parser_options = LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 547 | |
| 548 | /* turn logging off */ |
| 549 | prev_lo = ly_log_options(0); |
| 550 | |
| 551 | /* try to parse it into a data tree */ |
| 552 | if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) { |
| 553 | /* successfully parsed */ |
| 554 | free(any->value.mem); |
| 555 | any->value.tree = iter; |
| 556 | any->value_type = LYD_ANYDATA_DATATREE; |
| 557 | } |
| 558 | |
| 559 | /* turn loggin on again */ |
| 560 | ly_log_options(prev_lo); |
| 561 | } |
| 562 | |
| 563 | switch (any->value_type) { |
| 564 | case LYD_ANYDATA_DATATREE: |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 565 | if (any->schema->nodetype == LYS_ANYXML) { |
| 566 | /* print always as a string */ |
| 567 | ly_print_(ctx->out, "\"%s", DO_FORMAT ? "\n" : ""); |
| 568 | } |
| 569 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 570 | /* close opening tag and print data */ |
| 571 | prev_opts = ctx->options; |
| 572 | ctx->options &= ~LYD_PRINT_WITHSIBLINGS; |
| 573 | |
| 574 | LY_LIST_FOR(any->value.tree, iter) { |
| 575 | ret = json_print_node(ctx, iter); |
| 576 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 577 | } |
| 578 | |
| 579 | ctx->options = prev_opts; |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 580 | |
| 581 | if (any->schema->nodetype == LYS_ANYXML) { |
| 582 | /* terminate the string */ |
| 583 | ly_print_(ctx->out, "\""); |
| 584 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 585 | break; |
| 586 | case LYD_ANYDATA_JSON: |
| 587 | /* print without escaping special characters */ |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 588 | if (any->schema->nodetype == LYS_ANYXML) { |
| 589 | /* print as a string */ |
| 590 | ly_print_(ctx->out, "\"%s\"", any->value.str); |
| 591 | } else if (any->value.str[0]) { |
| 592 | /* print with indent */ |
| 593 | ly_print_(ctx->out, "%*s%s", INDENT, any->value.str); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 594 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 595 | break; |
| 596 | case LYD_ANYDATA_STRING: |
| 597 | case LYD_ANYDATA_XML: |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 598 | if (any->schema->nodetype == LYS_ANYXML) { |
| 599 | /* print as a string */ |
| 600 | ly_print_(ctx->out, "\"%s\"", any->value.str); |
| 601 | break; |
| 602 | } |
| 603 | /* fallthrough */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 604 | case LYD_ANYDATA_LYB: |
| 605 | /* JSON and LYB format is not supported */ |
| 606 | LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type); |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 607 | break; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | return LY_SUCCESS; |
| 611 | } |
| 612 | |
| 613 | /** |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 614 | * @brief Print content of a single container/list/anydata data node including its metadata. |
| 615 | * The envelope specific to nodes are expected to be printed by the caller. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 616 | * |
| 617 | * @param[in] ctx JSON printer context. |
| 618 | * @param[in] node Data node to print. |
| 619 | * @return LY_ERR value. |
| 620 | */ |
| 621 | static LY_ERR |
| 622 | json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 623 | { |
| 624 | struct lyd_node *child; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 625 | ly_bool has_content = 0; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 626 | |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 627 | LY_LIST_FOR(lyd_child(node), child) { |
| 628 | if (ly_should_print(child, ctx->options)) { |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | if (node->meta || child) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 633 | has_content = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 634 | } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 635 | has_content = 1; |
| 636 | } |
| 637 | |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 638 | if ((node->schema && (node->schema->nodetype == LYS_LIST)) || |
| 639 | (!node->schema && (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST))) { |
| 640 | ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? |
| 641 | (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : ""); |
| 642 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 643 | ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 644 | (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 645 | } |
| 646 | LEVEL_INC; |
| 647 | |
| 648 | json_print_attributes(ctx, node, 1); |
| 649 | |
| 650 | if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) { |
| 651 | /* print children */ |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 652 | LY_LIST_FOR(lyd_child(node), child) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 653 | LY_CHECK_RET(json_print_node(ctx, child)); |
| 654 | } |
| 655 | } else { |
| 656 | /* anydata */ |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 657 | json_print_any_content(ctx, (struct lyd_node_any *)node); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 660 | LEVEL_DEC; |
| 661 | if (DO_FORMAT && has_content) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 662 | ly_print_(ctx->out, "\n%*s}", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 663 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 664 | ly_print_(ctx->out, "}"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 665 | } |
| 666 | LEVEL_PRINTED; |
| 667 | |
| 668 | return LY_SUCCESS; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * @brief Print container data node including its metadata. |
| 673 | * |
| 674 | * @param[in] ctx JSON printer context. |
| 675 | * @param[in] node Data node to print. |
| 676 | * @return LY_ERR value. |
| 677 | */ |
| 678 | static int |
| 679 | json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 680 | { |
| 681 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
| 682 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 683 | |
| 684 | return LY_SUCCESS; |
| 685 | } |
| 686 | |
| 687 | /** |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 688 | * @brief Print anyxml data node including its metadata. |
| 689 | * |
| 690 | * @param[in] ctx JSON printer context. |
| 691 | * @param[in] node Data node to print. |
| 692 | * @return LY_ERR value. |
| 693 | */ |
| 694 | static int |
| 695 | json_print_anyxml(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 696 | { |
| 697 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
| 698 | LY_CHECK_RET(json_print_any_content(ctx, (struct lyd_node_any *)node)); |
| 699 | LEVEL_PRINTED; |
| 700 | |
| 701 | /* print attributes as sibling */ |
| 702 | json_print_attributes(ctx, node, 0); |
| 703 | |
| 704 | return LY_SUCCESS; |
| 705 | } |
| 706 | |
| 707 | /** |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 708 | * @brief Check whether a node is the last printed instance of a (leaf-)list. |
| 709 | * |
| 710 | * @param[in] ctx JSON printer context. |
| 711 | * @param[in] node Last printed node. |
| 712 | * @return Whether it is the last printed instance or not. |
| 713 | */ |
| 714 | static ly_bool |
| 715 | json_print_array_is_last_inst(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 716 | { |
Michal Vasko | d320337 | 2021-12-09 09:25:50 +0100 | [diff] [blame] | 717 | if (!is_open_array(ctx, node)) { |
| 718 | /* no array open */ |
| 719 | return 0; |
| 720 | } |
| 721 | |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 722 | if ((ctx->root == node) && !(ctx->options & LYD_PRINT_WITHSIBLINGS)) { |
| 723 | /* the only printed instance */ |
| 724 | return 1; |
| 725 | } |
| 726 | |
Michal Vasko | d320337 | 2021-12-09 09:25:50 +0100 | [diff] [blame] | 727 | if (!node->next || (node->next->schema != node->schema)) { |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 728 | /* last instance */ |
| 729 | return 1; |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 736 | * @brief Print single leaf-list or list instance. |
| 737 | * |
| 738 | * In case of list, metadata are printed inside the list object. For the leaf-list, |
| 739 | * metadata are marked in the context for later printing after closing the array next to it using |
| 740 | * json_print_metadata_leaflist(). |
| 741 | * |
| 742 | * @param[in] ctx JSON printer context. |
| 743 | * @param[in] node Data node to print. |
| 744 | * @return LY_ERR value. |
| 745 | */ |
| 746 | static LY_ERR |
| 747 | json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 748 | { |
| 749 | if (!is_open_array(ctx, node)) { |
| 750 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 751 | LY_CHECK_RET(json_print_array_open(ctx, node)); |
Radek Krejci | ee74a19 | 2021-04-09 09:55:34 +0200 | [diff] [blame] | 752 | if (node->schema->nodetype == LYS_LEAFLIST) { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 753 | ly_print_(ctx->out, "%*s", INDENT); |
Radek Krejci | ee74a19 | 2021-04-09 09:55:34 +0200 | [diff] [blame] | 754 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 755 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 756 | ly_print_(ctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | if (node->schema->nodetype == LYS_LIST) { |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 760 | if (!lyd_child(node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 761 | /* empty, e.g. in case of filter */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 762 | ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 763 | LEVEL_PRINTED; |
| 764 | } else { |
| 765 | /* print list's content */ |
| 766 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 767 | } |
| 768 | } else { |
| 769 | assert(node->schema->nodetype == LYS_LEAFLIST); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 770 | LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 771 | |
| 772 | if (node->meta && !ctx->print_sibling_metadata) { |
| 773 | ctx->print_sibling_metadata = node; |
| 774 | } |
| 775 | } |
| 776 | |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 777 | if (json_print_array_is_last_inst(ctx, node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 778 | json_print_array_close(ctx); |
| 779 | } |
| 780 | |
| 781 | return LY_SUCCESS; |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list(). |
| 786 | * This function is supposed to be called when the leaf-list array is closed. |
| 787 | * |
| 788 | * @param[in] ctx JSON printer context. |
| 789 | * @return LY_ERR value. |
| 790 | */ |
| 791 | static LY_ERR |
| 792 | json_print_metadata_leaflist(struct jsonpr_ctx *ctx) |
| 793 | { |
| 794 | const struct lyd_node *prev, *node, *iter; |
| 795 | |
| 796 | if (!ctx->print_sibling_metadata) { |
| 797 | return LY_SUCCESS; |
| 798 | } |
| 799 | |
| 800 | for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev; |
| 801 | prev->next && matching_node(node, prev); |
| 802 | node = prev, prev = node->prev) {} |
| 803 | |
| 804 | /* node is the first instance of the leaf-list */ |
| 805 | |
| 806 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 807 | ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 808 | LEVEL_INC; |
| 809 | LY_LIST_FOR(node, iter) { |
| 810 | PRINT_COMMA; |
| 811 | if (iter->meta) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 812 | ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 813 | LEVEL_INC; |
| 814 | LY_CHECK_RET(json_print_metadata(ctx, iter, NULL)); |
| 815 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 816 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 817 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 818 | ly_print_(ctx->out, "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 819 | } |
| 820 | LEVEL_PRINTED; |
| 821 | if (!matching_node(iter, iter->next)) { |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 826 | ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 827 | LEVEL_PRINTED; |
| 828 | |
| 829 | return LY_SUCCESS; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * @brief Print opaq data node including its attributes. |
| 834 | * |
| 835 | * @param[in] ctx JSON printer context. |
| 836 | * @param[in] node Opaq node to print. |
| 837 | * @return LY_ERR value. |
| 838 | */ |
| 839 | static LY_ERR |
| 840 | json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 841 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 842 | ly_bool first = 1, last = 1; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 843 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 844 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 845 | if (node->prev->next && matching_node(node->prev, &node->node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 846 | first = 0; |
| 847 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 848 | if (node->next && matching_node(&node->node, node->next)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 849 | last = 0; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | if (first) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 854 | LY_CHECK_RET(json_print_member2(ctx, lyd_parent(&node->node), node->format, &node->name, 0)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 855 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 856 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 857 | LY_CHECK_RET(json_print_array_open(ctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 858 | } |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 859 | if (node->hints & LYD_NODEHINT_LEAFLIST) { |
| 860 | ly_print_(ctx->out, "%*s", INDENT); |
| 861 | } |
| 862 | } else if (node->hints & LYD_NODEHINT_LEAFLIST) { |
| 863 | ly_print_(ctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 864 | } |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 865 | if (node->child || (node->hints & LYD_NODEHINT_LIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 866 | LY_CHECK_RET(json_print_inner(ctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 867 | LEVEL_PRINTED; |
| 868 | } else { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 869 | if (node->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 870 | ly_print_(ctx->out, "[null]"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 871 | } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 872 | ly_print_(ctx->out, "%s", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 873 | } else { |
| 874 | /* string */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 875 | ly_print_(ctx->out, "\"%s\"", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 876 | } |
| 877 | LEVEL_PRINTED; |
| 878 | |
| 879 | /* attributes */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 880 | json_print_attributes(ctx, (const struct lyd_node *)node, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 881 | |
| 882 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 883 | if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 884 | json_print_array_close(ctx); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 885 | LEVEL_PRINTED; |
| 886 | } |
| 887 | |
| 888 | return LY_SUCCESS; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * @brief Print all the types of data node including its metadata. |
| 893 | * |
| 894 | * @param[in] ctx JSON printer context. |
| 895 | * @param[in] node Data node to print. |
| 896 | * @return LY_ERR value. |
| 897 | */ |
| 898 | static LY_ERR |
| 899 | json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 900 | { |
| 901 | if (!ly_should_print(node, ctx->options)) { |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 902 | if (json_print_array_is_last_inst(ctx, node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 903 | json_print_array_close(ctx); |
| 904 | } |
| 905 | return LY_SUCCESS; |
| 906 | } |
| 907 | |
| 908 | if (!node->schema) { |
| 909 | LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node)); |
| 910 | } else { |
| 911 | switch (node->schema->nodetype) { |
| 912 | case LYS_RPC: |
| 913 | case LYS_ACTION: |
| 914 | case LYS_NOTIF: |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 915 | case LYS_ANYDATA: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 916 | case LYS_CONTAINER: |
| 917 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 918 | break; |
| 919 | case LYS_LEAF: |
| 920 | LY_CHECK_RET(json_print_leaf(ctx, node)); |
| 921 | break; |
| 922 | case LYS_LEAFLIST: |
| 923 | case LYS_LIST: |
| 924 | LY_CHECK_RET(json_print_leaf_list(ctx, node)); |
| 925 | break; |
| 926 | case LYS_ANYXML: |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame^] | 927 | LY_CHECK_RET(json_print_anyxml(ctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 928 | break; |
| 929 | default: |
| 930 | LOGINT(node->schema->module->ctx); |
| 931 | return EXIT_FAILURE; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | ctx->level_printed = ctx->level; |
| 936 | |
| 937 | if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) { |
| 938 | json_print_metadata_leaflist(ctx); |
| 939 | ctx->print_sibling_metadata = NULL; |
| 940 | } |
| 941 | |
| 942 | return LY_SUCCESS; |
| 943 | } |
| 944 | |
| 945 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 946 | json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 947 | { |
| 948 | const struct lyd_node *node; |
| 949 | struct jsonpr_ctx ctx = {0}; |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 950 | const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n"; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 951 | |
Radek Krejci | 2e87477 | 2020-08-28 16:36:33 +0200 | [diff] [blame] | 952 | if (!root) { |
| 953 | ly_print_(out, "{}%s", delimiter); |
| 954 | ly_print_flush(out); |
| 955 | return LY_SUCCESS; |
| 956 | } |
| 957 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 958 | ctx.out = out; |
| 959 | ctx.level = 1; |
| 960 | ctx.level_printed = 0; |
| 961 | ctx.options = options; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 962 | ctx.ctx = LYD_CTX(root); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 963 | |
| 964 | /* start */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 965 | ly_print_(ctx.out, "{%s", delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 966 | |
| 967 | /* content */ |
| 968 | LY_LIST_FOR(root, node) { |
Michal Vasko | b6b2530 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 969 | ctx.root = node; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 970 | LY_CHECK_RET(json_print_node(&ctx, node)); |
| 971 | if (!(options & LYD_PRINT_WITHSIBLINGS)) { |
| 972 | break; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /* end */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 977 | ly_print_(out, "%s}%s", delimiter, delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 978 | |
| 979 | assert(!ctx.open.count); |
| 980 | ly_set_erase(&ctx.open, NULL); |
| 981 | |
| 982 | ly_print_flush(out); |
| 983 | return LY_SUCCESS; |
| 984 | } |