Back to Devexpress

TdxBackendInMemoryJSONConnection Class

vcl-dxbackend-dot-connectionstring-dot-json.md

latest17.7 KB
Original Source

TdxBackendInMemoryJSONConnection Class

A component designed for interaction with data stored in memory.

Declaration

delphi
TdxBackendInMemoryJSONConnection = class(
    TdxBackendCustomInMemoryJSONConnection
)

Remarks

Data connection components allow you to bind TdxDashboard/TdxDashboardControl and TdxReport components to data.

Main API Members

The list below outlines key members of the TdxBackendInMemoryJSONConnection class. These members allow you to configure core data connection settings.

Data Connection Settings

ActiveSpecifies if the data connection is active.DisplayName

Specifies the data connection’s name both in report/UI layout template designer and Collection Editor dialogs.

The DisplayName property value is used to specify the data connection name within report/UI layout templates.

ConnectionString

Specifies a connection string (or stores serialized data in the JSON format).

Tip

Refer to the following help topic for detailed information and step-by-step instructions: How to Use Memory-Based or Remote API Data Sources.

SetJSONValueSets component data to the specified JSON value.

General-Purpose API Members

Collection | IndexSpecify the parent collection component.

Code Examples

Assign JSON Data as a Connection String

The following code example demonstrates an OnClick event handler that configures a TdxBackendInMemoryJSONConnection component, assigns a table defined within a JSON string to the ConnectionString property, and displays the Dashboard Designer dialog:

delphi
uses
  dxDashboard.Control, // Declares the TdxDashboardControl component
  dxBackend.ConnectionString.JSON; // Declares the TdxBackendInMemoryJSONConnection component
// ...

procedure TMyForm.cxDisplayDesignerButtonClick(Sender: TObject);
begin
  // Specify a user-friendly data connection name (for end-user dialogs):
  dxBackendInMemoryJSONConnection1.DisplayName := 'Memory-Based JSON Data Source';
  // Define a table that consists of three columns ("id", "Region", and "Sales") and five data rows:
  dxBackendInMemoryJSONConnection1.ConnectionString := 'Json=''' +

    '[{"id": 1, "Region": "Asia", "Sales": 4.7685},' + // Row #1
     '{"id": 2, "Region": "Australia", "Sales": 1.9576},' + // Row #2
     '{"id": 3, "Region": "Europe", "Sales": 3.3579},' + // Row #3
     '{"id": 4, "Region": "North America", "Sales": 3.7477},' + // Row #4
     '{"id": 5, "Region": "South America", "Sales": 1.8237}]'''; // Row #5

  dxDashboardControl1.ShowDesigner; // Displays the "Dashboard Designer" dialog
end;
cpp
#include "dxDashboard.Control.hpp" // Declares the TdxDashboardControl component
#include "dxBackend.ConnectionString.JSON.hpp" // Declares the TdxBackendInMemoryJSONConnection component

// Add the following linker directives to the corresponding CPP source file:
#pragma link "dxDashboard.Control" // Required to use dxDashboard.Control.hpp declarations
#if defined(_WIN64) // Required to use dxBackend.ConnectionString.JSON.hpp declarations
  #pragma link "dxBackend.ConnectionString.JSON.o"
#else
  #pragma link "dxBackend.ConnectionString.JSON.obj"
#endif
// ...

void __fastcall TMyForm::cxDisplayDesignerButtonClick(TObject *Sender)
{
  // Specify a user-friendly data connection name (for end-user dialogs):
  dxBackendInMemoryJSONConnection1->DisplayName = "Memory-Based JSON Data Source";
  // Define a table that consists of three columns ("id", "Region", and "Sales") and five data rows:
  dxBackendInMemoryJSONConnection1->ConnectionString = "Json=\"\""

    L"[{\"id\": 1, \"Region\": \"Asia\", \"Sales\": 1.9576}," // Row #1
     L"{\"id\": 2, \"Region\": \"Australia\", \"Sales\": 1.9576}," // Row #2
     L"{\"id\": 3, \"Region\": \"Europe\", \"Sales\": 3.3579}," // Row #3
     L"{\"id\": 4, \"Region\": \"North America\", \"Sales\": 3.7477}," // Row #4
     L"{\"id\": 5, \"Region\": \"South America\", \"Sales\": 1.8237}]\"\"" // Row #5

  dxDashboardControl1->ShowDesigner(); // Displays the "Dashboard Designer" dialog
}

Assign a JSON Data String Directly

The following code example demonstrates an OnClick event handler that calls the SetJSONValue procedure to assign a table defined within a JSON string to the TdxBackendInMemoryJSONConnection component and displays the Dashboard Designer dialog:

delphi
uses
  dxDashboard.Control, // Declares the TdxDashboardControl component
  dxBackend.ConnectionString.JSON; // Declares the TdxBackendInMemoryJSONConnection component
// ...

procedure TMyForm.cxDisplayDesignerButtonClick(Sender: TObject);
var
  AJSONData: string;
begin
  // Define a table that consists of three columns ("id", "Region", and "Sales") and five data rows:
  AJSONData :=

  '[{"id": 1, "Region": "Asia", "Sales": 4.7685},' + // Row #1
   '{"id": 2, "Region": "Australia", "Sales": 1.9576},' + // Row #2
   '{"id": 3, "Region": "Europe", "Sales": 3.3579},' + // Row #3
   '{"id": 4, "Region": "North America", "Sales": 3.7477},' + // Row #4
   '{"id": 5, "Region": "South America", "Sales": 1.8237}]'; // Row #5

  // Specify a user-friendly data connection name (for end-user dialogs):
  dxBackendInMemoryJSONConnection1.DisplayName := 'Memory-Based JSON Data Storage';
  dxBackendInMemoryJSONConnection1.SetJSONValue(AJSONData); // Assigns the defined JSON data string
  dxDashboardControl1.ShowDesigner; // Displays the Dashboard Designer dialog
end;
cpp
#include "dxDashboard.Control.hpp" // Declares the TdxDashboardControl component
#include "dxBackend.ConnectionString.JSON.hpp" // Declares the TdxBackendInMemoryJSONConnection component

// Add the following linker directives to the corresponding CPP source file:
#pragma link "dxDashboard.Control" // Required to use dxDashboard.Control.hpp declarations
#if defined(_WIN64) // Required to use dxBackend.ConnectionString.JSON.hpp declarations
  #pragma link "dxBackend.ConnectionString.JSON.o"
#else
  #pragma link "dxBackend.ConnectionString.JSON.obj"
#endif
// ...

void __fastcall TMyForm::cxDisplayDesignerButtonClick(TObject *Sender)
{
  // Define a table that consists of three columns ("id", "Region", and "Sales") and five data rows:
  UnicodeString AJSONData =

    L"[{\"id\": 1, \"Region\": \"Asia\", \"Sales\": 1.9576}," // Row #1
     L"{\"id\": 2, \"Region\": \"Australia\", \"Sales\": 1.9576}," // Row #2
     L"{\"id\": 3, \"Region\": \"Europe\", \"Sales\": 3.3579}," // Row #3
     L"{\"id\": 4, \"Region\": \"North America\", \"Sales\": 3.7477}," // Row #4
     L"{\"id\": 5, \"Region\": \"South America\", \"Sales\": 1.8237}]" // Row #5

  // Specify a user-friendly data connection name (for end-user dialogs):
  dxBackendInMemoryJSONConnection1->DisplayName = "Memory-Based JSON Data Source";
  dxBackendInMemoryJSONConnection1->SetJSONValue(AJSONData); // Assigns the defined JSON data string
  dxDashboardControl1->ShowDesigner(); // Displays the "Dashboard Designer" dialog
}

Load JSON Data from a Remote Source

The code example in this section demonstrates an OnClick event handler that configures a TdxBackendInMemoryJSONConnection component used to load JSON data from an external source using a connection string and displays the Dashboard Designer dialog.

Note

This code example demonstrates a sample connection string. You must replace specified parameter values with required URI, username, password, access tokens, etc.

delphi
uses
  dxDashboard.Control, // Declares the TdxDashboardControl component
  dxBackend.ConnectionString.JSON; // Declares the TdxBackendInMemoryJSONConnection component
// ...

procedure TMyForm::cxDisplayDesignerButtonClick(Sender: TObject);
begin
  dxBackendInMemoryJSONConnection1.Active := False; // Terminates the current connection (if one exists)
  // Specify a user-friendly data connection name (for end-user dialogs):
  dxBackendInMemoryJSONConnection1.DisplayName := 'Memory-Based JSON Data Source';
  dxBackendInMemoryJSONConnection1.ConnectionString :=

    'Uri=https://northwind.netcore.io/customers.json;' + // Specifies the path to a source JSON file
    'Username=user;' + // Specifies a valid user name
    'Password=pwd;' + // Specifies the corresponding password for the user name
    'MyAuthHeader1=secretToken1;MyAuthHeader2=secretToken2;' + // Specifies authentication tokens
    'query:id=123456;query:name=MyName;' // Specifies the required query ID and name

  dxBackendInMemoryJSONConnection1.Active := True; // Connects to the target Web service endpoint
  dxDashboardControl1.ShowDesigner(); // Displays the "Dashboard Designer" dialog
end;
cpp
#include "dxDashboard.Control.hpp" // Declares the TdxDashboardControl component
#include "dxBackend.ConnectionString.JSON.hpp" // Declares the TdxBackendInMemoryJSONConnection component

// Add the following linker directives to the corresponding CPP source file:
#pragma link "dxDashboard.Control" // Required to use dxDashboard.Control.hpp declarations
#if defined(_WIN64) // Required to use dxBackend.ConnectionString.JSON.hpp declarations
  #pragma link "dxBackend.ConnectionString.JSON.o"
#else
  #pragma link "dxBackend.ConnectionString.JSON.obj"
#endif
// ...

void __fastcall TMyForm::cxDisplayDesignerButtonClick(TObject *Sender)
{
  dxBackendInMemoryJSONConnection1->Active = false; // Terminates the current connection (if one exists)
  // Specify a user-friendly data connection name (for end-user dialogs):
  dxBackendInMemoryJSONConnection1->DisplayName = "Memory-Based JSON Data Source";
  dxBackendInMemoryJSONConnection1->ConnectionString =

    L"Uri=https://northwind.netcore.io/customers.json;" // Specifies the path to a source JSON file
    L"Username=user;" // Specifies a valid user name
    L"Password=pwd;" // Specifies the corresponding password for the user name
    L"MyAuthHeader1=secretToken1;MyAuthHeader2=secretToken2;" // Specifies authentication tokens
    L"query:id=123456;query:name=MyName;" // Specifies the required query ID and name

  dxBackendInMemoryJSONConnection1->Active = true; // Connects to the target Web service endpoint
  dxDashboardControl1->ShowDesigner(); // Displays the "Dashboard Designer" dialog
}

Generate Reports Based on In-Memory Data

The following code example loads an XML-based report template (TdxReport.Layout) from a REPX file, populates the template with test data defined in a connection string, and exports the resulting report as a PNG image:

delphi
uses
  dxReport, // Declares the TdxReport component and related types
  dxBackend.ConnectionString.JSON; // Declares the TdxBackendInMemoryJSONConnection component
// ...

procedure TMyForm.Button1Click(Sender: TObject);
var
  AJSONDataConnection: TdxBackendInMemoryJSONConnection;
  AReport: TdxReport;
  AFileStream: TFileStream;
begin
  AJSONDataConnection := TdxBackendInMemoryJSONConnection.Create(Self);
  try
    AJSONDataConnection.Name := 'JSONData';
    // Specify in-memory report data as a connection string
    AJSONDataConnection.ConnectionString :=
      'Json=''[{"id":1, "caption":"test1"},{"id":2, "caption":"test2"}]''';
    AReport := TdxReport.Create(Self);
    try
      AReport.ReportName := 'Report';
      AReport.Layout.LoadFromFile('Report.repx'); // Loads a report template
      AFileStream := TFileStream.Create('Report.png', fmOpenReadWrite);
      try
        AReport.ExportToImage(AFileStream); // Exports the report in the default image export format (PNG)
      finally
        AFileStream.Free;
      end;
    finally
      AReport.Free;
    end;
  finally
    AJSONDataConnection.Free;
  end;
end;
cpp
#include "dxReport.hpp" // Declares the TdxReport component
#include "dxBackend.ConnectionString.JSON.hpp" // Declares the TdxBackendInMemoryJSONConnection component

// Add the following linker directives to the corresponding CPP source file:
#pragma link "dxReport" // Required for dxReport.hpp declarations
#if defined(_WIN64) // Required to use dxBackend.ConnectionString.JSON.hpp declarations
  #pragma link "dxBackend.ConnectionString.JSON.o"
#else
  #pragma link "dxBackend.ConnectionString.JSON.obj"
// ...

void __fastcall TMyForm::Button1Click(TObject *Sender)
{
  TdxBackendInMemoryJSONConnection *AJSONDataConnection;
  TdxReport *AReport;
  TFileStream *AFileStream;
  AJSONDataConnection = new TdxBackendInMemoryJSONConnection(this);
  try
  {
    AJSONDataConnection->Name = "JSONData";
    // Specify in-memory report data as a connection string
    AJSONDataConnection->ConnectionString =
      "Json=\"\"[{\"id\":1, \"caption\":\"test1\"}, {\"id\":2, \"caption\":\"test2\"}]\"";
    AReport = new TdxReport(this);
    try
    {
      AReport->ReportName = "Report";
      AReport->Layout->LoadFromFile("Report.repx"); // Loads a report template
      AFileStream = new TFileStream('Report.png', fmOpenReadWrite);
      try
      {
        AReport->ExportToImage(AFileStream); // Exports the report in the default image export format (PNG)
      }
      __finally
      {
        delete AFileStream;
      }
    }
    __finally
    {
        delete AReport;
    }
  }
  __finally
  {
    delete AJSONDataConnection;
  }
}

Indirect TdxBackendInMemoryJSONConnection Class References

The following public API members reference the TdxBackendInMemoryJSONConnection class as a TdxBackendCustomDataConnection object:

TdxBackendDataConnectionCollection.AddCreates a data connection of the required type and ads the connection to the collection.TdxBackendDataConnectionCollection.ItemsProvides indexed access to all data connection components stored in the collection.TdxBackendDataConnectionManager.ItemsProvides indexed access to stored data connection components.

Other Data Connection Components

TdxBackendDataSetJSONConnectionA component designed to fetch data from one or multiple datasets (TDataSet descendant instances).TdxBackendDatabaseSQLConnectionA non-visual component designed to fetch data from a relational database (SQL Server, PostgreSQL, SQLite, etc.).

To see TdxDashboardControl and TdxReport components in action, run BI Dashboards Designer/Viewer and Report Designer/Viewer demos in the VCL Demo Center installed with compiled DevExpress VCL demos. Click different items in the sidebar on the left to switch between demo features.

Download: Compiled VCL Demos

Tip

You can find full source code for installed compiled Report and Dashboard demos in the following folders:

  • _%PUBLIC%\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressReports_
  • _%PUBLIC%\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressDashboards_

Inheritance

TObject TPersistent TComponent TcxCustomComponent TcxComponentCollectionItem TdxBackendCustomDataConnection TdxBackendCustomInMemoryJSONConnection TdxBackendInMemoryJSONConnection

See Also

Backend for VCL Reports/Dashboards

TdxBackendInMemoryJSONConnection Members

dxBackend.ConnectionString.JSON Unit