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