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