comparison Core/Lua/LuaContext.cpp @ 1658:54bafe0e7e7b

Optional argument "keepStrings" in "DumpJson()"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Sep 2015 09:44:38 +0200
parents 31f4adefb88f
children b1291df2f780
comparison
equal deleted inserted replaced
1657:5360cdba70d8 1658:54bafe0e7e7b
46 #include <lauxlib.h> 46 #include <lauxlib.h>
47 } 47 }
48 48
49 namespace Orthanc 49 namespace Orthanc
50 { 50 {
51 static bool OnlyContainsDigits(const std::string& s)
52 {
53 for (size_t i = 0; i < s.size(); i++)
54 {
55 if (!isdigit(s[i]))
56 {
57 return false;
58 }
59 }
60
61 return true;
62 }
63
64
51 LuaContext& LuaContext::GetLuaContext(lua_State *state) 65 LuaContext& LuaContext::GetLuaContext(lua_State *state)
52 { 66 {
53 const void* value = GetGlobalVariable(state, "_LuaContext"); 67 const void* value = GetGlobalVariable(state, "_LuaContext");
54 assert(value != NULL); 68 assert(value != NULL);
55 69
126 int LuaContext::DumpJson(lua_State *state) 140 int LuaContext::DumpJson(lua_State *state)
127 { 141 {
128 LuaContext& that = GetLuaContext(state); 142 LuaContext& that = GetLuaContext(state);
129 143
130 int nArgs = lua_gettop(state); 144 int nArgs = lua_gettop(state);
131 if (nArgs != 1) 145 if ((nArgs != 1 && nArgs != 2) ||
146 (nArgs == 2 && !lua_isboolean(state, 2)))
132 { 147 {
133 lua_pushnil(state); 148 lua_pushnil(state);
134 return 1; 149 return 1;
135 } 150 }
136 151
152 bool keepStrings = false;
153 if (nArgs == 2)
154 {
155 keepStrings = lua_toboolean(state, 2) ? true : false;
156 }
157
137 Json::Value json; 158 Json::Value json;
138 that.GetJson(json, 1); 159 that.GetJson(json, 1, keepStrings);
139 160
140 Json::FastWriter writer; 161 Json::FastWriter writer;
141 std::string s = writer.write(json); 162 std::string s = writer.write(json);
142 lua_pushlstring(state, s.c_str(), s.size()); 163 lua_pushlstring(state, s.c_str(), s.size());
143 164
374 } 395 }
375 } 396 }
376 397
377 398
378 void LuaContext::GetJson(Json::Value& result, 399 void LuaContext::GetJson(Json::Value& result,
379 int top) 400 int top,
401 bool keepStrings)
380 { 402 {
381 if (lua_istable(lua_, top)) 403 if (lua_istable(lua_, top))
382 { 404 {
383 Json::Value tmp = Json::objectValue; 405 Json::Value tmp = Json::objectValue;
384 bool isArray = true; 406 bool isArray = true;
399 // copy the key so that lua_tostring does not modify the original 421 // copy the key so that lua_tostring does not modify the original
400 lua_pushvalue(lua_, -2); 422 lua_pushvalue(lua_, -2);
401 // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table 423 // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
402 std::string key(lua_tostring(lua_, -1)); 424 std::string key(lua_tostring(lua_, -1));
403 Json::Value v; 425 Json::Value v;
404 GetJson(v, -2); 426 GetJson(v, -2, keepStrings);
405 427
406 tmp[key] = v; 428 tmp[key] = v;
407 429
408 size += 1; 430 size += 1;
409 try 431 try
410 { 432 {
411 if (boost::lexical_cast<size_t>(key) != size) 433 if (!OnlyContainsDigits(key) ||
434 boost::lexical_cast<size_t>(key) != size)
412 { 435 {
413 isArray = false; 436 isArray = false;
414 } 437 }
415 } 438 }
416 catch (boost::bad_lexical_cast&) 439 catch (boost::bad_lexical_cast&)
444 } 467 }
445 else if (lua_isnil(lua_, top)) 468 else if (lua_isnil(lua_, top))
446 { 469 {
447 result = Json::nullValue; 470 result = Json::nullValue;
448 } 471 }
449 else if (lua_isboolean(lua_, top)) 472 else if (!keepStrings &&
473 lua_isboolean(lua_, top))
450 { 474 {
451 result = lua_toboolean(lua_, top) ? true : false; 475 result = lua_toboolean(lua_, top) ? true : false;
452 } 476 }
453 else if (lua_isnumber(lua_, top)) 477 else if (!keepStrings &&
478 lua_isnumber(lua_, top))
454 { 479 {
455 // Convert to "int" if truncation does not loose precision 480 // Convert to "int" if truncation does not loose precision
456 double value = static_cast<double>(lua_tonumber(lua_, top)); 481 double value = static_cast<double>(lua_tonumber(lua_, top));
457 int truncated = static_cast<int>(value); 482 int truncated = static_cast<int>(value);
458 483