Nyx Node
Toggle main menu visibility
Loading...
Searching...
No Matches
transform_json_to_xml.c
1
/* NyxNode
2
* Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
3
* SPDX-License-Identifier: GPL-2.0-only (Mongoose backend) or GPL-3.0+
4
*/
5
6
/*--------------------------------------------------------------------------------------------------------------------*/
7
#if !defined(ARDUINO)
8
/*--------------------------------------------------------------------------------------------------------------------*/
9
10
#include <string.h>
11
12
#include "nyx_node_internal.h"
13
14
/*--------------------------------------------------------------------------------------------------------------------*/
15
16
static
nyx_xmldoc_t
*transform(
const
nyx_object_t
*dict)
// NOLINT(misc-no-recursion)
17
{
18
/*----------------------------------------------------------------------------------------------------------------*/
19
20
nyx_xmldoc_t
*node = nyx_xmldoc_new(NYX_XML_ELEM);
21
22
/*----------------------------------------------------------------------------------------------------------------*/
23
24
STR_t
key;
25
26
nyx_object_t
*obj1;
27
28
for
(
nyx_dict_iter_t
iter1 =
NYX_DICT_ITER
(dict); nyx_dict_iterate(&iter1, &key, &obj1);)
29
{
30
/*------------------------------------------------------------------------------------------------------------*/
31
32
/**/
if
(strcmp(key,
"<>"
) == 0)
33
{
34
str_t
value = nyx_object_to_cstring(obj1);
35
36
nyx_xmldoc_set_name(node, value);
37
38
nyx_memory_free
(value);
39
}
40
41
/*------------------------------------------------------------------------------------------------------------*/
42
43
else
if
(strcmp(key,
"$"
) == 0)
44
{
45
str_t
value = nyx_object_to_cstring(obj1);
46
47
nyx_xmldoc_set_content(node, value);
48
49
nyx_memory_free
(value);
50
}
51
52
/*------------------------------------------------------------------------------------------------------------*/
53
54
else
if
(key[0] ==
'@'
)
55
{
56
str_t
value = nyx_object_to_cstring(obj1);
57
58
nyx_xmldoc_add_attribute(node, key + 1, value);
59
60
nyx_memory_free
(value);
61
}
62
63
/*------------------------------------------------------------------------------------------------------------*/
64
65
else
if
(strcmp(key,
"children"
) == 0)
66
{
67
size_t
idx;
68
69
nyx_object_t
*obj2;
70
71
for
(
nyx_list_iter_t
iter2 =
NYX_LIST_ITER
(obj1); nyx_list_iterate(&iter2, &idx, &obj2);)
72
{
73
nyx_xmldoc_add_child(node, transform(obj2));
74
}
75
}
76
77
/*------------------------------------------------------------------------------------------------------------*/
78
}
79
80
/*----------------------------------------------------------------------------------------------------------------*/
81
82
return
node;
83
}
84
85
/*--------------------------------------------------------------------------------------------------------------------*/
86
87
nyx_xmldoc_t
*
nyx_object_to_xmldoc
(
const
nyx_object_t
*
object
)
88
{
89
return
object
!= NULL ? transform(
object
) : NULL;
90
}
91
92
/*--------------------------------------------------------------------------------------------------------------------*/
93
#endif
94
/*--------------------------------------------------------------------------------------------------------------------*/
NYX_DICT_ITER
#define NYX_DICT_ITER(dict)
Initializes a JSON dict iterator.
Definition
nyx_node.h:1126
nyx_dict_iter_t
Struct describing a JSON dict iterator.
Definition
nyx_node.h:1112
NYX_LIST_ITER
#define NYX_LIST_ITER(list)
Initializes a JSON list iterator.
Definition
nyx_node.h:1457
nyx_list_iter_t
Struct describing a JSON list iterator.
Definition
nyx_node.h:1443
STR_t
#define STR_t
Alias for const char *.
Definition
nyx_node.h:71
nyx_memory_free
__NYX_ZEROABLE__ size_t nyx_memory_free(__NYX_NULLABLE__ buff_t buff)
Similar to libc free except that it returns the amount of memory freed.
str_t
#define str_t
Alias for char *.
Definition
nyx_node.h:70
nyx_object_t
Struct describing a JSON object.
nyx_object_to_xmldoc
__NYX_NULLABLE__ nyx_xmldoc_t * nyx_object_to_xmldoc(__NYX_NULLABLE__ const nyx_object_t *object)
Converts a JSON Nyx / INDI command to the XML one.
nyx_xmldoc_t
Struct describing an XML document.
Author:
Jérôme ODIER
License:
LGPL-3.0-or-later
Copyright:
LPSC / CNRS