int json_object_string_to_lua (lua_State *L, const char *json_str) { json_t *o; int rc; if (!(o = json_loads (json_str, JSON_DECODE_ANY, NULL))) return (-1); rc = json_object_to_lua (L, o); json_decref (o); return (rc); }
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); }
static int json_object_to_lua_table (lua_State *L, json_object *o) { json_object_iter iter; lua_newtable (L); json_object_object_foreachC(o, iter) { lua_pushstring (L, iter.key); json_object_to_lua (L, iter.val); lua_rawset (L, -3); }
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); }
struct rdl * rdl_find (struct rdl *rdl, json_object *args) { lua_rdl_method_push (rdl, "find"); if (json_object_to_lua (rdl->L, args) < 0) { VERR (rdl->rl, "Failed to convert JSON to Lua\n"); return (NULL); } /* * stack: [ Method, object, args-table ] */ if (lua_pcall (rdl->L, 2, LUA_MULTRET, 0) || lua_isnoneornil (rdl->L, 1)) { VERR (rdl->rl, "find(%s): %s\n", json_object_to_json_string (args), lua_tostring (rdl->L, -1)); return (NULL); } return (lua_pop_new_rdl (rdl)); }