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