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: |
| 354 | json_print_string(ctx->out, value); |
| 355 | break; |
| 356 | |
| 357 | case LY_TYPE_INT8: |
| 358 | case LY_TYPE_INT16: |
| 359 | case LY_TYPE_INT32: |
| 360 | case LY_TYPE_UINT8: |
| 361 | case LY_TYPE_UINT16: |
| 362 | case LY_TYPE_UINT32: |
| 363 | case LY_TYPE_BOOL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 364 | ly_print_(ctx->out, "%s", value[0] ? value : "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 365 | break; |
| 366 | |
| 367 | case LY_TYPE_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 368 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 369 | break; |
| 370 | |
| 371 | default: |
| 372 | /* error */ |
| 373 | LOGINT_RET(ctx->ctx); |
| 374 | } |
| 375 | |
| 376 | if (dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 377 | free((char *)value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | return LY_SUCCESS; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @brief Print all the attributes of the opaq node. |
| 385 | * |
| 386 | * @param[in] ctx JSON printer context. |
| 387 | * @param[in] node Opaq node where the attributes are placed. |
| 388 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 389 | * @return LY_ERR value. |
| 390 | */ |
| 391 | static LY_ERR |
| 392 | json_print_attribute(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node, const struct lys_module *wdmod) |
| 393 | { |
| 394 | struct lyd_attr *attr; |
| 395 | |
| 396 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 397 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 398 | LEVEL_PRINTED; |
| 399 | } |
| 400 | |
| 401 | for (attr = node->attr; attr; attr = attr->next) { |
| 402 | PRINT_COMMA; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 403 | 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] | 404 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 405 | if (attr->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 406 | ly_print_(ctx->out, "%s", attr->value[0] ? attr->value : "null"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 407 | } else if (attr->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 408 | ly_print_(ctx->out, "[null]"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 409 | } else { |
| 410 | json_print_string(ctx->out, attr->value); |
| 411 | } |
| 412 | LEVEL_PRINTED; |
| 413 | } |
| 414 | |
| 415 | return LY_SUCCESS; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @brief Print all the metadata of the node. |
| 420 | * |
| 421 | * @param[in] ctx JSON printer context. |
| 422 | * @param[in] node Node where the metadata are placed. |
| 423 | * @param[in] wdmod With-defaults module to mark that default attribute is supposed to be printed. |
| 424 | * @return LY_ERR value. |
| 425 | */ |
| 426 | static LY_ERR |
| 427 | json_print_metadata(struct jsonpr_ctx *ctx, const struct lyd_node *node, const struct lys_module *wdmod) |
| 428 | { |
| 429 | struct lyd_meta *meta; |
| 430 | |
| 431 | if (wdmod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 432 | ly_print_(ctx->out, "%*s\"%s:default\":\"true\"", INDENT, wdmod->name); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 433 | LEVEL_PRINTED; |
| 434 | } |
| 435 | |
| 436 | for (meta = node->meta; meta; meta = meta->next) { |
| 437 | PRINT_COMMA; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 438 | 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] | 439 | LY_CHECK_RET(json_print_value(ctx, &meta->value)); |
| 440 | LEVEL_PRINTED; |
| 441 | } |
| 442 | |
| 443 | return LY_SUCCESS; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * @brief Print attributes/metadata of the given @p node. Accepts both regular as well as opaq nodes. |
| 448 | * |
| 449 | * @param[in] ctx JSON printer context. |
| 450 | * @param[in] node Data node where the attributes/metadata are placed. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 451 | * @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] | 452 | * @return LY_ERR value. |
| 453 | */ |
| 454 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 455 | 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] | 456 | { |
| 457 | const struct lys_module *wdmod = NULL; |
| 458 | |
| 459 | if ((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) { |
| 460 | /* we have implicit OR explicit default node */ |
| 461 | /* get with-defaults module */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 462 | 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] | 463 | } |
| 464 | |
| 465 | if (node->schema && node->meta) { |
| 466 | if (inner) { |
| 467 | LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1)); |
| 468 | } else { |
| 469 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
| 470 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 471 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 472 | LEVEL_INC; |
| 473 | LY_CHECK_RET(json_print_metadata(ctx, node, wdmod)); |
| 474 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 475 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 476 | LEVEL_PRINTED; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 477 | } else if (!node->schema && ((struct lyd_node_opaq *)node)->attr) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 478 | if (inner) { |
| 479 | LY_CHECK_RET(json_print_member2(ctx, NULL, LYD_JSON, NULL, "", 1)); |
| 480 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 481 | 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] | 482 | &((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] | 483 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 484 | ly_print_(ctx->out, "{%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 485 | LEVEL_INC; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 486 | 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] | 487 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 488 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 489 | LEVEL_PRINTED; |
| 490 | } |
| 491 | |
| 492 | return LY_SUCCESS; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @brief Print leaf data node including its metadata. |
| 497 | * |
| 498 | * @param[in] ctx JSON printer context. |
| 499 | * @param[in] node Data node to print. |
| 500 | * @return LY_ERR value. |
| 501 | */ |
| 502 | static LY_ERR |
| 503 | json_print_leaf(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 504 | { |
| 505 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 506 | 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] | 507 | LEVEL_PRINTED; |
| 508 | |
| 509 | /* print attributes as sibling */ |
| 510 | json_print_attributes(ctx, node, 0); |
| 511 | |
| 512 | return LY_SUCCESS; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * @brief Print anydata data node including its metadata. |
| 517 | * |
| 518 | * @param[in] ctx JSON printer context. |
| 519 | * @param[in] any Anydata node to print. |
| 520 | * @return LY_ERR value. |
| 521 | */ |
| 522 | static LY_ERR |
| 523 | json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any) |
| 524 | { |
| 525 | LY_ERR ret = LY_SUCCESS; |
| 526 | struct lyd_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 527 | uint32_t prev_opts, prev_lo; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 528 | |
| 529 | if (!any->value.tree) { |
| 530 | /* no content */ |
| 531 | return LY_SUCCESS; |
| 532 | } |
| 533 | |
| 534 | if (any->value_type == LYD_ANYDATA_LYB) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 535 | 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] | 536 | |
| 537 | /* turn logging off */ |
| 538 | prev_lo = ly_log_options(0); |
| 539 | |
| 540 | /* try to parse it into a data tree */ |
| 541 | if (lyd_parse_data_mem(ctx->ctx, any->value.mem, LYD_LYB, parser_options, 0, &iter) == LY_SUCCESS) { |
| 542 | /* successfully parsed */ |
| 543 | free(any->value.mem); |
| 544 | any->value.tree = iter; |
| 545 | any->value_type = LYD_ANYDATA_DATATREE; |
| 546 | } |
| 547 | |
| 548 | /* turn loggin on again */ |
| 549 | ly_log_options(prev_lo); |
| 550 | } |
| 551 | |
| 552 | switch (any->value_type) { |
| 553 | case LYD_ANYDATA_DATATREE: |
| 554 | /* close opening tag and print data */ |
| 555 | prev_opts = ctx->options; |
| 556 | ctx->options &= ~LYD_PRINT_WITHSIBLINGS; |
| 557 | |
| 558 | LY_LIST_FOR(any->value.tree, iter) { |
| 559 | ret = json_print_node(ctx, iter); |
| 560 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 561 | } |
| 562 | |
| 563 | ctx->options = prev_opts; |
| 564 | break; |
| 565 | case LYD_ANYDATA_JSON: |
| 566 | /* print without escaping special characters */ |
| 567 | if (!any->value.str[0]) { |
| 568 | return LY_SUCCESS; |
| 569 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 570 | ly_print_(ctx->out, "%*s%s", INDENT, any->value.str); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 571 | break; |
| 572 | case LYD_ANYDATA_STRING: |
| 573 | case LYD_ANYDATA_XML: |
| 574 | case LYD_ANYDATA_LYB: |
| 575 | /* JSON and LYB format is not supported */ |
| 576 | LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type); |
| 577 | return LY_SUCCESS; |
| 578 | } |
| 579 | |
| 580 | return LY_SUCCESS; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * @brief Print content of a single container/list data node including its metadata. |
| 585 | * The envelope specific to container and list are expected to be printed by the caller. |
| 586 | * |
| 587 | * @param[in] ctx JSON printer context. |
| 588 | * @param[in] node Data node to print. |
| 589 | * @return LY_ERR value. |
| 590 | */ |
| 591 | static LY_ERR |
| 592 | json_print_inner(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 593 | { |
| 594 | struct lyd_node *child; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 595 | struct lyd_node *children = lyd_child(node); |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 596 | ly_bool has_content = 0; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 597 | |
| 598 | if (node->meta || children) { |
| 599 | has_content = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 600 | } 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] | 601 | has_content = 1; |
| 602 | } |
| 603 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 604 | if (!node->schema || (node->schema->nodetype != LYS_LIST)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 605 | 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] | 606 | (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 607 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 608 | 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] | 609 | INDENT, (DO_FORMAT && has_content) ? "\n" : ""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 610 | } |
| 611 | LEVEL_INC; |
| 612 | |
| 613 | json_print_attributes(ctx, node, 1); |
| 614 | |
| 615 | if (!node->schema || !(node->schema->nodetype & LYS_ANYDATA)) { |
| 616 | /* print children */ |
| 617 | LY_LIST_FOR(children, child) { |
| 618 | LY_CHECK_RET(json_print_node(ctx, child)); |
| 619 | } |
| 620 | } else { |
| 621 | /* anydata */ |
| 622 | json_print_anydata(ctx, (struct lyd_node_any *)node); |
| 623 | } |
| 624 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 625 | LEVEL_DEC; |
| 626 | if (DO_FORMAT && has_content) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 627 | ly_print_(ctx->out, "\n%*s}", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 628 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 629 | ly_print_(ctx->out, "}"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 630 | } |
| 631 | LEVEL_PRINTED; |
| 632 | |
| 633 | return LY_SUCCESS; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * @brief Print container data node including its metadata. |
| 638 | * |
| 639 | * @param[in] ctx JSON printer context. |
| 640 | * @param[in] node Data node to print. |
| 641 | * @return LY_ERR value. |
| 642 | */ |
| 643 | static int |
| 644 | json_print_container(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 645 | { |
| 646 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
| 647 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 648 | |
| 649 | return LY_SUCCESS; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * @brief Print single leaf-list or list instance. |
| 654 | * |
| 655 | * In case of list, metadata are printed inside the list object. For the leaf-list, |
| 656 | * metadata are marked in the context for later printing after closing the array next to it using |
| 657 | * json_print_metadata_leaflist(). |
| 658 | * |
| 659 | * @param[in] ctx JSON printer context. |
| 660 | * @param[in] node Data node to print. |
| 661 | * @return LY_ERR value. |
| 662 | */ |
| 663 | static LY_ERR |
| 664 | json_print_leaf_list(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 665 | { |
| 666 | if (!is_open_array(ctx, node)) { |
| 667 | LY_CHECK_RET(json_print_member(ctx, node, 0)); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 668 | LY_CHECK_RET(json_print_array_open(ctx, node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 669 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 670 | ly_print_(ctx->out, ","); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | if (node->schema->nodetype == LYS_LIST) { |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 674 | if (!lyd_child(node)) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 675 | /* empty, e.g. in case of filter */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 676 | 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] | 677 | LEVEL_PRINTED; |
| 678 | } else { |
| 679 | /* print list's content */ |
| 680 | LY_CHECK_RET(json_print_inner(ctx, node)); |
| 681 | } |
| 682 | } else { |
| 683 | assert(node->schema->nodetype == LYS_LEAFLIST); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 684 | 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] | 685 | |
| 686 | if (node->meta && !ctx->print_sibling_metadata) { |
| 687 | ctx->print_sibling_metadata = node; |
| 688 | } |
| 689 | } |
| 690 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 691 | 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] | 692 | json_print_array_close(ctx); |
| 693 | } |
| 694 | |
| 695 | return LY_SUCCESS; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * @brief Print leaf-list's metadata in case they were marked in the last call to json_print_leaf_list(). |
| 700 | * This function is supposed to be called when the leaf-list array is closed. |
| 701 | * |
| 702 | * @param[in] ctx JSON printer context. |
| 703 | * @return LY_ERR value. |
| 704 | */ |
| 705 | static LY_ERR |
| 706 | json_print_metadata_leaflist(struct jsonpr_ctx *ctx) |
| 707 | { |
| 708 | const struct lyd_node *prev, *node, *iter; |
| 709 | |
| 710 | if (!ctx->print_sibling_metadata) { |
| 711 | return LY_SUCCESS; |
| 712 | } |
| 713 | |
| 714 | for (node = ctx->print_sibling_metadata, prev = ctx->print_sibling_metadata->prev; |
| 715 | prev->next && matching_node(node, prev); |
| 716 | node = prev, prev = node->prev) {} |
| 717 | |
| 718 | /* node is the first instance of the leaf-list */ |
| 719 | |
| 720 | LY_CHECK_RET(json_print_member(ctx, node, 1)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 721 | ly_print_(ctx->out, "[%s", (DO_FORMAT ? "\n" : "")); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 722 | LEVEL_INC; |
| 723 | LY_LIST_FOR(node, iter) { |
| 724 | PRINT_COMMA; |
| 725 | if (iter->meta) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 726 | ly_print_(ctx->out, "%*s%s", INDENT, DO_FORMAT ? "{\n" : "{"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 727 | LEVEL_INC; |
| 728 | LY_CHECK_RET(json_print_metadata(ctx, iter, NULL)); |
| 729 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 730 | ly_print_(ctx->out, "%s%*s}", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 731 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 732 | ly_print_(ctx->out, "null"); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 733 | } |
| 734 | LEVEL_PRINTED; |
| 735 | if (!matching_node(iter, iter->next)) { |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | LEVEL_DEC; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 740 | ly_print_(ctx->out, "%s%*s]", DO_FORMAT ? "\n" : "", INDENT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 741 | LEVEL_PRINTED; |
| 742 | |
| 743 | return LY_SUCCESS; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * @brief Print opaq data node including its attributes. |
| 748 | * |
| 749 | * @param[in] ctx JSON printer context. |
| 750 | * @param[in] node Opaq node to print. |
| 751 | * @return LY_ERR value. |
| 752 | */ |
| 753 | static LY_ERR |
| 754 | json_print_opaq(struct jsonpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 755 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 756 | ly_bool first = 1, last = 1; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 757 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 758 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 759 | const struct lyd_node_opaq *prev = (const struct lyd_node_opaq *)node->prev; |
| 760 | const struct lyd_node_opaq *next = (const struct lyd_node_opaq *)node->next; |
| 761 | 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] | 762 | first = 0; |
| 763 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 764 | 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] | 765 | last = 0; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (first) { |
| 770 | LY_CHECK_RET(json_print_member2(ctx, node->parent, node->format, &node->prefix, node->name, 0)); |
| 771 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 772 | if (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 773 | LY_CHECK_RET(json_print_array_open(ctx, (struct lyd_node *)node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 774 | LEVEL_INC; |
| 775 | } |
| 776 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 777 | if (node->child || (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 778 | LY_CHECK_RET(json_print_inner(ctx, (struct lyd_node *)node)); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 779 | LEVEL_PRINTED; |
| 780 | } else { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 781 | if (node->hints & LYD_VALHINT_EMPTY) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 782 | ly_print_(ctx->out, "[null]"); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 783 | } else if (node->hints & (LYD_VALHINT_BOOLEAN | LYD_VALHINT_DECNUM)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 784 | ly_print_(ctx->out, "%s", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 785 | } else { |
| 786 | /* string */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 787 | ly_print_(ctx->out, "\"%s\"", node->value); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 788 | } |
| 789 | LEVEL_PRINTED; |
| 790 | |
| 791 | /* attributes */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 792 | json_print_attributes(ctx, (const struct lyd_node *)node, 0); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 793 | |
| 794 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 795 | if (last && (node->hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 796 | json_print_array_close(ctx); |
| 797 | LEVEL_DEC; |
| 798 | LEVEL_PRINTED; |
| 799 | } |
| 800 | |
| 801 | return LY_SUCCESS; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * @brief Print all the types of data node including its metadata. |
| 806 | * |
| 807 | * @param[in] ctx JSON printer context. |
| 808 | * @param[in] node Data node to print. |
| 809 | * @return LY_ERR value. |
| 810 | */ |
| 811 | static LY_ERR |
| 812 | json_print_node(struct jsonpr_ctx *ctx, const struct lyd_node *node) |
| 813 | { |
| 814 | if (!ly_should_print(node, ctx->options)) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 815 | 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] | 816 | json_print_array_close(ctx); |
| 817 | } |
| 818 | return LY_SUCCESS; |
| 819 | } |
| 820 | |
| 821 | if (!node->schema) { |
| 822 | LY_CHECK_RET(json_print_opaq(ctx, (const struct lyd_node_opaq *)node)); |
| 823 | } else { |
| 824 | switch (node->schema->nodetype) { |
| 825 | case LYS_RPC: |
| 826 | case LYS_ACTION: |
| 827 | case LYS_NOTIF: |
| 828 | case LYS_CONTAINER: |
| 829 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 830 | break; |
| 831 | case LYS_LEAF: |
| 832 | LY_CHECK_RET(json_print_leaf(ctx, node)); |
| 833 | break; |
| 834 | case LYS_LEAFLIST: |
| 835 | case LYS_LIST: |
| 836 | LY_CHECK_RET(json_print_leaf_list(ctx, node)); |
| 837 | break; |
| 838 | case LYS_ANYXML: |
| 839 | case LYS_ANYDATA: |
| 840 | LY_CHECK_RET(json_print_container(ctx, node)); |
| 841 | break; |
| 842 | default: |
| 843 | LOGINT(node->schema->module->ctx); |
| 844 | return EXIT_FAILURE; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | ctx->level_printed = ctx->level; |
| 849 | |
| 850 | if (ctx->print_sibling_metadata && !matching_node(node->next, ctx->print_sibling_metadata)) { |
| 851 | json_print_metadata_leaflist(ctx); |
| 852 | ctx->print_sibling_metadata = NULL; |
| 853 | } |
| 854 | |
| 855 | return LY_SUCCESS; |
| 856 | } |
| 857 | |
| 858 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 859 | 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] | 860 | { |
| 861 | const struct lyd_node *node; |
| 862 | struct jsonpr_ctx ctx = {0}; |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 863 | const char *delimiter = (options & LYD_PRINT_SHRINK) ? "" : "\n"; |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 864 | |
Radek Krejci | 2e87477 | 2020-08-28 16:36:33 +0200 | [diff] [blame] | 865 | if (!root) { |
| 866 | ly_print_(out, "{}%s", delimiter); |
| 867 | ly_print_flush(out); |
| 868 | return LY_SUCCESS; |
| 869 | } |
| 870 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 871 | ctx.out = out; |
| 872 | ctx.level = 1; |
| 873 | ctx.level_printed = 0; |
| 874 | ctx.options = options; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 875 | ctx.ctx = LYD_CTX(root); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 876 | |
| 877 | /* start */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 878 | ly_print_(ctx.out, "{%s", delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 879 | |
| 880 | /* content */ |
| 881 | LY_LIST_FOR(root, node) { |
| 882 | LY_CHECK_RET(json_print_node(&ctx, node)); |
| 883 | if (!(options & LYD_PRINT_WITHSIBLINGS)) { |
| 884 | break; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /* end */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 889 | ly_print_(out, "%s}%s", delimiter, delimiter); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 890 | |
| 891 | assert(!ctx.open.count); |
| 892 | ly_set_erase(&ctx.open, NULL); |
| 893 | |
| 894 | ly_print_flush(out); |
| 895 | return LY_SUCCESS; |
| 896 | } |