In C++Builder 10.2.2, Embarcadero has added a new component to the FireDAC BatchMove architecture, to support generating JSON data from database tables, in an easy and flexible way.
RAD Studio (Delphi and C++ Builder) has offered support for JSON in different ways and for a long time. From the JSON system unit (originally part of the DBX space) to the DataSnap table mapping (also via DBX) and to FireDAC tables to JSON mapping, there are many ways to interact with JSON data structures. As an enhancement, in the 10.2.2 release, we now added the capability to map a dataset to a custom JSON structure -- the FireDAC JSON support produces a FireDAC specific structure,metadataa data, and record status information.
Important to note is the C++ Builder 10.2.2 Enterprise and above edition includes a RAD Server free deployment license ($5000 value) to allow you to use RAD Server as your backend, and consider using Sencha ExtJS for your Web Clients!
A RAD Server package Application
1. Create a File | New Items| C++ Builder Projects | EMS Package
2. Select “Create a package with resource”
3. Resource name = employee File type = select “Data Module”
4. Select only the GET Endpoint. For this example we will only show how to implement the GET endpoint to return data from a database table:
5. Click Finish.
6. On the generated Data Module form, add these 5 components from the tool palette:
FDConnection - to establish a connection to a DBMS and to manage associated datasets.
FDQuery - to execute SQL queries, browse the result sets, and edit the result set records.
FDBatchMove - to move data between text files and tables using datasets.
FDBAtchMoveDataSetReader - to define the source dataset from which to load information using TFDBatchMove.
FDBAtchMoveJSONWriter - is the base abstract class for all batch move JSON reader and JSON writer classes.
Configure the components, like this:
object EmployeeConnection: TFDConnection
Params.Strings = (
object EmployeeTable: TFDQuery
Connection = EmployeeConnection
SQL.Strings = (
'SELECT * FROM EMPLOYEE')
object FDBatchMoveDataSetReader1: TFDBatchMoveDataSetReader
DataSet = EmployeeTable
object FDBatchMoveJSONWriter1: TFDBatchMoveJSONWriter
DataDef.Fields = <>
object FDBatchMove1: TFDBatchMove
Reader = FDBatchMoveDataSetReader1
Writer = FDBatchMoveJSONWriter1
Mappings = <>
LogFileName = 'C:\Desktop\Data.log'
With this configuration in place, all you need to do to produce the JSON is connect the output to the JSON writer and execute the batch move operation!
C++ Builder implementation
So far we added to an EMS package an "employee" resource, and we can implement its Get operation with the following C++ stream-based code:
#include <memory> //for STL auto_ptr class
void TEmployeeResource1::Get(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse)
FDBatchMoveJSONWriter1->JsonWriter = AResponse->Body->JSONWriter;
The above code is using the JSONWriter property. With this code the data is written directly to the HTML response, reducing the work and the memory consumption!
Start your C++ Builder RAD Server:
And call your REST Endpoint for the GET: http://localhost:8080/employee.
You will get this JSON Array returned for the InterBase Employee table data:
Please login first in order for you to submit comments