From Clomosy Docs
TCLJSON is a structured and type-safe JSON processing library designed to facilitate the creation, manipulation, and validation of JSON data. It enables the definition of objects, arrays, and values representing standard JSON types, including strings, numbers, booleans, and null elements.
The library provides mechanisms for constructing hierarchical data models, performing key-value pair operations, merging multiple JSON structures, and retrieving nested elements through deep path access. TCLJSON ensures strict compliance with JSON syntax rules, offering validation methods to verify data integrity during parsing and serialization.
In addition to in-memory operations, TCLJSON supports persistent data handling through file-based loading and saving of JSON content. Its strongly typed architecture promotes reliability, consistency, and maintainability in applications that require structured data representation, configuration management, or data exchange using the JSON format.
| Information: What is TCLJSON? |
| TCLJSON allows you to create, manipulate, and serialize JSON data structures. It supports nested objects, arrays, type-safe values, deep path access, merging, cloning, and file operations. |
| Class | Description |
|---|---|
| TclJSONArray | Represents a JSON array with ordered values. Supports adding/removing values or objects, merging, cloning, and enumeration. |
| TclJSONObject | Represents a JSON object with key-value pairs. Provides methods for adding, removing, merging, cloning, and deep path access. |
| TclJSONPair | Represents a single key-value pair inside a JSON object. |
| TclJSONValue | Encapsulates a JSON value (string, integer, number, boolean, datetime, or null) and provides type-safe methods. |
| Dont Forget! |
| When working with nested objects or arrays, always free created JSON instances (Obj.Free;, Arr.Free;) after use to prevent memory leaks.
|
| Feature | Use of | Definition |
|---|---|---|
| ParseToJSONObject | obj = TCLJSON.ParseToJSONObject(jsonStr); | Parses a JSON string into a TCLJSONObject. |
| ParseObjectToString | jsonStr = TCLJSON.ParseObjectToString(obj); | Converts a TCLJSONObject to a JSON string. |
| ParseToJSONArray | arr = TCLJSON.ParseToJSONArray(jsonStr); | Parses a JSON string into a TCLJSONArray. |
| ParseArrayToString | jsonStr = TCLJSON.ParseArrayToString(arr); | Converts a TCLJSONArray to a JSON string. |
| LoadObjectFromFile | obj = TCLJSON.LoadObjectFromFile('data.json'); | Loads a JSON object from a file. |
| SaveObjectToFile | TCLJSON.SaveObjectToFile(obj, 'data.json'); | Saves a TCLJSONObject to a file. |
| IsValidJSON | valid = TCLJSON.IsValidJSON(jsonStr); | Checks whether a string is valid JSON. |
Example: TCLJSON in Action
var
MyForm: TClForm;
LoadBtn, SaveBtn, ParseBtn: TClProButton;
InputMemo, OutputMemo: TClMemo;
Obj: TCLJSONObject;
Arr: TCLJSONArray;
JsonStr: string;
Valid: Boolean;
void LoadJSONFromFile
{
Obj = TCLJSON.LoadObjectFromFile('data.json');
OutputMemo.Lines.Text = Obj.ToJSONString;
}
void SaveJSONToFile
{
if Obj <> nil
{
TCLJSON.SaveObjectToFile(Obj, 'output.json');
ShowMessage('JSON saved to output.json');
}
else
ShowMessage('No JSON object to save.');
}
void ParseJSON
{
try
JsonStr = InputMemo.Text;
Valid = TCLJSON.IsValidJSON(JsonStr);
if not Valid
{
ShowMessage('Invalid JSON format!');
exit;
}
// Parse string to object
Obj = TCLJSON.ParseToJSONObject(JsonStr);
ShowMessage('Parsed Object Key Count: ' + IntToStr(Obj.Count));
// Convert object back to string
JsonStr = TCLJSON.ParseObjectToString(Obj);
OutputMemo.Lines.Text = JsonStr;
// Create sample array and show conversion
Arr = TCLJSONArray.Create;
Arr.AddValue(TCLJSONValue.Str('Item1'));
Arr.AddValue(TCLJSONValue.Str('Item2'));
OutputMemo.Lines.Add('');
OutputMemo.Lines.Add('Array JSON:');
OutputMemo.Lines.Add(TCLJSON.ParseArrayToString(Arr));
except
ShowMessage('Error: ' + LastExceptionMessage);
}
}
{
MyForm = TClForm.Create(Self);
InputMemo = MyForm.AddNewMemo(MyForm, 'InputMemo', 'Enter JSON Text');
InputMemo.Align = alTop;
InputMemo.Height = 150;
OutputMemo = MyForm.AddNewMemo(MyForm, 'OutputMemo', 'Output');
OutputMemo.Align = alClient;
ParseBtn = MyForm.AddNewProButton(MyForm, 'ParseBtn', 'Parse JSON');
ParseBtn.Align = alBottom;
ParseBtn.OnClick = 'ParseJSON';
LoadBtn = MyForm.AddNewProButton(MyForm, 'LoadBtn', 'Load from File');
LoadBtn.Align = alBottom;
LoadBtn.OnClick = 'LoadJSONFromFile';
SaveBtn = MyForm.AddNewProButton(MyForm, 'SaveBtn', 'Save to File');
SaveBtn.Align = alBottom;
SaveBtn.OnClick = 'SaveJSONToFile';
MyForm.Run;
}
Output:
See Also