comparison OrthancFramework/Sources/Toolbox.cpp @ 4393:e8e95b80194f

removing usage of deprecated JsonCpp classes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 21 Dec 2020 18:55:32 +0100
parents 3af1d763763a
children f7104e9d044c
comparison
equal deleted inserted replaced
4392:3af1d763763a 4393:e8e95b80194f
25 25
26 #include "Compatibility.h" 26 #include "Compatibility.h"
27 #include "OrthancException.h" 27 #include "OrthancException.h"
28 #include "Logging.h" 28 #include "Logging.h"
29 29
30 #include <json/json.h> 30 #include <json/reader.h>
31 #include <json/version.h>
32 #include <json/writer.h>
33
34 #if !defined(JSONCPP_VERSION_MAJOR) || !defined(JSONCPP_VERSION_MINOR)
35 # error Cannot access the version of JsonCpp
36 #endif
37
38
39 /**
40 * We use deprecated "Json::Reader", "Json::StyledWriter" and
41 * "Json::StyledReader" if JsonCpp < 1.7.0. This choice is rather
42 * arbitrary, but if Json >= 1.9.0, gcc generates explicit deprecation
43 * warnings (clang was warning in earlier versions). For reference,
44 * these classes seem to have been deprecated since JsonCpp 1.4.0 (on
45 * February 2015) by the following changeset:
46 * https://github.com/open-source-parsers/jsoncpp/commit/8df98f6112890d6272734975dd6d70cf8999bb22
47 **/
48 #if (JSONCPP_VERSION_MAJOR >= 2 || \
49 (JSONCPP_VERSION_MAJOR == 1 && JSONCPP_VERSION_MINOR >= 8))
50 # define JSONCPP_USE_DEPRECATED 0
51 #else
52 # define JSONCPP_USE_DEPRECATED 1
53 #endif
54
31 55
32 #include <boost/algorithm/string/case_conv.hpp> 56 #include <boost/algorithm/string/case_conv.hpp>
33 #include <boost/algorithm/string/replace.hpp> 57 #include <boost/algorithm/string/replace.hpp>
34 #include <boost/lexical_cast.hpp> 58 #include <boost/lexical_cast.hpp>
35 #include <boost/regex.hpp> 59 #include <boost/regex.hpp>
2274 2298
2275 2299
2276 bool Toolbox::ReadJson(Json::Value& target, 2300 bool Toolbox::ReadJson(Json::Value& target,
2277 const std::string& source) 2301 const std::string& source)
2278 { 2302 {
2303 #if JSONCPP_USE_DEPRECATED == 1
2279 Json::Reader reader; 2304 Json::Reader reader;
2280 return reader.parse(source, target); 2305 return reader.parse(source, target);
2306 #else
2307 return ReadJson(target, source.c_str(), source.size());
2308 #endif
2281 } 2309 }
2282 2310
2283 2311
2284 bool Toolbox::ReadJson(Json::Value& target, 2312 bool Toolbox::ReadJson(Json::Value& target,
2285 const void* buffer, 2313 const void* buffer,
2286 size_t size) 2314 size_t size)
2287 { 2315 {
2316 #if JSONCPP_USE_DEPRECATED == 1
2288 Json::Reader reader; 2317 Json::Reader reader;
2289 return reader.parse(reinterpret_cast<const char*>(buffer), 2318 return reader.parse(reinterpret_cast<const char*>(buffer),
2290 reinterpret_cast<const char*>(buffer) + size, target); 2319 reinterpret_cast<const char*>(buffer) + size, target);
2320 #else
2321 Json::CharReaderBuilder builder;
2322 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
2323 assert(reader.get() != NULL);
2324 JSONCPP_STRING err;
2325 if (reader->parse(reinterpret_cast<const char*>(buffer),
2326 reinterpret_cast<const char*>(buffer) + size, &target, &err))
2327 {
2328 return true;
2329 }
2330 else
2331 {
2332 LOG(ERROR) << "Cannot parse JSON: " << err;
2333 return false;
2334 }
2335 #endif
2291 } 2336 }
2292 2337
2293 2338
2294 void Toolbox::WriteJson(std::string& target, 2339 void Toolbox::WriteJson(std::string& target,
2295 const Json::Value& source, 2340 const Json::Value& source,
2296 bool fast) 2341 bool fast)
2297 { 2342 {
2343 #if JSONCPP_USE_DEPRECATED == 1
2298 if (fast) 2344 if (fast)
2299 { 2345 {
2300 Json::FastWriter writer; 2346 Json::FastWriter writer;
2301 target = writer.write(source); 2347 target = writer.write(source);
2302 } 2348 }
2303 else 2349 else
2304 { 2350 {
2305 Json::StyledWriter writer; 2351 Json::StyledWriter writer;
2306 target = writer.write(source); 2352 target = writer.write(source);
2307 } 2353 }
2354 #else
2355 if (fast)
2356 {
2357 Json::StreamWriterBuilder builder;
2358 builder.settings_["indentation"] = "";
2359 target = Json::writeString(builder, source);
2360 }
2361 else
2362 {
2363 Json::StreamWriterBuilder builder;
2364 builder.settings_["indentation"] = " ";
2365 target = Json::writeString(builder, source);
2366 }
2367 #endif
2308 } 2368 }
2309 } 2369 }
2310 2370
2311 2371
2312 2372