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 */ |
| 38 | uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
| 39 | uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */ |
| 40 | const struct ly_ctx *ctx; /**< libyang context */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 41 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 42 | uint16_t level_printed; /* level where some data were already printed */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 43 | struct ly_set open; /* currently open array(s) */ |
| 44 | const struct lyd_node *print_sibling_metadata; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * @brief Mark that something was already written in the current level, |
| 49 | * used to decide if a comma is expected between the items |
| 50 | */ |
| 51 | #define LEVEL_PRINTED ctx->level_printed = ctx->level |
| 52 | |
| 53 | #define PRINT_COMMA \ |
| 54 | if (ctx->level_printed >= ctx->level) {\ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 55 | ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node); |
| 59 | |
| 60 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 61 | * Compare 2 nodes, despite it is regular data node or an opaq node, and |
| 62 | * decide if they corresponds to the same schema node. |
| 63 | * |
| 64 | * TODO: rewrite lyd_compare_single and use it instead of this |
| 65 | * |
| 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 |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 98 | json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 99 | { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame^] | 100 | ly_print_(ctx->out, "[%s", DO_FORMAT ? "\n" : ""); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 101 | LY_CHECK_RET(ly_set_add(&ctx->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 |
| 116 | is_open_array(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 117 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 118 | if (ctx->open.count && matching_node(node, (const struct lyd_node *)ctx->open.objs[ctx->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 |
| 131 | json_print_array_close(struct jsonpr_ctx *ctx) |
| 132 | { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 133 | LEVEL_DEC; |
| 134 | ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL); |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame^] | 135 | ly_print_(ctx->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. |
| 140 | * @param[in] node Node to process. |
| 141 | * @return The name of the module where the @p node belongs, it can be NULL in case the module name |
| 142 | * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context). |
| 143 | */ |
| 144 | static const char * |
| 145 | node_prefix(const struct lyd_node *node) |
| 146 | { |
| 147 | if (node->schema) { |
| 148 | return node->schema->module->name; |
| 149 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 150 | struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 151 | const struct lys_module *mod; |
| 152 | |
| 153 | switch (onode->format) { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 154 | case LY_PREF_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 155 | return onode->name.module_name; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 156 | case LY_PREF_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 157 | 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] | 158 | if (!mod) { |
| 159 | return NULL; |
| 160 | } |
| 161 | return mod->name; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 162 | default: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 163 | /* cannot be created */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 164 | LOGINT(LYD_CTX(node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @brief Compare 2 nodes if the belongs to the same module (if they come from the same namespace) |
| 173 | * |
| 174 | * Accepts both regulard a well as opaq nodes. |
| 175 | * |
| 176 | * @param[in] node1 The first node to compare. |
| 177 | * @param[in] node2 The second node to compare. |
| 178 | * @return 0 in case the nodes' modules are the same |
| 179 | * @return 1 in case the nodes belongs to different modules |
| 180 | */ |
| 181 | int |
| 182 | json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2) |
| 183 | { |
| 184 | assert(node1 || node2); |
| 185 | |
| 186 | if (!node1 || !node2) { |
| 187 | return 1; |
| 188 | } else if (node1->schema && node2->schema) { |
| 189 | if (node1->schema->module == node2->schema->module) { |
| 190 | /* belongs to the same module */ |
| 191 | return 0; |
| 192 | } else { |
| 193 | /* different modules */ |
| 194 | return 1; |
| 195 | } |
| 196 | } else { |
| 197 | const char *pref1 = node_prefix(node1); |
| 198 | const char *pref2 = node_prefix(node2); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 199 | if ((pref1 && pref2) && (pref1 == pref2)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 200 | return 0; |
| 201 | } else { |
| 202 | return 1; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @brief Print the @p text as JSON string - encode special characters and add quotation around the string. |
| 209 | * |
| 210 | * @param[in] out The output handler. |
| 211 | * @param[in] text The string to print. |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 212 | * @return LY_ERR value. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 213 | */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 214 | static LY_ERR |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 215 | json_print_string(struct ly_out *out, const char *text) |
| 216 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 217 | uint64_t i, n; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 218 | |
| 219 | if (!text) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 220 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 223 | ly_write_(out, "\"", 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 224 | for (i = n = 0; text[i]; i++) { |
| 225 | const unsigned char ascii = text[i]; |
| 226 | if (ascii < 0x20) { |
| 227 | /* control character */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 228 | ly_print_(out, "\\u%.4X", ascii); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 229 | } else { |
| 230 | switch (ascii) { |
| 231 | case '"': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 232 | ly_print_(out, "\\\""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 233 | break; |
| 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 | default: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 238 | ly_write_(out, &text[i], 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 239 | n++; |
| 240 | } |
| 241 | } |
| 242 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 243 | ly_write_(out, "\"", 1); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 244 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 245 | return LY_SUCCESS; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @brief Print JSON object's member name, ending by ':'. It resolves if the prefix is supposed to be printed. |
| 250 | * |
| 251 | * @param[in] ctx JSON printer context. |
| 252 | * @param[in] node The data node being printed. |
| 253 | * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier. |
| 254 | * @return LY_ERR value. |
| 255 | */ |
| 256 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 257 | json_print_member(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool is_attr) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 258 | { |
| 259 | PRINT_COMMA; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 260 | if ((LEVEL == 1) || json_nscmp(node, (const struct lyd_node *)node->parent)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 261 | /* print "namespace" */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 262 | ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 263 | node_prefix(node), node->schema->name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 264 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 265 | ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 266 | 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 | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 283 | json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LY_PREFIX_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) { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 295 | case LY_PREF_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; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 298 | case LY_PREF_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 299 | mod = ly_ctx_get_module_implemented_ns(ctx->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 */ |
| 306 | LOGINT_RET(ctx->ctx); |
| 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 | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 316 | ly_print_(ctx->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 | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 318 | ly_print_(ctx->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 | * |
| 327 | * @param[in] ctx JSON printer context. |
| 328 | * @param[in] val Data value to be printed. |
| 329 | * @return LY_ERR value. |
| 330 | */ |
| 331 | static LY_ERR |
| 332 | json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val) |
| 333 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 334 | ly_bool dynamic = 0; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 335 | const char *value = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 336 | |
| 337 | /* leafref is not supported */ |
| 338 | switch (val->realtype->basetype) { |
| 339 | case LY_TYPE_BINARY: |
| 340 | case LY_TYPE_STRING: |
| 341 | case LY_TYPE_BITS: |
| 342 | case LY_TYPE_ENUM: |
| 343 | case LY_TYPE_INST: |
| 344 | case LY_TYPE_INT64: |
| 345 | case LY_TYPE_UINT64: |
| 346 | case LY_TYPE_DEC64: |
| 347 | case LY_TYPE_IDENT: |
Michal Vasko | c5b59af | 2020-11-03 17:13:41 +0100 | [diff] [blame] | 348 | case LY_TYPE_UNION: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 349 | json_print_string(ctx->out, value); |
| 350 | break; |
| 351 | |
| 352 | case LY_TYPE_INT8: |
| 353 | case LY_TYPE_INT16: |
| 354 | case LY_TYPE_INT32: |
| 355 | case LY_TYPE_UINT8: |
| 356 | case LY_TYPE_UINT16: |
| 357 | case LY_TYPE_UINT32: |
| 358 | case LY_TYPE_BOOL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 359 | ly_print_(ctx->out, "%s", value[0] ? value : "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 360 | break; |
| 361 | |
| 362 | case LY_TYPE_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 363 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 364 | break; |
| 365 | |
| 366 | default: |
| 367 | /* error */ |
| 368 | LOGINT_RET(ctx->ctx); |
| 369 | } |
| 370 | |
| 371 | if (dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 372 | free((char *)value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | return LY_SUCCESS; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * @brief Print all the attributes of the opaq node. |
| 380 | * |
| 381 | * @param[in] ctx JSON printer context. |
| 382 | * @param[in] node Opaq node where the attributes are placed. |
| 383 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 384 | * @return LY_ERR value. |
| 385 | */ |
| 386 | static LY_ERR |
| 387 | json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod) |
| 388 | { |
| 389 | struct lyd_attr *attr; |
| 390 | |
| 391 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 392 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 393 | LEVEL_PRINTED; |
| 394 | } |
| 395 | |
| 396 | for (attr = node->attr; attr; attr = attr->next) { |
| 397 | PRINT_COMMA; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 398 | json_print_member2(ctx, &node->node, attr->format, &attr->name, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 399 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 400 | if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 401 | ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 402 | } else if (attr->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 403 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 404 | } else { |
| 405 | json_print_string(ctx->out, attr->value); |
| 406 | } |
| 407 | LEVEL_PRINTED; |
| 408 | } |
| 409 | |
| 410 | return LY_SUCCESS; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * @brief Print all the metadata of the node. |
| 415 | * |
| 416 | * @param[in] ctx JSON printer context. |
| 417 | * @param[in] node Node where the metadata are placed. |
| 418 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 419 | * @return LY_ERR value. |
| 420 | */ |
| 421 | static LY_ERR |
| 422 | json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod) |
| 423 | { |
| 424 | struct lyd_meta *meta; |
| 425 | |
| 426 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 427 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 428 | LEVEL_PRINTED; |
| 429 | } |
| 430 | |
| 431 | for (meta = node->meta; meta; meta = meta->next) { |
| 432 | PRINT_COMMA; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 433 | ly_print_(ctx->out, "%*s\"%s:%s\":%s", INDENT, meta->annotation->module->name, meta->name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 434 | LY_CHECK_RET(json_print_value(ctx, &meta->value)); |
| 435 | LEVEL_PRINTED; |
| 436 | } |
| 437 | |
| 438 | return LY_SUCCESS; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes. |
| 443 | * |
| 444 | * @param[in] ctx JSON printer context. |
| 445 | * @param[in] node Data node where the attributes/metadata are placed. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 446 | * @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] | 447 | * @return LY_ERR value. |
| 448 | */ |
| 449 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 450 | json_print_attributes(struct jsonpr_ctx *ctx, const struct lyd_node *node, ly_bool inner) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 451 | { |
| 452 | const struct lys_module *wdmod = NULL; |
| 453 | |
| 454 | if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) { |
| 455 | /* we have implicit OR explicit default node */ |
| 456 | /* get with-defaults module */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 457 | 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] | 458 | } |
| 459 | |
| 460 | if (node->schema && node->meta) { |
| 461 | if (inner) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 462 | LY_CHECK_RET(json_print_member2(ctx, NULL, LY_PREF_JSON, NULL, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 463 | } else { |
| 464 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
| 465 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 466 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 467 | LEVEL_INC; |
| 468 | LY_CHECK_RET(json_print_metadata(ctx, node, wdmod)); |
| 469 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 470 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 471 | LEVEL_PRINTED; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 472 | } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 473 | if (inner) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 474 | LY_CHECK_RET(json_print_member2(ctx, NULL, LY_PREF_JSON, NULL, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 475 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 476 | LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 477 | &((struct lyd_node_opaq *)node)->name, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 478 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 479 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 480 | LEVEL_INC; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 481 | LY_CHECK_RET(json_print_attribute(ctx, (struct lyd_node_opaq *)node, wdmod)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 482 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 483 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 484 | LEVEL_PRINTED; |
| 485 | } |
| 486 | |
| 487 | return LY_SUCCESS; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * @brief Print leaf data node including its metadata. |
| 492 | * |
| 493 | * @param[in] ctx JSON printer context. |
| 494 | * @param[in] node Data node to print. |
| 495 | * @return LY_ERR value. |
| 496 | */ |
| 497 | static LY_ERR |
| 498 | json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 499 | { |
| 500 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 501 | LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 502 | LEVEL_PRINTED; |
| 503 | |
| 504 | /* print attributes as sibling */ |
| 505 | json_print_attributes(ctx, node, 0); |
| 506 | |
| 507 | return LY_SUCCESS; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * @brief Print anydata data node including its metadata. |
| 512 | * |
| 513 | * @param[in] ctx JSON printer context. |
| 514 | * @param[in] any Anydata node to print. |
| 515 | * @return LY_ERR value. |
| 516 | */ |
| 517 | static LY_ERR |
| 518 | json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any) |
| 519 | { |
| 520 | LY_ERR ret = LY_SUCCESS; |
| 521 | struct lyd_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 522 | uint32_t prev_opts, prev_lo; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 523 | |
| 524 | if (!any->value.tree) { |
| 525 | /* no content */ |
| 526 | return LY_SUCCESS; |
| 527 | } |
| 528 | |
| 529 | if (any->value_type == LYD_ANYDATA_LYB) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 530 | 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] | 531 | |
| 532 | /* turn logging off */ |
| 533 | prev_lo = ly_log_options(0); |
| 534 | |
| 535 | /* try to parse it into a data tree */ |
| 536 | if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) { |
| 537 | /* successfully parsed */ |
| 538 | free(any->value.mem); |
| 539 | any->value.tree = iter; |
| 540 | any->value_type = LYD_ANYDATA_DATATREE; |
| 541 | } |
| 542 | |
| 543 | /* turn loggin on again */ |
| 544 | ly_log_options(prev_lo); |
| 545 | } |
| 546 | |
| 547 | switch (any->value_type) { |
| 548 | case LYD_ANYDATA_DATATREE: |
| 549 | /* close opening tag and print data */ |
| 550 | prev_opts = ctx->options; |
| 551 | ctx->options &= ~LYD_PRINT_WITHSIBLINGS; |
| 552 | |
| 553 | LY_LIST_FOR(any->value.tree, iter) { |
| 554 | ret = json_print_node(ctx, iter); |
| 555 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 556 | } |
| 557 | |
| 558 | ctx->options = prev_opts; |
| 559 | break; |
| 560 | case LYD_ANYDATA_JSON: |
| 561 | /* print without escaping special characters */ |
| 562 | if (!any->value.str[0]) { |
| 563 | return LY_SUCCESS; |
| 564 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 565 | ly_print_(ctx->out, "%*s%s", INDENT, any->value.str); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 566 | break; |
| 567 | case LYD_ANYDATA_STRING: |
| 568 | case LYD_ANYDATA_XML: |
| 569 | case LYD_ANYDATA_LYB: |
| 570 | /* JSON and LYB format is not supported */ |
| 571 | LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type); |
| 572 | return LY_SUCCESS; |
| 573 | } |
| 574 | |
| 575 | return LY_SUCCESS; |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * @brief Print content of a single container/list data node including its metadata. |
| 580 | * The envelope specific to container and list are expected to be printed by the caller. |
| 581 | * |
| 582 | * @param[in] ctx JSON printer context. |
| 583 | * @param[in] node Data node to print. |
| 584 | * @return LY_ERR value. |
| 585 | */ |
| 586 | static LY_ERR |
| 587 | json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 588 | { |
| 589 | struct lyd_node *child; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 590 | ly_bool has_content = 0; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 591 | |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 592 | LY_LIST_FOR(lyd_child(node), child) { |
| 593 | if (ly_should_print(child, ctx->options)) { |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | if (node->meta || child) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 598 | has_content = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 599 | } else if (node->schema && (node->schema->nodetype & LYD_NODE_ANY) && ((struct lyd_node_any *)node)->value.tree) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 600 | has_content = 1; |
| 601 | } |
| 602 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 603 | if (!node->schema || (node->schema->nodetype != LYS_LIST)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 604 | ly_print_(ctx->out, "%s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? "," : "", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 605 | (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 606 | } else { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame^] | 607 | ly_print_(ctx->out, "%s%*s{%s", (is_open_array(ctx, node) && ctx->level_printed >= ctx->level) ? (DO_FORMAT ? ",\n" : ",") : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 608 | INDENT, (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 609 | } |
| 610 | LEVEL_INC; |
| 611 | |
| 612 | json_print_attributes(ctx, node, 1); |
| 613 | |
| 614 | if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) { |
| 615 | /* print children */ |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 616 | LY_LIST_FOR(lyd_child(node), child) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 617 | LY_CHECK_RET(json_print_node(ctx, child)); |
| 618 | } |
| 619 | } else { |
| 620 | /* anydata */ |
| 621 | json_print_anydata(ctx, (struct lyd_node_any *)node); |
| 622 | } |
| 623 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 624 | LEVEL_DEC; |
| 625 | if (DO_FORMAT && has_content) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 626 | ly_print_(ctx->out, "\n%*s}", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 627 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 628 | ly_print_(ctx->out, "}"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 629 | } |
| 630 | LEVEL_PRINTED; |
| 631 | |
| 632 | return LY_SUCCESS; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * @brief Print container data node including its metadata. |
| 637 | * |
| 638 | * @param[in] ctx JSON printer context. |
| 639 | * @param[in] node Data node to print. |
| 640 | * @return LY_ERR value. |
| 641 | */ |
| 642 | static int |
| 643 | json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 644 | { |
| 645 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
| 646 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 647 | |
| 648 | return LY_SUCCESS; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @brief Print single leaf-list or list instance. |
| 653 | * |
| 654 | * In case of list, metadata are printed inside the list object. For the leaf-list, |
| 655 | * metadata are marked in the context for later printing after closing the array next to it using |
| 656 | * json_print_metadata_leaflist(). |
| 657 | * |
| 658 | * @param[in] ctx JSON printer context. |
| 659 | * @param[in] node Data node to print. |
| 660 | * @return LY_ERR value. |
| 661 | */ |
| 662 | static LY_ERR |
| 663 | json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 664 | { |
| 665 | if (!is_open_array(ctx, node)) { |
| 666 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 667 | LY_CHECK_RET(json_print_array_open(ctx, node)); |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame^] | 668 | if (node->schema->nodetype == LYS_LEAFLIST) |
| 669 | ly_print_(ctx->out, "%*s", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 670 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Christian Hopps | be51c94 | 2021-04-02 16:29:51 -0400 | [diff] [blame^] | 671 | ly_print_(ctx->out, ",%s%*s", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | if (node->schema->nodetype == LYS_LIST) { |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 675 | if (!lyd_child(node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 676 | /* empty, e.g. in case of filter */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 677 | ly_print_(ctx->out, "%s%snull", (ctx->level_printed >= ctx->level) ? "," : "", DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 678 | LEVEL_PRINTED; |
| 679 | } else { |
| 680 | /* print list's content */ |
| 681 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 682 | } |
| 683 | } else { |
| 684 | assert(node->schema->nodetype == LYS_LEAFLIST); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 685 | LY_CHECK_RET(json_print_value(ctx, &((const struct lyd_node_term *)node)->value)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 686 | |
| 687 | if (node->meta && !ctx->print_sibling_metadata) { |
| 688 | ctx->print_sibling_metadata = node; |
| 689 | } |
| 690 | } |
| 691 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 692 | if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 693 | json_print_array_close(ctx); |
| 694 | } |
| 695 | |
| 696 | return LY_SUCCESS; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list(). |
| 701 | * This function is supposed to be called when the leaf-list array is closed. |
| 702 | * |
| 703 | * @param[in] ctx JSON printer context. |
| 704 | * @return LY_ERR value. |
| 705 | */ |
| 706 | static LY_ERR |
| 707 | json_print_metadata_leaflist(struct jsonpr_ctx *ctx) |
| 708 | { |
| 709 | const struct lyd_node *prev, *node, *iter; |
| 710 | |
| 711 | if (!ctx->print_sibling_metadata) { |
| 712 | return LY_SUCCESS; |
| 713 | } |
| 714 | |
| 715 | for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev; |
| 716 | prev->next && matching_node(node, prev); |
| 717 | node = prev, prev = node->prev) {} |
| 718 | |
| 719 | /* node is the first instance of the leaf-list */ |
| 720 | |
| 721 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 722 | ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 723 | LEVEL_INC; |
| 724 | LY_LIST_FOR(node, iter) { |
| 725 | PRINT_COMMA; |
| 726 | if (iter->meta) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 727 | ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 728 | LEVEL_INC; |
| 729 | LY_CHECK_RET(json_print_metadata(ctx, iter, NULL)); |
| 730 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 731 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 732 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 733 | ly_print_(ctx->out, "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 734 | } |
| 735 | LEVEL_PRINTED; |
| 736 | if (!matching_node(iter, iter->next)) { |
| 737 | break; |
| 738 | } |
| 739 | } |
| 740 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 741 | ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 742 | LEVEL_PRINTED; |
| 743 | |
| 744 | return LY_SUCCESS; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * @brief Print opaq data node including its attributes. |
| 749 | * |
| 750 | * @param[in] ctx JSON printer context. |
| 751 | * @param[in] node Opaq node to print. |
| 752 | * @return LY_ERR value. |
| 753 | */ |
| 754 | static LY_ERR |
| 755 | json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 756 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 757 | ly_bool first = 1, last = 1; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 758 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 759 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 760 | if (node->prev->next && matching_node(node->prev, &node->node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 761 | first = 0; |
| 762 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 763 | if (node->next && matching_node(&node->node, node->next)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 764 | last = 0; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | if (first) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 769 | LY_CHECK_RET(json_print_member2(ctx, lyd_parent(&node->node), node->format, &node->name, 0)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 770 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 771 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 772 | LY_CHECK_RET(json_print_array_open(ctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 773 | LEVEL_INC; |
| 774 | } |
| 775 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 776 | if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 777 | LY_CHECK_RET(json_print_inner(ctx, &node->node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 778 | LEVEL_PRINTED; |
| 779 | } else { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 780 | if (node->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 781 | ly_print_(ctx->out, "[null]"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 782 | } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 783 | ly_print_(ctx->out, "%s", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 784 | } else { |
| 785 | /* string */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 786 | ly_print_(ctx->out, "\"%s\"", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 787 | } |
| 788 | LEVEL_PRINTED; |
| 789 | |
| 790 | /* attributes */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 791 | json_print_attributes(ctx, (const struct lyd_node *)node, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 792 | |
| 793 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 794 | if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 795 | json_print_array_close(ctx); |
| 796 | LEVEL_DEC; |
| 797 | LEVEL_PRINTED; |
| 798 | } |
| 799 | |
| 800 | return LY_SUCCESS; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * @brief Print all the types of data node including its metadata. |
| 805 | * |
| 806 | * @param[in] ctx JSON printer context. |
| 807 | * @param[in] node Data node to print. |
| 808 | * @return LY_ERR value. |
| 809 | */ |
| 810 | static LY_ERR |
| 811 | json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 812 | { |
| 813 | if (!ly_should_print(node, ctx->options)) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 814 | if (is_open_array(ctx, node) && (!node->next || (node->next->schema != node->schema))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 815 | json_print_array_close(ctx); |
| 816 | } |
| 817 | return LY_SUCCESS; |
| 818 | } |
| 819 | |
| 820 | if (!node->schema) { |
| 821 | LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node)); |
| 822 | } else { |
| 823 | switch (node->schema->nodetype) { |
| 824 | case LYS_RPC: |
| 825 | case LYS_ACTION: |
| 826 | case LYS_NOTIF: |
| 827 | case LYS_CONTAINER: |
| 828 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 829 | break; |
| 830 | case LYS_LEAF: |
| 831 | LY_CHECK_RET(json_print_leaf(ctx, node)); |
| 832 | break; |
| 833 | case LYS_LEAFLIST: |
| 834 | case LYS_LIST: |
| 835 | LY_CHECK_RET(json_print_leaf_list(ctx, node)); |
| 836 | break; |
| 837 | case LYS_ANYXML: |
| 838 | case LYS_ANYDATA: |
| 839 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 840 | break; |
| 841 | default: |
| 842 | LOGINT(node->schema->module->ctx); |
| 843 | return EXIT_FAILURE; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | ctx->level_printed = ctx->level; |
| 848 | |
| 849 | if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) { |
| 850 | json_print_metadata_leaflist(ctx); |
| 851 | ctx->print_sibling_metadata = NULL; |
| 852 | } |
| 853 | |
| 854 | return LY_SUCCESS; |
| 855 | } |
| 856 | |
| 857 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 858 | 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] | 859 | { |
| 860 | const struct lyd_node *node; |
| 861 | struct jsonpr_ctx ctx = {0}; |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 862 | const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n"; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 863 | |
Radek Krejci | 2e87477 | 2020-08-28 16:36:33 +0200 | [diff] [blame] | 864 | if (!root) { |
| 865 | ly_print_(out, "{}%s", delimiter); |
| 866 | ly_print_flush(out); |
| 867 | return LY_SUCCESS; |
| 868 | } |
| 869 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 870 | ctx.out = out; |
| 871 | ctx.level = 1; |
| 872 | ctx.level_printed = 0; |
| 873 | ctx.options = options; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 874 | ctx.ctx = LYD_CTX(root); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 875 | |
| 876 | /* start */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 877 | ly_print_(ctx.out, "{%s", delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 878 | |
| 879 | /* content */ |
| 880 | LY_LIST_FOR(root, node) { |
| 881 | LY_CHECK_RET(json_print_node(&ctx, node)); |
| 882 | if (!(options & LYD_PRINT_WITHSIBLINGS)) { |
| 883 | break; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /* end */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 888 | ly_print_(out, "%s}%s", delimiter, delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 889 | |
| 890 | assert(!ctx.open.count); |
| 891 | ly_set_erase(&ctx.open, NULL); |
| 892 | |
| 893 | ly_print_flush(out); |
| 894 | return LY_SUCCESS; |
| 895 | } |