TDBXJsonTools copy/convert data between TDataSet – TJSONObject – DBXReader

Categories:Delphi, FireMonkeyTags: , ,

You can copy data from DBXReader to ClientDataSet and vice-versa using dbxExpress framework, you probably already read some of my blog posts where I explain how it works in desktop applications, however in mobile platforms we don’t have ClientDataSet and in case you want something similar you always, what can you do?

DataSnap Mobile Connectors has a class called TDBXJsonTools, this class allows developers to copy/convert data between DBXReader, TJSONObject, DBXReader and other types, you can do that in any mobile platform supported by the connectors, like: Java/Android, Java/BlackBerry, Objective-C, C#/Windows Phone and FreePascal.

In a native desktop application you would use TDBXDataSetReader.CopyReaderToClientDataSet(Reader, ClientDataSet) to copy the DBXReader to a ClientDataSet, in the mobile apps you will use TDBXJsonTools.CreateTDataSetFromJSON(DBXReader.asJSONObject).

Meanwhile, this is not only about copy/convert data, it gives you several ways to interact and transport data using different types and take advantage of the native types in each platform. For example, when using DataSet in FireMonkey on iOS it will use TDataSet class from FreePascal/Delphi RTL, when using with Java, TDataSet will be extension of TDBXReader and so on.

DBXReader is unidirectional and navigation is limited, when using FireMonkey on iOS developers can copy the DBXReader data to a DataSet, which is bi-directional, it will enable the navigation and use of other DataSet features.

The following code represents the TDBXJsonTools interface for FreePascal.

  TDBXJsonTools = class
  public
    class procedure jsonToDBX(obj: TJSONValue; var value: TDBXValue; dbxTypeName: String);
    class procedure JSONToValueType(json: TJSONArray; var vt: TDBXValueType);
    class function DBXParametersToJSONObject(dbxParameters: TDSParams) : TJSONObject;
    class function DBXReaderToJSONObject(dbxReader: TDBXReader): TJSONObject;
    class function CreateTDataSetFromJSON(value: TJSONObject): TDataset;
    class function TDataSetToJSONObject(value: TDataset): TJSONObject;
    class function GetTFieldTypeByTDBXDataTypes(DBXDataTypes: TDBXDataTypes) : TFieldType;
    class function GetTDBXDataTypesByTFieldType(FieldType: TFieldType) : TDBXDataTypes;
    class function CreateTStreamFromJSONArray(value: TJSONArray): TStream;
    class function StreamToJSONArray(value: TStream): TJSONArray;
    class function JSONToTableType(value: TJSONValue; dbxTypeName: String): TObject;
    class function SerializeTableType(Objetc: TObject): TJSONObject;

  end;

My recommendation is to continue using DBXReader as the primarily source of data, and when necessary take advantage of the TDBXJsonTools class.

One Response to TDBXJsonTools copy/convert data between TDataSet – TJSONObject – DBXReader

  1. Reply Alfredo says:

    Quais as versões o Delphi compatíveis com o componente? Trata-se de componente pago ou livre?

    Att
    Alfredo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>