With PhotonJSON you can include JSON encoding/decoding functionalities in your Livecode projects, for any desktop or mobile platform, or even for developing rest APIs with Livecode server, we have tested it over several projects, some of them relatively big.
In this library several ideas converge from many developers who created solutions like this for LiveCode and for other programming languages. Although there are several libraries for working with JSON in LiveCode, we take as a personal challenge to create our own version for which we took as a base the great work done by Bob Hall in the FastJSON library. We needed a library with the flexibility of FastJSON but with the speed of Monte Goulding’s mergJSON, so we started writing PhotonJSON using FastJSON’s ideas and code as a base, although in some cases we completely rewrote the algorithms.
In PhotonJSON, we have achieved a performance very close to mergJSON, not forgetting that mergJSON uses an external, processing at a lower level than LiveCode and as a result a better processing speed compared to native Livecode.
Convert a JSON to an LC Array in a less time equal to or very close to that of mergJSON.
When converting an LC Array to JSON you can specify whether the JSON is beautifully formatted.
Added a formatting function and another to minify existing JSON.
How to use PhotonJSON
To use PhotonJSON in your desktop or mobile applications you just have to load the library´s stack and use it as a library with:
start using stack "PhotonJSON"
Once we have PhotonJSON integrated into our project, we can access all the functions and properties of it.
NOTE Before continuing, let’s clarify that in LiveCode if two libraries have a function with the same name the one from the last library that was started as a library will be executed, that is, the one that is first in the message path.
PhotonJSON has 5 functions and a public property that can be accessed from another script. It also integrates 4 functions for compatibility with projects that use other libraries and want to start using this one. Please note the above note.
|cVersion||property||Version||The current version of the library.|
|validateJSON||function||pJSONString – JSON string||Boolean||Validate if a string is a valid JSON.|
|beautifyJSON||function||pJSONString – JSON string pSpacesNum – Numbers of spaces pIsUnicode – whether it is treated as Unicode||JSON string||JSON string formatted in a more visually pleasing way.|
|minifyJSON||function||pJSONString – JSON string pIsUnicode – whether it is treated as Unicode||JSON string||Remove unnecessary spaces from JSON string.|
|JSONStringify||function||pDataArray – LC Array pBeautify – JSON is formatted nice pIsUnicode – whether it is treated as Unicode||JSON string||Gets the JSON string from an LC Array.|
|JSONParser||function||pJSONString – JSON string pIsUnicode – whether it is treated as Unicode||LC Array||Gets the LC Array of a JSON string.|
These are four functions that are optionally included for PhotonJSON for fast compatibility with projects using mergeJSON or FastJSON. They expect the same parameters as in their original versions and in the same order.
ArrayToJSON(), JsonToArray(), arrayFromJson(), and jsonFromArray().
If you don’t want PhotonJSON to overwrite the previous functions, just comment the code for it in the PhotonJSON library.
Why don’t we use these names for the library and use JSONStringify and JSONParser as in the HTML5 API. Since we created this library to be used in a plugin that will be integrated with the LiveCode IDE and as we clarify in the note. When a function is called and it is in two libraries, the first one in the path will take precedence over the others. So in order not to interfere with other projects where you don’t want to use this library, we decided to use this naming convention and not include the others.
We have find several advantages of using this library over the others available for Livecode, the encoding/decoding speed is very close to those using externals, in some cases even faster, the error handling and debugging is easier and more intuitive than the ones using externals and as a plus it give us other functions to work with JSON strings, format, minify and validate. After several months of development and testing we have decided to make it public and share our results with the Livecode community. Suggestions and ideas are welcome, you can find source code and download links below.