10 #include "tolua++/include/tolua++.h" 11 #include "json/json.h" 29 public std::runtime_error
31 typedef std::runtime_error
Super;
50 Super(a_Parent.what()),
58 Super(a_Parent.what()),
78 lua_createtable(a_LuaState, static_cast<int>(a_Value.size()), 0);
82 for (
const auto & v: a_Value)
86 lua_rawseti(a_LuaState, -2, idx);
100 lua_createtable(a_LuaState, 0, static_cast<int>(a_Value.size()));
104 auto names = a_Value.getMemberNames();
105 for (
const auto & n: names)
117 lua_rawset(a_LuaState, -3);
128 switch (a_Value.type())
130 case Json::nullValue:
137 case Json::uintValue:
138 case Json::realValue:
140 a_LuaState.
Push(static_cast<lua_Number>(a_Value.asDouble()));
144 case Json::booleanValue:
146 a_LuaState.
Push(a_Value.asBool());
150 case Json::stringValue:
152 a_LuaState.
Push(a_Value.asString());
156 case Json::arrayValue:
162 case Json::objectValue:
179 lua_pushnil(a_LuaState);
180 while (lua_next(a_LuaState, -2) != 0)
182 if (lua_type(a_LuaState, -2) == LUA_TNUMBER)
194 catch (
const std::exception & exc)
212 catch (
const std::exception & exc)
218 lua_pop(a_LuaState, 1);
230 switch (lua_type(a_LuaState, -1))
236 return Json::Value(v);
240 return Json::Value(Json::nullValue);
246 return Json::Value(v);
252 return Json::Value(v);
260 LOGD(
"Attempting to serialize an unhandled Lua value type: %d", lua_type(a_LuaState, -1));
261 return Json::Value(Json::nullValue);
290 LOGWARNING(
"cJson:Parse(): Cannot read input string");
298 if (!reader.parse(input, root,
false))
339 L.
Push(
Printf(
"Cannot serialize into Json, value \"%s\" caused an error \"%s\"", exc.
GetValueName().c_str(), exc.what()));
345 Json::StreamWriterBuilder builder;
346 if (lua_istable(L, 3))
349 while (lua_next(L, -2) != 0)
351 if (lua_type(L, -2) == LUA_TSTRING)
356 builder[propName] = propValue;
363 if (!builder.validate(&invalid))
365 LOGINFO(
"cJson:Serialize(): detected invalid settings:");
366 for (
const auto & n: invalid.getMemberNames())
368 LOGINFO(
" \"%s\" (\"%s\")", n.c_str(), invalid[n].asCString());
372 auto writer(builder.newStreamWriter());
375 std::stringstream ss;
376 writer->write(root, &ss);
387 tolua_beginmodule(a_LuaState,
nullptr);
390 tolua_usertype(a_LuaState,
"cJson");
391 tolua_cclass(a_LuaState,
"cJson",
"cJson",
"",
nullptr);
394 tolua_beginmodule(a_LuaState,
"cJson");
397 tolua_endmodule(a_LuaState);
398 tolua_endmodule(a_LuaState);
bool CheckParamString(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are strings; also logs warning if not...
bool CheckParamUserTable(int a_StartParam, const char *a_UserTable, int a_EndParam=-1)
Returns true if the specified parameters on the stack are of the specified usertable type; also logs ...
CannotSerializeException(const CannotSerializeException &a_Parent, const AString &a_ValueNamePrefix)
Constructs a new instance of the exception that takes the error message and value name from the paren...
Exception thrown when the input cannot be serialized.
static void PushJsonArray(const Json::Value &a_Value, cLuaState &a_LuaState)
Pushes the specified Json array as a table on top of the specified Lua state.
static Json::Value JsonSerializeValue(cLuaState &a_LuaState)
Serializes the Lua value at the top of the specified Lua state into a Json value. ...
static void Bind(cLuaState &a_LuaState)
Registers the Json library in the specified Lua state.
void LogStackTrace(int a_StartingDepth=0)
Logs all items in the current stack trace to the server console.
bool GetStackValue(int a_StackPos, AString &a_Value)
Encapsulates a Lua state and provides some syntactic sugar for common operations. ...
static int tolua_cJson_Serialize(lua_State *a_LuaState)
static int tolua_cJson_Parse(lua_State *a_LuaState)
bool CheckParamTable(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are tables; also logs warning if not...
CannotSerializeException(int a_ValueIndex, const char *a_ErrorMsg)
Constructs a new instance of the exception based on the provided values directly. ...
bool CheckParamEnd(int a_Param)
Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters) ...
CannotSerializeException(const AString &a_ValueName, const char *a_ErrorMsg)
Constructs a new instance of the exception based on the provided values directly. ...
static void PushJsonObject(const Json::Value &a_Value, cLuaState &a_LuaState)
Pushes the specified Json object as a table on top of the specified Lua state.
void LOGINFO(const char *a_Format, fmt::ArgList a_ArgList)
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
static void PushJsonValue(const Json::Value &a_Value, cLuaState &a_LuaState)
Pushes the specified Json value as an appropriate type on top of the specified Lua state...
static Json::Value JsonSerializeTable(cLuaState &a_LuaState)
Serializes the Lua table at the top of the specified Lua state's stack into a Json value...
CannotSerializeException(const CannotSerializeException &a_Parent, int a_ValueNamePrefixIndex)
Constructs a new instance of the exception that takes the error message and value name from the paren...
const AString & GetValueName() const
bool GetStackValues(int a_StartStackPos, Arg1 &&a_Arg1, Args &&...args)
Retrieves a list of values from the Lua stack, starting at the specified index.
void Push(Arg1 &&a_Arg1, Arg2 &&a_Arg2, Args &&...a_Args)
Pushes multiple arguments onto the Lua stack.