aboutsummaryrefslogtreecommitdiffstats
path: root/src/gnmi/xml2json.cpp
blob: 15dac41b0bc153575eea7fb783f81fd23cc2548a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (c) 2019 PANTHEON.tech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "xml2json.h"
#include "log.h"

#include <exception>
#include <sstream>
#include <iostream>

XML2JSON::XML2JSON(const XML2JSON& other)
{

}

XML2JSON::~XML2JSON()
{

}

void XML2JSON::setData(const std::string& data, XML2JSON::string_type_t type)
{
    switch (type) {
        case string_type_t::JSONSTRING:
            {
                Json::Reader read;
                DEBUG("JSONSTRING: %s", data.c_str());

                read.parse(data, jsonRoot);
                DEBUG("Root size: %d", jsonRoot.size());
            }
            break;

        case string_type_t::XMLSTRING:
            {
                int rc = 0;
                rc = xmlDoc.load_string(data.c_str(),
                                   pugi::parse_default | pugi::parse_comments);
                if (0 != rc) {
                    ERROR("Error parse xml string.");
                    throw std::logic_error("Error parse xml string.");
                }
            }
            break;

        default:
            throw std::logic_error("Unknown type.");
            break;
    }
}

void XML2JSON::setPrefix(const std::string& prefix)
{
    this->prefix = prefix;
}

std::string XML2JSON::getJson()
{
    switch (type) {
        case string_type_t::JSONSTRING:
            //TODO:
            return std::string("");

        case string_type_t::XMLSTRING:
            //TODO:
            return std::string("");

        default:
            break;
    }

    throw std::logic_error("Unknown type.");
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
std::string XML2JSON::getXML()
{
    std::ostringstream stream;

    switch (type) {
        case string_type_t::JSONSTRING:
            createXML();

        case string_type_t::XMLSTRING:
            xmlDoc.print(stream);
            DEBUG("XML doc: %s", xmlDoc.path().c_str());
            return stream.str();

        default:
            break;
    }

    throw std::logic_error("Unknown type.");
}
#pragma GCC diagnostic pop

const std::list<gNMIData> & XML2JSON::getgNMIData()
{
    createGNMIData();

    return gnmiData;
}

void XML2JSON::parseJSON()
{
//     parseJSONValueType(jsonRoot);
}

void XML2JSON::parseJSONValueType(const Json::Value& value,
                                  pugi::xml_node &node)
{
    switch (value.type()) {
        case Json::nullValue:
            break;

        case Json::intValue:
            DEBUG("Value: %d", value.asInt());
            node.append_child(pugi::node_pcdata).set_value(
                                        std::to_string(value.asInt()).c_str());
            break;

        case Json::uintValue:
            DEBUG("Value: %d", value.asUInt());
            node.append_child(pugi::node_pcdata).set_value(
                                        std::to_string(value.asUInt()).c_str());
            break;

        case Json::realValue:
            DEBUG("Value: %f", value.asFloat());
            node.append_child(pugi::node_pcdata).set_value(
                                    std::to_string(value.asFloat()).c_str());
            break;

        case Json::stringValue:
            DEBUG("Value: %s", value.asString().c_str());
            node.append_child(pugi::node_pcdata).set_value(
                                                    value.asString().c_str());
            break;

        case Json::booleanValue:
            DEBUG("Value: %d", value.asBool());
            node.append_child(pugi::node_pcdata).set_value(
                                        std::to_string(value.asBool()).c_str());
            break;

        case Json::arrayValue:
            parseJSONArray(value, node);
            break;

        case Json::objectValue:
            parseJSONObject(value, node);
            break;

        default:
            DEBUG("Unknown value type.");
            break;
    }
}

void XML2JSON::parseJSONValueType(const Json::Value& value,
                                  const std::string& prefix)
{
    switch (value.type()) {
        case Json::nullValue:
            {
                gNMIData data;
                std::string tmp(prefix);
                tmp.pop_back();
                data.setXPath(tmp);
                gnmiData.push_back(data);
            }
            break;

        case Json::intValue:
            //             DEBUG("Value: %d", value.asInt());
            {
                gNMIData data;
                std::string tmp(prefix);
                tmp.pop_back();
                data.setXPath(tmp);
                data.setValue(value.asInt());
                gnmiData.push_back(data);
            }
            break;

        case Json::uintValue:
            //             DEBUG("Value: %d", value.asUInt());
            {
                gNMIData data;
                std::string tmp(prefix);
                tmp.pop_back();
                data.setXPath(tmp);
                data.setValue(value.asUInt());
                gnmiData.push_back(data);
            }
            break;

        case Json::realValue:
            //             DEBUG("Value: %f", value.asFloat());
            {
                gNMIData data;
                std::string tmp(prefix);
                tmp.pop_back();
                data.setXPath(tmp);
                data.setValue(value.asFloat());
                gnmiData.push_back(data);
            }
            break;

        case Json::stringValue:
            //             DEBUG("Value: %s", value.asString().c_str());
            {
                gNMIData data;
                std::string tmp(prefix);
                tmp.pop_back();
                data.setXPath(tmp);
                data.setValue(value.asString());
                gnmiData.push_back(data);
            }
            break;

        case Json::booleanValue:
            //             DEBUG("Value: %d", value.asBool());
        {
                gNMIData data;
                std::string tmp(prefix);
                tmp.pop_back();
                data.setXPath(tmp);
                data.setValue(value.asBool() ? "true" : "false");
                gnmiData.push_back(data);
            }
            break;

        case Json::arrayValue:
            parseJSONArray(value, prefix);
            break;

        case Json::objectValue:
            parseJSONObject(value, prefix);
            break;

        default:
            DEBUG("Unknown value type.");
            break;
    }
}

void XML2JSON::parseJSONArray(const Json::Value& value, pugi::xml_node &node)
{
    if (Json::arrayValue != value.type()) {
        ERROR("Wrong JSON Value type");
        throw std::logic_error("Wrong JSON Value type");
    }

    for (const auto &elm : value) {
        parseJSONValueType(elm, node);
    }
}

void XML2JSON::parseJSONArray(const Json::Value& value,
                              const std::string& prefix)
{
    if (Json::arrayValue != value.type()) {
        ERROR("Wrong JSON Value type");
        throw std::logic_error("Wrong JSON Value type");
    }

    for (const auto &elm : value) {
        parseJSONValueType(elm, prefix);
    }
}

void XML2JSON::parseJSONObject(const Json::Value& value, pugi::xml_node &node)
{
    if (Json::objectValue != value.type()) {
        ERROR("Wrong JSON Value type");
        throw std::logic_error("Wrong JSON Value type");
    }

    for (const auto &name : value.getMemberNames()) {
        DEBUG("Name: %s", name.c_str());
        auto child = node.append_child(name.c_str());
        parseJSONValueType(value.get(name, value), child);
    }
}

void XML2JSON::parseJSONObject(const Json::Value& value,
                               const std::string& prefix)
{
    if (Json::objectValue != value.type()) {
        ERROR("Wrong JSON Value type");
        throw std::logic_error("Wrong JSON Value type");
    }

    for (const auto &name : value.getMemberNames()) {
        DEBUG("Name: %s", name.c_str());
        std::string tmp = prefix + name + "/";
        parseJSONValueType(value.get(name, value), tmp);
    }
}

void XML2JSON::createXML()
{
//     auto elm = xmlDoc.append_child(pugi::xml_node_type::node_document);
    parseJSONValueType(jsonRoot, xmlDoc);
}

void XML2JSON::createGNMIData()
{
    std::string prefix = this->prefix + "/";
    parseJSONValueType(jsonRoot, prefix);
}