原工程地址:https://github.com/viniciussanchez/dataset-serialize 由于使用的了高版本的类信息所以无法在Delphi7中无法使用,这里将其他修改为使用superobject来做JSON部分的处理
测试环境为Delphi7 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.
[Optional]
For ease I recommend using the 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
添加Search Path Project > Options > Resource Compiler > Directories and Conditionals > Include file search path
../dataset-serialize/src
../dataset-serialize/src/core
../dataset-serialize/src/helpers
../dataset-serialize/src/providers
../dataset-serialize/src/types
All features offered by DataSet Serialize are located in the class helper in unit DataSetSerializeHelper. To get your project started, simply add your reference where your functionality is needed. Here's an example:
uses DataSetSerializeHelper;
Let's now look at each feature, its rules and peculiarities, to deliver the best to all users.
Creating a JSON object with information from a DataSet record seems like a very simple task for Delphi users. 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:
var
LJSONArray: TsuperArray;
LJSONObject: IsuperObject;
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
{}
) will be returned;ToJSONArray
[]
) will be returned;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:
var
LJSONArray: TSuperArray;
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
[]
) will be returned;LoadStructure
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:
begin
LJSONArray := qrySamples.ValidateJSON('{"country":"Brazil"}');
end;
Upon receiving {"country": "Brazil"}
, assuming our DataSet has 3 fields (ID, NAME, COUNTRY), and the ID and NAME field are required, the following will be returned:
[{"field":"ID","error":"Id not informed"},{"field":"NAME","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
[]
) is returned;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:
begin
qrySamples.LoadFromJSON('{"NAME":"Vinicius Sanchez","COUNTRY":"Brazil"}');
end;
Parameters
AOwns
- Indicates who is responsible for destroying the passed JSON as a parameter;LoadFromJSON
With DataSet Serialize you can still change the DataSet registration simply by using MergeFromJSONObject. The function is similar to LoadFromJSON. An example of use is for REST servers when the verb used in the request is PUT (not necessarily), in this case we do not want to include a new record but to change the current record.
begin
qrySamples.MergeFromJSONObject('{"NAME":"Vinicius","COUNTRY":"United States"}');
end;
Parameters
AOwns
- Indicates who is responsible for destroying the passed JSON as a parameter;MergeFromJSONObject
Check out our sample project for each situation presented above in operation. If you have any questions or suggestion, please contact, make your pull request or create an issue.
Alone we go faster. Together we go further.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。