-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cpp
More file actions
89 lines (78 loc) · 1.61 KB
/
json.cpp
File metadata and controls
89 lines (78 loc) · 1.61 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
#include "json.hpp"
#include <iostream>
#include <lua.hpp>
#include "textmodule_lua.hpp"
#include "textmodule_option.hpp"
int json_parse(lua_State* L) {
try {
lua_Json j = tm_jsonparse(tm_tosstring_s(L, 1, "{}"));
tm_pushjson(L, &j);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int json_dump(lua_State* L) {
try {
lua_Json j = tm_tabletojson(L, 1);
if(lua_isnone(L, 2))
lua_pushsstring(L, tm_jsondump(&j));
else
lua_pushsstring(L, tm_jsondump(&j, lua_tointeger(L, 2)));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int json_lint(lua_State* L) {
try {
lua_Json j = tm_tojson(L, 1);
if (lua_isnumber(L, 2)) {
lua_pushsstring(L, tm_jsondump(&j, lua_tonumber(L, 2)));
return 1;
}
else if (lua_isnoneornil(L, 2)) {
lua_pushsstring(L, tm_jsondump(&j));
return 1;
}
else
return luaL_argerror(L, 2, "number/nil expected");
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int json_size(lua_State* L) {
try {
lua_Json j = tm_tojson(L, 1);
lua_pushnumber(L, j.size());
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
static luaL_Reg TEXTMODULE_JSON_REG[] = {
{"parse", json_parse},
{"dump", json_dump},
{"lint", json_lint},
{"size", json_size},
{nullptr, nullptr}
};
void luaReg_json(lua_State* L, lua_Option opt) {
if (opt["api"]["json"]) {
tm_debuglog_apiloaded(opt, "json");
lua_newtable(L);
luaL_register(L, NULL, TEXTMODULE_JSON_REG);
lua_setfield(L, -2, "json");
}
else {
tm_debuglog_apinoloaded(opt, "json");
}
}