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