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