#1 2025-12-01 16:04:52

idigger
Member
Registered: 2022-11-05
Posts: 23

woDontStoreVoid in function SaveJson is not working properly

The following code.

type
{$RTTI EXPLICIT PROPERTIES([vcPublished]) FIELDS([vcPublic]) METHODS([])}
  TReq = packed record
    id: Cardinal;
    desc: UTF8String;
    amount: Double;
    memo: UTF8String;
  end;
{$RTTI EXPLICIT METHODS([]) FIELDS([]) PROPERTIES([])}
var
  req: TReq;
  s: UTF8String;
begin
  with req do begin
    id := 1000;
    desc := 'test';
  end;
  SaveJson(req, TypeInfo(TReq), TEXTWRITEROPTIONS_SETASTEXT[False], s, [woDontStoreVoid]);
  WriteLn(s);
end.

The output is

{"id":1000,"desc":"test","amount":0,"memo":""}

instead of

{"id":1000,"desc":"test"}

I'm using the latest trunk.

Offline

#2 2025-12-01 16:59:17

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,308
Website

Re: woDontStoreVoid in function SaveJson is not working properly

I understand the confusion.

TTextWriterWriteObjectOptions are for class instances - as stated by the "WriteObject" naming, and the documentation.
So woDontStoreVoid won't work on record fields.

What you expect is [woDontStoreDefault] instead of TEXTWRITEROPTIONS_SETASTEXT[False].

Offline

#3 Yesterday 06:55:40

idigger
Member
Registered: 2022-11-05
Posts: 23

Re: woDontStoreVoid in function SaveJson is not working properly

Thank you for your guidance. Everything works fine now when the above record is changed to class.

Offline

#4 Today 05:15:20

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 67

Re: woDontStoreVoid in function SaveJson is not working properly

As far as I know, this method works.

s:=SaveJson(req, 'TReq',[twoIgnoreDefaultInRecord]);

Offline

Board footer

Powered by FluxBB