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