View Full Version : [DEV] Added Utility API functions: decodeJSON, encodeJSON
celestian
February 7th, 2022, 17:59
Are these documented yet?
[DEV] Added Utility API functions: encodeCSV, decodeJSON, encodeJSON
If not can you give the use case? I would image decodeJSON(string) returns array and encodeJSON(array) returns json string?
Moon Wizard
February 7th, 2022, 18:10
They are conversion between a JSON/CSV text string <--> Lua table.
Here are the extensions I used to test with to review the functions.
Regards,
JPG
Moon Wizard
February 7th, 2022, 18:19
The CSV encode/decode uses numerical Lua indexes to denote the individual fields of the CSV file in order.
The JSON encode/decode should be fairly straightforward since JSON is basically like a Lua table for mapping. There is some code to determine if a Lua table has contiguous numerical indexes for array vs. object in JSON.
Regards,
JPG
celestian
February 7th, 2022, 18:22
The CSV encode/decode uses numerical Lua indexes to denote the individual fields of the CSV file in order.
The JSON encode/decode should be fairly straightforward since JSON is basically like a Lua table for mapping. There is some code to determine if a Lua table has contiguous numerical indexes for array vs. object in JSON.
Regards,
JPG
Sounds good. I'll do some testing this weekend and see if I can replace my encode/decodeJSON I use now ;)
mccartysr
February 9th, 2022, 19:04
It doesn't appear to like boolean values in lua tables. The below code produces json that doesn't have the boolean values. It worked for tables that didn't have booleans.
This takes a rRoll table, encodes, decodes, and then prints the original table for reference.
local sTest = Utility.encodeJSON(rRoll);
Debug.chat("encode",sTest);
Debug.chat("decode",Utility.decodeJSON(sTest));
Debug.chat("real",rRoll);
Produces
s'encode' | s'{
"aDice": [
"d20"
],
"sDesc": "[CHECK] Charisma",
"sType": "check",
"nMod": 0
}'
s'decode' | { s'sType' = s'check', s'aDice' = { #1 = s'd20' }, s'nMod' = #0, s'sDesc' = s'[CHECK] Charisma' }
s'real' | { s'aDice' = { #1 = s'd20' }, s'bTower' = bTRUE, s'sDesc' = s'[CHECK] Charisma', s'sType' = s'check', s'nMod' = #0, s'RR' = bTRUE, s'bSecret' = bTRUE }
Moon Wizard
February 9th, 2022, 20:01
Thanks, I'll take a look at it.
Regards,
JPG
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.