Cosmere RPG Beta Launch
Page 1 of 2 12 Last
  1. #1

    [DEV] Added Utility API functions: decodeJSON, encodeJSON

    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?
    ---
    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.

  2. #2
    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
    Attached Files Attached Files

  3. #3
    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

  4. #4
    Quote Originally Posted by Moon Wizard View Post
    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
    ---
    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.

  5. #5

    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);
    Produces
    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 }

  6. #6
    Thanks, I'll take a look at it.

    Regards,
    JPG

  7. #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

  8. #8
    @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

  9. #9
    I believe you can use pcall for trapping this.

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

    Jason

  10. #10
    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

Page 1 of 2 12 Last

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (1 members and 2 guests)

  1. humby

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
TALES of the VALIANT

Log in

Log in