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> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 19 | #include "common.h" |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 20 | #include "log.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 21 | #include "out_internal.h" |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 22 | #include "parser_data.h" |
| 23 | #include "plugins_types.h" |
| 24 | #include "printer_data.h" |
| 25 | #include "printer_internal.h" |
| 26 | #include "set.h" |
| 27 | #include "tree.h" |
| 28 | #include "tree_data.h" |
| 29 | #include "tree_data_internal.h" |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 30 | #include "tree_schema.h" |
| 31 | |
| 32 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 33 | * @brief JSON printer context. |
| 34 | */ |
| 35 | struct jsonpr_ctx { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 36 | struct ly_out *out; /**< output specification */ |
| 37 | uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
| 38 | uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */ |
| 39 | const struct ly_ctx *ctx; /**< libyang context */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 40 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 41 | uint16_t level_printed; /* level where some data were already printed */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 42 | struct ly_set open; /* currently open array(s) */ |
| 43 | const struct lyd_node *print_sibling_metadata; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * @brief Mark that something was already written in the current level, |
| 48 | * used to decide if a comma is expected between the items |
| 49 | */ |
| 50 | #define LEVEL_PRINTED ctx->level_printed = ctx->level |
| 51 | |
| 52 | #define PRINT_COMMA \ |
| 53 | if (ctx->level_printed >= ctx->level) {\ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 54 | ly_print_(ctx->out, ",%s", (DO_FORMAT ? "\n" : ""));\ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static LY_ERR json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node); |
| 58 | |
| 59 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 60 | * Compare 2 nodes, despite it is regular data node or an opaq node, and |
| 61 | * decide if they corresponds to the same schema node. |
| 62 | * |
| 63 | * TODO: rewrite lyd_compare_single and use it instead of this |
| 64 | * |
| 65 | * @return 1 - matching nodes, 0 - non-matching nodes |
| 66 | */ |
| 67 | static int |
| 68 | matching_node(const struct lyd_node *node1, const struct lyd_node *node2) |
| 69 | { |
| 70 | assert(node1 || node2); |
| 71 | |
| 72 | if (!node1 || !node2) { |
| 73 | return 0; |
| 74 | } else if (node1->schema != node2->schema) { |
| 75 | return 0; |
| 76 | } |
| 77 | if (!node1->schema) { |
| 78 | /* compare node names */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 79 | struct lyd_node_opaq *onode1 = (struct lyd_node_opaq *)node1; |
| 80 | struct lyd_node_opaq *onode2 = (struct lyd_node_opaq *)node2; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 81 | if ((onode1->name != onode2->name) || (onode1->prefix.id != onode2->prefix.id)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @brief Open the JSON array ('[') for the specified @p node |
| 91 | * |
| 92 | * @param[in] ctx JSON printer context. |
| 93 | * @param[in] node First node of the array. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 94 | * @return LY_ERR value. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 95 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 96 | static LY_ERR |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 97 | json_print_array_open(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 98 | { |
| 99 | /* leaf-list's content is always printed on a single line */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 100 | 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] | 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 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 133 | 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] | 134 | |
| 135 | LEVEL_DEC; |
| 136 | ly_set_rm_index(&ctx->open, ctx->open.count - 1, NULL); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 137 | if (schema && (schema->nodetype == LYS_LEAFLIST)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 138 | /* leaf-list's content is always printed on a single line */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 139 | ly_print_(ctx->out, "]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 140 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 141 | ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @brief Get the node's module name to use as the @p node prefix in JSON. |
| 147 | * @param[in] node Node to process. |
| 148 | * @return The name of the module where the @p node belongs, it can be NULL in case the module name |
| 149 | * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context). |
| 150 | */ |
| 151 | static const char * |
| 152 | node_prefix(const struct lyd_node *node) |
| 153 | { |
| 154 | if (node->schema) { |
| 155 | return node->schema->module->name; |
| 156 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 157 | struct lyd_node_opaq *onode = (struct lyd_node_opaq *)node; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 158 | const struct lys_module *mod; |
| 159 | |
| 160 | switch (onode->format) { |
| 161 | case LYD_JSON: |
| 162 | return onode->prefix.module_name; |
| 163 | case LYD_XML: |
| 164 | mod = ly_ctx_get_module_implemented_ns(onode->ctx, onode->prefix.module_ns); |
| 165 | if (!mod) { |
| 166 | return NULL; |
| 167 | } |
| 168 | return mod->name; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 169 | case LYD_LYB: |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 170 | case LYD_UNKNOWN: |
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. |
| 286 | * @param[in] prefix Prefix structure to provide prefix string if prefix to print. |
| 287 | * @param[in] name Name of the memeber to print. |
| 288 | * @param[in] is_attr Flag if the metadata sign (@) is supposed to be added before the identifier. |
| 289 | * @return LY_ERR value. |
| 290 | */ |
| 291 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 292 | json_print_member2(struct jsonpr_ctx *ctx, const struct lyd_node *parent, LYD_FORMAT format, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 293 | const struct ly_prefix *prefix, const char *name, ly_bool is_attr) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 294 | { |
| 295 | const char *module_name = NULL; |
| 296 | |
| 297 | PRINT_COMMA; |
| 298 | |
| 299 | /* determine prefix string */ |
| 300 | if (prefix) { |
| 301 | const struct lys_module *mod; |
| 302 | |
| 303 | switch (format) { |
| 304 | case LYD_JSON: |
| 305 | module_name = prefix->module_name; |
| 306 | break; |
| 307 | case LYD_XML: |
| 308 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, prefix->module_ns); |
| 309 | if (mod) { |
| 310 | module_name = mod->name; |
| 311 | } |
| 312 | break; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 313 | case LYD_LYB: |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 314 | case LYD_UNKNOWN: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 315 | /* cannot be created */ |
| 316 | LOGINT_RET(ctx->ctx); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* print the member */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 321 | if (module_name && (!parent || (node_prefix(parent) != module_name))) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 322 | ly_print_(ctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 323 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 324 | ly_print_(ctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name, DO_FORMAT ? " " : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | return LY_SUCCESS; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * @brief Print data value. |
| 332 | * |
| 333 | * @param[in] ctx JSON printer context. |
| 334 | * @param[in] val Data value to be printed. |
| 335 | * @return LY_ERR value. |
| 336 | */ |
| 337 | static LY_ERR |
| 338 | json_print_value(struct jsonpr_ctx *ctx, const struct lyd_value *val) |
| 339 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 340 | ly_bool dynamic = 0; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 341 | 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] | 342 | |
| 343 | /* leafref is not supported */ |
| 344 | switch (val->realtype->basetype) { |
| 345 | case LY_TYPE_BINARY: |
| 346 | case LY_TYPE_STRING: |
| 347 | case LY_TYPE_BITS: |
| 348 | case LY_TYPE_ENUM: |
| 349 | case LY_TYPE_INST: |
| 350 | case LY_TYPE_INT64: |
| 351 | case LY_TYPE_UINT64: |
| 352 | case LY_TYPE_DEC64: |
| 353 | case LY_TYPE_IDENT: |
Michal Vasko | c5b59af | 2020-11-03 17:13:41 +0100 | [diff] [blame^] | 354 | case LY_TYPE_UNION: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 355 | json_print_string(ctx->out, value); |
| 356 | break; |
| 357 | |
| 358 | case LY_TYPE_INT8: |
| 359 | case LY_TYPE_INT16: |
| 360 | case LY_TYPE_INT32: |
| 361 | case LY_TYPE_UINT8: |
| 362 | case LY_TYPE_UINT16: |
| 363 | case LY_TYPE_UINT32: |
| 364 | case LY_TYPE_BOOL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 365 | ly_print_(ctx->out, "%s", value[0] ? value : "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 366 | break; |
| 367 | |
| 368 | case LY_TYPE_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 369 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 370 | break; |
| 371 | |
| 372 | default: |
| 373 | /* error */ |
| 374 | LOGINT_RET(ctx->ctx); |
| 375 | } |
| 376 | |
| 377 | if (dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 378 | free((char *)value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | return LY_SUCCESS; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * @brief Print all the attributes of the opaq node. |
| 386 | * |
| 387 | * @param[in] ctx JSON printer context. |
| 388 | * @param[in] node Opaq node where the attributes are placed. |
| 389 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 390 | * @return LY_ERR value. |
| 391 | */ |
| 392 | static LY_ERR |
| 393 | json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod) |
| 394 | { |
| 395 | struct lyd_attr *attr; |
| 396 | |
| 397 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 398 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 399 | LEVEL_PRINTED; |
| 400 | } |
| 401 | |
| 402 | for (attr = node->attr; attr; attr = attr->next) { |
| 403 | PRINT_COMMA; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 404 | json_print_member2(ctx, (struct lyd_node *)node, attr->format, &attr->prefix, attr->name, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 405 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 406 | if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 407 | ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 408 | } else if (attr->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 409 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 410 | } else { |
| 411 | json_print_string(ctx->out, attr->value); |
| 412 | } |
| 413 | LEVEL_PRINTED; |
| 414 | } |
| 415 | |
| 416 | return LY_SUCCESS; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * @brief Print all the metadata of the node. |
| 421 | * |
| 422 | * @param[in] ctx JSON printer context. |
| 423 | * @param[in] node Node where the metadata are placed. |
| 424 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 425 | * @return LY_ERR value. |
| 426 | */ |
| 427 | static LY_ERR |
| 428 | json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod) |
| 429 | { |
| 430 | struct lyd_meta *meta; |
| 431 | |
| 432 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 433 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 434 | LEVEL_PRINTED; |
| 435 | } |
| 436 | |
| 437 | for (meta = node->meta; meta; meta = meta->next) { |
| 438 | PRINT_COMMA; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 439 | 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] | 440 | LY_CHECK_RET(json_print_value(ctx, &meta->value)); |
| 441 | LEVEL_PRINTED; |
| 442 | } |
| 443 | |
| 444 | return LY_SUCCESS; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes. |
| 449 | * |
| 450 | * @param[in] ctx JSON printer context. |
| 451 | * @param[in] node Data node where the attributes/metadata are placed. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 452 | * @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] | 453 | * @return LY_ERR value. |
| 454 | */ |
| 455 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 456 | 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] | 457 | { |
| 458 | const struct lys_module *wdmod = NULL; |
| 459 | |
| 460 | if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) { |
| 461 | /* we have implicit OR explicit default node */ |
| 462 | /* get with-defaults module */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 463 | 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] | 464 | } |
| 465 | |
| 466 | if (node->schema && node->meta) { |
| 467 | if (inner) { |
| 468 | LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1)); |
| 469 | } else { |
| 470 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
| 471 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 472 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 473 | LEVEL_INC; |
| 474 | LY_CHECK_RET(json_print_metadata(ctx, node, wdmod)); |
| 475 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 476 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 477 | LEVEL_PRINTED; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 478 | } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 479 | if (inner) { |
| 480 | LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1)); |
| 481 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 482 | LY_CHECK_RET(json_print_member2(ctx, node, ((struct lyd_node_opaq *)node)->format, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 483 | &((struct lyd_node_opaq *)node)->prefix, ((struct lyd_node_opaq *)node)->name, 1)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 484 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 485 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 486 | LEVEL_INC; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 487 | 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] | 488 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 489 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 490 | LEVEL_PRINTED; |
| 491 | } |
| 492 | |
| 493 | return LY_SUCCESS; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * @brief Print leaf data node including its metadata. |
| 498 | * |
| 499 | * @param[in] ctx JSON printer context. |
| 500 | * @param[in] node Data node to print. |
| 501 | * @return LY_ERR value. |
| 502 | */ |
| 503 | static LY_ERR |
| 504 | json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 505 | { |
| 506 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 507 | 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] | 508 | LEVEL_PRINTED; |
| 509 | |
| 510 | /* print attributes as sibling */ |
| 511 | json_print_attributes(ctx, node, 0); |
| 512 | |
| 513 | return LY_SUCCESS; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * @brief Print anydata data node including its metadata. |
| 518 | * |
| 519 | * @param[in] ctx JSON printer context. |
| 520 | * @param[in] any Anydata node to print. |
| 521 | * @return LY_ERR value. |
| 522 | */ |
| 523 | static LY_ERR |
| 524 | json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any) |
| 525 | { |
| 526 | LY_ERR ret = LY_SUCCESS; |
| 527 | struct lyd_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 528 | uint32_t prev_opts, prev_lo; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 529 | |
| 530 | if (!any->value.tree) { |
| 531 | /* no content */ |
| 532 | return LY_SUCCESS; |
| 533 | } |
| 534 | |
| 535 | if (any->value_type == LYD_ANYDATA_LYB) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 536 | 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] | 537 | |
| 538 | /* turn logging off */ |
| 539 | prev_lo = ly_log_options(0); |
| 540 | |
| 541 | /* try to parse it into a data tree */ |
| 542 | if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) { |
| 543 | /* successfully parsed */ |
| 544 | free(any->value.mem); |
| 545 | any->value.tree = iter; |
| 546 | any->value_type = LYD_ANYDATA_DATATREE; |
| 547 | } |
| 548 | |
| 549 | /* turn loggin on again */ |
| 550 | ly_log_options(prev_lo); |
| 551 | } |
| 552 | |
| 553 | switch (any->value_type) { |
| 554 | case LYD_ANYDATA_DATATREE: |
| 555 | /* close opening tag and print data */ |
| 556 | prev_opts = ctx->options; |
| 557 | ctx->options &= ~LYD_PRINT_WITHSIBLINGS; |
| 558 | |
| 559 | LY_LIST_FOR(any->value.tree, iter) { |
| 560 | ret = json_print_node(ctx, iter); |
| 561 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 562 | } |
| 563 | |
| 564 | ctx->options = prev_opts; |
| 565 | break; |
| 566 | case LYD_ANYDATA_JSON: |
| 567 | /* print without escaping special characters */ |
| 568 | if (!any->value.str[0]) { |
| 569 | return LY_SUCCESS; |
| 570 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 571 | ly_print_(ctx->out, "%*s%s", INDENT, any->value.str); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 572 | break; |
| 573 | case LYD_ANYDATA_STRING: |
| 574 | case LYD_ANYDATA_XML: |
| 575 | case LYD_ANYDATA_LYB: |
| 576 | /* JSON and LYB format is not supported */ |
| 577 | LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type); |
| 578 | return LY_SUCCESS; |
| 579 | } |
| 580 | |
| 581 | return LY_SUCCESS; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * @brief Print content of a single container/list data node including its metadata. |
| 586 | * The envelope specific to container and list are expected to be printed by the caller. |
| 587 | * |
| 588 | * @param[in] ctx JSON printer context. |
| 589 | * @param[in] node Data node to print. |
| 590 | * @return LY_ERR value. |
| 591 | */ |
| 592 | static LY_ERR |
| 593 | json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 594 | { |
| 595 | struct lyd_node *child; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 596 | struct lyd_node *children = lyd_child(node); |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 597 | ly_bool has_content = 0; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 598 | |
| 599 | if (node->meta || children) { |
| 600 | has_content = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 601 | } 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] | 602 | has_content = 1; |
| 603 | } |
| 604 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 605 | if (!node->schema || (node->schema->nodetype != LYS_LIST)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 606 | 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] | 607 | (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 608 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 609 | 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] | 610 | INDENT, (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 611 | } |
| 612 | LEVEL_INC; |
| 613 | |
| 614 | json_print_attributes(ctx, node, 1); |
| 615 | |
| 616 | if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) { |
| 617 | /* print children */ |
| 618 | LY_LIST_FOR(children, child) { |
| 619 | LY_CHECK_RET(json_print_node(ctx, child)); |
| 620 | } |
| 621 | } else { |
| 622 | /* anydata */ |
| 623 | json_print_anydata(ctx, (struct lyd_node_any *)node); |
| 624 | } |
| 625 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 626 | LEVEL_DEC; |
| 627 | if (DO_FORMAT && has_content) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 628 | ly_print_(ctx->out, "\n%*s}", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 629 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 630 | ly_print_(ctx->out, "}"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 631 | } |
| 632 | LEVEL_PRINTED; |
| 633 | |
| 634 | return LY_SUCCESS; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * @brief Print container data node including its metadata. |
| 639 | * |
| 640 | * @param[in] ctx JSON printer context. |
| 641 | * @param[in] node Data node to print. |
| 642 | * @return LY_ERR value. |
| 643 | */ |
| 644 | static int |
| 645 | json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 646 | { |
| 647 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
| 648 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 649 | |
| 650 | return LY_SUCCESS; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * @brief Print single leaf-list or list instance. |
| 655 | * |
| 656 | * In case of list, metadata are printed inside the list object. For the leaf-list, |
| 657 | * metadata are marked in the context for later printing after closing the array next to it using |
| 658 | * json_print_metadata_leaflist(). |
| 659 | * |
| 660 | * @param[in] ctx JSON printer context. |
| 661 | * @param[in] node Data node to print. |
| 662 | * @return LY_ERR value. |
| 663 | */ |
| 664 | static LY_ERR |
| 665 | json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 666 | { |
| 667 | if (!is_open_array(ctx, node)) { |
| 668 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 669 | LY_CHECK_RET(json_print_array_open(ctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 670 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 671 | ly_print_(ctx->out, ","); |
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 | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 760 | const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev; |
| 761 | const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next; |
| 762 | if (prev->next && matching_node((const struct lyd_node *)prev, (const struct lyd_node *)node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 763 | first = 0; |
| 764 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 765 | if (next && matching_node((const struct lyd_node *)node, (const struct lyd_node *)next)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 766 | last = 0; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | if (first) { |
| 771 | LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0)); |
| 772 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 773 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 774 | LY_CHECK_RET(json_print_array_open(ctx, (struct lyd_node *)node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 775 | LEVEL_INC; |
| 776 | } |
| 777 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 778 | if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 779 | LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 780 | LEVEL_PRINTED; |
| 781 | } else { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 782 | if (node->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 783 | ly_print_(ctx->out, "[null]"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 784 | } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 785 | ly_print_(ctx->out, "%s", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 786 | } else { |
| 787 | /* string */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 788 | ly_print_(ctx->out, "\"%s\"", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 789 | } |
| 790 | LEVEL_PRINTED; |
| 791 | |
| 792 | /* attributes */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 793 | json_print_attributes(ctx, (const struct lyd_node *)node, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 794 | |
| 795 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 796 | if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 797 | json_print_array_close(ctx); |
| 798 | LEVEL_DEC; |
| 799 | LEVEL_PRINTED; |
| 800 | } |
| 801 | |
| 802 | return LY_SUCCESS; |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * @brief Print all the types of data node including its metadata. |
| 807 | * |
| 808 | * @param[in] ctx JSON printer context. |
| 809 | * @param[in] node Data node to print. |
| 810 | * @return LY_ERR value. |
| 811 | */ |
| 812 | static LY_ERR |
| 813 | json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 814 | { |
| 815 | if (!ly_should_print(node, ctx->options)) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 816 | 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] | 817 | json_print_array_close(ctx); |
| 818 | } |
| 819 | return LY_SUCCESS; |
| 820 | } |
| 821 | |
| 822 | if (!node->schema) { |
| 823 | LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node)); |
| 824 | } else { |
| 825 | switch (node->schema->nodetype) { |
| 826 | case LYS_RPC: |
| 827 | case LYS_ACTION: |
| 828 | case LYS_NOTIF: |
| 829 | case LYS_CONTAINER: |
| 830 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 831 | break; |
| 832 | case LYS_LEAF: |
| 833 | LY_CHECK_RET(json_print_leaf(ctx, node)); |
| 834 | break; |
| 835 | case LYS_LEAFLIST: |
| 836 | case LYS_LIST: |
| 837 | LY_CHECK_RET(json_print_leaf_list(ctx, node)); |
| 838 | break; |
| 839 | case LYS_ANYXML: |
| 840 | case LYS_ANYDATA: |
| 841 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 842 | break; |
| 843 | default: |
| 844 | LOGINT(node->schema->module->ctx); |
| 845 | return EXIT_FAILURE; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | ctx->level_printed = ctx->level; |
| 850 | |
| 851 | if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) { |
| 852 | json_print_metadata_leaflist(ctx); |
| 853 | ctx->print_sibling_metadata = NULL; |
| 854 | } |
| 855 | |
| 856 | return LY_SUCCESS; |
| 857 | } |
| 858 | |
| 859 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 860 | 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] | 861 | { |
| 862 | const struct lyd_node *node; |
| 863 | struct jsonpr_ctx ctx = {0}; |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 864 | const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n"; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 865 | |
Radek Krejci | 2e87477 | 2020-08-28 16:36:33 +0200 | [diff] [blame] | 866 | if (!root) { |
| 867 | ly_print_(out, "{}%s", delimiter); |
| 868 | ly_print_flush(out); |
| 869 | return LY_SUCCESS; |
| 870 | } |
| 871 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 872 | ctx.out = out; |
| 873 | ctx.level = 1; |
| 874 | ctx.level_printed = 0; |
| 875 | ctx.options = options; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 876 | ctx.ctx = LYD_CTX(root); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 877 | |
| 878 | /* start */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 879 | ly_print_(ctx.out, "{%s", delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 880 | |
| 881 | /* content */ |
| 882 | LY_LIST_FOR(root, node) { |
| 883 | LY_CHECK_RET(json_print_node(&ctx, node)); |
| 884 | if (!(options & LYD_PRINT_WITHSIBLINGS)) { |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /* end */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 890 | ly_print_(out, "%s}%s", delimiter, delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 891 | |
| 892 | assert(!ctx.open.count); |
| 893 | ly_set_erase(&ctx.open, NULL); |
| 894 | |
| 895 | ly_print_flush(out); |
| 896 | return LY_SUCCESS; |
| 897 | } |