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