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