-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGpJsonSerializerCtx.cpp
130 lines (104 loc) · 2.91 KB
/
GpJsonSerializerCtx.cpp
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
#include <GpJson/GpJsonSerializerCtx.hpp>
#include <GpJson/GpJsonToObject.hpp>
#include <GpJson/GpJsonUtilsInternal.hpp>
GP_WARNING_PUSH()
#if defined(GP_COMPILER_CLANG) || defined(GP_COMPILER_GCC)
GP_WARNING_DISABLE(switch-enum)
#endif// #if defined(GP_COMPILER_CLANG) || defined(GP_COMPILER_GCC)
# include <rapidjson/document.h>
GP_WARNING_POP()
namespace GPlatform {
GpJsonSerializerCtx::~GpJsonSerializerCtx (void) noexcept
{
Clear();
}
void GpJsonSerializerCtx::Clear (void) noexcept
{
if (iJsonDOM)
{
delete static_cast<rapidjson::Document*>(iJsonDOM);
iJsonDOM = nullptr;
}
if (iJsonRootValue)
{
if (iIsArray)
{
delete static_cast<rapidjson::Document::ConstArray*>(iJsonRootValue);
} else
{
delete static_cast<rapidjson::Document::ConstObject*>(iJsonRootValue);
}
iJsonRootValue = nullptr;
}
}
void GpJsonSerializerCtx::Parse (GpSpanCharRW aJsonStr)
{
Clear();
iJsonDOM = new rapidjson::Document();
GpJsonToObject::ParseResT parseRes = GpJsonToObject::SParseJsonDomInsitu
(
aJsonStr,
*static_cast<rapidjson::Document*>(iJsonDOM)
);
if (std::holds_alternative<rapidjson::Document::ConstObject>(parseRes))
{
iIsArray = false;
iJsonRootValue = new rapidjson::Document::ConstObject
(
std::get<rapidjson::Document::ConstObject>(parseRes)
);
} else if (std::holds_alternative<rapidjson::Document::ConstArray>(parseRes))
{
iIsArray = true;
iJsonRootValue = new rapidjson::Document::ConstArray
(
std::get<rapidjson::Document::ConstArray>(parseRes)
);
}
}
std::optional<std::string> GpJsonSerializerCtx::FindMemberStr (const std::string& aName) const
{
if (IsArray())
{
return std::nullopt;
}
const rapidjson::Document::ConstObject& jsonObject = *reinterpret_cast<const rapidjson::Document::ConstObject*>(RootAsObject());
rapidjson::Document::ConstMemberIterator iter = jsonObject.FindMember(std::data(aName));
if (iter != jsonObject.MemberEnd())
{
const auto& mitVal = iter->value;
return std::string(_JsonValue2SV(mitVal));
} else
{
return std::nullopt;
}
}
const void* GpJsonSerializerCtx::RootAsObject (void) const
{
THROW_COND_GP
(
iJsonRootValue != nullptr,
"Json root value is null. Call Init first"_sv
);
THROW_COND_GP
(
iIsArray == false,
"Json root value is array"_sv
);
return iJsonRootValue;
}
const void* GpJsonSerializerCtx::RootAsArray (void) const
{
THROW_COND_GP
(
iJsonRootValue != nullptr,
"Json root value is null. Call Init first"_sv
);
THROW_COND_GP
(
iIsArray == true,
"Json root value is object"_sv
);
return iJsonRootValue;
}
}// namespace GPlatform