changeset 3444:6fe42a335a80

WebServiceParameters::GetBooleanUserProperty()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 24 Jun 2019 17:15:03 +0200
parents 0371b65d5f76
children 4944b03bb9c6
files Core/WebServiceParameters.cpp Core/WebServiceParameters.h
diffstat 2 files changed, 46 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/Core/WebServiceParameters.cpp	Mon Jun 24 16:55:28 2019 +0200
+++ b/Core/WebServiceParameters.cpp	Mon Jun 24 17:15:03 2019 +0200
@@ -289,13 +289,22 @@
     {
       if (!IsReservedKey(*it))
       {
-        if (peer[*it].type() != Json::stringValue)
+        switch (peer[*it].type())
         {
-          throw OrthancException(ErrorCode_BadFileFormat);
-        }
-        else
-        {
-          userProperties_[*it] = peer[*it].asString();
+          case Json::stringValue:
+            userProperties_[*it] = peer[*it].asString();
+            break;
+
+          case Json::booleanValue:
+            userProperties_[*it] = peer[*it].asBool() ? "1" : "0";
+            break;
+
+          case Json::intValue:
+            userProperties_[*it] = boost::lexical_cast<std::string>(peer[*it].asInt());
+            break;
+
+          default:
+            throw OrthancException(ErrorCode_BadFileFormat);
         }
       }
     }
@@ -402,6 +411,33 @@
       return true;
     }
   }
+  
+
+  bool WebServiceParameters::GetBooleanUserProperty(const std::string& key,
+                                                    bool defaultValue) const
+  {
+    Dictionary::const_iterator found = userProperties_.find(key);
+
+    if (found == userProperties_.end())
+    {
+      return defaultValue;
+    }
+    else if (found->second == "0" ||
+             found->second == "false")
+    {
+      return false;
+    }
+    else if (found->second == "1" ||
+             found->second == "true")
+    {
+      return true;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadFileFormat, "Bad value for a Boolean user property in the parameters "
+                             "of a Web service: Property \"" + key + "\" equals: " + found->second);
+    }    
+  }
 
 
   bool WebServiceParameters::IsAdvancedFormatNeeded() const
--- a/Core/WebServiceParameters.h	Mon Jun 24 16:55:28 2019 +0200
+++ b/Core/WebServiceParameters.h	Mon Jun 24 17:15:03 2019 +0200
@@ -162,7 +162,10 @@
     void ListUserProperties(std::set<std::string>& target) const; 
 
     bool LookupUserProperty(std::string& value,
-                            const std::string& key) const; 
+                            const std::string& key) const;
+
+    bool GetBooleanUserProperty(const std::string& key,
+                                bool defaultValue) const;
 
     bool IsAdvancedFormatNeeded() const;