# HG changeset patch # User Sebastien Jodogne # Date 1610525972 -3600 # Node ID 11ea0a05115b6becd4fc8baedf35045aafaf1830 # Parent fd958175c5b96ee31f0c8ce8086dc604a74831db new function in OrthancPluginCppWrapper: ReadJsonWithoutComments() diff -r fd958175c5b9 -r 11ea0a05115b OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp --- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Tue Jan 12 13:07:15 2021 +0100 +++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Wed Jan 13 09:19:32 2021 +0100 @@ -313,6 +313,37 @@ } + static bool ReadJsonInternal(Json::Value& target, + const void* buffer, + size_t size, + bool collectComments) + { +#if JSONCPP_USE_DEPRECATED == 1 + Json::Reader reader; + return reader.parse(reinterpret_cast(buffer), + reinterpret_cast(buffer) + size, target, collectComments); +#else + Json::CharReaderBuilder builder; + builder.settings_["collectComments"] = collectComments; + + const std::unique_ptr reader(builder.newCharReader()); + assert(reader.get() != NULL); + + JSONCPP_STRING err; + if (reader->parse(reinterpret_cast(buffer), + reinterpret_cast(buffer) + size, &target, &err)) + { + return true; + } + else + { + LOG(ERROR) << "Cannot parse JSON: " << err; + return false; + } +#endif + } + + bool ReadJson(Json::Value& target, const std::string& source) { @@ -324,29 +355,25 @@ const void* buffer, size_t size) { -#if JSONCPP_USE_DEPRECATED == 1 - Json::Reader reader; - return reader.parse(reinterpret_cast(buffer), - reinterpret_cast(buffer) + size, target); -#else - Json::CharReaderBuilder builder; - const std::unique_ptr reader(builder.newCharReader()); - assert(reader.get() != NULL); - JSONCPP_STRING err; - if (reader->parse(reinterpret_cast(buffer), - reinterpret_cast(buffer) + size, &target, &err)) - { - return true; - } - else - { - LogError("Cannot parse JSON: " + err); - return false; - } -#endif + return ReadJsonInternal(target, buffer, size, true); } + bool ReadJsonWithoutComments(Json::Value& target, + const std::string& source) + { + return ReadJsonWithoutComments(target, source.empty() ? NULL : source.c_str(), source.size()); + } + + + bool ReadJsonWithoutComments(Json::Value& target, + const void* buffer, + size_t size) + { + return ReadJsonInternal(target, buffer, size, false); + } + + void WriteFastJson(std::string& target, const Json::Value& source) { diff -r fd958175c5b9 -r 11ea0a05115b OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h --- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h Tue Jan 12 13:07:15 2021 +0100 +++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h Wed Jan 13 09:19:32 2021 +0100 @@ -483,6 +483,13 @@ const void* buffer, size_t size); + bool ReadJsonWithoutComments(Json::Value& target, + const std::string& source); + + bool ReadJsonWithoutComments(Json::Value& target, + const void* buffer, + size_t size); + void WriteFastJson(std::string& target, const Json::Value& source);