JSON is a collection of functions for building and reading JSON documents. Each document is kept in memory under a name (jsonName), and a location inside a document is addressed with a path (path).
Documents are stored by name in one place shared by all modules. Use names that do not clash with other modules, and remove documents you no longer need with RemoveJson. All documents are removed when a project is opened.
A failed call leaves an error flag on that document. Check it with GetResult and read the details with GetErrorMessage.
Document root — used with AddObject / AddArray to add to the root
JSON.AddArray("doc", "", "tags")
Do not start a path with / or separate parts with / or :. "/title" becomes a key literally named /title, and "items/0" or "items:0" are also taken as literal key names.
When writing (SetValue, SetNullValue, AddArray), missing objects and arrays along the path are created. Writing trims[2].x for the first time creates the trims array and fills positions 0 and 1 with empty objects {}.
array TrimX[10] = {0.0}; // trim value Xarray TrimY[10] = {0.0}; // trim value YFUNCTION bool SaveTrims(string filePath){ JSON.CreateJson("trim"); for(i, 0, 9) { JSON.SetValue("trim", $"trims[{i}].x", TrimX[i]); // creates the trims array and its element objects JSON.SetValue("trim", $"trims[{i}].y", TrimY[i]); } bool ok = JSON.SaveJsonFile("trim", filePath); JSON.RemoveJson("trim"); return ok;}FUNCTION bool LoadTrims(string filePath){ if( SYS.ExistsFile(filePath) == false ) { LogError("trim file not found : {0}", filePath); return false; } JSON.LoadJsonFile("trim", filePath); if( JSON.GetResult("trim") == false ) // false if the file content is not JSON { LogError("trim file error : {0}", JSON.GetErrorMessage("trim")); JSON.RemoveJson("trim"); return false; } int n = JSON.GetItemCount("trim", "trims"); // -1 if there is no array → the loop below does not run if( n > 10 ) { n = 10; } for(i, 0, n - 1) { TrimX[i] = JSON.GetDoubleValue("trim", $"trims[{i}].x", 0.0); TrimY[i] = JSON.GetDoubleValue("trim", $"trims[{i}].y", 0.0); } JSON.RemoveJson("trim"); return true;}
LoadJsonFile returns true even when the file content is not JSON, so check the result with GetResult. Calling it with a new name for a file that does not exist raises a runtime error, so check with SYS.ExistsFile first.
JSON.CreateJson("doc");JSON.AddArray("doc", "", "tags"); // empty array tags at the rootJSON.AddObjectToArray("doc", "tags", 2); // append 2 empty objectsJSON.SetValue("doc", "tags[0].name", "auto");JSON.SetValue("doc", "tags[1].name", "vision");JSON.MoveArrayItem("doc", "tags", 1, true); // swap element 1 with the one before → vision, autoJSON.RemoveFromArray("doc", "tags", 5); // no such index → only an error flag is leftif( JSON.GetResult("doc") == false ){ LogError("json edit failed : {0}", JSON.GetErrorMessage("doc")); JSON.ResetResult("doc"); // clear the error flag with ResetResult}
Creates an empty document ({}) and returns true. If the name already exists, its content is cleared and false is returned (the error flag is kept).
bool LoadJson(string jsonName, string jsonString)
Reads a JSON string (object or array) into a document, replacing any document of the same name. On a syntax error it returns false — the name is left as an empty document with an error flag. On success the document starts with no error flag.
Reads a file and processes it like LoadJson. It returns true even if the file content has a JSON syntax error, so check GetResult. If the file cannot be read it returns false with an error flag (the previous content is kept); with a new name it raises a runtime error.
Saves the document as an indented UTF-8 file. Folders are not created. If saving fails it returns false with an error flag. An unknown document name raises a runtime error.
string ToJsonString(string jsonName)
Returns the whole document as a JSON string with indentation and line breaks. Returns "" for an unknown name.
string FormatJson(string jsonString)
Returns the JSON string reformatted with indentation. Registered documents are not touched; if the input is not JSON, it is returned unchanged.
The SetValue overload that matches the type of the value (string, integer, real, bool) is used. Missing objects and arrays along the path are created, and so is the document if it does not exist. Any value or object already at that location is replaced.
defaultValue is returned when the path does not exist or the value is null, and it can be omitted. A missing path does not set the error flag. If a value exists but cannot be converted to the requested type (for example, reading "abc" with GetIntValue or an object with GetStringValue), a runtime error is raised.
Returns the value at that location as an indented JSON string. Use it to take out a whole object or array (a string value includes its quotes). Returns "" if the path does not exist.
Adds an empty object under the key objectName in the object at path. An existing key is replaced with an empty object. If path does not exist, nothing is created and only an error flag is set. The root is "".
Removes all elements of the array. Sets an error flag if it is not an array. An unknown document name raises a runtime error.
bool MoveArrayItem(string jsonName, string arrayPath, int index, bool moveUp)
Swaps element index with the element before it (moveUp = true) or after it (false). Moving the first element up, the last element down, or an out-of-range index returns false with an error flag.
ToJsonString and SaveJsonFile always write the indented format with line breaks. There is no function that produces a single-line string. SaveJsonFile writes UTF-8 without a BOM.
long values are stored as real numbers (double), so large values such as ticks may lose their last digits. Store such values as strings (for example with DATE.ConvertDateTimeString — DATE).
SaveJsonFile does not create folders. Call SYS.CreateDirectory first if needed (SYS).
Arguments with default values (defaultValue, numOfAdd) can be omitted. Function names are case-sensitive, and a call to an unknown name or with the wrong number of arguments is reported as a build error (Compile diagnostics).