10 #include "tolua++/include/tolua++.h"
11 #include "json/json.h"
12 #include "../JsonUtils.h"
30 public std::runtime_error
32 using Super = std::runtime_error;
52 Super(a_Parent.what()),
60 Super(a_Parent.what()),
80 lua_createtable(a_LuaState,
static_cast<int>(a_Value.size()), 0);
84 for (
const auto & v: a_Value)
88 lua_rawseti(a_LuaState, -2, idx);
102 lua_createtable(a_LuaState, 0,
static_cast<int>(a_Value.size()));
106 auto names = a_Value.getMemberNames();
107 for (
const auto & n: names)
119 lua_rawset(a_LuaState, -3);
130 switch (a_Value.type())
132 case Json::nullValue:
139 case Json::uintValue:
140 case Json::realValue:
142 a_LuaState.
Push(
static_cast<lua_Number
>(a_Value.asDouble()));
146 case Json::booleanValue:
148 a_LuaState.
Push(a_Value.asBool());
152 case Json::stringValue:
154 a_LuaState.
Push(a_Value.asString());
158 case Json::arrayValue:
164 case Json::objectValue:
181 lua_pushnil(a_LuaState);
182 while (lua_next(a_LuaState, -2) != 0)
184 if (lua_type(a_LuaState, -2) == LUA_TNUMBER)
196 catch (
const std::exception & exc)
214 catch (
const std::exception & exc)
220 lua_pop(a_LuaState, 1);
232 switch (lua_type(a_LuaState, -1))
238 return Json::Value(v);
242 return Json::Value(Json::nullValue);
248 return Json::Value(v);
254 return Json::Value(v);
262 LOGD(
"Attempting to serialize an unhandled Lua value type: %d", lua_type(a_LuaState, -1));
263 return Json::Value(Json::nullValue);
292 LOGWARNING(
"cJson:Parse(): Cannot read input string");
302 L.
Push(
cLuaState::Nil, fmt::format(FMT_STRING(
"Parsing Json failed: {}"), ParseError));
342 FMT_STRING(
"Cannot serialize into Json, value \"{}\" caused an error \"{}\""),
350 Json::StreamWriterBuilder builder;
351 if (lua_istable(L, 3))
354 while (lua_next(L, -2) != 0)
356 if (lua_type(L, -2) == LUA_TSTRING)
361 builder[propName] = propValue;
368 if (!builder.validate(&invalid))
370 LOGINFO(
"cJson:Serialize(): detected invalid settings:");
371 for (
const auto & n: invalid.getMemberNames())
373 LOGINFO(
" \"%s\" (\"%s\")", n.c_str(), invalid[n].asCString());
377 auto writer(builder.newStreamWriter());
380 std::stringstream ss;
381 writer->write(root, &ss);
392 tolua_beginmodule(a_LuaState,
nullptr);
395 tolua_usertype(a_LuaState,
"cJson");
396 tolua_cclass(a_LuaState,
"cJson",
"cJson",
"",
nullptr);
399 tolua_beginmodule(a_LuaState,
"cJson");
402 tolua_endmodule(a_LuaState);
403 tolua_endmodule(a_LuaState);
static int tolua_cJson_Serialize(lua_State *a_LuaState)
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.
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 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 JsonSerializeTable(cLuaState &a_LuaState)
Serializes the Lua table at the top of the specified Lua state's stack into a Json value.
static int tolua_cJson_Parse(lua_State *a_LuaState)
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.
void LOGWARNING(std::string_view a_Format, const Args &... args)
void LOGINFO(std::string_view a_Format, const Args &... args)
Implements custom fmtlib formatting for cChunkCoords.
bool ParseString(const AString &a_JsonStr, Json::Value &a_Root, AString *a_ErrorMsg)
Exception thrown when the input cannot be serialized.
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...
CannotSerializeException(int a_ValueIndex, const char *a_ErrorMsg)
Constructs a new instance of the exception based on the provided values directly.
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...
const AString & GetValueName() const
CannotSerializeException(const AString &a_ValueName, const char *a_ErrorMsg)
Constructs a new instance of the exception based on the provided values directly.
static void Bind(cLuaState &a_LuaState)
Registers the Json library in the specified Lua state.
Encapsulates a Lua state and provides some syntactic sugar for common operations.
void Push(Arg1 &&a_Arg1, Arg2 &&a_Arg2, Args &&... a_Args)
Pushes multiple arguments onto the Lua stack.
void LogStackTrace(int a_StartingDepth=0)
Logs all items in the current stack trace to the server console.
bool CheckParamEnd(int a_Param)
Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters)
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 ...
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.
bool GetStackValue(int a_StackPos, AString &a_Value)
bool GetStackValues(int a_StartStackPos, Arg1 &&a_Arg1, Args &&... args)
Retrieves a list of values from the Lua stack, starting at the specified index.