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