changeset 1658:54bafe0e7e7b

Optional argument "keepStrings" in "DumpJson()"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Sep 2015 09:44:38 +0200
parents 5360cdba70d8
children 87a606265de8
files Core/Lua/LuaContext.cpp Core/Lua/LuaContext.h Core/Lua/LuaFunctionCall.cpp Core/Lua/LuaFunctionCall.h NEWS OrthancServer/LuaScripting.cpp
diffstat 6 files changed, 84 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Lua/LuaContext.cpp	Tue Sep 29 16:31:48 2015 +0200
+++ b/Core/Lua/LuaContext.cpp	Wed Sep 30 09:44:38 2015 +0200
@@ -48,6 +48,20 @@
 
 namespace Orthanc
 {
+  static bool OnlyContainsDigits(const std::string& s)
+  {
+    for (size_t i = 0; i < s.size(); i++)
+    {
+      if (!isdigit(s[i]))
+      {
+        return false;
+      }
+    }
+
+    return true;
+  }
+  
+
   LuaContext& LuaContext::GetLuaContext(lua_State *state)
   {
     const void* value = GetGlobalVariable(state, "_LuaContext");
@@ -128,14 +142,21 @@
     LuaContext& that = GetLuaContext(state);
 
     int nArgs = lua_gettop(state);
-    if (nArgs != 1)
+    if ((nArgs != 1 && nArgs != 2) ||
+        (nArgs == 2 && !lua_isboolean(state, 2)))
     {
       lua_pushnil(state);
       return 1;
     }
 
+    bool keepStrings = false;
+    if (nArgs == 2)
+    {
+      keepStrings = lua_toboolean(state, 2) ? true : false;
+    }
+
     Json::Value json;
-    that.GetJson(json, 1);
+    that.GetJson(json, 1, keepStrings);
 
     Json::FastWriter writer;
     std::string s = writer.write(json);
@@ -376,7 +397,8 @@
 
 
   void LuaContext::GetJson(Json::Value& result,
-                           int top)
+                           int top,
+                           bool keepStrings)
   {
     if (lua_istable(lua_, top))
     {
@@ -401,14 +423,15 @@
         // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
         std::string key(lua_tostring(lua_, -1));
         Json::Value v;
-        GetJson(v, -2);
+        GetJson(v, -2, keepStrings);
 
         tmp[key] = v;
 
         size += 1;
         try
         {
-          if (boost::lexical_cast<size_t>(key) != size)
+          if (!OnlyContainsDigits(key) ||
+              boost::lexical_cast<size_t>(key) != size)
           {
             isArray = false;
           }
@@ -446,11 +469,13 @@
     {
       result = Json::nullValue;
     }
-    else if (lua_isboolean(lua_, top))
+    else if (!keepStrings &&
+             lua_isboolean(lua_, top))
     {
       result = lua_toboolean(lua_, top) ? true : false;
     }
-    else if (lua_isnumber(lua_, top))
+    else if (!keepStrings &&
+             lua_isnumber(lua_, top))
     {
       // Convert to "int" if truncation does not loose precision
       double value = static_cast<double>(lua_tonumber(lua_, top));
--- a/Core/Lua/LuaContext.h	Tue Sep 29 16:31:48 2015 +0200
+++ b/Core/Lua/LuaContext.h	Wed Sep 30 09:44:38 2015 +0200
@@ -72,7 +72,8 @@
                          const std::string& command);
 
     void GetJson(Json::Value& result,
-                 int top);
+                 int top,
+                 bool keepStrings);
     
   public:
     LuaContext();
--- a/Core/Lua/LuaFunctionCall.cpp	Tue Sep 29 16:31:48 2015 +0200
+++ b/Core/Lua/LuaFunctionCall.cpp	Wed Sep 30 09:44:38 2015 +0200
@@ -130,10 +130,11 @@
   }
 
 
-  void LuaFunctionCall::ExecuteToJson(Json::Value& result)
+  void LuaFunctionCall::ExecuteToJson(Json::Value& result,
+                                      bool keepStrings)
   {
     ExecuteInternal(1);
-    context_.GetJson(result, lua_gettop(context_.lua_));
+    context_.GetJson(result, lua_gettop(context_.lua_), keepStrings);
   }
 
 
--- a/Core/Lua/LuaFunctionCall.h	Tue Sep 29 16:31:48 2015 +0200
+++ b/Core/Lua/LuaFunctionCall.h	Wed Sep 30 09:44:38 2015 +0200
@@ -69,7 +69,8 @@
 
     bool ExecutePredicate();
 
-    void ExecuteToJson(Json::Value& result);                    
+    void ExecuteToJson(Json::Value& result,
+                       bool keepStrings);
 
     void ExecuteToString(std::string& result);
   };
--- a/NEWS	Tue Sep 29 16:31:48 2015 +0200
+++ b/NEWS	Wed Sep 30 09:44:38 2015 +0200
@@ -10,6 +10,11 @@
 * New function "OrthancPluginRegisterErrorCode()" to declare custom error codes
 * New function "OrthancPluginRegisterDictionaryTag()" to declare DICOM tags
 
+Lua
+---
+
+* Optional argument "keepStrings" in "DumpJson()"
+
 Maintenance
 -----------
 
--- a/OrthancServer/LuaScripting.cpp	Tue Sep 29 16:31:48 2015 +0200
+++ b/OrthancServer/LuaScripting.cpp	Wed Sep 30 09:44:38 2015 +0200
@@ -83,18 +83,23 @@
     const char* uri = lua_tostring(state, 1);
     bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
 
-    std::string result;
-    if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                               RequestOrigin_Lua, uri))
+    try
     {
-      lua_pushlstring(state, result.c_str(), result.size());
+      std::string result;
+      if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                 RequestOrigin_Lua, uri))
+      {
+        lua_pushlstring(state, result.c_str(), result.size());
+        return 1;
+      }
     }
-    else
+    catch (OrthancException& e)
     {
-      LOG(ERROR) << "Lua: Error in RestApiGet() for URI: " << uri;
-      lua_pushnil(state);
+      LOG(ERROR) << "Lua: " << e.What();
     }
 
+    LOG(ERROR) << "Lua: Error in RestApiGet() for URI: " << uri;
+    lua_pushnil(state);
     return 1;
   }
 
@@ -127,21 +132,26 @@
     const char* bodyData = lua_tolstring(state, 2, &bodySize);
     bool builtin = (nArgs == 3 ? lua_toboolean(state, 3) != 0 : false);
 
-    std::string result;
-    if (isPost ?
-        HttpToolbox::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                                RequestOrigin_Lua, uri, bodyData, bodySize) :
-        HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                               RequestOrigin_Lua, uri, bodyData, bodySize))
+    try
     {
-      lua_pushlstring(state, result.c_str(), result.size());
+      std::string result;
+      if (isPost ?
+          HttpToolbox::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                  RequestOrigin_Lua, uri, bodyData, bodySize) :
+          HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                 RequestOrigin_Lua, uri, bodyData, bodySize))
+      {
+        lua_pushlstring(state, result.c_str(), result.size());
+        return 1;
+      }
     }
-    else
+    catch (OrthancException& e)
     {
-      LOG(ERROR) << "Lua: Error in " << (isPost ? "RestApiPost()" : "RestApiPut()") << " for URI: " << uri;
-      lua_pushnil(state);
+      LOG(ERROR) << "Lua: " << e.What();
     }
 
+    LOG(ERROR) << "Lua: Error in " << (isPost ? "RestApiPost()" : "RestApiPut()") << " for URI: " << uri;
+    lua_pushnil(state);
     return 1;
   }
 
@@ -185,16 +195,22 @@
     const char* uri = lua_tostring(state, 1);
     bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
 
-    if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                                  RequestOrigin_Lua, uri))
+    try
     {
-      lua_pushboolean(state, 1);
+      if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                    RequestOrigin_Lua, uri))
+      {
+        lua_pushboolean(state, 1);
+        return 1;
+      }
     }
-    else
+    catch (OrthancException& e)
     {
-      LOG(ERROR) << "Lua: Error in RestApiDelete() for URI: " << uri;
+      LOG(ERROR) << "Lua: " << e.What();
+    }
+
+    LOG(ERROR) << "Lua: Error in RestApiDelete() for URI: " << uri;
       lua_pushnil(state);
-    }
 
     return 1;
   }
@@ -316,7 +332,7 @@
   {
     Json::Value operations;
     LuaFunctionCall call2(lua_, "_AccessJob");
-    call2.ExecuteToJson(operations);
+    call2.ExecuteToJson(operations, false);
      
     if (operations.type() != Json::arrayValue)
     {