Repository navigation
Expand file tree
/
Copy pathjson_utility.cpp
More file actions
86 lines (77 loc) · 2.42 KB
/
Copy pathjson_utility.cpp
File metadata and controls
86 lines (77 loc) · 2.42 KB
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
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/reader.h"
#include "MyLog.h"
#include "json_utility.h"
namespace JsonUtility
{
int GetJsonInfoList(const char *pcJsonBuff, int iJsonLen,
std::map<std::string, std::string> &oNodeInfoMap)
{
static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
rapidjson::Document document;
document.Parse(pcJsonBuff, iJsonLen);
if (document.HasParseError())
{
LOG_ERROR("Parse error: ({}:{}) {}.",
document.GetParseError(), document.GetErrorOffset(),
rapidjson::GetParseError_En(document.GetParseError()));
return -1;
}
if (!document.IsObject())
{
LOG_ERROR("Json root is not object.");
return -1;
}
for (auto &iter: document.GetObject())
{
const char *name = iter.name.GetString();
if (iter.value.IsString())
{
const char *value = iter.value.GetString();
oNodeInfoMap[name] = value;
}
else if (iter.value.IsNumber())
{
double dValue = iter.value.GetDouble();
std::string dString = std::to_string(dValue);
oNodeInfoMap[name] = dString;
}
else if (iter.value.IsArray())
{
std::string strArray;
for (auto &v: iter.value.GetArray())
{
if (v.IsString())
{
strArray = strArray + v.GetString() + ",";
}
else if (v.IsNumber())
{
double dv = v.GetDouble();
std::string dStr = std::to_string(dv);
strArray += dStr + ",";
}
else
{
LOG_DEBUG("Name: {} type: {} is continue.", name, kTypeNames[v.GetType()]);
continue;
}
}
if (strArray.empty())
{
continue;
}
oNodeInfoMap[name] = strArray;
}
else
{
LOG_DEBUG("Name: {} type: {} is continue.", name, kTypeNames[iter.value.GetType()]);
continue;
}
}
return 0;
}
}