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 */ |
| 38 | uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
| 39 | uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */ |
| 40 | const struct ly_ctx *ctx; /**< libyang context */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 41 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 42 | uint16_t level_printed; /* level where some data were already printed */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 43 | struct ly_set open; /* currently open array(s) */ |
| 44 | const struct lyd_node *print_sibling_metadata; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * @brief Mark that something was already written in the current level, |
| 49 | * used to decide if a comma is expected between the items |
| 50 | */ |
| 51 | #define LEVEL_PRINTED ctx->level_printed = ctx->level |
| 52 | |
| 53 | #define PRINT_COMMA \ |
| 54 | if (ctx->level_printed >= ctx->level) {\ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 55 | ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node); |
| 59 | |
| 60 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 61 | * Compare 2 nodes, despite it is regular data node or an opaq node, and |
| 62 | * decide if they corresponds to the same schema node. |
| 63 | * |
| 64 | * TODO: rewrite lyd_compare_single and use it instead of this |
| 65 | * |
| 66 | * @return 1 - matching nodes, 0 - non-matching nodes |
| 67 | */ |
| 68 | static int |
| 69 | matching_node(const struct lyd_node *node1, const struct lyd_node *node2) |
| 70 | { |
| 71 | assert(node1 || node2); |
| 72 | |
| 73 | if (!node1 || !node2) { |
| 74 | return 0; |
| 75 | } else if (node1->schema != node2->schema) { |
| 76 | return 0; |
| 77 | } |
| 78 | if (!node1->schema) { |
| 79 | /* compare node names */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 80 | struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1; |
| 81 | struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 82 | 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] | 83 | return 0; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @brief Open the JSON array ('[') for the specified @p node |
| 92 | * |
| 93 | * @param[in] ctx JSON printer context. |
| 94 | * @param[in] node First node of the array. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 95 | * @return LY_ERR value. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 96 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 97 | static LY_ERR |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 98 | json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 99 | { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 100 | ly_print_(ctx->out, "[%s", DO_FORMAT ? "\n" : ""); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 101 | LY_CHECK_RET(ly_set_add(&ctx->open, (void *)node, 0, NULL)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 102 | LEVEL_INC; |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 103 | |
| 104 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @brief Get know if the array for the provided @p node is currently open. |
| 109 | * |
| 110 | * @param[in] ctx JSON printer context. |
| 111 | * @param[in] node Data node to check. |
| 112 | * @return 1 in case the printer is currently in the array belonging to the provided @p node. |
| 113 | * @return 0 in case the provided @p node is not connected with the currently open array (or there is no open array). |
| 114 | */ |
| 115 | static int |
| 116 | is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 117 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 118 | 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] | 119 | return 1; |
| 120 | } else { |
| 121 | return 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @brief Close the most inner JSON array. |
| 127 | * |
| 128 | * @param[in] ctx JSON printer context. |
| 129 | */ |
| 130 | static void |
| 131 | json_print_array_close(struct jsonpr_ctx *ctx) |
| 132 | { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 133 | LEVEL_DEC; |
| 134 | ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL); |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 135 | ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @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] | 140 | * |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 141 | * @param[in] node Node to process. |
| 142 | * @return The name of the module where the @p node belongs, it can be NULL in case the module name |
| 143 | * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context). |
| 144 | */ |
| 145 | static const char * |
| 146 | node_prefix(const struct lyd_node *node) |
| 147 | { |
| 148 | if (node->schema) { |
| 149 | return node->schema->module->name; |
| 150 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 151 | struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 152 | const struct lys_module *mod; |
| 153 | |
| 154 | switch (onode->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 155 | case LY_VALUE_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 156 | return onode->name.module_name; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 157 | case LY_VALUE_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 158 | 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] | 159 | if (!mod) { |
| 160 | return NULL; |
| 161 | } |
| 162 | return mod->name; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 163 | default: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 164 | /* cannot be created */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 165 | LOGINT(LYD_CTX(node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace) |
| 174 | * |
| 175 | * Accepts both regulard a well as opaq nodes. |
| 176 | * |
| 177 | * @param[in] node1 The first node to compare. |
| 178 | * @param[in] node2 The second node to compare. |
| 179 | * @return 0 in case the nodes' modules are the same |
| 180 | * @return 1 in case the nodes belongs to different modules |
| 181 | */ |
| 182 | int |
| 183 | json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2) |
| 184 | { |
| 185 | assert(node1 || node2); |
| 186 | |
| 187 | if (!node1 || !node2) { |
| 188 | return 1; |
| 189 | } else if (node1->schema && node2->schema) { |
| 190 | if (node1->schema->module == node2->schema->module) { |
| 191 | /* belongs to the same module */ |
| 192 | return 0; |
| 193 | } else { |
| 194 | /* different modules */ |
| 195 | return 1; |
| 196 | } |
| 197 | } else { |
| 198 | const char *pref1 = node_prefix(node1); |
| 199 | const char *pref2 = node_prefix(node2); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 200 | if ((pref1 && pref2) && (pref1 == pref2)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 201 | return 0; |
| 202 | } else { |
| 203 | return 1; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @brief Print the @p text as JSON string - encode special characters and add quotation around the string. |
| 210 | * |
| 211 | * @param[in] out The output handler. |
| 212 | * @param[in] text The string to print. |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 213 | * @return LY_ERR value. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 214 | */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 215 | static LY_ERR |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 216 | json_print_string(struct ly_out *out, const char *text) |
| 217 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 218 | uint64_t i, n; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 219 | |
| 220 | if (!text) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 221 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 224 | ly_write_(out, "\"", 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 225 | for (i = n = 0; text[i]; i++) { |
| 226 | const unsigned char ascii = text[i]; |
| 227 | if (ascii < 0x20) { |
| 228 | /* control character */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 229 | ly_print_(out, "\\u%.4X", ascii); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 230 | } else { |
| 231 | switch (ascii) { |
| 232 | case '"': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 233 | ly_print_(out, "\\\""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 234 | break; |
| 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 | default: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 239 | ly_write_(out, &text[i], 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 240 | n++; |
| 241 | } |
| 242 | } |
| 243 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 244 | ly_write_(out, "\"", 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 245 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 246 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed. |
| 251 | * |
| 252 | * @param[in] ctx JSON printer context. |
| 253 | * @param[in] node The data node being printed. |
| 254 | * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier. |
| 255 | * @return LY_ERR value. |
| 256 | */ |
| 257 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 258 | 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] | 259 | { |
| 260 | PRINT_COMMA; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 261 | if ((LEVEL == 1) || json_nscmp(node, (const struct lyd_node *)node->parent)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 262 | /* print "namespace" */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 263 | ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 264 | node_prefix(node), node->schema->name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 265 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 266 | ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 267 | node->schema->name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return LY_SUCCESS; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @brief More generic alternative to json_print_member() to print some special cases of the member names. |
| 275 | * |
| 276 | * @param[in] ctx JSON printer context. |
| 277 | * @param[in] parent Parent node to compare modules deciding if the prefix is printed. |
| 278 | * @param[in] format Format to decide how to process the @p prefix. |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 279 | * @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] | 280 | * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier. |
| 281 | * @return LY_ERR value. |
| 282 | */ |
| 283 | static LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 284 | 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] | 285 | const struct ly_opaq_name *name, ly_bool is_attr) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 286 | { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 287 | const char *module_name = NULL, *name_str; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 288 | |
| 289 | PRINT_COMMA; |
| 290 | |
| 291 | /* determine prefix string */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 292 | if (name) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 293 | const struct lys_module *mod; |
| 294 | |
| 295 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 296 | case LY_VALUE_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 297 | module_name = name->module_name; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 298 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 299 | case LY_VALUE_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 300 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, name->module_ns); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 301 | if (mod) { |
| 302 | module_name = mod->name; |
| 303 | } |
| 304 | break; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 305 | default: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 306 | /* cannot be created */ |
| 307 | LOGINT_RET(ctx->ctx); |
| 308 | } |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 309 | |
| 310 | name_str = name->name; |
| 311 | } else { |
| 312 | name_str = ""; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* print the member */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 316 | if (module_name && (!parent || (node_prefix(parent) != module_name))) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 317 | 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] | 318 | } else { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 319 | 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] | 320 | } |
| 321 | |
| 322 | return LY_SUCCESS; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @brief Print data value. |
| 327 | * |
| 328 | * @param[in] ctx JSON printer context. |
| 329 | * @param[in] val Data value to be printed. |
| 330 | * @return LY_ERR value. |
| 331 | */ |
| 332 | static LY_ERR |
| 333 | json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val) |
| 334 | { |
aPiecek | 0f6bf3e | 2021-08-25 15:47:49 +0200 | [diff] [blame^] | 335 | ly_bool dynamic; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 336 | 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] | 337 | |
| 338 | /* leafref is not supported */ |
| 339 | switch (val->realtype->basetype) { |
| 340 | case LY_TYPE_BINARY: |
| 341 | case LY_TYPE_STRING: |
| 342 | case LY_TYPE_BITS: |
| 343 | case LY_TYPE_ENUM: |
| 344 | case LY_TYPE_INST: |
| 345 | case LY_TYPE_INT64: |
| 346 | case LY_TYPE_UINT64: |
| 347 | case LY_TYPE_DEC64: |
| 348 | case LY_TYPE_IDENT: |
Michal Vasko | c5b59af | 2020-11-03 17:13:41 +0100 | [diff] [blame] | 349 | case LY_TYPE_UNION: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 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 | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 399 | json_print_member2(ctx, &node->node, attr->format, &attr->name, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 400 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 401 | if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 402 | ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 403 | } else if (attr->hints & LYD_VALHINT_EMPTY) { |
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. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 447 | * @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] | 448 | * @return LY_ERR value. |
| 449 | */ |
| 450 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 451 | 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] | 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) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 463 | 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] | 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) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 475 | 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] | 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, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 478 | &((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; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 591 | ly_bool has_content = 0; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 592 | |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 593 | LY_LIST_FOR(lyd_child(node), child) { |
| 594 | if (ly_should_print(child, ctx->options)) { |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | if (node->meta || child) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 599 | has_content = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 600 | } 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] | 601 | has_content = 1; |
| 602 | } |
| 603 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 604 | if (!node->schema || (node->schema->nodetype != LYS_LIST)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 605 | 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] | 606 | (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 607 | } else { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 608 | ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? (DO_FORMAT ? ",\n" : ",") : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 609 | INDENT, (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 610 | } |
| 611 | LEVEL_INC; |
| 612 | |
| 613 | json_print_attributes(ctx, node, 1); |
| 614 | |
| 615 | if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) { |
| 616 | /* print children */ |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 617 | LY_LIST_FOR(lyd_child(node), child) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 618 | LY_CHECK_RET(json_print_node(ctx, child)); |
| 619 | } |
| 620 | } else { |
| 621 | /* anydata */ |
| 622 | json_print_anydata(ctx, (struct lyd_node_any *)node); |
| 623 | } |
| 624 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 625 | LEVEL_DEC; |
| 626 | if (DO_FORMAT && has_content) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 627 | ly_print_(ctx->out, "\n%*s}", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 628 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 629 | ly_print_(ctx->out, "}"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 630 | } |
| 631 | LEVEL_PRINTED; |
| 632 | |
| 633 | return LY_SUCCESS; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * @brief Print container data node including its metadata. |
| 638 | * |
| 639 | * @param[in] ctx JSON printer context. |
| 640 | * @param[in] node Data node to print. |
| 641 | * @return LY_ERR value. |
| 642 | */ |
| 643 | static int |
| 644 | json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 645 | { |
| 646 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
| 647 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 648 | |
| 649 | return LY_SUCCESS; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * @brief Print single leaf-list or list instance. |
| 654 | * |
| 655 | * In case of list, metadata are printed inside the list object. For the leaf-list, |
| 656 | * metadata are marked in the context for later printing after closing the array next to it using |
| 657 | * json_print_metadata_leaflist(). |
| 658 | * |
| 659 | * @param[in] ctx JSON printer context. |
| 660 | * @param[in] node Data node to print. |
| 661 | * @return LY_ERR value. |
| 662 | */ |
| 663 | static LY_ERR |
| 664 | json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 665 | { |
| 666 | if (!is_open_array(ctx, node)) { |
| 667 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 668 | LY_CHECK_RET(json_print_array_open(ctx, node)); |
Radek Krejci | ee74a19 | 2021-04-09 09:55:34 +0200 | [diff] [blame] | 669 | if (node->schema->nodetype == LYS_LEAFLIST) { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 670 | ly_print_(ctx->out, "%*s", INDENT); |
Radek Krejci | ee74a19 | 2021-04-09 09:55:34 +0200 | [diff] [blame] | 671 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 672 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame] | 673 | ly_print_(ctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | if (node->schema->nodetype == LYS_LIST) { |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 677 | if (!lyd_child(node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 678 | /* empty, e.g. in case of filter */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 679 | 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] | 680 | LEVEL_PRINTED; |
| 681 | } else { |
| 682 | /* print list's content */ |
| 683 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 684 | } |
| 685 | } else { |
| 686 | assert(node->schema->nodetype == LYS_LEAFLIST); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 687 | 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] | 688 | |
| 689 | if (node->meta && !ctx->print_sibling_metadata) { |
| 690 | ctx->print_sibling_metadata = node; |
| 691 | } |
| 692 | } |
| 693 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 694 | if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 695 | json_print_array_close(ctx); |
| 696 | } |
| 697 | |
| 698 | return LY_SUCCESS; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list(). |
| 703 | * This function is supposed to be called when the leaf-list array is closed. |
| 704 | * |
| 705 | * @param[in] ctx JSON printer context. |
| 706 | * @return LY_ERR value. |
| 707 | */ |
| 708 | static LY_ERR |
| 709 | json_print_metadata_leaflist(struct jsonpr_ctx *ctx) |
| 710 | { |
| 711 | const struct lyd_node *prev, *node, *iter; |
| 712 | |
| 713 | if (!ctx->print_sibling_metadata) { |
| 714 | return LY_SUCCESS; |
| 715 | } |
| 716 | |
| 717 | for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev; |
| 718 | prev->next && matching_node(node, prev); |
| 719 | node = prev, prev = node->prev) {} |
| 720 | |
| 721 | /* node is the first instance of the leaf-list */ |
| 722 | |
| 723 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 724 | ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 725 | LEVEL_INC; |
| 726 | LY_LIST_FOR(node, iter) { |
| 727 | PRINT_COMMA; |
| 728 | if (iter->meta) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 729 | ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 730 | LEVEL_INC; |
| 731 | LY_CHECK_RET(json_print_metadata(ctx, iter, NULL)); |
| 732 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 733 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 734 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 735 | ly_print_(ctx->out, "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 736 | } |
| 737 | LEVEL_PRINTED; |
| 738 | if (!matching_node(iter, iter->next)) { |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 743 | ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 744 | LEVEL_PRINTED; |
| 745 | |
| 746 | return LY_SUCCESS; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * @brief Print opaq data node including its attributes. |
| 751 | * |
| 752 | * @param[in] ctx JSON printer context. |
| 753 | * @param[in] node Opaq node to print. |
| 754 | * @return LY_ERR value. |
| 755 | */ |
| 756 | static LY_ERR |
| 757 | json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 758 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 759 | ly_bool first = 1, last = 1; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 760 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 761 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 762 | if (node->prev->next && matching_node(node->prev, &node->node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 763 | first = 0; |
| 764 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 765 | if (node->next && matching_node(&node->node, node->next)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 766 | last = 0; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | if (first) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 771 | 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] | 772 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 773 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 774 | LY_CHECK_RET(json_print_array_open(ctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 775 | LEVEL_INC; |
| 776 | } |
| 777 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 778 | if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 779 | LY_CHECK_RET(json_print_inner(ctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 780 | LEVEL_PRINTED; |
| 781 | } else { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 782 | if (node->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 783 | ly_print_(ctx->out, "[null]"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 784 | } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 785 | ly_print_(ctx->out, "%s", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 786 | } else { |
| 787 | /* string */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 788 | ly_print_(ctx->out, "\"%s\"", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 789 | } |
| 790 | LEVEL_PRINTED; |
| 791 | |
| 792 | /* attributes */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 793 | json_print_attributes(ctx, (const struct lyd_node *)node, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 794 | |
| 795 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 796 | if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 797 | json_print_array_close(ctx); |
| 798 | LEVEL_DEC; |
| 799 | LEVEL_PRINTED; |
| 800 | } |
| 801 | |
| 802 | return LY_SUCCESS; |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * @brief Print all the types of data node including its metadata. |
| 807 | * |
| 808 | * @param[in] ctx JSON printer context. |
| 809 | * @param[in] node Data node to print. |
| 810 | * @return LY_ERR value. |
| 811 | */ |
| 812 | static LY_ERR |
| 813 | json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 814 | { |
| 815 | if (!ly_should_print(node, ctx->options)) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 816 | if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 817 | json_print_array_close(ctx); |
| 818 | } |
| 819 | return LY_SUCCESS; |
| 820 | } |
| 821 | |
| 822 | if (!node->schema) { |
| 823 | LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node)); |
| 824 | } else { |
| 825 | switch (node->schema->nodetype) { |
| 826 | case LYS_RPC: |
| 827 | case LYS_ACTION: |
| 828 | case LYS_NOTIF: |
| 829 | case LYS_CONTAINER: |
| 830 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 831 | break; |
| 832 | case LYS_LEAF: |
| 833 | LY_CHECK_RET(json_print_leaf(ctx, node)); |
| 834 | break; |
| 835 | case LYS_LEAFLIST: |
| 836 | case LYS_LIST: |
| 837 | LY_CHECK_RET(json_print_leaf_list(ctx, node)); |
| 838 | break; |
| 839 | case LYS_ANYXML: |
| 840 | case LYS_ANYDATA: |
| 841 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 842 | break; |
| 843 | default: |
| 844 | LOGINT(node->schema->module->ctx); |
| 845 | return EXIT_FAILURE; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | ctx->level_printed = ctx->level; |
| 850 | |
| 851 | if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) { |
| 852 | json_print_metadata_leaflist(ctx); |
| 853 | ctx->print_sibling_metadata = NULL; |
| 854 | } |
| 855 | |
| 856 | return LY_SUCCESS; |
| 857 | } |
| 858 | |
| 859 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 860 | 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] | 861 | { |
| 862 | const struct lyd_node *node; |
| 863 | struct jsonpr_ctx ctx = {0}; |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 864 | const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n"; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 865 | |
Radek Krejci | 2e87477 | 2020-08-28 16:36:33 +0200 | [diff] [blame] | 866 | if (!root) { |
| 867 | ly_print_(out, "{}%s", delimiter); |
| 868 | ly_print_flush(out); |
| 869 | return LY_SUCCESS; |
| 870 | } |
| 871 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 872 | ctx.out = out; |
| 873 | ctx.level = 1; |
| 874 | ctx.level_printed = 0; |
| 875 | ctx.options = options; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 876 | ctx.ctx = LYD_CTX(root); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 877 | |
| 878 | /* start */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 879 | ly_print_(ctx.out, "{%s", delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 880 | |
| 881 | /* content */ |
| 882 | LY_LIST_FOR(root, node) { |
| 883 | LY_CHECK_RET(json_print_node(&ctx, node)); |
| 884 | if (!(options & LYD_PRINT_WITHSIBLINGS)) { |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /* end */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 890 | ly_print_(out, "%s}%s", delimiter, delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 891 | |
| 892 | assert(!ctx.open.count); |
| 893 | ly_set_erase(&ctx.open, NULL); |
| 894 | |
| 895 | ly_print_flush(out); |
| 896 | return LY_SUCCESS; |
| 897 | } |