#1 2020-05-27 14:18:43

Jailson
Member
Registered: 2020-05-27
Posts: 1

Serialize record to json ignore field

I'm serialize record to json using RecordSaveJSON, and I need to ignore a field.
Ex: 
TMyRecord = record
      id: string;
      name: string;
      last_name: string;
end;
I need to ignore the id field when converting to json -> { "name":"john",   "last_name":"wick"}

Offline

#2 2020-05-27 16:21:55

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: Serialize record to json ignore field

maybe it is not the best way, but if I need to edit json (in a quick-coded way), I usually use TDocVariant. Smth like this:

var
  LRecord: TMyRecord;
  LDocVar: TDocVariantData;
begin
  LRecord.id := '1';
  LRecord.name := '22';
  LRecord.last_name := '333';
  LDocVar.InitFromTypeInfo(LRecord, TypeInfo(TMyRecord), False, []);
  LDocVar.Delete('id');
  Memo1.Text := UTF8ToString(LDocVar.ToJSON);
  ...
{"name":"22","last_name":"333"}

btw, the record is better to be packed, I guess:

In practice, the record types should be defined as packed record, so that low-level access will be easier to manage by the serializers.

Offline

#3 2020-05-28 15:41:43

xalo
Member
Registered: 2016-09-22
Posts: 32

Re: Serialize record to json ignore field

IMHO the Customized serialization would be a better approach.

Offline

Board footer

Powered by FluxBB