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