From Clomosy Docs
The TCLJSONValue represents the fundamental data element within the TCLJSON library. It serves as a strongly typed container for all supported JSON data types, including string, number, boolean, null, and datetime.
| Feature | Use of | Definition |
|---|---|---|
| Create(AValue: TJSONValue) | val = TCLJSONValue.Create(SomeJSONValue); | Initializes a TCLJSONValue instance by wrapping an existing TJSONValue reference. |
| AsString | text = val.AsString; | Returns the stored value as a string if its type is string-compatible. |
| AsInteger | num = val.AsInteger; | Converts and retrieves the value as an integer type. |
| AsFloat | fnum = val.AsFloat; | Retrieves the numeric value as a floating-point number. |
| AsBoolean | flag = val.AsBoolean; | Returns the stored value as a boolean (True or False). |
| AsDateTime | dt = val.AsDateTime; | Retrieves the stored value as a TDateTime object if it represents a date/time. |
| IsNull | val.IsNull | Checks whether the current value is null. |
| IsString | val.IsString | Returns True if the value is a string type. |
| IsNumber | val.IsNumber | Returns True if the value is numeric (integer or float). |
| IsBoolean | val.IsBoolean | Returns True if the value represents a boolean type. |
| IsDateTime | val.IsDateTime | Returns True if the stored value is a date/time type. |
| ToString | jsonText = val.ToString; | Converts the internal value to its JSON-compatible string representation. |
| GetValueByPath | childVal = val.GetValueByPath('data.user.name'); | Retrieves a nested JSON value using dot notation within a hierarchical structure. |
| Str | val = TCLJSONValue.Str('Hello'); | Creates a TCLJSONValue instance representing a string type. |
| Num | val = TCLJSONValue.Num(25.75); | Creates a TCLJSONValue instance containing a floating-point number. |
| Int | val = TCLJSONValue.Int(100); | Creates a TCLJSONValue instance representing an integer number. |
| Bool | val = TCLJSONValue.Bool(True); | Creates a TCLJSONValue instance representing a boolean value. |
| DT | val = TCLJSONValue.DT(Now); | Creates a TCLJSONValue instance containing a TDateTime value. |
| Null | val = TCLJSONValue.Null; | Creates a TCLJSONValue instance representing a null JSON value. |
| Important: Data Type |
| Avoid mixing data types when reading or writing JSON values; each accessor (AsString, AsFloat) must correspond to the original type. |
Example: TCLJSONValue in Use
var
MyForm: TClForm;
InputEdt: TClProEdit;
StrBtn, NumBtn, BoolBtn, CheckBtn, ClearBtn: TClProButton;
OutputMemo: TClMemo;
JVal: TCLJSONValue;
void CreateStringValue
{
JVal = TCLJSONValue.Str(InputEdt.Text);
OutputMemo.Lines.Text = 'String Value: ' + JVal.AsString;
}
void CreateNumberValue
{
JVal = TCLJSONValue.Num(StrToFloat(InputEdt.Text));
OutputMemo.Lines.Text = 'Number Value: ' + FloatToStr(JVal.AsFloat);
}
void CreateBooleanValue
var
inputLower: string;
{
inputLower = LowerCase(Trim(InputEdt.Text));
if ((inputLower == 'true') || (inputLower == '1') || (inputLower == 'yes'))
JVal = TCLJSONValue.Bool(True)
else
JVal = TCLJSONValue.Bool(False);
if (JVal.AsBoolean)
OutputMemo.Lines.Text = 'Boolean Value: True'
else
OutputMemo.Lines.Text = 'Boolean Value: False';
}
void CheckValueType
var
info: string;
{
if (JVal.IsNull)
info = 'Type: Null'
else if (JVal.IsNumber)
info = 'Type: Number'
else if (JVal.IsString)
info = 'Type: String'
else if (JVal.IsBoolean)
info = 'Type: Boolean'
else if (JVal.IsDateTime)
info = 'Type: DateTime'
else
info = 'Unknown type';
ShowMessage(info);
}
void ClearValue
{
JVal = TCLJSONValue.Null;
OutputMemo.Lines.Text = 'Value cleared (Null)';
}
{
MyForm = TClForm.Create(Self);
InputEdt = MyForm.AddNewProEdit(MyForm, 'InputEdt', 'Enter a value...');
InputEdt.Align = alTop;
InputEdt.Height = 35;
InputEdt.Margins.Left = 10;
InputEdt.Margins.Right = 10;
OutputMemo = MyForm.AddNewMemo(MyForm, 'OutputMemo', 'Output');
OutputMemo.Align = alClient;
OutputMemo.Height = 150;
OutputMemo.Margins.Left = 10;
OutputMemo.Margins.Right = 10;
OutputMemo.TextSettings.WordWrap = True;
StrBtn = MyForm.AddNewProButton(MyForm, 'StrBtn', 'Create String Value');
StrBtn.Align = alBottom;
StrBtn.OnClick = 'CreateStringValue';
NumBtn = MyForm.AddNewProButton(MyForm, 'NumBtn', 'Create Number Value');
NumBtn.Align = alBottom;
NumBtn.OnClick = 'CreateNumberValue';
BoolBtn = MyForm.AddNewProButton(MyForm, 'BoolBtn', 'Create Boolean Value');
BoolBtn.Align = alBottom;
BoolBtn.OnClick = 'CreateBooleanValue';
CheckBtn = MyForm.AddNewProButton(MyForm, 'CheckBtn', 'Check Value Type');
CheckBtn.Align = alBottom;
CheckBtn.OnClick = 'CheckValueType';
ClearBtn = MyForm.AddNewProButton(MyForm, 'ClearBtn', 'Clear Value');
ClearBtn.Align = alBottom;
ClearBtn.OnClick = 'ClearValue';
MyForm.Run;
}
Output:
See Also