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 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 298 | case LY_VALUE_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 299 | module_name = name->module_name; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 300 | break; |
Michal Vasko | 8976265 | 2023-02-09 13:58:37 +0100 | [diff] [blame] | 301 | case LY_VALUE_XML: { |
| 302 | const struct lys_module *mod = NULL; |
| 303 | |
| 304 | if (name->module_ns) { |
| 305 | mod = ly_ctx_get_module_implemented_ns(pctx->ctx, name->module_ns); |
| 306 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 307 | if (mod) { |
| 308 | module_name = mod->name; |
| 309 | } |
| 310 | break; |
Michal Vasko | 8976265 | 2023-02-09 13:58:37 +0100 | [diff] [blame] | 311 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 312 | default: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 313 | /* cannot be created */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 314 | LOGINT_RET(pctx->ctx); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 315 | } |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 316 | |
| 317 | name_str = name->name; |
| 318 | } else { |
| 319 | name_str = ""; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* print the member */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 323 | if (module_name && (!parent || (node_prefix(parent) != module_name))) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 324 | 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] | 325 | } else { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 326 | 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] | 327 | } |
| 328 | |
| 329 | return LY_SUCCESS; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @brief Print data value. |
| 334 | * |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 335 | * @param[in] pctx JSON printer context. |
| 336 | * @param[in] ctx Context used to print the value. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 337 | * @param[in] val Data value to be printed. |
| 338 | * @return LY_ERR value. |
| 339 | */ |
| 340 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 341 | 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] | 342 | { |
aPiecek | 0f6bf3e | 2021-08-25 15:47:49 +0200 | [diff] [blame] | 343 | ly_bool dynamic; |
Michal Vasko | 183b911 | 2021-11-04 16:02:24 +0100 | [diff] [blame] | 344 | LY_DATA_TYPE basetype; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 345 | 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] | 346 | |
Michal Vasko | 183b911 | 2021-11-04 16:02:24 +0100 | [diff] [blame] | 347 | basetype = val->realtype->basetype; |
| 348 | |
| 349 | print_val: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 350 | /* leafref is not supported */ |
Michal Vasko | 183b911 | 2021-11-04 16:02:24 +0100 | [diff] [blame] | 351 | switch (basetype) { |
| 352 | case LY_TYPE_UNION: |
| 353 | /* use the resolved type */ |
| 354 | basetype = val->subvalue->value.realtype->basetype; |
| 355 | goto print_val; |
| 356 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 357 | case LY_TYPE_BINARY: |
| 358 | case LY_TYPE_STRING: |
| 359 | case LY_TYPE_BITS: |
| 360 | case LY_TYPE_ENUM: |
| 361 | case LY_TYPE_INST: |
| 362 | case LY_TYPE_INT64: |
| 363 | case LY_TYPE_UINT64: |
| 364 | case LY_TYPE_DEC64: |
| 365 | case LY_TYPE_IDENT: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 366 | json_print_string(pctx->out, value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 367 | break; |
| 368 | |
| 369 | case LY_TYPE_INT8: |
| 370 | case LY_TYPE_INT16: |
| 371 | case LY_TYPE_INT32: |
| 372 | case LY_TYPE_UINT8: |
| 373 | case LY_TYPE_UINT16: |
| 374 | case LY_TYPE_UINT32: |
| 375 | case LY_TYPE_BOOL: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 376 | ly_print_(pctx->out, "%s", value[0] ? value : "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 377 | break; |
| 378 | |
| 379 | case LY_TYPE_EMPTY: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 380 | ly_print_(pctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 381 | break; |
| 382 | |
| 383 | default: |
| 384 | /* error */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 385 | LOGINT_RET(pctx->ctx); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if (dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 389 | free((char *)value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return LY_SUCCESS; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * @brief Print all the attributes of the opaq node. |
| 397 | * |
| 398 | * @param[in] ctx JSON printer context. |
| 399 | * @param[in] node Opaq node where the attributes are placed. |
| 400 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 401 | * @return LY_ERR value. |
| 402 | */ |
| 403 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 404 | 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] | 405 | { |
| 406 | struct lyd_attr *attr; |
| 407 | |
| 408 | if (wdmod) { |
Michal Vasko | b68c3b4 | 2022-05-20 10:36:40 +0200 | [diff] [blame] | 409 | ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 410 | LEVEL_PRINTED; |
| 411 | } |
| 412 | |
| 413 | for (attr = node->attr; attr; attr = attr->next) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 414 | json_print_member2(pctx, &node->node, attr->format, &attr->name, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 415 | |
Michal Vasko | 69bb5fe | 2022-12-01 13:52:21 +0100 | [diff] [blame] | 416 | if (attr->hints & (LYD_VALHINT_STRING | LYD_VALHINT_OCTNUM | LYD_VALHINT_HEXNUM | LYD_VALHINT_NUM64)) { |
| 417 | json_print_string(pctx->out, attr->value); |
| 418 | } else if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 419 | ly_print_(pctx->out, "%s", attr->value[0] ? attr->value : "null"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 420 | } else if (attr->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 421 | ly_print_(pctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 422 | } else { |
Michal Vasko | 69bb5fe | 2022-12-01 13:52:21 +0100 | [diff] [blame] | 423 | /* unknown value format with no hints, use universal string */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 424 | json_print_string(pctx->out, attr->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 425 | } |
| 426 | LEVEL_PRINTED; |
| 427 | } |
| 428 | |
| 429 | return LY_SUCCESS; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * @brief Print all the metadata of the node. |
| 434 | * |
| 435 | * @param[in] ctx JSON printer context. |
| 436 | * @param[in] node Node where the metadata are placed. |
| 437 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 438 | * @return LY_ERR value. |
| 439 | */ |
| 440 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 441 | 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] | 442 | { |
| 443 | struct lyd_meta *meta; |
| 444 | |
| 445 | if (wdmod) { |
Michal Vasko | b68c3b4 | 2022-05-20 10:36:40 +0200 | [diff] [blame] | 446 | ly_print_(pctx->out, "%*s\"%s:default\":true", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 447 | LEVEL_PRINTED; |
| 448 | } |
| 449 | |
| 450 | for (meta = node->meta; meta; meta = meta->next) { |
| 451 | PRINT_COMMA; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 452 | ly_print_(pctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : ""); |
| 453 | LY_CHECK_RET(json_print_value(pctx, LYD_CTX(node), &meta->value)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 454 | LEVEL_PRINTED; |
| 455 | } |
| 456 | |
| 457 | return LY_SUCCESS; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes. |
| 462 | * |
| 463 | * @param[in] ctx JSON printer context. |
| 464 | * @param[in] node Data node where the attributes/metadata are placed. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 465 | * @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] | 466 | * @return LY_ERR value. |
| 467 | */ |
| 468 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 469 | 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] | 470 | { |
| 471 | const struct lys_module *wdmod = NULL; |
| 472 | |
Michal Vasko | 6f39c82 | 2022-12-01 10:12:14 +0100 | [diff] [blame] | 473 | if (node->schema && (node->schema->nodetype != LYS_CONTAINER) && (node->flags & LYD_DEFAULT) && |
| 474 | (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 475 | /* we have implicit OR explicit default node */ |
| 476 | /* get with-defaults module */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 477 | 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] | 478 | } |
| 479 | |
Michal Vasko | b68c3b4 | 2022-05-20 10:36:40 +0200 | [diff] [blame] | 480 | if (node->schema && (node->meta || wdmod)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 481 | if (inner) { |
Michal Vasko | f2b0a04 | 2022-12-01 13:52:51 +0100 | [diff] [blame] | 482 | 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] | 483 | } else { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 484 | LY_CHECK_RET(json_print_member(pctx, node, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 485 | } |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 486 | ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 487 | LEVEL_INC; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 488 | LY_CHECK_RET(json_print_metadata(pctx, node, wdmod)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 489 | LEVEL_DEC; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 490 | ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 491 | LEVEL_PRINTED; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 492 | } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 493 | if (inner) { |
Michal Vasko | f2b0a04 | 2022-12-01 13:52:51 +0100 | [diff] [blame] | 494 | 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] | 495 | } else { |
Michal Vasko | f2b0a04 | 2022-12-01 13:52:51 +0100 | [diff] [blame] | 496 | 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] | 497 | &((struct lyd_node_opaq *)node)->name, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 498 | } |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 499 | ly_print_(pctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 500 | LEVEL_INC; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 501 | 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] | 502 | LEVEL_DEC; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 503 | ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 504 | LEVEL_PRINTED; |
| 505 | } |
| 506 | |
| 507 | return LY_SUCCESS; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * @brief Print leaf data node including its metadata. |
| 512 | * |
| 513 | * @param[in] ctx JSON printer context. |
| 514 | * @param[in] node Data node to print. |
| 515 | * @return LY_ERR value. |
| 516 | */ |
| 517 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 518 | json_print_leaf(struct jsonpr_ctx *pctx, const struct lyd_node *node) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 519 | { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 520 | LY_CHECK_RET(json_print_member(pctx, node, 0)); |
| 521 | 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] | 522 | LEVEL_PRINTED; |
| 523 | |
| 524 | /* print attributes as sibling */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 525 | json_print_attributes(pctx, node, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 526 | |
| 527 | return LY_SUCCESS; |
| 528 | } |
| 529 | |
| 530 | /** |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 531 | * @brief Print anydata/anyxml content. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 532 | * |
| 533 | * @param[in] ctx JSON printer context. |
| 534 | * @param[in] any Anydata node to print. |
| 535 | * @return LY_ERR value. |
| 536 | */ |
| 537 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 538 | 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] | 539 | { |
| 540 | LY_ERR ret = LY_SUCCESS; |
| 541 | struct lyd_node *iter; |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 542 | const struct lyd_node *prev_parent; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 543 | uint32_t prev_opts, temp_lo = 0; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 544 | |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 545 | assert(any->schema->nodetype & LYD_NODE_ANY); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 546 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 547 | if ((any->schema->nodetype == LYS_ANYDATA) && (any->value_type != LYD_ANYDATA_DATATREE)) { |
| 548 | LOGINT_RET(pctx->ctx); |
| 549 | } |
| 550 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 551 | if (any->value_type == LYD_ANYDATA_LYB) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 552 | 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] | 553 | |
| 554 | /* turn logging off */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 555 | ly_temp_log_options(&temp_lo); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 556 | |
| 557 | /* try to parse it into a data tree */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 558 | 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] | 559 | /* successfully parsed */ |
| 560 | free(any->value.mem); |
| 561 | any->value.tree = iter; |
| 562 | any->value_type = LYD_ANYDATA_DATATREE; |
| 563 | } |
| 564 | |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 565 | /* turn logging on again */ |
| 566 | ly_temp_log_options(NULL); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | switch (any->value_type) { |
| 570 | case LYD_ANYDATA_DATATREE: |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 571 | /* print as an object */ |
| 572 | ly_print_(pctx->out, "{%s", DO_FORMAT ? "\n" : ""); |
| 573 | LEVEL_INC; |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 574 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 575 | /* close opening tag and print data */ |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 576 | prev_parent = pctx->parent; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 577 | prev_opts = pctx->options; |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 578 | pctx->parent = &any->node; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 579 | pctx->options &= ~LYD_PRINT_WITHSIBLINGS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 580 | LY_LIST_FOR(any->value.tree, iter) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 581 | ret = json_print_node(pctx, iter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 582 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 583 | } |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 584 | pctx->parent = prev_parent; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 585 | pctx->options = prev_opts; |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 586 | |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 587 | /* terminate the object */ |
Michal Vasko | 23b51a8 | 2022-03-29 14:22:34 +0200 | [diff] [blame] | 588 | LEVEL_DEC; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 589 | if (DO_FORMAT) { |
| 590 | ly_print_(pctx->out, "\n%*s}", INDENT); |
| 591 | } else { |
| 592 | ly_print_(pctx->out, "}"); |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 593 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 594 | break; |
| 595 | case LYD_ANYDATA_JSON: |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 596 | if (!any->value.json) { |
| 597 | /* no content */ |
| 598 | if (any->schema->nodetype == LYS_ANYXML) { |
| 599 | ly_print_(pctx->out, "null"); |
| 600 | } else { |
| 601 | ly_print_(pctx->out, "{}"); |
| 602 | } |
| 603 | } else { |
| 604 | /* print without escaping special characters */ |
| 605 | ly_print_(pctx->out, "%s", any->value.json); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 606 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 607 | break; |
| 608 | case LYD_ANYDATA_STRING: |
| 609 | case LYD_ANYDATA_XML: |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 610 | if (!any->value.str) { |
| 611 | /* no content */ |
| 612 | if (any->schema->nodetype == LYS_ANYXML) { |
| 613 | ly_print_(pctx->out, "null"); |
| 614 | } else { |
| 615 | ly_print_(pctx->out, "{}"); |
| 616 | } |
| 617 | } else { |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 618 | /* print as a string */ |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 619 | json_print_string(pctx->out, any->value.str); |
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 | break; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 622 | case LYD_ANYDATA_LYB: |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 623 | /* LYB format is not supported */ |
| 624 | 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] | 625 | break; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return LY_SUCCESS; |
| 629 | } |
| 630 | |
| 631 | /** |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 632 | * @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] | 633 | * 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] | 634 | * |
| 635 | * @param[in] ctx JSON printer context. |
| 636 | * @param[in] node Data node to print. |
| 637 | * @return LY_ERR value. |
| 638 | */ |
| 639 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 640 | json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 641 | { |
| 642 | struct lyd_node *child; |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 643 | const struct lyd_node *prev_parent; |
Michal Vasko | 23b51a8 | 2022-03-29 14:22:34 +0200 | [diff] [blame] | 644 | struct lyd_node_opaq *opaq = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 645 | ly_bool has_content = 0; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 646 | |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 647 | LY_LIST_FOR(lyd_child(node), child) { |
Michal Vasko | 8db584d | 2022-03-30 13:42:48 +0200 | [diff] [blame] | 648 | if (lyd_node_should_print(child, pctx->options)) { |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | } |
| 652 | if (node->meta || child) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 653 | has_content = 1; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 654 | } |
Michal Vasko | 23b51a8 | 2022-03-29 14:22:34 +0200 | [diff] [blame] | 655 | if (!node->schema) { |
| 656 | opaq = (struct lyd_node_opaq *)node; |
| 657 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 658 | |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 659 | if ((node->schema && (node->schema->nodetype == LYS_LIST)) || |
Michal Vasko | 23b51a8 | 2022-03-29 14:22:34 +0200 | [diff] [blame] | 660 | (opaq && (opaq->hints != LYD_HINT_DATA) && (opaq->hints & LYD_NODEHINT_LIST))) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 661 | 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] | 662 | (DO_FORMAT ? ",\n" : ",") : "", INDENT, (DO_FORMAT && has_content) ? "\n" : ""); |
| 663 | } else { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 664 | 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] | 665 | (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 666 | } |
| 667 | LEVEL_INC; |
| 668 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 669 | json_print_attributes(pctx, node, 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 670 | |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 671 | /* print children */ |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 672 | prev_parent = pctx->parent; |
| 673 | pctx->parent = node; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 674 | LY_LIST_FOR(lyd_child(node), child) { |
| 675 | LY_CHECK_RET(json_print_node(pctx, child)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 676 | } |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 677 | pctx->parent = prev_parent; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 678 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 679 | LEVEL_DEC; |
| 680 | if (DO_FORMAT && has_content) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 681 | ly_print_(pctx->out, "\n%*s}", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 682 | } else { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 683 | ly_print_(pctx->out, "}"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 684 | } |
| 685 | LEVEL_PRINTED; |
| 686 | |
| 687 | return LY_SUCCESS; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * @brief Print container data node including its metadata. |
| 692 | * |
| 693 | * @param[in] ctx JSON printer context. |
| 694 | * @param[in] node Data node to print. |
| 695 | * @return LY_ERR value. |
| 696 | */ |
| 697 | static int |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 698 | json_print_container(struct jsonpr_ctx *pctx, const struct lyd_node *node) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 699 | { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 700 | LY_CHECK_RET(json_print_member(pctx, node, 0)); |
| 701 | LY_CHECK_RET(json_print_inner(pctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 702 | |
| 703 | return LY_SUCCESS; |
| 704 | } |
| 705 | |
| 706 | /** |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 707 | * @brief Print anydata/anyxml data node including its metadata. |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 708 | * |
| 709 | * @param[in] ctx JSON printer context. |
| 710 | * @param[in] node Data node to print. |
| 711 | * @return LY_ERR value. |
| 712 | */ |
| 713 | static int |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 714 | json_print_any(struct jsonpr_ctx *pctx, const struct lyd_node *node) |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 715 | { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 716 | LY_CHECK_RET(json_print_member(pctx, node, 0)); |
| 717 | 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] | 718 | LEVEL_PRINTED; |
| 719 | |
| 720 | /* print attributes as sibling */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 721 | json_print_attributes(pctx, node, 0); |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 722 | |
| 723 | return LY_SUCCESS; |
| 724 | } |
| 725 | |
| 726 | /** |
Michal Vasko | aa22f42 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 727 | * @brief Check whether a node is the last printed instance of a (leaf-)list. |
| 728 | * |
| 729 | * @param[in] ctx JSON printer context. |
| 730 | * @param[in] node Last printed node. |
| 731 | * @return Whether it is the last printed instance or not. |
| 732 | */ |
| 733 | static ly_bool |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 734 | 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] | 735 | { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 736 | if (!is_open_array(pctx, node)) { |
Michal Vasko | 06217f3 | 2021-12-09 09:25:50 +0100 | [diff] [blame] | 737 | /* no array open */ |
| 738 | return 0; |
| 739 | } |
| 740 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 741 | if ((pctx->root == node) && !(pctx->options & LYD_PRINT_WITHSIBLINGS)) { |
Michal Vasko | aa22f42 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 742 | /* the only printed instance */ |
| 743 | return 1; |
| 744 | } |
| 745 | |
Michal Vasko | 06217f3 | 2021-12-09 09:25:50 +0100 | [diff] [blame] | 746 | if (!node->next || (node->next->schema != node->schema)) { |
Michal Vasko | aa22f42 | 2021-12-02 10:48:32 +0100 | [diff] [blame] | 747 | /* last instance */ |
| 748 | return 1; |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 755 | * @brief Print single leaf-list or list instance. |
| 756 | * |
| 757 | * In case of list, metadata are printed inside the list object. For the leaf-list, |
| 758 | * metadata are marked in the context for later printing after closing the array next to it using |
| 759 | * json_print_metadata_leaflist(). |
| 760 | * |
| 761 | * @param[in] ctx JSON printer context. |
| 762 | * @param[in] node Data node to print. |
| 763 | * @return LY_ERR value. |
| 764 | */ |
| 765 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 766 | 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] | 767 | { |
Michal Vasko | 57c8d43 | 2022-11-30 15:35:56 +0100 | [diff] [blame] | 768 | const struct lys_module *wdmod = NULL; |
| 769 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 770 | if (!is_open_array(pctx, node)) { |
| 771 | LY_CHECK_RET(json_print_member(pctx, node, 0)); |
| 772 | LY_CHECK_RET(json_print_array_open(pctx, node)); |
Radek Krejci | ee74a19 | 2021-04-09 09:55:34 +0200 | [diff] [blame] | 773 | if (node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 774 | ly_print_(pctx->out, "%*s", INDENT); |
Radek Krejci | ee74a19 | 2021-04-09 09:55:34 +0200 | [diff] [blame] | 775 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 776 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 777 | ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | if (node->schema->nodetype == LYS_LIST) { |
Michal Vasko | 2fe1034 | 2022-12-01 11:54:02 +0100 | [diff] [blame] | 781 | /* print list's content */ |
| 782 | LY_CHECK_RET(json_print_inner(pctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 783 | } else { |
| 784 | assert(node->schema->nodetype == LYS_LEAFLIST); |
Michal Vasko | 57c8d43 | 2022-11-30 15:35:56 +0100 | [diff] [blame] | 785 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 786 | 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] | 787 | |
Michal Vasko | 57c8d43 | 2022-11-30 15:35:56 +0100 | [diff] [blame] | 788 | if (!pctx->print_sibling_metadata) { |
| 789 | if ((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) { |
| 790 | /* we have implicit OR explicit default node, get with-defaults module */ |
| 791 | wdmod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-netconf-with-defaults"); |
| 792 | } |
| 793 | if (node->meta || wdmod) { |
| 794 | /* we will be printing metadata for these siblings */ |
| 795 | pctx->print_sibling_metadata = node; |
| 796 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 800 | if (json_print_array_is_last_inst(pctx, node)) { |
| 801 | json_print_array_close(pctx); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | return LY_SUCCESS; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list(). |
| 809 | * This function is supposed to be called when the leaf-list array is closed. |
| 810 | * |
| 811 | * @param[in] ctx JSON printer context. |
| 812 | * @return LY_ERR value. |
| 813 | */ |
| 814 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 815 | json_print_metadata_leaflist(struct jsonpr_ctx *pctx) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 816 | { |
| 817 | const struct lyd_node *prev, *node, *iter; |
Michal Vasko | 57c8d43 | 2022-11-30 15:35:56 +0100 | [diff] [blame] | 818 | const struct lys_module *wdmod = NULL; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 819 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 820 | if (!pctx->print_sibling_metadata) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 821 | return LY_SUCCESS; |
| 822 | } |
| 823 | |
Michal Vasko | 57c8d43 | 2022-11-30 15:35:56 +0100 | [diff] [blame] | 824 | if (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG)) { |
| 825 | /* get with-defaults module */ |
| 826 | wdmod = ly_ctx_get_module_implemented(pctx->ctx, "ietf-netconf-with-defaults"); |
| 827 | } |
| 828 | |
| 829 | /* node is the first instance of the leaf-list */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 830 | for (node = pctx->print_sibling_metadata, prev = pctx->print_sibling_metadata->prev; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 831 | prev->next && matching_node(node, prev); |
| 832 | node = prev, prev = node->prev) {} |
| 833 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 834 | LY_CHECK_RET(json_print_member(pctx, node, 1)); |
| 835 | ly_print_(pctx->out, "[%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 836 | LEVEL_INC; |
| 837 | LY_LIST_FOR(node, iter) { |
| 838 | PRINT_COMMA; |
Michal Vasko | 57c8d43 | 2022-11-30 15:35:56 +0100 | [diff] [blame] | 839 | if (iter->meta || (iter->flags & LYD_DEFAULT)) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 840 | ly_print_(pctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 841 | LEVEL_INC; |
Michal Vasko | 57c8d43 | 2022-11-30 15:35:56 +0100 | [diff] [blame] | 842 | 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] | 843 | LEVEL_DEC; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 844 | ly_print_(pctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 845 | } else { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 846 | ly_print_(pctx->out, "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 847 | } |
| 848 | LEVEL_PRINTED; |
| 849 | if (!matching_node(iter, iter->next)) { |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | LEVEL_DEC; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 854 | ly_print_(pctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 855 | LEVEL_PRINTED; |
| 856 | |
| 857 | return LY_SUCCESS; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * @brief Print opaq data node including its attributes. |
| 862 | * |
| 863 | * @param[in] ctx JSON printer context. |
| 864 | * @param[in] node Opaq node to print. |
| 865 | * @return LY_ERR value. |
| 866 | */ |
| 867 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 868 | 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] | 869 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 870 | ly_bool first = 1, last = 1; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 871 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 872 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 873 | if (node->prev->next && matching_node(node->prev, &node->node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 874 | first = 0; |
| 875 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 876 | if (node->next && matching_node(&node->node, node->next)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 877 | last = 0; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | if (first) { |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 882 | 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] | 883 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 884 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 885 | LY_CHECK_RET(json_print_array_open(pctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 886 | } |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 887 | if (node->hints & LYD_NODEHINT_LEAFLIST) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 888 | ly_print_(pctx->out, "%*s", INDENT); |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 889 | } |
| 890 | } else if (node->hints & LYD_NODEHINT_LEAFLIST) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 891 | ly_print_(pctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 892 | } |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 893 | if (node->child || (node->hints & LYD_NODEHINT_LIST)) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 894 | LY_CHECK_RET(json_print_inner(pctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 895 | LEVEL_PRINTED; |
| 896 | } else { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 897 | if (node->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 898 | ly_print_(pctx->out, "[null]"); |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 899 | } 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] | 900 | ly_print_(pctx->out, "%s", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 901 | } else { |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 902 | /* string or a large number */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 903 | ly_print_(pctx->out, "\"%s\"", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 904 | } |
| 905 | LEVEL_PRINTED; |
| 906 | |
| 907 | /* attributes */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 908 | json_print_attributes(pctx, (const struct lyd_node *)node, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 909 | |
| 910 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 911 | if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 912 | json_print_array_close(pctx); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 913 | LEVEL_PRINTED; |
| 914 | } |
| 915 | |
| 916 | return LY_SUCCESS; |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * @brief Print all the types of data node including its metadata. |
| 921 | * |
| 922 | * @param[in] ctx JSON printer context. |
| 923 | * @param[in] node Data node to print. |
| 924 | * @return LY_ERR value. |
| 925 | */ |
| 926 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 927 | json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 928 | { |
Michal Vasko | 8db584d | 2022-03-30 13:42:48 +0200 | [diff] [blame] | 929 | if (!lyd_node_should_print(node, pctx->options)) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 930 | if (json_print_array_is_last_inst(pctx, node)) { |
| 931 | json_print_array_close(pctx); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 932 | } |
| 933 | return LY_SUCCESS; |
| 934 | } |
| 935 | |
| 936 | if (!node->schema) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 937 | 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] | 938 | } else { |
| 939 | switch (node->schema->nodetype) { |
| 940 | case LYS_RPC: |
| 941 | case LYS_ACTION: |
| 942 | case LYS_NOTIF: |
| 943 | case LYS_CONTAINER: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 944 | LY_CHECK_RET(json_print_container(pctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 945 | break; |
| 946 | case LYS_LEAF: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 947 | LY_CHECK_RET(json_print_leaf(pctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 948 | break; |
| 949 | case LYS_LEAFLIST: |
| 950 | case LYS_LIST: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 951 | LY_CHECK_RET(json_print_leaf_list(pctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 952 | break; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 953 | case LYS_ANYDATA: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 954 | case LYS_ANYXML: |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 955 | LY_CHECK_RET(json_print_any(pctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 956 | break; |
| 957 | default: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 958 | LOGINT(pctx->ctx); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 959 | return EXIT_FAILURE; |
| 960 | } |
| 961 | } |
| 962 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 963 | pctx->level_printed = pctx->level; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 964 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 965 | if (pctx->print_sibling_metadata && !matching_node(node->next, pctx->print_sibling_metadata)) { |
| 966 | json_print_metadata_leaflist(pctx); |
| 967 | pctx->print_sibling_metadata = NULL; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | return LY_SUCCESS; |
| 971 | } |
| 972 | |
| 973 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 974 | 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] | 975 | { |
| 976 | const struct lyd_node *node; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 977 | struct jsonpr_ctx pctx = {0}; |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 978 | const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n"; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 979 | |
Radek Krejci | 2e87477 | 2020-08-28 16:36:33 +0200 | [diff] [blame] | 980 | if (!root) { |
| 981 | ly_print_(out, "{}%s", delimiter); |
| 982 | ly_print_flush(out); |
| 983 | return LY_SUCCESS; |
| 984 | } |
| 985 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 986 | pctx.out = out; |
Michal Vasko | 26743a2 | 2022-03-29 14:48:10 +0200 | [diff] [blame] | 987 | pctx.parent = NULL; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 988 | pctx.level = 1; |
| 989 | pctx.level_printed = 0; |
| 990 | pctx.options = options; |
| 991 | pctx.ctx = LYD_CTX(root); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 992 | |
| 993 | /* start */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 994 | ly_print_(pctx.out, "{%s", delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 995 | |
| 996 | /* content */ |
| 997 | LY_LIST_FOR(root, node) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 998 | pctx.root = node; |
| 999 | LY_CHECK_RET(json_print_node(&pctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 1000 | if (!(options & LYD_PRINT_WITHSIBLINGS)) { |
| 1001 | break; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* end */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1006 | ly_print_(out, "%s}%s", delimiter, delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 1007 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1008 | assert(!pctx.open.count); |
| 1009 | ly_set_erase(&pctx.open, NULL); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 1010 | |
| 1011 | ly_print_flush(out); |
| 1012 | return LY_SUCCESS; |
| 1013 | } |