diff Applications/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 1771:f302bbddf94d

sync, trying to fix DicomVolumeImageReslicer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 May 2021 15:09:32 +0200
parents 9ac2a65d4172
children 87de32ac3ea7
line wrap: on
line diff
--- a/Applications/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp	Wed May 12 10:53:37 2021 +0200
+++ b/Applications/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp	Wed May 12 15:09:32 2021 +0200
@@ -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<const char*>(buffer),
+                        reinterpret_cast<const char*>(buffer) + size, target, collectComments);
+#else
+    Json::CharReaderBuilder builder;
+    builder.settings_["collectComments"] = collectComments;
+    
+    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
+    assert(reader.get() != NULL);
+    
+    JSONCPP_STRING err;
+    if (reader->parse(reinterpret_cast<const char*>(buffer),
+                      reinterpret_cast<const char*>(buffer) + size, &target, &err))
+    {
+      return true;
+    }
+    else
+    {
+      LogError("Cannot parse JSON: " + std::string(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<const char*>(buffer),
-                        reinterpret_cast<const char*>(buffer) + size, target);
-#else
-    Json::CharReaderBuilder builder;
-    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
-    assert(reader.get() != NULL);
-    JSONCPP_STRING err;
-    if (reader->parse(reinterpret_cast<const char*>(buffer),
-                      reinterpret_cast<const char*>(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)
   {
@@ -2699,10 +2726,18 @@
           delete *it;
         }
 
+        size_ = 0;
         content_.clear();
       }
 
-      void Flatten(std::string& target) const
+      /**
+       * Since Orthanc 1.9.3, this function also clears the content of
+       * the ChunkedBuffer in order to mimic the behavior of the
+       * original class "Orthanc::ChunkedBuffer". This prevents the
+       * forgetting of calling "Clear()" in order to reduce memory
+       * consumption.
+       **/
+      void Flatten(std::string& target)
       {
         target.resize(size_);
 
@@ -2718,10 +2753,14 @@
             memcpy(&target[pos], (*it)->c_str(), s);
             pos += s;
           }
+
+          delete *it;
         }
 
-        assert(size_ == 0 ||
-               pos == target.size());
+        assert(pos == target.size());
+
+        size_ = 0;
+        content_.clear();
       }
 
       void AddChunk(const void* data,
@@ -2752,7 +2791,7 @@
         return headers_;
       }
 
-      const ChunkedBuffer& GetBody() const
+      ChunkedBuffer& GetBody()
       {
         return body_;
       }