Nyx Node
Loading...
Searching...
No Matches
transform_xml_to_json.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 <ctype.h>
11#include <string.h>
12
13#include "nyx_node_internal.h"
14
15/*--------------------------------------------------------------------------------------------------------------------*/
16
17static nyx_object_t *transform(const nyx_xmldoc_t *curr_node) // NOLINT(misc-no-recursion)
18{
19 /*----------------------------------------------------------------------------------------------------------------*/
20
21 nyx_dict_t *result = nyx_dict_new();
22
23 /*----------------------------------------------------------------------------------------------------------------*/
24
25 nyx_dict_set(result, "<>", nyx_string_from(nyx_string_dup(curr_node->name), true));
26
27 /*----------------------------------------------------------------------------------------------------------------*/
28
29 for(nyx_xmldoc_t *new_node = curr_node->children; new_node != NULL; new_node = new_node->next)
30 {
31 if(new_node->type == NYX_XML_TEXT)
32 {
33 STR_t content_s = (STR_t) /* NOSONAR */ new_node->data - 0x0000;
34 size_t length = strlen(content_s);
35 str_t content_e = (str_t) /* NOSONAR */ new_node->data + length;
36
37 while(length > 0 && (isspace((unsigned char) *(content_s + 0)) || *(content_s + 0) == '"')) {
38 content_s++;
39 length--;
40 }
41
42 while(length > 0 && (isspace((unsigned char) *(content_e - 1)) || *(content_e - 1) == '"')) {
43 content_e--;
44 length--;
45 }
46
47 if(length > 0)
48 {
49 *content_e = '\0';
50
51 nyx_dict_set(result, "$", nyx_string_from(nyx_string_dup(content_s), true));
52 }
53
54 break;
55 }
56 }
57
58 /*----------------------------------------------------------------------------------------------------------------*/
59
60 for(nyx_xmldoc_t *attribute = curr_node->attributes; attribute != NULL; attribute = attribute->next)
61 {
62 nyx_string_builder_t *sb = nyx_string_builder_from(NYX_SB_ESCAPE_JSON, "@", attribute->name);
63
64 /**/ str_t attribute_name = nyx_string_builder_to_string(sb);
65 /**/
66 /**/ /**/
67 /**/ /**/ nyx_dict_set(result, attribute_name, nyx_string_from(nyx_string_dup(attribute->data), true));
68 /**/ /**/
69 /**/
70 /**/ nyx_memory_free(attribute_name);
71
72 nyx_string_builder_free(sb);
73 }
74
75 /*----------------------------------------------------------------------------------------------------------------*/
76
77 if(curr_node->children)
78 {
79 nyx_list_t *list = NULL;
80
81 for(nyx_xmldoc_t *new_node = curr_node->children; new_node != NULL; new_node = new_node->next)
82 {
83 if(new_node->type == NYX_XML_ELEM)
84 {
85 if(list == NULL)
86 {
87 nyx_dict_set(result, "children", list = nyx_list_new());
88 }
89
90 nyx_list_push(list, transform(new_node));
91 }
92 }
93 }
94
95 /*----------------------------------------------------------------------------------------------------------------*/
96
97 return (nyx_object_t *) result;
98}
99
100/*--------------------------------------------------------------------------------------------------------------------*/
101
103{
104 return xmldoc != NULL ? transform(xmldoc) : NULL;
105}
106
107/*--------------------------------------------------------------------------------------------------------------------*/
108#endif
109/*--------------------------------------------------------------------------------------------------------------------*/
Struct describing a JSON dict object.
Struct describing a JSON list object.
#define STR_t
Alias for const char *.
Definition nyx_node.h:71
__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.
__NYX_NULLABLE__ str_t nyx_string_dup(__NYX_NULLABLE__ STR_t s)
Similar to libc strdup.
#define str_t
Alias for char *.
Definition nyx_node.h:70
Struct describing a JSON object.
__NYX_NULLABLE__ nyx_object_t * nyx_xmldoc_to_object(__NYX_NULLABLE__ const nyx_xmldoc_t *xmldoc)
Converts an XML Nyx / INDI command to the JSON one.
Struct describing an XML document.