-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary.cpp
More file actions
245 lines (212 loc) · 7.28 KB
/
library.cpp
File metadata and controls
245 lines (212 loc) · 7.28 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "library.h"
#include <nlohmann/json.hpp>
#define SOL_CHECK_ARGUMENTS 1
#include <sol/sol.hpp>
#include <fstream>
namespace B2T
{
sol::object jsonParseObj(const nlohmann::json::object_t &obj, sol::state_view &lua);
sol::object jsonParseArray(const nlohmann::json::array_t &obj, sol::state_view &lua);
sol::object jsonParseValue(const nlohmann::json::value_type &value, sol::state_view &lua)
{
if (value.is_object())
return jsonParseObj(value, lua);
else if (value.is_array())
return jsonParseArray(value, lua);
if (value.is_null())
return sol::object(lua, sol::in_place, sol::nil); // todo: return B2T.null would be better
else if (value.is_number_float())
return sol::object(lua, sol::in_place, value.get<double>());
else if (value.is_number_integer())
return sol::object(lua, sol::in_place, value.get<long>());
else if (value.is_string())
return sol::object(lua, sol::in_place, value.get<std::string>());
else if (value.is_boolean())
return sol::object(lua, sol::in_place, value.get<bool>());
}
sol::object jsonParseObj(const nlohmann::json::object_t &obj, sol::state_view &lua)
{
sol::table table = lua.create_table(0, obj.size());
for (const auto &item : obj)
{
if (item.second.is_object())
table[item.first] = jsonParseObj(item.second, lua);
else if (item.second.is_array())
table[item.first] = jsonParseArray(item.second, lua);
else
table[item.first] = jsonParseValue(item.second, lua);
}
return table;
}
sol::object jsonParseArray(const nlohmann::json::array_t &array, sol::state_view &lua)
{
sol::table table = lua.create_table(array.size());
for (int key = 0; key < array.size(); ++key)
{
auto &value = array[key];
if (value.is_object())
table[key + 1] = jsonParseObj(value, lua);
else if (value.is_array())
table[key + 1] = jsonParseArray(value, lua);
else
table[key + 1] = jsonParseValue(value, lua);
}
return table;
}
enum class BinType : int {
cbor = 0,
msgpack = 1,
ubjson = 2
};
enum class Type : int {
json = 0,
table = 1
};
nlohmann::json luaTableToJson(sol::table table, sol::state_view &lua);
nlohmann::json luaToJson(sol::object data, sol::state_view &lua)
{
nlohmann::json json;
sol::type type = data.get_type();
if (type == sol::type::string)
json = data.as<const char *>();
else if (type == sol::type::number)
{
double val = data.as<double>();
double ip;
if (modf(val, &ip) == 0.0)
json = (long)(val);
else
json = val;
}
else if (type == sol::type::boolean)
json = data.as<bool>();
else if (type == sol::type::table)
json = luaTableToJson(data, lua);
return json;
}
nlohmann::json luaTableToJson(sol::table table, sol::state_view &lua)
{
bool allNums = true;
for (const auto &item : table)
{
if (item.first.get_type() != sol::type::number)
{
allNums = false;
break;
}
}
if (allNums)
{
nlohmann::json::array_t array(table.size());
for (size_t i = 0; i < table.size(); ++i)
array[i] = luaToJson(table[i + 1], lua);
return array;
}
else
{
nlohmann::json::object_t object;
for (const auto &item : table)
object[item.first.as<std::string>()] = luaToJson(item.second, lua);
return object;
}
}
sol::table open_B2T(sol::this_state L)
{
sol::state_view lua(L);
sol::table module = lua.create_table();
module["json"] = Type::json;
module["table"] = Type::table;
module["cbor"] = BinType::cbor;
module["msgpack"] = BinType::msgpack;
module["ubjson"] = BinType::ubjson;
module["to_bin"] = [](sol::object data, Type inType, BinType binType, sol::this_state L) ->sol::object {
sol::state_view lua(L);
nlohmann::json json;
if (inType == Type::table)
json = luaToJson(data, lua);
else
json = json.parse(data.as<std::string>());
std::vector<uint8_t> binData;
switch (binType)
{
case BinType::cbor:
binData = nlohmann::json::to_cbor(json);
break;
case BinType::msgpack:
binData = nlohmann::json::to_msgpack(json);
break;
case BinType::ubjson:
binData = nlohmann::json::to_ubjson(json);
break;
default:
return sol::object(lua, sol::in_place, sol::nil);
}
return sol::object(lua, sol::in_place, std::string((char*) binData.data(), binData.size()));
};
module["from_bin"] = [](std::string binData, Type outType, BinType binType, sol::object jsonIndent, sol::this_state L) {
sol::state_view lua(L);
nlohmann::json json;
switch (binType)
{
case BinType::cbor:
json = nlohmann::json::from_cbor(binData);
break;
case BinType::msgpack:
json = nlohmann::json::from_msgpack(binData);
break;
case BinType::ubjson:
json = nlohmann::json::from_ubjson(binData);
break;
default:
return sol::object(lua, sol::in_place, sol::nil);
}
if(outType == Type::json)
{
int indent = -1;
if(jsonIndent.is<int>())
indent = jsonIndent.as<int>();
return sol::object(lua, sol::in_place, json.dump(indent));
}
else
return sol::object(lua, sol::in_place,jsonParseValue(json, lua));
};
return module;
}
}
#ifndef _DEBUG
extern "C" int luaopen_B2T(lua_State *L)
{
return sol::stack::call_lua(L, 1, B2T::open_B2T);
}
#else
int main()
{
sol::state lua;
lua.open_libraries();
lua["b2t"] = B2T::open_B2T(lua.lua_state());
lua.script(R"(
local jsonData = [[{
"string":"testString",
"number":64,
"array": [0, 1, 2, 3],
"bool": true
}]]
local cborData = b2t.to_bin(jsonData, b2t.json, b2t.cbor)
local luaData = b2t.from_bin(cborData, b2t.table, b2t.cbor)
print(b2t.from_bin(cborData, b2t.json, b2t.cbor, 4))
print("-------------------------")
for k,v in pairs(luaData) do
print(k .. ": ".. tostring(v))
end
print("-------------------------")
local luaData2 = {
string = "MyString",
number = 42,
bool = false,
array = { 9, 8, 7, 6, 5.432, 1, 0 }
}
local msgData = b2t.to_bin(luaData2, b2t.table, b2t.msgpack)
print(b2t.from_bin(msgData, b2t.json, b2t.msgpack))
)");
}
#endif