comparison OrthancFramework/Sources/Toolbox.cpp @ 4397:3aeb5171fbd4

new function Toolbox::ReadJsonWithoutComments()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Dec 2020 08:59:23 +0100
parents d07a65100a95
children d9473bd5ed43
comparison
equal deleted inserted replaced
4396:d07a65100a95 4397:3aeb5171fbd4
2295 } 2295 }
2296 } 2296 }
2297 } 2297 }
2298 2298
2299 2299
2300 static bool ReadJsonInternal(Json::Value& target,
2301 const void* buffer,
2302 size_t size,
2303 bool collectComments)
2304 {
2305 #if JSONCPP_USE_DEPRECATED == 1
2306 Json::Reader reader;
2307 return reader.parse(reinterpret_cast<const char*>(buffer),
2308 reinterpret_cast<const char*>(buffer) + size, target, collectComments);
2309 #else
2310 Json::CharReaderBuilder builder;
2311 builder.settings_["collectComments"] = collectComments;
2312
2313 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
2314 assert(reader.get() != NULL);
2315
2316 JSONCPP_STRING err;
2317 if (reader->parse(reinterpret_cast<const char*>(buffer),
2318 reinterpret_cast<const char*>(buffer) + size, &target, &err))
2319 {
2320 return true;
2321 }
2322 else
2323 {
2324 LOG(ERROR) << "Cannot parse JSON: " << err;
2325 return false;
2326 }
2327 #endif
2328 }
2329
2330
2300 bool Toolbox::ReadJson(Json::Value& target, 2331 bool Toolbox::ReadJson(Json::Value& target,
2301 const std::string& source) 2332 const std::string& source)
2302 { 2333 {
2303 #if JSONCPP_USE_DEPRECATED == 1 2334 return ReadJson(target, source.empty() ? NULL : source.c_str(), source.size());
2304 Json::Reader reader;
2305 return reader.parse(source, target);
2306 #else
2307 return ReadJson(target, source.c_str(), source.size());
2308 #endif
2309 } 2335 }
2310 2336
2311 2337
2312 bool Toolbox::ReadJson(Json::Value& target, 2338 bool Toolbox::ReadJson(Json::Value& target,
2313 const void* buffer, 2339 const void* buffer,
2314 size_t size) 2340 size_t size)
2315 { 2341 {
2316 #if JSONCPP_USE_DEPRECATED == 1 2342 return ReadJsonInternal(target, buffer, size, true);
2317 Json::Reader reader; 2343 }
2318 return reader.parse(reinterpret_cast<const char*>(buffer), 2344
2319 reinterpret_cast<const char*>(buffer) + size, target); 2345
2320 #else 2346 bool Toolbox::ReadJsonWithoutComments(Json::Value& target,
2321 Json::CharReaderBuilder builder; 2347 const std::string& source)
2322 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); 2348 {
2323 assert(reader.get() != NULL); 2349 return ReadJsonWithoutComments(target, source.empty() ? NULL : source.c_str(), source.size());
2324 JSONCPP_STRING err; 2350 }
2325 if (reader->parse(reinterpret_cast<const char*>(buffer), 2351
2326 reinterpret_cast<const char*>(buffer) + size, &target, &err)) 2352
2327 { 2353 bool Toolbox::ReadJsonWithoutComments(Json::Value& target,
2328 return true; 2354 const void* buffer,
2329 } 2355 size_t size)
2330 else 2356 {
2331 { 2357 return ReadJsonInternal(target, buffer, size, false);
2332 LOG(ERROR) << "Cannot parse JSON: " << err;
2333 return false;
2334 }
2335 #endif
2336 } 2358 }
2337 2359
2338 2360
2339 void Toolbox::WriteFastJson(std::string& target, 2361 void Toolbox::WriteFastJson(std::string& target,
2340 const Json::Value& source) 2362 const Json::Value& source)