This EPGP World of Warcraft addon outputs an epgp.lua database file.
I wrote a plugin to convert the Lua data into a JSON object for display on a guild website. It was working in older versions of the addon, but now I'm having trouble trying to get it to convert the file properly. Here are two snippets that show the conversion problem - see this demo.
The first works great at forming a nested array:
["roster_info"] = {
{
"Agantica", -- [1]
"ROGUE", -- [2]
"09/03-2013", -- [3]
}, -- [1]
{
"Intikamim", -- [1]
"PALADIN", -- [2]
"17/02-2013", -- [3]
}, -- [2]
},
becomes
"roster_info" : [
[
"Agantica",
"ROGUE",
"09/03-2013"
],
[
"Intikamim",
"PALADIN",
"17/02-2013"
]
]
But the string replacment sees this next snippet as a nested array when it should be an object inside of an array:
["bonus_loot_log"] = {
{
["player"] = "Magebox",
["timestamp"] = "2013-03-07 13:44:00",
["coinsLeft"] = "-1",
["reward"] = "|cffa335ee|Hitem:86815:0:0:0:0:0:0:632235520:90:0:445|h[Attenuating Bracers]|h|r",
}, -- [1]
{
["player"] = "Lîutasila",
["coinsLeft"] = "-1",
["timestamp"] = "2013-03-07 13:47:00",
}, -- [2]
},
becomes
"bonus_loot_log" : [
[
"player" : "Magebox",
"timestamp" : "2013-03-07 13:44:00",
"coinsLeft" : "-1",
"reward" : "|cffa335ee|Hitem:86815:0:0:0:0:0:0:632235520:90:0:445|h[Attenuating Bracers]|h|r"
],
[
"player": "Lîutasila",
"coinsLeft": "-1",
"timestamp": "2013-03-07 13:47:00"
]
]
Here is the string conversion script that only works on the first snippet.
lua_string
.replace(/\[(.*)\]\s\=\s/g,'$1:') // change equal to colon & remove outer brackets
.replace(/[\t\r\n]/g,'') // remove tabs & returns
.replace(/\}\,\s--\s\[\d+\]\}/g,']]') // replace sets ending with a comment with square brackets
.replace(/\,\s--\s\[\d+\]/g,',') // remove close subgroup and comment
.replace(/,(\}|\])/g,'$1') // remove trailing comma
.replace(/\}\,\{/g,'],[') // replace curly bracket set with square brackets
.replace(/\{\{/g,'[[') // change double curlies to square brackets
.replace(/EPGP_DB\s\=/,'');
So, I need some help getting the Lua to convert properly with an array of objects (second example).
You generally cannot convert any Lua table to JSON data simply by using string operations. The problem is that while Lua uses tables for both arrays and dictionaries, JSON needs two different types. There are other syntactical differences.
This is best solved by a module which converts between Lua and JSON representation. Take a look at the Lua wiki on JSON modules and find a Lua module to convert Lua to JSON. There are multiple modules, some which are pure Lua, being a good choice to embed into WoW. They correctly detect whether a table represents an array or dictionary and output the relevant JSON.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With