| 138 | } |
| 139 | |
| 140 | static int encode(lua_State* L) { |
| 141 | int fmt = 0; // compact |
| 142 | if (lua_type(L, 2) == LUA_TBOOLEAN) |
| 143 | { |
| 144 | if (lua_istrue(L, 2)) |
| 145 | { |
| 146 | fmt = 1; // pretty |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | static const char* const fmts[] = { "compact", "pretty", "msgpack", nullptr }; |
| 152 | fmt = luaL_checkoption(L, 2, "compact", fmts); |
| 153 | } |
| 154 | |
| 155 | auto& up = *pluto_newclassinst(L, soup::UniquePtr<soup::JsonNode>); |
| 156 | checkJson(L, 1, up); |
| 157 | |
| 158 | switch (fmt) |
| 159 | { |
| 160 | case 0: default: |
| 161 | pluto_pushstring(L, up->encode()); |
| 162 | break; |
| 163 | |
| 164 | case 1: |
| 165 | pluto_pushstring(L, up->encodePretty()); |
| 166 | break; |
| 167 | |
| 168 | case 2: |
| 169 | soup::StringWriter sw; |
| 170 | up->msgpackEncode(sw); |
| 171 | pluto_pushstring(L, std::move(sw.data)); |
| 172 | break; |
| 173 | } |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | static int decode(lua_State* L) |
| 178 | { |
no test coverage detected