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