forked from flux-framework/flux-sched
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjansson-lua.c
232 lines (207 loc) · 6.29 KB
/
jansson-lua.c
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
/*****************************************************************************\
* Copyright (c) 2014 Lawrence Livermore National Security, LLC. Produced at
* the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS).
* LLNL-CODE-658032 All rights reserved.
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the license, or (at your option)
* any later version.
*
* Flux is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
* See also: http://www.gnu.org/licenses/
\*****************************************************************************/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include <jansson.h>
static void * json_nullptr;
static int json_object_to_lua_table (lua_State *L, json_t *o);
static int json_array_to_lua (lua_State *L, json_t *o);
void lua_push_json_null (lua_State *L)
{
lua_pushlightuserdata (L, json_nullptr);
}
int lua_is_json_null (lua_State *L, int index)
{
return (lua_touserdata (L, index) == json_null);
}
int json_object_to_lua (lua_State *L, json_t *o)
{
if (o == NULL)
lua_pushnil (L);
switch (json_typeof (o)) {
case JSON_OBJECT:
json_object_to_lua_table (L, o);
break;
case JSON_ARRAY:
json_array_to_lua (L, o);
break;
case JSON_STRING:
lua_pushstring (L, json_string_value (o));
break;
case JSON_INTEGER:
lua_pushinteger (L, json_integer_value (o));
break;
case JSON_REAL:
lua_pushnumber (L, json_real_value (o));
break;
case JSON_TRUE:
lua_pushboolean (L, 1);
break;
case JSON_FALSE:
lua_pushboolean (L, 0);
break;
case JSON_NULL:
/* XXX: crap. */
break;
}
return (1);
}
static int json_array_to_lua (lua_State *L, json_t *o)
{
int i;
int index;
int n = json_array_size (o);
lua_newtable (L);
index = lua_gettop (L);
for (i = 0; i < n; i++) {
json_t *entry = json_array_get (o, i);
if (entry == NULL)
continue;
json_object_to_lua (L, entry);
lua_rawseti (L, index, i+1);
}
return (1);
}
static int json_object_to_lua_table (lua_State *L, json_t *o)
{
const char *key;
json_t *value;
lua_newtable (L);
json_object_foreach(o, key, value) {
lua_pushstring (L, key);
json_object_to_lua (L, value);
lua_rawset (L, -3);
}
return (1);
}
static json_t * lua_table_to_json (lua_State *L, int i);
static int lua_is_integer (lua_State *L, int index)
{
if (lua_type (L, index) == LUA_TNUMBER) {
double ip, l = lua_tonumber (L, index);
if (modf (l, &ip) == 0.0)
return (1);
}
return (0);
}
int lua_value_to_json (lua_State *L, int i, json_t **valp)
{
int index = (i < 0) ? (lua_gettop (L) + 1) + i : i;
json_t *o = NULL;
if (lua_isnoneornil (L, i))
return (-1);
switch (lua_type (L, index)) {
case LUA_TNUMBER:
if (lua_is_integer (L, index))
o = json_integer (lua_tointeger (L, index));
else
o = json_real (lua_tonumber (L, index));
break;
case LUA_TBOOLEAN:
o = lua_toboolean (L, index) ? json_true () : json_false ();
break;
case LUA_TSTRING:
o = json_string (lua_tostring (L, index));
break;
case LUA_TTABLE:
o = lua_table_to_json (L, index);
break;
case LUA_TNIL:
o = json_object ();
break;
case LUA_TLIGHTUSERDATA:
fprintf (stderr, "Got userdata\n");
if (lua_touserdata (L, index) == json_null)
break;
default:
luaL_error (L, "Unexpected Lua type %s",
lua_typename (L, lua_type (L, index)));
return (-1);
}
*valp = o;
return (0);
}
static int lua_table_is_array (lua_State *L, int index)
{
int haskeys = 0;
lua_pushnil (L);
while (lua_next (L, index)) {
haskeys = 1;
/* If key is not a number abort */
if (!lua_is_integer (L, -2)) {
lua_pop (L, 2); /* pop key and value */
return (0);
}
lua_pop (L, 1);
}
return (haskeys);
}
static json_t * lua_table_to_json_array (lua_State *L, int index)
{
int rc;
json_t *o = json_array ();
lua_pushnil (L);
while ((rc = lua_next (L, index))) {
json_t *val;
if (lua_value_to_json (L, -1, &val) < 0) {
json_decref (o);
return (NULL);
}
if (json_array_append_new (o, val) < 0)
fprintf (stderr, "json_array_append_new failed!\n");
lua_pop (L, 1);
}
return (o);
}
static json_t * lua_table_to_json (lua_State *L, int index)
{
json_t *o;
if (!lua_istable (L, index))
fprintf (stderr, "Object at index=%d is not table, is %s\n",
index, lua_typename (L, lua_type (L, index)));
if (lua_table_is_array (L, index))
return lua_table_to_json_array (L, index);
o = json_object ();
lua_pushnil (L);
while (lua_next (L, index)) {
json_t *val;
/* -2: key, -1: value */
const char *key = lua_tostring (L, -2);
if (lua_value_to_json (L, -1, &val) < 0) {
json_decref (o);
return (NULL);
}
json_object_set_new (o, key, val);
/* Remove value, save 'key' for next iteration: */
lua_pop (L, 1);
}
return (o);
}
/*
* vi: ts=4 sw=4 expandtab
*/