# HG changeset patch # User Sebastien Jodogne # Date 1608570587 -3600 # Node ID 3af1d763763a05b1731bb05f324a402eef07c573 # Parent 0c4ff5609548f18e9921da895c639186eb7fb57a confining Json::Reader and Json::*Writer into Toolbox diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/DicomFormat/DicomMap.h --- a/OrthancFramework/Sources/DicomFormat/DicomMap.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/DicomFormat/DicomMap.h Mon Dec 21 18:09:47 2020 +0100 @@ -28,7 +28,7 @@ #include #include -#include +#include namespace Orthanc { diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/DicomNetworking/IStoreRequestHandler.h --- a/OrthancFramework/Sources/DicomNetworking/IStoreRequestHandler.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/DicomNetworking/IStoreRequestHandler.h Mon Dec 21 18:09:47 2020 +0100 @@ -26,7 +26,7 @@ #include #include -#include +#include namespace Orthanc { diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/DicomNetworking/RemoteModalityParameters.h --- a/OrthancFramework/Sources/DicomNetworking/RemoteModalityParameters.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/DicomNetworking/RemoteModalityParameters.h Mon Dec 21 18:09:47 2020 +0100 @@ -26,7 +26,7 @@ #include #include -#include +#include namespace Orthanc { diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h --- a/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h Mon Dec 21 18:09:47 2020 +0100 @@ -30,7 +30,7 @@ #include #include #include -#include +#include #if ORTHANC_ENABLE_DCMTK != 1 # error The macro ORTHANC_ENABLE_DCMTK must be set to 1 diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/HttpClient.cpp --- a/OrthancFramework/Sources/HttpClient.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/HttpClient.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -1067,8 +1067,7 @@ std::string s; if (ApplyInternal(s, answerHeaders)) { - Json::Reader reader; - return reader.parse(s, answerBody); + return Toolbox::ReadJson(answerBody, s); } else { diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/HttpClient.h --- a/OrthancFramework/Sources/HttpClient.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/HttpClient.h Mon Dec 21 18:09:47 2020 +0100 @@ -29,7 +29,7 @@ #include #include #include -#include +#include #if !defined(ORTHANC_ENABLE_CURL) # error The macro ORTHANC_ENABLE_CURL must be defined diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/Images/Font.cpp --- a/OrthancFramework/Sources/Images/Font.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/Images/Font.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -62,8 +62,7 @@ void Font::LoadFromMemory(const std::string& font) { Json::Value v; - Json::Reader reader; - if (!reader.parse(font, v) || + if (!Toolbox::ReadJson(v, font) || v.type() != Json::objectValue || !v.isMember("Name") || !v.isMember("Size") || diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/JobsEngine/JobsEngine.cpp --- a/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -25,8 +25,8 @@ #include "../Logging.h" #include "../OrthancException.h" +#include "../Toolbox.h" -#include namespace Orthanc { @@ -197,8 +197,7 @@ const std::string& serialized) { Json::Value value; - Json::Reader reader; - if (reader.parse(serialized, value)) + if (Toolbox::ReadJson(value, serialized)) { LoadRegistryFromJson(unserializer, value); } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/Lua/LuaContext.cpp --- a/OrthancFramework/Sources/Lua/LuaContext.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/Lua/LuaContext.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -113,8 +113,7 @@ const char* str = lua_tostring(state, 1); Json::Value value; - Json::Reader reader; - if (reader.parse(str, str + strlen(str), value)) + if (Toolbox::ReadJson(value, str, strlen(str))) { that.PushJson(value); } @@ -148,8 +147,8 @@ Json::Value json; that.GetJson(json, state, 1, keepStrings); - Json::FastWriter writer; - std::string s = writer.write(json); + std::string s; + Toolbox::WriteJson(s, json, true /* fast */); lua_pushlstring(state, s.c_str(), s.size()); return 1; @@ -635,8 +634,7 @@ std::string s; ExecuteInternal(&s, command); - Json::Reader reader; - if (!reader.parse(s, output)) + if (!Toolbox::ReadJson(output, s)) { throw OrthancException(ErrorCode_BadJson); } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/Lua/LuaFunctionCall.h --- a/OrthancFramework/Sources/Lua/LuaFunctionCall.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/Lua/LuaFunctionCall.h Mon Dec 21 18:09:47 2020 +0100 @@ -31,8 +31,6 @@ #include "../DicomFormat/DicomArray.h" #include "../DicomFormat/DicomMap.h" -#include - namespace Orthanc { class ORTHANC_PUBLIC LuaFunctionCall : public boost::noncopyable diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/RestApi/RestApiCall.cpp --- a/OrthancFramework/Sources/RestApi/RestApiCall.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/RestApi/RestApiCall.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -25,17 +25,6 @@ namespace Orthanc { - bool RestApiCall::ParseJsonRequestInternal(Json::Value& result, - const void* body, - size_t size) - { - result.clear(); - Json::Reader reader; - return reader.parse(reinterpret_cast(body), - reinterpret_cast(body) + size, result); - } - - std::string RestApiCall::FlattenUri() const { std::string s = "/"; diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/RestApi/RestApiCall.h --- a/OrthancFramework/Sources/RestApi/RestApiCall.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/RestApi/RestApiCall.h Mon Dec 21 18:09:47 2020 +0100 @@ -45,11 +45,6 @@ const UriComponents& trailing_; const UriComponents& fullUri_; - protected: - static bool ParseJsonRequestInternal(Json::Value& result, - const void* body, - size_t size); - public: RestApiCall(RestApiOutput& output, RestApi& context, diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/RestApi/RestApiOutput.cpp --- a/OrthancFramework/Sources/RestApi/RestApiOutput.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/RestApi/RestApiOutput.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -95,9 +95,8 @@ } else { - Json::StyledWriter writer; - std::string s = writer.write(value); - + std::string s; + Toolbox::WriteJson(s, value, false /* styled, not fast */); output_.SetContentType(MIME_JSON_UTF8); output_.Answer(s); } @@ -122,9 +121,7 @@ contentType == MimeType_Json) { Json::Value json; - Json::Reader reader; - if (reader.parse(reinterpret_cast(buffer), - reinterpret_cast(buffer) + length, json)) + if (Toolbox::ReadJson(json, buffer, length)) { AnswerJson(json); } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/RestApi/RestApiOutput.h --- a/OrthancFramework/Sources/RestApi/RestApiOutput.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/RestApi/RestApiOutput.h Mon Dec 21 18:09:47 2020 +0100 @@ -25,7 +25,7 @@ #include "../HttpServer/HttpOutput.h" #include "../HttpServer/HttpFileSender.h" -#include +#include namespace Orthanc { diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/RestApi/RestApiPostCall.h --- a/OrthancFramework/Sources/RestApi/RestApiPostCall.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/RestApi/RestApiPostCall.h Mon Dec 21 18:09:47 2020 +0100 @@ -70,7 +70,7 @@ virtual bool ParseJsonRequest(Json::Value& result) const ORTHANC_OVERRIDE { - return ParseJsonRequestInternal(result, reinterpret_cast(bodyData_), bodySize_); + return Toolbox::ReadJson(result, bodyData_, bodySize_); } }; } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/RestApi/RestApiPutCall.h --- a/OrthancFramework/Sources/RestApi/RestApiPutCall.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/RestApi/RestApiPutCall.h Mon Dec 21 18:09:47 2020 +0100 @@ -70,7 +70,7 @@ virtual bool ParseJsonRequest(Json::Value& result) const ORTHANC_OVERRIDE { - return ParseJsonRequestInternal(result, reinterpret_cast(bodyData_), bodySize_); + return Toolbox::ReadJson(result, bodyData_, bodySize_); } }; } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/Toolbox.cpp --- a/OrthancFramework/Sources/Toolbox.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/Toolbox.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -27,6 +27,8 @@ #include "OrthancException.h" #include "Logging.h" +#include + #include #include #include @@ -2269,6 +2271,41 @@ } } } + + + bool Toolbox::ReadJson(Json::Value& target, + const std::string& source) + { + Json::Reader reader; + return reader.parse(source, target); + } + + + bool Toolbox::ReadJson(Json::Value& target, + const void* buffer, + size_t size) + { + Json::Reader reader; + return reader.parse(reinterpret_cast(buffer), + reinterpret_cast(buffer) + size, target); + } + + + void Toolbox::WriteJson(std::string& target, + const Json::Value& source, + bool fast) + { + if (fast) + { + Json::FastWriter writer; + target = writer.write(source); + } + else + { + Json::StyledWriter writer; + target = writer.write(source); + } + } } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/Toolbox.h --- a/OrthancFramework/Sources/Toolbox.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/Toolbox.h Mon Dec 21 18:09:47 2020 +0100 @@ -28,7 +28,7 @@ #include #include #include -#include +#include #if !defined(ORTHANC_ENABLE_BASE64) @@ -260,6 +260,17 @@ static void SimplifyDicomAsJson(Json::Value& target, const Json::Value& source, DicomToJsonFormat format); + + static bool ReadJson(Json::Value& target, + const std::string& source); + + static bool ReadJson(Json::Value& target, + const void* buffer, + size_t size); + + static void WriteJson(std::string& target, + const Json::Value& source, + bool fast); }; } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/Sources/WebServiceParameters.h --- a/OrthancFramework/Sources/WebServiceParameters.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/Sources/WebServiceParameters.h Mon Dec 21 18:09:47 2020 +0100 @@ -31,7 +31,7 @@ #include #include #include -#include +#include namespace Orthanc { diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/UnitTestsSources/LuaTests.cpp --- a/OrthancFramework/UnitTestsSources/LuaTests.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/UnitTestsSources/LuaTests.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -175,9 +175,8 @@ std::string s; f.ExecuteToString(s); - Json::FastWriter writer; - std::string t = writer.write(a); - + std::string t; + Orthanc::Toolbox::WriteJson(t, a, true /* fast */); ASSERT_EQ(s, t); } } diff -r 0c4ff5609548 -r 3af1d763763a OrthancFramework/UnitTestsSources/RestApiTests.cpp --- a/OrthancFramework/UnitTestsSources/RestApiTests.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancFramework/UnitTestsSources/RestApiTests.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -341,7 +341,9 @@ Json::Value m; root.CreateSiteMap(m); - std::cout << m; + + std::string s; + Toolbox::WriteJson(s, m, false /* styled, not fast */); Json::Value d; ASSERT_FALSE(GetDirectory(d, root, "/hello")); diff -r 0c4ff5609548 -r 3af1d763763a OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -2670,18 +2670,17 @@ case _OrthancPluginService_GetInstanceJson: case _OrthancPluginService_GetInstanceSimplifiedJson: { - Json::StyledWriter writer; std::string s; if (service == _OrthancPluginService_GetInstanceJson) { - s = writer.write(instance.GetJson()); + Toolbox::WriteJson(s, instance.GetJson(), false /* styled writer */); } else { Json::Value simplified; Toolbox::SimplifyDicomAsJson(simplified, instance.GetJson(), DicomToJsonFormat_Human); - s = writer.write(simplified); + Toolbox::WriteJson(s, simplified, false /* styled writer */); } *p.resultStringToFree = CopyString(s); @@ -2882,8 +2881,9 @@ json, Plugins::Convert(p.format), static_cast(p.flags), p.maxStringLength); - Json::FastWriter writer; - *p.targetStringToFree = CopyString(writer.write(json)); + std::string s; + Toolbox::WriteJson(s, json, true /* fast */); + *p.targetStringToFree = CopyString(s); return; } @@ -3379,8 +3379,9 @@ dicom->DatasetToJson(json, Plugins::Convert(p.format), static_cast(p.flags), p.maxStringLength); - Json::FastWriter writer; - *p.result = CopyString(writer.write(json)); + std::string s; + Toolbox::WriteJson(s, json, true /* fast */); + *p.result = CopyString(s); } @@ -3396,13 +3397,9 @@ { json = Json::objectValue; } - else - { - Json::Reader reader; - if (!reader.parse(p.json, json)) - { - throw OrthancException(ErrorCode_BadJson); - } + else if (!Toolbox::ReadJson(json, p.json)) + { + throw OrthancException(ErrorCode_BadJson); } std::string dicom; diff -r 0c4ff5609548 -r 3af1d763763a OrthancServer/Plugins/Engine/PluginsJob.cpp --- a/OrthancServer/Plugins/Engine/PluginsJob.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancServer/Plugins/Engine/PluginsJob.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -41,8 +41,8 @@ #include "../../../OrthancFramework/Sources/Logging.h" #include "../../../OrthancFramework/Sources/OrthancException.h" +#include "../../../OrthancFramework/Sources/Toolbox.h" -#include #include namespace Orthanc @@ -143,9 +143,7 @@ } else { - Json::Reader reader; - - if (!reader.parse(content, value) || + if (!Toolbox::ReadJson(value, content) || value.type() != Json::objectValue) { throw OrthancException(ErrorCode_Plugin, @@ -164,9 +162,7 @@ } else { - Json::Reader reader; - - if (!reader.parse(serialized, value) || + if (!Toolbox::ReadJson(value, serialized) || value.type() != Json::objectValue) { throw OrthancException(ErrorCode_Plugin, diff -r 0c4ff5609548 -r 3af1d763763a OrthancServer/Sources/OrthancConfiguration.cpp --- a/OrthancServer/Sources/OrthancConfiguration.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancServer/Sources/OrthancConfiguration.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -70,8 +70,7 @@ content = Toolbox::SubstituteVariables(content, env); Json::Value tmp; - Json::Reader reader; - if (!reader.parse(content, tmp) || + if (!Toolbox::ReadJson(tmp, content) || tmp.type() != Json::objectValue) { throw OrthancException(ErrorCode_BadJson, @@ -254,9 +253,8 @@ { std::string property = serverIndex_->GetGlobalProperty(GlobalProperty_Modalities, "{}"); - Json::Reader reader; Json::Value modalities; - if (reader.parse(property, modalities)) + if (Toolbox::ReadJson(modalities, property)) { LoadModalitiesFromJson(modalities); } @@ -294,9 +292,8 @@ { std::string property = serverIndex_->GetGlobalProperty(GlobalProperty_Peers, "{}"); - Json::Reader reader; Json::Value peers; - if (reader.parse(property, peers)) + if (Toolbox::ReadJson(peers, property)) { LoadPeersFromJson(peers); } @@ -366,9 +363,9 @@ Json::Value modalities; SaveModalitiesToJson(modalities); - Json::FastWriter writer; - std::string s = writer.write(modalities); - + std::string s; + Toolbox::WriteJson(s, modalities, true /* fast */); + serverIndex_->SetGlobalProperty(GlobalProperty_Modalities, s); } } @@ -398,8 +395,8 @@ Json::Value peers; SavePeersToJson(peers); - Json::FastWriter writer; - std::string s = writer.write(peers); + std::string s; + Toolbox::WriteJson(s, peers, true /* fast */); serverIndex_->SetGlobalProperty(GlobalProperty_Peers, s); } @@ -872,8 +869,7 @@ void OrthancConfiguration::Format(std::string& result) const { - Json::StyledWriter w; - result = w.write(json_); + Toolbox::WriteJson(result, json_, false /* styled, not fast */); } @@ -892,9 +888,9 @@ Json::Value current; ReadConfiguration(current, configurationFileArg_); - Json::FastWriter writer; - std::string a = writer.write(json_); - std::string b = writer.write(current); + std::string a, b; + Toolbox::WriteJson(a, json_, true /* fast */); + Toolbox::WriteJson(b, current, true /* fast */); return a != b; } diff -r 0c4ff5609548 -r 3af1d763763a OrthancServer/Sources/ServerContext.cpp --- a/OrthancServer/Sources/ServerContext.cpp Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancServer/Sources/ServerContext.cpp Mon Dec 21 18:09:47 2020 +0100 @@ -259,8 +259,8 @@ Json::Value value; jobsEngine_.GetRegistry().Serialize(value); - Json::FastWriter writer; - std::string serialized = writer.write(value); + std::string serialized; + Toolbox::WriteJson(serialized, value, true /* fast */); index_.SetGlobalProperty(GlobalProperty_JobsRegistry, serialized); } @@ -822,8 +822,7 @@ std::string tmp; ReadDicomAsJsonInternal(tmp, instancePublicId); - Json::Reader reader; - if (!reader.parse(tmp, result)) + if (!Toolbox::ReadJson(result, tmp)) { throw OrthancException(ErrorCode_CorruptedFile); } diff -r 0c4ff5609548 -r 3af1d763763a OrthancServer/Sources/ServerToolbox.h --- a/OrthancServer/Sources/ServerToolbox.h Mon Dec 21 08:47:29 2020 +0100 +++ b/OrthancServer/Sources/ServerToolbox.h Mon Dec 21 18:09:47 2020 +0100 @@ -35,7 +35,6 @@ #include "ServerEnumerations.h" -#include #include #include