Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynArrayLoadJson does not work like Delphi in FPC #266

Closed
idigger opened this issue Jun 26, 2024 · 2 comments
Closed

DynArrayLoadJson does not work like Delphi in FPC #266

idigger opened this issue Jun 26, 2024 · 2 comments

Comments

@idigger
Copy link

idigger commented Jun 26, 2024

System Information

  • Operating system: Windows
  • Processor architecture: x86-64
  • Device: Computer
  • FPC version: 3.3.1 [2024/06/26] for i386, Trunk 0c745fb257
  • Mormot2 version: master 70e2e9c

note: The latest FPC already supports RTTI for records (https://gitlab.com/freepascal.org/fpc/source/-/issues/40798)

Example Project

program project1;

{$ifdef FPC}
{$mode delphi}
{$else}
{$APPTYPE CONSOLE}
{$endif}

{$I mormot.defines.inc}

uses
  {$I mormot.uses.inc}
  {$IFDEF DCC}System.{$ENDIF}Classes, {$IFDEF DCC}System.{$ENDIF}SysUtils,
  mormot.core.base,
  mormot.core.rtti,
  mormot.core.variants,
  mormot.core.json;

type

  {$RTTI EXPLICIT
     PROPERTIES([vcPublished])
     FIELDS([vcPublic])
     METHODS([])}

  TEFloor = packed record
    el: WORD;
    fl: array of Byte;
  end;

  TECtrl = packed record
    ecid: WORD;
    expd: array of Byte;
    efl: array of TEFloor;
    icod: UTF8String;
    pt: array of Integer;
    adv: BYTE;
  end;
  TECtrls = Array of TECtrl;

var
  tmp1, tmp2: UTf8String;
  ec: TECtrls;
begin
  tmp1 := '[{"ecid":12,"expd":[80,17,18],"efl":[{"el":0,"fl":[1,2]},{"el":1,"fl":[4,5]}],"icod":"123456","pt":[1,2,3],"adv":5}]';
  DynArrayLoadJson(ec, tmp1, TypeInfo(TECtrls));
  WriteLn(tmp1);

  tmp2 := DynArraySaveJson(ec, TypeInfo(TECtrls));
  WriteLn(tmp2);

  if tmp1 = tmp2 then WriteLn('ok')
  else                WriteLn('failed');

end.

The above programs are compiled with fpc and run as follows.

# fpc project1.pas -Fic:\mORMot2\src -Fuc:\mORMot2\src\core;c:\mORMot2\src\lib -Flc:\mORMot2\static\i386-win32
Free Pascal Compiler version 3.3.1 [2024/06/26] for i386
Copyright (c) 1993-2024 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling project1.pas
Linking project1.exe
895 lines compiled, 0.3 sec, 563504 bytes code, 20004 bytes data

# project1
[{"ecid":12,"expd":[80,17,18],"efl":[{"el":0,"fl":[1,2]},{"el":1,"fl":[4,5]}],"icod":"123456","pt":[1,2,3],"adv":5}]
[]
failed

Relevant 3rd party information

The above program is compiled by delphi and the results are as follows

# dcc32 project1.pas -Ic:\mORMot2\src;c:\mORMot2\static\i386-win32 -Uc:\mORMot2\src\core;c:\mORMot2\src\lib -NSSystem;Winapi
Embarcadero Delphi for Win32 compiler version 36.0
Copyright (c) 1983,2024 Embarcadero Technologies, Inc.
c:\mORMot2\src\mormot.defines.inc(792)
c:\mORMot2\src\mormot.uses.inc(50)
project1.pas(56)
897 lines, 0.11 seconds, 1439256 bytes code, 103032 bytes data.

# project1.exe
[{"ecid":12,"expd":[80,17,18],"efl":[{"el":0,"fl":[1,2]},{"el":1,"fl":[4,5]}],"icod":"123456","pt":[1,2,3],"adv":5}]
[{"ecid":12,"expd":[80,17,18],"efl":[{"el":0,"fl":[1,2]},{"el":1,"fl":[4,5]}],"icod":"123456","pt":[1,2,3],"adv":5}]
ok

synopse pushed a commit that referenced this issue Jun 27, 2024
@synopse
Copy link
Owner

synopse commented Jun 27, 2024

Please try above commit.

I don't have FPC trunk installed yet, but I will try to.

@idigger
Copy link
Author

idigger commented Jun 27, 2024

project1.pas adds the following statement before TEFloor = packed record.

{$RTTI EXPLICIT
PROPERTIES([vcPublished])
FIELDS([vcPublic])
METHODS([])}

Recompile and run, everything is fine.

You were amazing.

Thank you for your quick response and quick repair.

@idigger idigger closed this as completed Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants