//***************************************************************************** void LuaUtils::PushJson(lua_State *L, const JsonNode *node) { if (!node) { lua_pushnil(L); return ; } switch (node->Type()) { case JNT_NULL: lua_pushnil(L); break; case JNT_BOOL: lua_pushboolean(L, node->Value<bool>()); break; case JNT_INT: lua_pushinteger(L, node->Value<int>()); break; case JNT_DOUBLE: lua_pushnumber(L, node->Value<double>()); break; case JNT_STRING: lua_pushstring(L, node->Value<std::string>().c_str()); break; case JNT_LIST: lua_newtable(L); for (unsigned i = 0, size = node->Size();i < size;++i) { lua_pushinteger(L, i + 1); PushJson(L, node->Get(i)); lua_settable(L, -3); } break; case JNT_OBJECT: const JsonObjectNode *objNode = static_cast<const JsonObjectNode *>(node); lua_newtable(L); for (JsonObjectNode::const_iterator iter = objNode->begin(); iter != objNode->end(); ++iter) { PushJson(L, iter->second); lua_setfield(L, -2, iter->first.c_str()); } break; } }
void LuaContext::PushJson(const Json::Value& value) { if (value.isString()) { const std::string s = value.asString(); lua_pushlstring(lua_, s.c_str(), s.size()); } else if (value.isDouble()) { lua_pushnumber(lua_, value.asDouble()); } else if (value.isInt()) { lua_pushinteger(lua_, value.asInt()); } else if (value.isUInt()) { lua_pushinteger(lua_, value.asUInt()); } else if (value.isBool()) { lua_pushboolean(lua_, value.asBool()); } else if (value.isNull()) { lua_pushnil(lua_); } else if (value.isArray()) { lua_newtable(lua_); // http://lua-users.org/wiki/SimpleLuaApiExample for (Json::Value::ArrayIndex i = 0; i < value.size(); i++) { // Push the table index (note the "+1" because of Lua conventions) lua_pushnumber(lua_, i + 1); // Push the value of the cell PushJson(value[i]); // Stores the pair in the table lua_rawset(lua_, -3); } } else if (value.isObject()) { lua_newtable(lua_); Json::Value::Members members = value.getMemberNames(); for (Json::Value::Members::const_iterator it = members.begin(); it != members.end(); ++it) { // Push the index of the cell lua_pushlstring(lua_, it->c_str(), it->size()); // Push the value of the cell PushJson(value[*it]); // Stores the pair in the table lua_rawset(lua_, -3); } } else { throw OrthancException(ErrorCode_JsonToLuaTable); } }
//***************************************************************************** void LuaUtils::PushJson(lua_State *L, const JsonNodePtr &node) { PushJson(L, node.Data()); }