PDA

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

darrenan
October 25th, 2024, 03:01
Is there any way to catch and handle the exception thrown (see below for an example) when the string is not valid JSON? I'd like to have a control be able to handle both JSON and plain text. Ideally Utility.decodeJSON would just return false if it couldn't parse, but I'm open to other solutions.


[10/24/2024 6:54:42 PM] [ERROR] API: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: P. Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <29e3dfd7379d434ca9881f8263fe42fe>:0
at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in <29e3dfd7379d434ca9881f8263fe42fe>:0
at GDHAFPICAOJ.IIKPPAALNLH (System.String LLBENGIMMLL) [0x0001d] in <ff9ba60e419b48698079433070395f06>:0
at LGILNBNEHEJ.HCMIJPIFPCF (System.IntPtr FAMBEIDCDAM) [0x00022] in <ff9ba60e419b48698079433070395f06>:0

Moon Wizard
October 25th, 2024, 04:32
@darrenan,

I just pushed a small update under the hood for v4.6.0 in the Test channel to attempt to capture any exceptions, and simply return nil and log a warning in the console.

Can you switch to the Test channel, and make sure it's working for your use case?

Regards,
JPG

jharp
October 25th, 2024, 04:45
I believe you can use pcall for trapping this.

https://www.lua.org/pil/8.4.html

Jason

Moon Wizard
October 25th, 2024, 04:49
pcall can not be used for this; because it's C# code where the exception is triggered. The JSON APIs are driven by a C# library which is attempting to decode a string to a Lua table.

Regards,
JPG

jharp
October 25th, 2024, 06:29
:( Sad

darrenan
October 25th, 2024, 21:19
@darrenan,

I just pushed a small update under the hood for v4.6.0 in the Test channel to attempt to capture any exceptions, and simply return nil and log a warning in the console.

Can you switch to the Test channel, and make sure it's working for your use case?

Regards,
JPG

Confirmed, that works nicely. Thanks MW.