TTMSFNCDataLinkJSON
TTMSFNCCustomDataLinkJSON
is designed for working with JSON data, allowing the manipulation of records, and providing functionality to load and save JSON data from/to streams. It also supports metadata handling and custom data root elements.
The TTMSFNCCustomDataLinkJSON
component can be linked to a TTMSFNCDataSet
component, allowing seamless integration of JSON data into a dataset for display, manipulation, and binding within your application.
Loading a file
To load a JSON file into the TTMSFNCCustomDataLinkJSON
component, there are two options available: via the FileName
property or programmatically using the LoadFromFile
and LoadFromStream
methods.
When using the FileName
property, the file is loaded automatically at design time.
In both cases, the DataRootElement
and MetaDataElement
properties play an important role. If DataRootElement
is left empty, the JSON file is expected to be a formatted array where each item has an identical structure. If MetaDataElement
is specified, it should contain field definitions where each field includes an "id"
(the actual JSON property name) and a "name"
(the display name in the dataset). This allows for dynamic mapping between the JSON properties and the dataset fields, ensuring smooth data handling.
{
"columns": [
{
"id": "name",
"name": "Name"
},
{
"id": "email",
"name": "Email"
},
{
"id": "phoneNumber",
"name": "Phone Number"
}
],
"data": [
{
"name": "John",
"email": "john@example.com",
"phoneNumber": "(353) 01 222 3333"
},
{
"name": "Mark",
"email": "mark@gmail.com",
"phoneNumber": "(01) 22 888 4444"
}
]
}
Automatic saving
The TTMSFNCCustomDataLinkJSON
class supports automatic saving of changes through the AutoSaveChanges
property. When this property is set to True
, any modifications made to the data are automatically saved without requiring an explicit call to save methods. This ensures that changes are persisted seamlessly as they occur.
DateTime format
The DateTimeFormat
property specifies the format used to handle date and time values in the JSON data. If this property is left empty, the default ISO 8601 format (yyyy-mm-ddThh:nn:ss.zzzZ
) is used to detect, convert, and save dates as TDateTime
.
When a custom format is required, you can specify it using the same patterns supported by Delphi's date and time formatting functions, such as those used with FormatDateTime
. For example:
Methods
Method name | Description |
---|---|
LoadFromFile(AFileName: string; AEncoding: TEncoding) | Loads JSON data from a specified file with the given encoding. |
LoadFromStream(AStream: TStream; AEncoding: TEncoding) | Loads JSON data from a stream with a specific encoding. |
SaveToFile(AFileName: string; AEncoding: TEncoding) | Saves JSON data to a specified file using the given encoding. |
SaveToStream(AStream: TStream; AEncoding: TEncoding) | Saves the current data to a stream in JSON format with a specific encoding. |
Properties
Property name | Description |
---|---|
AutoSaveChanges: Boolean | Controls whether changes to the data are automatically saved. |
DataRootElement: string | Specifies the root element in the JSON structure where the data records are located. |
DateTimeFormat: string | Specifies the DateTime format to be used. Default is empty, in which case the ISO8601 format is used. |
FileEncoding | Defines the encoding to be used for the file operations (loading and saving data). |
FileName: string | The name of the CSV file associated with the data link. It is used when loading and saving data to a file. |
MetaDataElement: string | Specifies the element in the JSON structure where metadata information is stored. |