From Clomosy Docs

Revision as of 12:43, 21 October 2025 by Salih (talk | contribs)

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.
Important: Type-Safe JSON Values
TCLJSONValue provides class functions like Str, Int, Bool, Num, DT, and Null to safely create JSON values without manual conversion.
Feature Use of Definition
TCLJSONObject obj : TCLJSONObject; Creates a variable for JSON object.
TCLJSONArray arr : TCLJSONArray; Creates a variable for JSON array.
AddPair obj.AddPair('key', TCLJSONValue.Str('value')); Adds a key-value pair to JSON object.
AddObject obj.AddObject('child', ChildObj); Adds a nested JSON object.
AddArray obj.AddArray('letters', Arr); Adds a JSON array inside object.
Merge obj.Merge(ExtraObj); Merges another object into current.
GetValueByPath val = obj.GetValueByPath('level1.level2'); Retrieves a value from nested path.
ToJSONString obj.ToJSONString; Converts object/array to JSON string.
IsValidJSON TCLJSON.IsValidJSON(str); Checks if string is valid JSON.
SaveObjectToFile TCLJSON.SaveObjectToFile(obj, 'file.json'); Saves JSON object to file.


Example: TCLJSON in Action

var
  MyForm: TClForm;
  NameEdt, AgeEdt: TClProEdit;
  ActiveChk: TClCheckBox;
  ShowBtn: TClProButton;
  ResultMemo: TClMemo;

void GenerateJSON
var
  Obj, Child, Extra: TCLJSONObject;
  Arr: TCLJSONArray;
  Val: TCLJSONValue;
{
  try
    // Main JSON object
    Obj = TCLJSONObject.Create;
    try
      Obj.AddPair('name', TCLJSONValue.Str(NameEdt.Text));
      Obj.AddPair('age', TCLJSONValue.Int(StrToInt(AgeEdt.Text)));
      Obj.AddPair('active', TCLJSONValue.Bool(ActiveChk.isChecked));

      // Nested object
      Child = TCLJSONObject.Create;
      Child.AddPair('ChildLevel', TCLJSONValue.Str('This is a deep value'));
      Obj.AddObject('ParentLevel', Child);

      // Array
      Arr = TCLJSONArray.Create;
      Arr.AddValue(TCLJSONValue.Str('X'));
      Arr.AddValue(TCLJSONValue.Str('Y'));
      Arr.AddValue(TCLJSONValue.Str('Z'));
      Obj.AddArray('letters', Arr);

      // Merge object
      Extra = TCLJSONObject.Create;
      Extra.AddPair('status', TCLJSONValue.Str('ok'));
      Obj.Merge(Extra);

      // Deep value access (Optional)
      Val = Obj.GetValueByPath('ParentLevel.ChildLevel');

      // Add JSON to Memo
      ResultMemo.Lines.Text = Obj.ToJSONString;

      // Deep value shown (Optional)
      ShowMessage('Deep value: ' + Val.AsString);

    except
      ShowMessage('Error: ' + LastExceptionMessage);
    }
  except
    ShowMessage('Unexpected Error: ' + LastExceptionMessage);
  }
}

{
  MyForm = TClForm.Create(Self);

  NameEdt = MyForm.AddNewProEdit(MyForm,'NameEdt','Name');
  NameEdt.Align = alTop;
  NameEdt.Height = 35;
  NameEdt.Margins.Left = 25;
  NameEdt.Margins.Bottom = 25;
  NameEdt.Margins.Right = 25;

  AgeEdt = MyForm.AddNewProEdit(MyForm,'AgeEdt','Age');
  AgeEdt.Align = alTop;
  AgeEdt.Height = 35;
  AgeEdt.Margins.Left = 25;
  AgeEdt.Margins.Bottom = 25;
  AgeEdt.Margins.Right = 25;

  ActiveChk = MyForm.AddNewCheckBox(MyForm,'ActiveChk','Active');
  ActiveChk.Align = alTop;
  ActiveChk.Margins.Left = 15;
  ActiveChk.Margins.Top = 10;

  ResultMemo = MyForm.AddNewMemo(MyForm,'ResultMemo','JSON Output');
  ResultMemo.Align = alClient;
  ResultMemo.Height = 200;
  ResultMemo.StyledSettings = ssFamily;
  ResultMemo.TextSettings.WordWrap = True;

  ShowBtn = MyForm.AddNewProButton(MyForm,'ShowBtn','Show JSON');
  ShowBtn.Align = alBottom;
  ShowBtn.Margins.Left = 15;
  ShowBtn.Margins.Right = 15;
  ShowBtn.Margins.Bottom = 10;
  ShowBtn.OnClick = 'GenerateJSON';

  MyForm.Run;
}


Output:

TCLJSONExample.png

See Also