fengari-lua / fengari

🌙 φεγγάρι - The Lua VM written in JS ES6 for Node and the browser
MIT License
1.8k stars 64 forks source link

Lua Table to JSON? #167

Closed RussellHaley closed 5 years ago

RussellHaley commented 5 years ago

Sorry, I can't seem to find an answer to this anywhere:

I want to build a response in a lua table, then convert it to a JSON object and then stringify it to send out a websocket. How do I do that? I assume the last step is JSON:stringify(myjson).

daurnimator commented 5 years ago

You could use e.g. dkjson?

One alternative would be to use the JSON.stringify replacer argument to specify how to serialise lua objects.

A simpler alternative may be to not build the response in a lua table, but instead build JS objects directly. e.g. with js.new(js.global.Object)

RussellHaley commented 5 years ago

Thanks, js.new(js.global.Object) is what I needed.