-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathluajson.cpp
141 lines (132 loc) · 3.98 KB
/
luajson.cpp
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
#include "luajson.h"
#include "json.hpp"
/*
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_Number k;
lua_Number max = 0;
lua_Integer size = 0;
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
if (lua_type(L, -2) == LUA_TNUMBER && (k = lua_tonumber(L, -2))) {
if (floor(k) == k && k >= 1) {
if (k > max)
max = k;
size++;
lua_pop(L, 1);
continue;
}
}
lua_pop(L, 2);
return false;
}
return max == size;
}
void lua_getjson(lua_State *L, json &j)
{
if (lua_type(L, -1) == LUA_TNUMBER) {
auto x = lua_tonumber(L, -1);
auto xi = lua_tointeger(L, -1);
if (x == xi) {
j = xi;
} else {
j = x;
}
return;
} else if (lua_isboolean(L, -1)) {
j = lua_toboolean(L, -1);
} 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(L, j[x-1]);
lua_pop(L, 1);
} else {
size_t len;
if (lua_type(L, -2) == LUA_TNUMBER) {
auto x = lua_tonumber(L, -2);
auto x64 = lua_tointeger(L, -2);
if (x == x64) {
lua_getjson(L, j[std::to_string(x64-1)]);
} else {
lua_getjson(L, j[std::to_string(x)]);
}
} else {
const char *key = lua_tolstring(L, -2, &len);
std::string k = std::string(key, len);
lua_getjson(L, j[k]);
if (k == "_" && j["@type"].empty())
j["@type"] = j["_"];
}
lua_pop(L, 1);
}
}
} else {
j = false;
return;
}
}
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 (v == static_cast<lua_Integer>(v)) {
lua_pushinteger(L, static_cast<lua_Integer>(v));
} 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);
}
}