-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathluajson.cpp
More file actions
177 lines (165 loc) · 5.38 KB
/
Copy pathluajson.cpp
File metadata and controls
177 lines (165 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "luajson.h"
#include "json.hpp"
#include <cmath>
#include <stdexcept>
#include <string>
/*
This code was originally wrote by vysheng for tdbot
you can find the original source code at https://github.com/vysheng/tdbot/blob/master/clilua.cpp
*/
using json = nlohmann::json;
static bool lua_isarray(lua_State *L)
{
lua_Integer k;
lua_Integer max = 0;
lua_Integer size = 0;
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
if (tdlua_lua_key_integer_value(L, -2, k)) {
if (k >= 1) {
if (k > max)
max = k;
size++;
lua_pop(L, 1);
continue;
}
}
lua_pop(L, 2);
return false;
}
return max == size;
}
namespace {
std::string lua_json_path_for_key(lua_State *L, int index, const std::string &path)
{
if (lua_type(L, index) == LUA_TNUMBER) {
lua_Integer integer = 0;
if (tdlua_lua_key_integer_value(L, index, integer)) {
return path + "[" + std::to_string(integer) + "]";
}
const lua_Number number = lua_tonumber(L, index);
return path + "[" + std::to_string(number) + "]";
}
if (lua_type(L, index) == LUA_TSTRING) {
return path + "." + lua_tostring(L, index);
}
throw std::runtime_error("tdlua: object keys must be strings or numbers at " + path);
}
void lua_getjson_value(lua_State *L, json &j, const std::string &path)
{
if (lua_type(L, -1) == LUA_TNUMBER) {
lua_Integer integer = 0;
if (tdlua_lua_integer_value(L, -1, integer)) {
j = integer;
} else {
j = lua_tonumber(L, -1);
}
return;
} else if (lua_isboolean(L, -1)) {
j = static_cast<bool>(lua_toboolean(L, -1));
return;
} else if (lua_isstring(L, -1)) {
size_t len;
const char *s = lua_tolstring(L, -1, &len);
j = std::string(s, len);
return;
} else if (lua_istable(L, -1)) {
bool arr = lua_isarray(L);
if (arr) {
j = json::array();
} else {
j = json::object();
}
lua_pushnil(L);
while (lua_next(L, -2)) {
if (arr) {
int x = (int)lua_tointeger(L, -2);
lua_getjson_value(L, j[x-1],
path + "[" + std::to_string(x) + "]");
lua_pop(L, 1);
} else {
if (lua_type(L, -2) == LUA_TNUMBER) {
lua_Integer integer = 0;
const std::string key = tdlua_lua_key_integer_value(L, -2, integer)
? std::to_string(integer)
: std::to_string(lua_tonumber(L, -2));
lua_getjson_value(L, j[key],
lua_json_path_for_key(L, -2, path));
} else {
size_t len;
const char *key = lua_tolstring(L, -2, &len);
if (!key) {
throw std::runtime_error(
"tdlua: object keys must be strings or numbers at " + path);
}
std::string k(key, len);
lua_getjson_value(L, j[k], path + "." + k);
if (k == "_" && j.find("@type") == j.end())
j["@type"] = j["_"];
}
lua_pop(L, 1);
}
}
} else {
const char *type = lua_typename(L, lua_type(L, -1));
throw std::runtime_error("tdlua: unsupported Lua type '" + std::string(type) +
"' at " + path);
}
}
}
void lua_getjson(lua_State *L, json &j)
{
lua_getjson_value(L, j, "");
}
void lua_pushjson (lua_State *L, const json j) {
if (j.is_null()) {
lua_pushnil(L);
} else if (j.is_boolean()) {
lua_pushboolean(L, j.get<bool>());
} else if (j.is_string()) {
auto s = j.get<std::string>();
lua_pushlstring(L, s.c_str(), s.length());
} else if (j.is_number_integer()) {
auto v = j.get<int64_t>();
if (tdlua_can_push_integer(v)) {
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 503
lua_pushinteger(L, static_cast<lua_Integer>(v));
#else
lua_pushnumber(L, static_cast<lua_Number>(v));
#endif
} else {
std::string s = std::to_string(v);
lua_pushlstring (L, s.c_str(), s.length());
}
} else if (j.is_number_float()) {
auto v = j.get<double>();
lua_pushnumber(L, v);
} else if (j.is_array()) {
lua_newtable (L);
int p = 1;
for (auto it = j.begin(); it != j.end(); it++, p++) {
lua_pushnumber(L, p);
lua_pushjson(L, *it);
lua_settable(L, -3);
}
} else if (j.is_object()) {
lua_newtable(L);
for (auto it = j.begin(); it != j.end(); it++) {
auto s = it.key();
//if (s == "@type") lua_pushstring(L, "_"); else
lua_pushlstring(L, s.c_str(), s.length());
lua_pushjson(L, it.value());
lua_settable(L, -3);
//*
if (s == "@type") {
lua_pushstring(L, "_");
lua_pushstring(L, "@type");
lua_gettable(L, -3);
lua_settable(L, -3);
}
//*/
}
} else {
lua_pushnil(L);
}
}