# HG changeset patch # User Sebastien Jodogne # Date 1608621097 -3600 # Node ID f7104e9d044cdff9cdcc7cfe96c705a0634043cd # Parent e8e95b80194f645a574ee5af154b2f4e8947b832 functions to read/write JSON in OrthancPluginCppWrapper.h diff -r e8e95b80194f -r f7104e9d044c OrthancFramework/Sources/Lua/LuaContext.cpp --- a/OrthancFramework/Sources/Lua/LuaContext.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancFramework/Sources/Lua/LuaContext.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -148,7 +148,7 @@ that.GetJson(json, state, 1, keepStrings); std::string s; - Toolbox::WriteJson(s, json, true /* fast */); + Toolbox::WriteFastJson(s, json); lua_pushlstring(state, s.c_str(), s.size()); return 1; diff -r e8e95b80194f -r f7104e9d044c OrthancFramework/Sources/RestApi/RestApiOutput.cpp --- a/OrthancFramework/Sources/RestApi/RestApiOutput.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancFramework/Sources/RestApi/RestApiOutput.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -96,7 +96,7 @@ else { std::string s; - Toolbox::WriteJson(s, value, false /* styled, not fast */); + Toolbox::WriteStyledJson(s, value); output_.SetContentType(MIME_JSON_UTF8); output_.Answer(s); } diff -r e8e95b80194f -r f7104e9d044c OrthancFramework/Sources/Toolbox.cpp --- a/OrthancFramework/Sources/Toolbox.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancFramework/Sources/Toolbox.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -2336,34 +2336,30 @@ } - void Toolbox::WriteJson(std::string& target, - const Json::Value& source, - bool fast) + void Toolbox::WriteFastJson(std::string& target, + const Json::Value& source) { #if JSONCPP_USE_DEPRECATED == 1 - if (fast) - { - Json::FastWriter writer; - target = writer.write(source); - } - else - { - Json::StyledWriter writer; - target = writer.write(source); - } + Json::FastWriter writer; + target = writer.write(source); #else - if (fast) - { - Json::StreamWriterBuilder builder; - builder.settings_["indentation"] = ""; - target = Json::writeString(builder, source); - } - else - { - Json::StreamWriterBuilder builder; - builder.settings_["indentation"] = " "; - target = Json::writeString(builder, source); - } + Json::StreamWriterBuilder builder; + builder.settings_["indentation"] = ""; + target = Json::writeString(builder, source); +#endif + } + + + void Toolbox::WriteStyledJson(std::string& target, + const Json::Value& source) + { +#if JSONCPP_USE_DEPRECATED == 1 + Json::StyledWriter writer; + target = writer.write(source); +#else + Json::StreamWriterBuilder builder; + builder.settings_["indentation"] = " "; + target = Json::writeString(builder, source); #endif } } diff -r e8e95b80194f -r f7104e9d044c OrthancFramework/Sources/Toolbox.h --- a/OrthancFramework/Sources/Toolbox.h Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancFramework/Sources/Toolbox.h Tue Dec 22 08:11:37 2020 +0100 @@ -268,9 +268,11 @@ const void* buffer, size_t size); - static void WriteJson(std::string& target, - const Json::Value& source, - bool fast); + static void WriteFastJson(std::string& target, + const Json::Value& source); + + static void WriteStyledJson(std::string& target, + const Json::Value& source); }; } diff -r e8e95b80194f -r f7104e9d044c OrthancFramework/UnitTestsSources/LuaTests.cpp --- a/OrthancFramework/UnitTestsSources/LuaTests.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancFramework/UnitTestsSources/LuaTests.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -176,7 +176,7 @@ f.ExecuteToString(s); std::string t; - Orthanc::Toolbox::WriteJson(t, a, true /* fast */); + Orthanc::Toolbox::WriteFastJson(t, a); ASSERT_EQ(s, t); } } diff -r e8e95b80194f -r f7104e9d044c OrthancFramework/UnitTestsSources/RestApiTests.cpp --- a/OrthancFramework/UnitTestsSources/RestApiTests.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancFramework/UnitTestsSources/RestApiTests.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -343,7 +343,7 @@ root.CreateSiteMap(m); std::string s; - Toolbox::WriteJson(s, m, false /* styled, not fast */); + Toolbox::WriteStyledJson(s, m); Json::Value d; ASSERT_FALSE(GetDirectory(d, root, "/hello")); diff -r e8e95b80194f -r f7104e9d044c OrthancFramework/UnitTestsSources/ToolboxTests.cpp --- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -45,13 +45,13 @@ ASSERT_TRUE(Toolbox::ReadJson(c, b)); std::string d, e; - Toolbox::WriteJson(d, a, true); - Toolbox::WriteJson(e, c, true); + Toolbox::WriteFastJson(d, a); + Toolbox::WriteFastJson(e, c); ASSERT_EQ(d, e); std::string f, g; - Toolbox::WriteJson(f, a, false); - Toolbox::WriteJson(g, c, false); + Toolbox::WriteStyledJson(f, a); + Toolbox::WriteStyledJson(g, c); ASSERT_EQ(f, g); /** diff -r e8e95b80194f -r f7104e9d044c OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -2674,13 +2674,13 @@ if (service == _OrthancPluginService_GetInstanceJson) { - Toolbox::WriteJson(s, instance.GetJson(), false /* styled writer */); + Toolbox::WriteStyledJson(s, instance.GetJson()); } else { Json::Value simplified; Toolbox::SimplifyDicomAsJson(simplified, instance.GetJson(), DicomToJsonFormat_Human); - Toolbox::WriteJson(s, simplified, false /* styled writer */); + Toolbox::WriteStyledJson(s, simplified); } *p.resultStringToFree = CopyString(s); @@ -2882,7 +2882,7 @@ static_cast(p.flags), p.maxStringLength); std::string s; - Toolbox::WriteJson(s, json, true /* fast */); + Toolbox::WriteFastJson(s, json); *p.targetStringToFree = CopyString(s); return; } @@ -3380,7 +3380,7 @@ static_cast(p.flags), p.maxStringLength); std::string s; - Toolbox::WriteJson(s, json, true /* fast */); + Toolbox::WriteFastJson(s, json); *p.result = CopyString(s); } diff -r e8e95b80194f -r f7104e9d044c OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp --- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -24,9 +24,33 @@ #include #include #include + + #include +#include #include +#if !defined(JSONCPP_VERSION_MAJOR) || !defined(JSONCPP_VERSION_MINOR) +# error Cannot access the version of JsonCpp +#endif + + +/** + * We use deprecated "Json::Reader", "Json::StyledWriter" and + * "Json::StyledReader" if JsonCpp < 1.7.0. This choice is rather + * arbitrary, but if Json >= 1.9.0, gcc generates explicit deprecation + * warnings (clang was warning in earlier versions). For reference, + * these classes seem to have been deprecated since JsonCpp 1.4.0 (on + * February 2015) by the following changeset: + * https://github.com/open-source-parsers/jsoncpp/commit/8df98f6112890d6272734975dd6d70cf8999bb22 + **/ +#if (JSONCPP_VERSION_MAJOR >= 2 || \ + (JSONCPP_VERSION_MAJOR == 1 && JSONCPP_VERSION_MINOR >= 8)) +# define JSONCPP_USE_DEPRECATED 0 +#else +# define JSONCPP_USE_DEPRECATED 1 +#endif + #if !ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 2, 0) static const OrthancPluginErrorCode OrthancPluginErrorCode_NullPointer = OrthancPluginErrorCode_Plugin; @@ -202,10 +226,7 @@ ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); } - const char* tmp = reinterpret_cast(buffer_.data); - - Json::Reader reader; - if (!reader.parse(tmp, tmp + buffer_.size, target)) + if (!ReadJson(target, buffer_.data, buffer_.size)) { LogError("Cannot convert some memory buffer to JSON"); ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); @@ -292,12 +313,80 @@ } + bool ReadJson(Json::Value& target, + const std::string& source) + { +#if JSONCPP_USE_DEPRECATED == 1 + Json::Reader reader; + return reader.parse(source, target); +#else + return ReadJson(target, source.c_str(), source.size()); +#endif + } + + + bool ReadJson(Json::Value& target, + 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 + } + + + void WriteFastJson(std::string& target, + const Json::Value& source) + { +#if JSONCPP_USE_DEPRECATED == 1 + Json::FastWriter writer; + target = writer.write(source); +#else + Json::StreamWriterBuilder builder; + builder.settings_["indentation"] = ""; + target = Json::writeString(builder, source); +#endif + } + + + void WriteStyledJson(std::string& target, + const Json::Value& source) + { +#if JSONCPP_USE_DEPRECATED == 1 + Json::StyledWriter writer; + target = writer.write(source); +#else + Json::StreamWriterBuilder builder; + builder.settings_["indentation"] = " "; + target = Json::writeString(builder, source); +#endif + } + + bool MemoryBuffer::RestApiPost(const std::string& uri, const Json::Value& body, bool applyPlugins) { - Json::FastWriter writer; - return RestApiPost(uri, writer.write(body), applyPlugins); + std::string s; + WriteFastJson(s, body); + return RestApiPost(uri, s, applyPlugins); } @@ -305,8 +394,9 @@ const Json::Value& body, bool applyPlugins) { - Json::FastWriter writer; - return RestApiPut(uri, writer.write(body), applyPlugins); + std::string s; + WriteFastJson(s, body); + return RestApiPut(uri, s, applyPlugins); } @@ -315,8 +405,8 @@ { Clear(); - Json::FastWriter writer; - std::string s = writer.write(tags); + std::string s; + WriteFastJson(s, tags); Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), NULL, flags)); } @@ -327,8 +417,8 @@ { Clear(); - Json::FastWriter writer; - std::string s = writer.write(tags); + std::string s; + WriteFastJson(s, tags); Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), pixelData.GetObject(), flags)); } @@ -390,8 +480,7 @@ ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); } - Json::Reader reader; - if (!reader.parse(str_, target)) + if (!ReadJson(target, str_)) { LogError("Cannot convert some memory buffer to JSON"); ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); @@ -1190,19 +1279,16 @@ #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */ void AnswerJson(const Json::Value& value, - OrthancPluginRestOutput* output - ) + OrthancPluginRestOutput* output) { - Json::StyledWriter writer; - std::string bodyString = writer.write(value); - + std::string bodyString; + WriteStyledJson(bodyString, value); OrthancPluginAnswerBuffer(GetGlobalContext(), output, bodyString.c_str(), bodyString.size(), "application/json"); } void AnswerString(const std::string& answer, const char* mimeType, - OrthancPluginRestOutput* output - ) + OrthancPluginRestOutput* output) { OrthancPluginAnswerBuffer(GetGlobalContext(), output, answer.c_str(), answer.size(), mimeType); } @@ -1324,8 +1410,9 @@ const Json::Value& body, bool applyPlugins) { - Json::FastWriter writer; - return RestApiPost(result, uri, writer.write(body), applyPlugins); + std::string s; + WriteFastJson(s, body); + return RestApiPost(result, uri, s, applyPlugins); } @@ -1357,8 +1444,9 @@ const Json::Value& body, bool applyPlugins) { - Json::FastWriter writer; - return RestApiPut(result, uri, writer.write(body), applyPlugins); + std::string s; + WriteFastJson(s, body); + return RestApiPut(result, uri, s, applyPlugins); } @@ -2020,8 +2108,7 @@ } else { - Json::FastWriter writer; - content_ = writer.write(content); + WriteFastJson(content_, content); } } @@ -2041,8 +2128,7 @@ } else { - Json::FastWriter writer; - serialized_ = writer.write(serialized); + WriteFastJson(serialized_, serialized); hasSerialized_ = true; } } @@ -2902,8 +2988,7 @@ std::string body; Execute(answerHeaders, body); - Json::Reader reader; - if (!reader.parse(body, answerBody)) + if (!ReadJson(answerBody, body)) { LogError("Cannot convert HTTP answer body to JSON"); ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); diff -r e8e95b80194f -r f7104e9d044c OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h --- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h Tue Dec 22 08:11:37 2020 +0100 @@ -476,6 +476,19 @@ #endif + bool ReadJson(Json::Value& target, + const std::string& source); + + bool ReadJson(Json::Value& target, + const void* buffer, + size_t size); + + void WriteFastJson(std::string& target, + const Json::Value& source); + + void WriteStyledJson(std::string& target, + const Json::Value& source); + bool RestApiGet(Json::Value& result, const std::string& uri, bool applyPlugins); diff -r e8e95b80194f -r f7104e9d044c OrthancServer/Sources/OrthancConfiguration.cpp --- a/OrthancServer/Sources/OrthancConfiguration.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancServer/Sources/OrthancConfiguration.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -364,7 +364,7 @@ SaveModalitiesToJson(modalities); std::string s; - Toolbox::WriteJson(s, modalities, true /* fast */); + Toolbox::WriteFastJson(s, modalities); serverIndex_->SetGlobalProperty(GlobalProperty_Modalities, s); } @@ -396,7 +396,7 @@ SavePeersToJson(peers); std::string s; - Toolbox::WriteJson(s, peers, true /* fast */); + Toolbox::WriteFastJson(s, peers); serverIndex_->SetGlobalProperty(GlobalProperty_Peers, s); } @@ -869,7 +869,7 @@ void OrthancConfiguration::Format(std::string& result) const { - Toolbox::WriteJson(result, json_, false /* styled, not fast */); + Toolbox::WriteStyledJson(result, json_); } @@ -889,8 +889,8 @@ ReadConfiguration(current, configurationFileArg_); std::string a, b; - Toolbox::WriteJson(a, json_, true /* fast */); - Toolbox::WriteJson(b, current, true /* fast */); + Toolbox::WriteFastJson(a, json_); + Toolbox::WriteFastJson(b, current); return a != b; } diff -r e8e95b80194f -r f7104e9d044c OrthancServer/Sources/ServerContext.cpp --- a/OrthancServer/Sources/ServerContext.cpp Mon Dec 21 18:55:32 2020 +0100 +++ b/OrthancServer/Sources/ServerContext.cpp Tue Dec 22 08:11:37 2020 +0100 @@ -260,7 +260,7 @@ jobsEngine_.GetRegistry().Serialize(value); std::string serialized; - Toolbox::WriteJson(serialized, value, true /* fast */); + Toolbox::WriteFastJson(serialized, value); index_.SetGlobalProperty(GlobalProperty_JobsRegistry, serialized); }