changeset 4392:3af1d763763a

confining Json::Reader and Json::*Writer into Toolbox
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 21 Dec 2020 18:09:47 +0100
parents 0c4ff5609548
children e8e95b80194f
files OrthancFramework/Sources/DicomFormat/DicomMap.h OrthancFramework/Sources/DicomNetworking/IStoreRequestHandler.h OrthancFramework/Sources/DicomNetworking/RemoteModalityParameters.h OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h OrthancFramework/Sources/HttpClient.cpp OrthancFramework/Sources/HttpClient.h OrthancFramework/Sources/Images/Font.cpp OrthancFramework/Sources/JobsEngine/JobsEngine.cpp OrthancFramework/Sources/Lua/LuaContext.cpp OrthancFramework/Sources/Lua/LuaFunctionCall.h OrthancFramework/Sources/RestApi/RestApiCall.cpp OrthancFramework/Sources/RestApi/RestApiCall.h OrthancFramework/Sources/RestApi/RestApiOutput.cpp OrthancFramework/Sources/RestApi/RestApiOutput.h OrthancFramework/Sources/RestApi/RestApiPostCall.h OrthancFramework/Sources/RestApi/RestApiPutCall.h OrthancFramework/Sources/Toolbox.cpp OrthancFramework/Sources/Toolbox.h OrthancFramework/Sources/WebServiceParameters.h OrthancFramework/UnitTestsSources/LuaTests.cpp OrthancFramework/UnitTestsSources/RestApiTests.cpp OrthancServer/Plugins/Engine/OrthancPlugins.cpp OrthancServer/Plugins/Engine/PluginsJob.cpp OrthancServer/Sources/OrthancConfiguration.cpp OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerToolbox.h
diffstat 26 files changed, 103 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- 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 <set>
 #include <map>
-#include <json/json.h>
+#include <json/value.h>
 
 namespace Orthanc
 {
--- 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 <vector>
 #include <string>
-#include <json/json.h>
+#include <json/value.h>
 
 namespace Orthanc
 {
--- 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 <stdint.h>
 #include <string>
-#include <json/json.h>
+#include <json/value.h>
 
 namespace Orthanc
 {
--- 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 <dcmtk/dcmdata/dcmetinf.h>
 #include <dcmtk/dcmdata/dcpixseq.h>
 #include <dcmtk/dcmdata/dcfilefo.h>
-#include <json/json.h>
+#include <json/value.h>
 
 #if ORTHANC_ENABLE_DCMTK != 1
 #  error The macro ORTHANC_ENABLE_DCMTK must be set to 1
--- 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
     {
--- 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 <string>
 #include <boost/noncopyable.hpp>
 #include <boost/shared_ptr.hpp>
-#include <json/json.h>
+#include <json/value.h>
 
 #if !defined(ORTHANC_ENABLE_CURL)
 #  error The macro ORTHANC_ENABLE_CURL must be defined
--- 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") ||
--- 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 <json/reader.h>
 
 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);
     }
--- 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);
     }
--- 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 <json/json.h>
-
 namespace Orthanc
 {
   class ORTHANC_PUBLIC LuaFunctionCall : public boost::noncopyable
--- 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<const char*>(body),
-                        reinterpret_cast<const char*>(body) + size, result);
-  }
-
-
   std::string RestApiCall::FlattenUri() const
   {
     std::string s = "/";
--- 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,
--- 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<const char*>(buffer),
-                       reinterpret_cast<const char*>(buffer) + length, json))
+      if (Toolbox::ReadJson(json, buffer, length))
       {
         AnswerJson(json);
       }
--- 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 <json/json.h>
+#include <json/value.h>
 
 namespace Orthanc
 {
--- 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<const char*>(bodyData_), bodySize_);
+      return Toolbox::ReadJson(result, bodyData_, bodySize_);
     }
   };
 }
--- 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<const char*>(bodyData_), bodySize_);
+      return Toolbox::ReadJson(result, bodyData_, bodySize_);
     }      
   };
 }
--- 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 <json/json.h>
+
 #include <boost/algorithm/string/case_conv.hpp>
 #include <boost/algorithm/string/replace.hpp>
 #include <boost/lexical_cast.hpp>
@@ -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<const char*>(buffer),
+                        reinterpret_cast<const char*>(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);
+    }
+  }
 }
 
 
--- 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 <stdint.h>
 #include <vector>
 #include <string>
-#include <json/json.h>
+#include <json/value.h>
 
 
 #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);
   };
 }
 
--- 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 <map>
 #include <set>
 #include <string>
-#include <json/json.h>
+#include <json/value.h>
 
 namespace Orthanc
 {
--- 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);
   }
 }
--- 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"));
--- 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<DicomToJsonFlags>(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<DicomToJsonFlags>(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;
--- 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 <json/reader.h>
 #include <cassert>
 
 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,
--- 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;
   }
--- 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);
       }
--- 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 <json/json.h>
 #include <boost/noncopyable.hpp>
 #include <list>