C++ Builder 10.2.2: DataSet Mapping to JSON for JavaScript client support

Posted by on in Blogs

Introduction

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!

Embarcadero’s first step in simplifying the use of Delphi and C++ Builder RAD Server as a backend for Sencha EXTJS JavaScript applications is offering a better way to produce the JSON data from a database table.

While we have built this support for the scenario of using RAD Server, FireDAC, and ExtJS, the same component and technology can be used for any web service architecture written in Delphi and C++Builder (even pure and simple WebBroker), any dataset other than FireDAC, and any JavaScript client. It is a completely open and a fairly flexible solution. But it certainly works great for our specific scenario of RAD Server backend and Sencha ExtJS Web Client!

A RAD Server package Application

1. Create a File | New Items| C++ Builder Projects | EMS Package

CBuilderEMSPackage

2. Select “Create a package with resource

CBuilderEMSPackageResource

3.  Resource name = employee    File type = select “Data Module

CBuilderResourceName

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:

CBuilderGET

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.

FDBatchMove

 Configure the components, like this:

      object EmployeeConnection: TFDConnection

    Params.Strings = (

      'ConnectionDef=EMPLOYEE')

  end

      object EmployeeTable: TFDQuery

    Connection = EmployeeConnection

    SQL.Strings = (

      'SELECT * FROM EMPLOYEE')

  end

      object FDBatchMoveDataSetReader1: TFDBatchMoveDataSetReader

     DataSet = EmployeeTable

  end

  object FDBatchMoveJSONWriter1: TFDBatchMoveJSONWriter

    DataDef.Fields = <>

  end

  object FDBatchMove1: TFDBatchMove

    Reader = FDBatchMoveDataSetReader1

    Writer = FDBatchMoveJSONWriter1

    Mappings = <>

    LogFileName = 'C:\Desktop\Data.log'

  end

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;

   FDBatchMove1->Execute();

}

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:

CBuilderRADServer

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:

CBuilderGETEndpointCall

This is great that C++ Builder 10.2.2 added this new DataSet Mapping to JSON for JavaScript client support!  And this is only a first initial step of the support that Embarcadero RAD Studio, C++ Builder, and Delphi is planning to simplify the use of C++ Builder and RAD Server as the backend for JavaScript and ExtJS applications. More support will be made available soon. Stay tuned!

 



About
Gold User, Rank: 90, Points: 4
Al Mannarino has 25+ years of software development experience, including object-oriented analysis and design (OOAD) and developing and deploying production applications. He is currently a Principal Software Consultant and Evangelist for Embarcadero Technologies. Prior to joining Embarcadero, Al spent three years working with CodeGear, a division of Borland that was acquired by Embarcadero in 2008. He also worked for five years as a lead systems engineer for Borland supporting application lifecycle management, software delivery optimization and developer tools solutions. Prior to Borland, Al served as a systems engineer for companies including Objectivity, Versant, Red Brick Systems, Information Builders, and was an electrical engineer for Grumman Aerospace performing application implementations on complex electrical-mechanical systems. Al has a bachelor's of science degree in electrical engineering from Manhattan College.

Comments

Check out more tips and tricks in this development video: