MCPcopy Create free account
hub / github.com/Tencent/UnLua / Encoder

Class Encoder

Plugins/UnLuaExtensions/LuaRapidjson/Source/src/rapidjson.cpp:143–334  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141
142
143class Encoder {
144 bool pretty;
145 bool sort_keys;
146 bool empty_table_as_array;
147 int max_depth;
148 static const int MAX_DEPTH_DEFAULT = 128;
149public:
150 Encoder(lua_State*L, int opt) : pretty(false), sort_keys(false), empty_table_as_array(false), max_depth(MAX_DEPTH_DEFAULT)
151 {
152 if (lua_isnoneornil(L, opt))
153 return;
154 luaL_checktype(L, opt, LUA_TTABLE);
155
156 pretty = luax::optboolfield(L, opt, "pretty", false);
157 sort_keys = luax::optboolfield(L, opt, "sort_keys", false);
158 empty_table_as_array = luax::optboolfield(L, opt, "empty_table_as_array", false);
159 max_depth = luax::optintfield(L, opt, "max_depth", MAX_DEPTH_DEFAULT);
160 }
161
162private:
163 template<typename Writer>
164 void encodeValue(lua_State* L, Writer* writer, int idx, int depth = 0)
165 {
166 size_t len;
167 const char* s;
168 int64_t integer;
169 int t = lua_type(L, idx);
170 switch (t) {
171 case LUA_TBOOLEAN:
172 writer->Bool(lua_toboolean(L, idx) != 0);
173 return;
174 case LUA_TNUMBER:
175 if (luax::isinteger(L, idx, &integer))
176 writer->Int64(integer);
177 else {
178 if (!writer->Double(lua_tonumber(L, idx)))
179 luaL_error(L, "error while encode double value.");
180 }
181 return;
182 case LUA_TSTRING:
183 s = lua_tolstring(L, idx, &len);
184 writer->String(s, static_cast<SizeType>(len));
185 return;
186 case LUA_TTABLE:
187 return encodeTable(L, writer, idx, depth + 1);
188 case LUA_TNIL:
189 writer->Null();
190 return;
191 case LUA_TLIGHTUSERDATA:
192 if (values::isnull(L, idx)) {
193 writer->Null();
194 return;
195 }
196 // otherwise fall thought
197 case LUA_TFUNCTION: // fall thought
198 case LUA_TUSERDATA: // fall thought
199 case LUA_TTHREAD: // fall thought
200 case LUA_TNONE: // fall thought

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected