<a href="https://github.com/viniciussanchez/dataset-serialize/blob/master/img/dataset-serialize.png">
<img alt="DataSet-Serialize" height="120" src="https://github.com/viniciussanchez/dataset-serialize/blob/master/img/dataset-serialize.png">
</a>
# DataSet Serialize for Delphi and Lazarus (FPC)
![Delphi Supported Versions](https://img.shields.io/badge/Delphi%20Supported%20Versions-XE3..10.3%20Rio-blue.svg)
![Platforms](https://img.shields.io/badge/Supported%20platforms-Win32%20and%20Win64-red.svg)
DataSet Serialize is a set of features to make working with JSON and DataSet simple. It has features such as exporting or importing records into a DataSet, validate if JSON has all required attributes (previously entered in the DataSet), exporting or importing the structure of DataSet fields in JSON format. In addition to managing nested JSON through master detail or using TDataSetField (you choose the way that suits you best). All this using class helpers, which makes it even simpler and easier to use.
## Prerequisites for Delphi
* `[Optional]` For ease I recommend using the [**Boss**](https://github.com/HashLoad/boss) (Dependency Manager for Delphi) for installation, simply by running the command below on a terminal (Windows PowerShell for example):
```
boss install github.com/viniciussanchez/dataset-serialize
```
## Manual Installation for Delphi
If you choose to install manually, simply add the following folders to your project, in *Project > Options > Resource Compiler > Directories and Conditionals > Include file search path*
```
../dataset-serialize/src
```
## Getting Started
All features offered by DataSet Serialize are located in the class helper in unit DataSet.Serialize. To get your project started, simply add your reference where your functionality is needed. Here's an example:
```pascal
uses DataSet.Serialize;
```
Let's now look at each feature, its rules and peculiarities, to deliver the best to all users.
## DataSet to JSON
Creating a JSON object with information from a DataSet record seems like a very simple task. But that task just got easier. DataSet Serialize has two functions for this, namely ToJSONObject and ToJSONArray. Let's look at the use of the functions:
```pascal
var
LJSONArray: TJSONArray;
LJSONObject: TJSONObject;
begin
LJSONObject := qrySamples.ToJSONObject(); // export a single record
LJSONArray := qrySamples.ToJSONArray(); // export all records
end;
```
What is the difference between the two functions? ToJSONObject will only convert the current DataSet record to a TJSONObject. ToJSONArray will convert to a TJSONArray all the records of the DataSet and not just the selected record.
**Parameters**
* `AChildRecords` - Indicates whether or not to export child records (via master detail or TDataSetField).
* `AOnlyUpdatedRecords` - Indicates whether to export only records that have been modified (records added, changed, or deleted). This feature is only available when using FireDAC. The CachedUpdates property must be True;
**ToJSONObject**
* If the DataSet is empty or not assigned, a blank JSON object (`{}`) will be returned;
* The field that does not have the visible (True) property will be ignored.
* The attribute name in JSON will always be the field name in lower case, even if the field name is in upper case;
* If the field is of type TDataSetField, a nested JSON is generated (JSONObject if it is just a child record, or JSONArray if it is more than one). The most suitable way for this type of situation is to create a master detail;
* All child records will be exported as a JSONArray;
* When using the AOnlyUpdatedRecords parameter of the ToJSONObject or ToJSONArray method, the JSON item is added to an "object_state" property responsible for defining what happened to the record (deleted, included or changed);
* When a JSON array is created to represent a nested JSON using Master Detail, the DataSet name will be the name of the generated JSON attribute. There is a rule where if the DataSet name starts with qry (query) or mt (memtable), these initials are ignored, leaving only the rest as the attribute name in JSON.
**ToJSONArray**
* If the DataSet is empty or not assigned, a blank JSON array (`[]`) will be returned;
* Follows the same rules as ToJSONObject;
## Save and load structure
A not very useful but important feature is SaveStructure and LoadStructure. As the name already said, it is possible to save the entire structure of fields configured in the DataSet and also load a structure in JSON format. Here's an example of how to load and export DataSet fields:
```pascal
var
LJSONArray: TJSONArray;
begin
LJSONArray := qrySamples.SaveStructure;
qrySamples.LoadStructure(LJSONArray, True);
end;
```
The following properties are controlled:
`Alignment, FieldName, DisplayLabel, DataType, Size, Key, Origin, Required, Visible, ReadOnly, and AutoGenerateValue`;
**Parameters**
* `AOwns` - Indicates who is responsible for destroying the passed JSON as a parameter.
**SaveStructure**
* If the field count of DataSet equals zero, a blank JSON array (`[]`) will be returned;
**LoadStructure**
* DataSet cannot be activated and Must not have fields defined;
## Validate JSON
The ValidateJSON function is very useful when we want to validate on a server for example if the JSON we received in the request has all the required information. Practically, all fields in the DataSet are traversed, checking if the required fields were entered in JSON. If the field is required and has not been entered in JSON, it will be added to the JSON Array returned by the function. See the example below:
```pascal
begin
LJSONArray := qrySamples.ValidateJSON('{"country":"Brazil"}');
end;
```
Upon receiving `{"country": "Brazil"}`, assuming our DataSet has 3 fields (ID, FIRST_NAME, COUNTRY), and the ID and FIRST_NAME field are required, the following will be returned:
```
[{"field":"id","error":"Id not informed"},{"field":"firstName","error":"Name not informed"}]
```
**Parameters**
* `ALang` - Responsible for changing the language used in the assembly of messages (default is English);
* `AOwns` - Indicates who is responsible for destroying the passed JSON as a parameter;
**ValidateJSON**
* If JSON is not assigned or fields count equals zero an exception is raised.
* The default language of messages is English;
* Even if all required fields are entered, an empty JSON array (`[]`) is returned;
* A required field must have its Required property equal to True.
* The DisplayLabel property can be used to customize the message;
## Load from JSON
DataSet Serializa allows you to load a DataSet with a JSONObject, JSONArray and even a nested JSON all summarized in one method: LoadFromJSON(). Here's an example of how to use it:
```pascal
begin
qrySamples.LoadFromJSON('{"firstName":"Vinicius Sanchez","country":"Brazil"}');
end;
```
**Parameters**
* `AOwns` - Indicates who is responsible for destroying the passed JSON as a parameter;
**LoadFromJSON**
* If JSON or DataSet has not been assigned, nothing will be done;
* If DataSet is not activated and is not a TFDMemTable class, nothing will be done;
* If the DataSet is of type TFDMemTable, it's not active and fields count equals zero, the fields will be created according to the JSON need. The DataType will be equal to ftString and Size equal to 4096;
* When the "object_state" property is in JSON, the following validations are made:
* If equal to INSERTED, an Append is performed on the DataSet;
* If it is MODIFIED or DELETED, a Locate is made in the DataSet to find the record to be manipulated. If the registry is not found, nothing will be done. To execute Locate, a search for Key Fields is done within JSON;
* If MODIFIED equals the Edit method of the DataSet;
* If DELETED equals the Delete method of the DataSet;
* If the "object_state" property is not found in JSON, then the Append method is called;
* When loading a DataSet with JSON, fields that are ReadOnly
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
利用Horse-Paginate (delphi中间件)做分页 -源码 (238个子文件)
dcov_execute_console.bat 143B
dcov_execute_vcl.bat 131B
Horse.Core.RouterTree.dcu 386KB
DataSet.Serialize.Import.dcu 200KB
Horse.Core.dcu 138KB
Horse.HTTP.dcu 131KB
Horse.Core.Param.dcu 118KB
Horse.dcu 73KB
DataSet.Serialize.Export.dcu 67KB
Horse.Core.Param.Field.dcu 66KB
Horse.Core.Param.Header.dcu 56KB
Horse.Paginate.dcu 56KB
Horse.Provider.Console.dcu 24KB
Horse.Core.Group.dcu 13KB
Unit2.dcu 12KB
DataSet.Serialize.dcu 11KB
DataSet.Serialize.Utils.dcu 11KB
Horse.Core.Route.dcu 9KB
DataSet.Serialize.Config.dcu 9KB
Horse.Commons.dcu 7KB
Horse.Exception.dcu 6KB
Horse.Provider.Abstract.dcu 5KB
Horse.Provider.IOHandleSSL.dcu 5KB
Web.WebConst.dcu 5KB
Horse.Jhonson.dcu 5KB
Horse.WebModule.dcu 5KB
Horse.Rtti.dcu 4KB
Horse.Core.Group.Contract.dcu 4KB
Horse.Core.Route.Contract.dcu 2KB
DataSet.Serialize.Consts.dcu 2KB
DataSet.Serialize.UpdatedStatus.dcu 2KB
Horse.Constants.dcu 661B
DataSet.Serialize.Language.dcu 608B
Horse.Proc.dcu 466B
Horse.Provider.Daemon.dcu 421B
Horse.Provider.Apache.dcu 419B
Horse.Provider.ISAPI.dcu 415B
Horse.Provider.VCL.dcu 412B
Horse.Provider.CGI.dcu 409B
mobileD11.deployproj 13KB
DataSet.Serialize.Samples.Basic.dfm 30KB
DataSet.Serialize.Samples.Master.Detail.dfm 21KB
DataSet.Serialize.Samples.Configuration.dfm 13KB
Unit2.dfm 1015B
Main.Form.dfm 1007B
Unit2.dfm 993B
Horse.WebModule.dfm 232B
Main.Service.dfm 219B
horsepkg.dpk 2KB
jhonson.dpk 634B
Console.dpr 3KB
VCL.dpr 3KB
Apache.dpr 1KB
WinSvc.dpr 973B
master_detail.dpr 914B
configuration.dpr 914B
basic.dpr 890B
mobileD11.dpr 889B
Samples.dpr 599B
Console.dpr 482B
Samples.dpr 369B
Project2.dpr 255B
VCL.dpr 244B
Daemon.dpr 241B
CGI.dpr 221B
ISAPI.dpr 204B
mobileD11.dproj 84KB
Daemon.dproj 61KB
Console.dproj 60KB
Console.dproj 59KB
WinSvc.dproj 58KB
VCL.dproj 58KB
basic.dproj 58KB
Apache.dproj 52KB
ISAPI.dproj 51KB
Project2.dproj 51KB
configuration.dproj 50KB
horsepkg.dproj 49KB
Samples.dproj 47KB
master_detail.dproj 47KB
VCL.dproj 46KB
Samples.dproj 46KB
jhonson.dproj 43KB
CGI.dproj 20KB
DataSet.Serialize.Samples.Mobile.fmx 15KB
.gitignore 2KB
.gitignore 2KB
.gitignore 394B
.gitignore 279B
Samples.groupproj 4KB
DataSet.Serialize.Samples.groupproj 2KB
DUnitX.groupproj 2KB
basic.ico 134KB
configuration.ico 134KB
__recovery.ini 198B
boss-lock.json 945B
boss-lock.json 937B
boss-lock.json 722B
boss-lock.json 722B
boss.json 340B
共 238 条
- 1
- 2
- 3
zjg076000
- 粉丝: 17
- 资源: 64
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0