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