summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/m_json.cpp
blob: 60f75411e914318a82de9b7eff6c3343d273a9ba (plain)
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
#include  "stdafx.h"

static void lua2json(lua_State *L, JSONNode &node)
{
	switch (lua_type(L, -1))
	{
	case LUA_TNIL:
		node.nullify();
		break;
	case LUA_TSTRING:
		node = lua_tostring(L, -1);
		break;
	case LUA_TBOOLEAN:
		node = lua_toboolean(L, -1) != 0;
		break;
	case LUA_TNUMBER:
	{
		lua_Integer val = lua_tointeger(L, -1);
		if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MIN)
			node = (long)val;
		else
			node = lua_tonumber(L, -1);
		break;
	}
	case LUA_TTABLE:
	{
		ptrA name(mir_strdup(node.name()));
		node.cast(JSON_ARRAY);
		node.set_name((char*)name);

		lua_pushnil(L);
		while (lua_next(L, -2) != 0) {
			JSONNode child;
			if (!lua_isnumber(L, -2)) {
				if (node.type() == JSON_ARRAY) {
					node.cast(JSON_NODE);
					node.set_name((char*)name);
				}
				const char *key = lua_tostring(L, -2);
				child.set_name(key);
			}
			lua2json(L, child);
			node << child;

			lua_pop(L, 1);
		}

		break;
	}
	}
}

/***********************************************/

static int json__index(lua_State *L)
{
	JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;

	switch (node.type())
	{
	case JSON_NULL:
		lua_pushnil(L);
		break;
	case JSON_STRING:
		lua_pushstring(L, node.as_string().c_str());
		break;
	case JSON_NUMBER:
		lua_pushnumber(L, node.as_int());
		break;
	case JSON_BOOL:
		lua_pushboolean(L, node.as_bool());
		break;
	case JSON_NODE:
	case JSON_ARRAY:
		new (L) JSONNode(node);
		luaL_setmetatable(L, MT_JSON);
	}

	return 1;
}

static int json__newindex(lua_State *L)
{
	JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
	const char *key = lua_tostring(L, 2);

	JSONNode child = node[key];
	if (child.isnull()) {
		child.set_name(key);
		lua2json(L, child);
		node << child;
		return 0;
	}

	lua2json(L, child);
	node[key] = child;

	return 0;
}

static int json__len(lua_State *L)
{
	JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;

	lua_pushnumber(L, node.size());

	return 1;
}

static int json__tostring(lua_State *L)
{
	JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;

	lua_pushstring(L, node.write().c_str());

	return 1;
}

static int json__gc(lua_State *L)
{
	JSON *json = (JSON*)luaL_checkudata(L, 1, MT_JSON);

	delete json;

	return 0;
}

const struct luaL_Reg jsonApi[] =
{
	{ "__index", json__index },
	{ "__newindex", json__newindex },
	{ "__len", json__len },
	{ "__tostring", json__tostring },
	{ "__gc", json__gc },

	{ nullptr, nullptr }
};


/***********************************************/

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) == nullptr)
		{
			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 },

	{ nullptr, nullptr }
};

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;
}