You are not logged in.
Hi,
For reporting purposes I need to load some JSON content into a TDataset descendant.
So I tried to use JSONTableToDataSet function from mORMotVCL unit as below
(notice that TSQLRecordPerson.LastName property is declared without an index specifier):
program str_prop_indx;
{$IFDEF FPC}
{$MODE DELPHI}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
SysUtils, SynCommons, mORMot, mORMotVCL;
type
TSQLRecordPerson = class(TSQLRecord)
private
FFirstName: RawUTF8;
FLastName: RawUTF8;
published
// with index specifier
property FirstName: RawUTF8 index 30 read FFirstName write FFirstName;
// with NO index specifier
property LastName: RawUTF8 read FLastName write FLastName;
end;
var
lJSONStr: RawUTF8;
lSynSQLTableDS: TSynSQLTableDataSet;
lSQLTableJSON: TSQLTableJSON;
begin
lJSONStr := '[{"FirstName":"John", "LastName":"Doe"},{"FirstName":"Mark", "LastName":"Twain"}]';
lSynSQLTableDS := mORMotVCL.JSONTableToDataSet(nil, lJSONStr, [TSQLRecordPerson]);
try
lSynSQLTableDS.First;
while not lSynSQLTableDS.Eof do
begin
Write(' FirstName="' + lSynSQLTableDS.FieldByName('FirstName').AsString + '"');
Write(' LastName="' + lSynSQLTableDS.FieldByName('LastName').AsString + '"');
WriteLn;
lSynSQLTableDS.Next;
end;
Writeln('RecordCount=' + IntToStr(lSynSQLTableDS.RecordCount));
Write(' FirstName DataSize=', lSynSQLTableDS.FieldByName('FirstName').DataSize);
Write(' LastName DataSize=', lSynSQLTableDS.FieldByName('LastName').DataSize);
WriteLn;
finally
lSynSQLTableDS.Free;
end;
WriteLn('Press Enter to exit ...');
ReadLn;
end.
Delphi 7 output is:
FirstName="John" LastName="Doe"
FirstName="Mark" LastName="Twain"
RecordCount=2
FirstName DataSize=31 LastName DataSize=6
Press Enter to exit ...
FPC output is:
FirstName="John" LastName="D"
FirstName="Mark" LastName="T"
RecordCount=2
FirstName DataSize=31 LastName DataSize=2
Press Enter to exit ...
In case of FPC the content of the LastName field is truncated to 1 character.
During the JSONTableToDataSet call the JSON string is loaded into a TSQLTableJSON
and then its content will initialize a TSynSQLTableDataSet.
I suspect that something goes wrong in the method TSQLTable.FieldLengthMax() but I was unable to find what exactly.
Can someone look into this.
Thank you
Offline