MCPcopy Create free account
hub / github.com/axmolengine/axmol / jsonParse

Function jsonParse

core/ui/UIWebView/UIWebViewCommon.h:116–173  ·  view source on GitHub ↗

These are the results that must be returned by this method assert(jsonParse(R"({"foo":"bar"})", "foo", -1) == "bar"); assert(jsonParse(R"({"foo":""})", "foo", -1) == ""); assert(jsonParse(R"(["foo", "bar", "baz"])", "", 0) == "foo"); assert(jsonParse(R"(["foo", "bar", "baz"])", "", 2) == "baz"); The following is a special case, where the exact json string is not returned due to how rapidjson re-cr

Source from the content-addressed store, hash-verified

114// to how rapidjson re-creates the nested object, original: "{"bar": 1}", parsed result: "{"bar":1}"
115// assert(jsonParse(R"({"foo": {"bar": 1}})", "foo", -1) == R"({"bar":1})");
116inline std::string jsonParse(std::string_view s, std::string_view key, const int index)
117{
118 const char* value = nullptr;
119 size_t value_sz{};
120 StringBuffer sb;
121 Writer<StringBuffer> writer(sb);
122 Document d;
123 d.Parse(s.data());
124 if (key.empty() && index > -1)
125 {
126 if (d.IsArray())
127 {
128 auto&& jsonArray = d.GetArray();
129 if (SizeType(index) < jsonArray.Size())
130 {
131 auto&& arrayValue = jsonArray[SizeType(index)];
132 value = arrayValue.GetString();
133 value_sz = arrayValue.GetStringLength();
134 }
135 }
136 }
137 else
138 {
139 auto&& fieldItr = d.FindMember(key.data());
140 if (fieldItr != d.MemberEnd())
141 {
142 auto&& jsonValue = fieldItr->value;
143 if (jsonValue.IsString())
144 {
145 value = jsonValue.GetString();
146 value_sz = jsonValue.GetStringLength();
147 }
148 else
149 {
150 jsonValue.Accept(writer);
151 value = sb.GetString();
152 value_sz = sb.GetLength();
153 }
154 }
155 }
156
157 if (value != nullptr)
158 {
159 if (value[0] != '"')
160 {
161 return std::string(value, value_sz);
162 }
163
164 const auto n = jsonUnescape(value, value_sz, nullptr);
165 if (n > 0)
166 {
167 const auto decoded = std::unique_ptr<char[]>(new char[n + 1]);
168 jsonUnescape(value, value_sz, decoded.get());
169 return std::string(decoded.get(), n);
170 }
171 }
172 return "";
173}

Callers 1

on_messageMethod · 0.85

Calls 13

SizeTypeEnum · 0.85
jsonUnescapeFunction · 0.85
GetArrayMethod · 0.80
FindMemberMethod · 0.80
MemberEndMethod · 0.80
ParseMethod · 0.45
dataMethod · 0.45
emptyMethod · 0.45
IsArrayMethod · 0.45
SizeMethod · 0.45
IsStringMethod · 0.45
GetLengthMethod · 0.45

Tested by

no test coverage detected