Best practice storing json?
david.bovill at gmail.com
Sun Aug 15 06:46:05 EDT 2021
I’ve been historically a bit lazy in taking care of how I store json data - so mostly it works but some times I get a different result on the server than I do locally on OSX. I suspect this is due to encoding differences in how the data is stored and which characters are in the json. I’d like to standardise on utf8 encoded json as my text storage of choice, and use a standard way to store this while in Livecode I parse the json and just deal with arrays.
As I understand it I should really be using textEncode and textDecode() - and I should store the utf8 encoded json files using “binfile:” not “file:” - however this is not what the dictionary entry fo textEncode() illustrates:
> put textEncode(field "output","UTF-8") into tOutput
> put tOutput into url ("file:output.txt")
So if I want to implement best practice in storing utf8 encoded json locally on Mac, on mobile and on unix servers according to:
> The textEncode function takes text, and returns it as binary data, encoded with the specified encoding.
> It is highly recommended that any time you interface with things outside LiveCode (files, network sockets, processes, etc) that you explicitly textEncode any text you send outside LiveCode and textDecode all text received into LiveCode. If this doesnt happen, a platform-dependent encoding will be used (which normally does not support Unicode text).
How should I do that? Should I not do something like this (in pseudocode):
> function fetchFromUrl jsonURL
> put url someURL into utf8JSON
> put textDecode(jsonURL, "UTF-8") into encodedJSON
> put json_ToArray (encodedJSON) into someArray
> return someArray
> end fetchFromUrl
> command exportToUrl someArray, jsonURL
> put json_FromArray (someArray) into encodedJSON
> put textEncode(encodedJSON, "UTF-8") into decodedJSON
> put decodedJSON into url someURL
> end exportToUrl
And. If so - should I be using “binfile:” or “file:” for writing to the file system?
📆 Schedule a call with me
More information about the use-livecode