-
February 7th, 2022, 17:59 #1
[DEV] Added Utility API functions: decodeJSON, encodeJSON
Are these documented yet?
[DEV] Added Utility API functions: encodeCSV, decodeJSON, encodeJSON---
Fantasy Grounds AD&D Reference Bundle, AD&D Adventure Bundle 1, AD&D Adventure Bundle 2
Documentation for AD&D 2E ruleset FGU Reference Module, or Web.
Custom Maps (I2, S4, T1-4, Barrowmaze,Lost City of Barakus)
Note: Please do not message me directly on this site, post in the forums or ping me in FG's discord.
-
February 7th, 2022, 18:10 #2
Supreme Deity
- Join Date
- Mar 2007
- Posts
- 21,928
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
-
February 7th, 2022, 18:19 #3
Supreme Deity
- Join Date
- Mar 2007
- Posts
- 21,928
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
-
February 7th, 2022, 18:22 #4---
Fantasy Grounds AD&D Reference Bundle, AD&D Adventure Bundle 1, AD&D Adventure Bundle 2
Documentation for AD&D 2E ruleset FGU Reference Module, or Web.
Custom Maps (I2, S4, T1-4, Barrowmaze,Lost City of Barakus)
Note: Please do not message me directly on this site, post in the forums or ping me in FG's discord.
-
February 9th, 2022, 19:04 #5
- Join Date
- Apr 2020
- Posts
- 211
booleans
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.
Code:local sTest = Utility.encodeJSON(rRoll); Debug.chat("encode",sTest); Debug.chat("decode",Utility.decodeJSON(sTest)); Debug.chat("real",rRoll);
Code: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 }
-
February 9th, 2022, 20:01 #6
Supreme Deity
- Join Date
- Mar 2007
- Posts
- 21,928
Thanks, I'll take a look at it.
Regards,
JPG
-
October 25th, 2024, 03:01 #7
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.
Code:[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
-
October 25th, 2024, 04:32 #8
Supreme Deity
- Join Date
- Mar 2007
- Posts
- 21,928
@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
-
October 25th, 2024, 04:45 #9
- Join Date
- May 2016
- Posts
- 620
I believe you can use pcall for trapping this.
https://www.lua.org/pil/8.4.html
Jason
-
October 25th, 2024, 04:49 #10
Supreme Deity
- Join Date
- Mar 2007
- Posts
- 21,928
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
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks