You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
Sorry for the noob question, but I can't quite figure it out.
I'm trying to use the Python JSON lib instead of any of the lua ones due to issues with package installation on target, but I can't get even the minimal example to work. Here's what I'm trying:
importlupaimportjsonlua=lupa.LuaRuntime(unpack_returned_tuples=True)
lua.execute("json = {}")
lua.globals()["json"]["encode"] =json.dumpslua.globals()["json"]["decode"] =json.loadslua_function="""function construct_json_list() local out = {"a", "b", "c"} return json.encode("throwaway", out)end"""lua.execute(lua_function)
print(lua.globals()["construct_json_list"]())
This simply throws TypeError: Object of type _LuaTable is not JSON serializable. Obviously json.dumps does not support Lua Tables.
tl;dr how to convert a lua table to a python object?
Any help is appreciated.
The text was updated successfully, but these errors were encountered:
deflua_table_to_python_obj(obj):
iftype(obj).__name__=="_LuaTable":
# Check if the Lua table is array-likekeys=list(obj.keys())
ifkeys==list(range(1, len(keys) +1)):
# Convert to Python list if keys are consecutive integers starting from 1return [lua_table_to_python(obj[key]) forkeyinkeys]
else:
# Convert to Python dictionary otherwisereturn {key: lua_table_to_python(obj[key]) forkeyinobj}
returnobj
Is this truly what I'm meant to do? recursively convert tables to objects by hand? or am I missing something obvious?
I'd use lupa.lua_type(obj) == 'table', and try to avoid the list(range()), but yes, it would usually be something like this, depending on your specific needs. It's not clear that an implementation in Lupa would cover a sufficiently large number of use cases, but since users can always implement their own mapping, it might still work out.
Want to provide a PR that adds this as a method of the _LuaTable class, say, .copy_to_python()?
I'd expect that this can be made quite fast when using the Lua C-API instead of going through Lupa's Python API.
Hello,
Sorry for the noob question, but I can't quite figure it out.
I'm trying to use the Python JSON lib instead of any of the lua ones due to issues with package installation on target, but I can't get even the minimal example to work. Here's what I'm trying:
This simply throws
TypeError: Object of type _LuaTable is not JSON serializable
. Obviouslyjson.dumps
does not support Lua Tables.tl;dr how to convert a lua table to a python object?
Any help is appreciated.
The text was updated successfully, but these errors were encountered: