MCPcopy Create free account
hub / github.com/Snapchat/Valdi / jsonValueToValue

Function jsonValueToValue

valdi_core/src/valdi_core/cpp/Utils/ValueUtils.cpp:168–222  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

166}
167
168Value jsonValueToValue(const Json::Value& jsonValue) {
169 switch (jsonValue.type()) {
170 case Json::nullValue:
171 return Value();
172 case Json::intValue:
173 [[fallthrough]];
174 case Json::uintValue: {
175 if (jsonValue.isInt()) {
176 return Value(jsonValue.asInt());
177 } else {
178 return Value(static_cast<int64_t>(jsonValue.asInt64()));
179 }
180 }
181 case Json::realValue:
182 return Value(jsonValue.asDouble());
183 case Json::stringValue: {
184 const char* start;
185 const char* end;
186 jsonValue.getString(&start, &end);
187 return Value(StringCache::getGlobal().makeString(start, end - start));
188 }
189 case Json::booleanValue:
190 return Value(jsonValue.asBool());
191 case Json::arrayValue: {
192 auto size = jsonValue.size();
193 auto out = ValueArray::make(static_cast<size_t>(size));
194
195 size_t i = 0;
196 for (const auto& value : jsonValue) {
197 out->emplace(static_cast<size_t>(i++), jsonValueToValue(value));
198 }
199
200 return Value(out);
201 }
202 case Json::objectValue: {
203 auto valueMap = makeShared<ValueMap>();
204
205 auto it = jsonValue.begin();
206 auto end = jsonValue.end();
207
208 valueMap->reserve(end - it);
209
210 while (it != end) {
211 const char* end;
212 const char* start = it.memberName(&end);
213 auto key = StringCache::getGlobal().makeString(start, end - start);
214 (*valueMap)[key] = jsonValueToValue(*it);
215
216 it++;
217 }
218
219 return Value(valueMap);
220 }
221 }
222}
223
224Result<Value> correctJsonToValue(const std::string_view& str) {
225 Json::Reader reader;

Callers 1

correctJsonToValueFunction · 0.85

Calls 13

getGlobalFunction · 0.85
isIntMethod · 0.80
asIntMethod · 0.80
asDoubleMethod · 0.80
makeStringMethod · 0.80
ValueClass · 0.70
typeMethod · 0.65
getStringMethod · 0.65
endMethod · 0.65
sizeMethod · 0.45
emplaceMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected