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
|
#include "stdafx.h"
static int lua_Decode(lua_State *L)
{
const char *string = luaL_checkstring(L, 1);
JSONNode *node = json_parse(string);
new (L) JSON(node);
luaL_setmetatable(L, MT_JSON);
return 1;
}
static int lua_Encode(lua_State *L)
{
switch (lua_type(L, 1))
{
case LUA_TNIL:
lua_pushliteral(L, "null");
break;
case LUA_TBOOLEAN:
lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false");
break;
case LUA_TNUMBER:
{
if (lua_isinteger(L, 1)) {
lua_pushfstring(L, "%I", lua_tointeger(L, 1));
break;
}
char decpoint = lua_getlocaledecpoint();
if (decpoint != '.') {
char p[2] = { decpoint };
luaL_gsub(L, lua_tostring(L, 1), p, ".");
}
else
lua_pushfstring(L, "%f", lua_tonumber(L, 1));
break;
}
case LUA_TSTRING:
lua_pushfstring(L, "\"%s\"", lua_tostring(L, 1));
break;
case LUA_TTABLE:
{
JSONNode node;
lua_pushnil(L);
lua_pushvalue(L, 1);
lua2json(L, node);
lua_pop(L, 2);
lua_pushstring(L, node.write().c_str());
break;
}
case LUA_TUSERDATA:
{
JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
lua_pushstring(L, node.write().c_str());
break;
}
case LUA_TLIGHTUSERDATA:
if (lua_touserdata(L, 1) == NULL)
{
lua_pushliteral(L, "null");
break;
}
default:
luaL_argerror(L, 1, luaL_typename(L, 1));
}
return 1;
}
static const luaL_Reg methods[] =
{
{ "Decode", lua_Decode },
{ "Encode", lua_Encode },
{ NULL, NULL }
};
extern "C" LUAMOD_API int luaopen_m_json(lua_State *L)
{
luaL_newlib(L, methods);
luaL_newmetatable(L, MT_JSON);
luaL_setfuncs(L, jsonApi, 0);
lua_pop(L, 1);
return 1;
}
|