How to serialize TList <T> to Json without garbage

I need to serialize this json sting to a Delphi class.

{ "Master":{ "version":"1.0" }, "Details":[ { "idColisEntreeDetail":0, "codeBarre":"123456789" }, { "idColisEntreeDetail":0, "codeBarre":"234567890" } ] } 

Here is my class:

 unit unit2; interface uses Generics.Collections, Rest.Json; type TDetails = class private FCodeBarre: String; FIdColisEntreeDetail: Extended; public property codeBarre: String read FCodeBarre write FCodeBarre; property idColisEntreeDetail: Extended read FIdColisEntreeDetail write FIdColisEntreeDetail; end; TMaster = class private FVersion: String; public property version: String read FVersion write FVersion; end; TMyClass = class private FDetails: TList<TDetails>; FMaster: TMaster; public property Details: TList<TDetails> read FDetails write FDetails; property Master: TMaster read FMaster write FMaster; constructor Create; destructor Destroy; override; end; implementation { TDetails } { TMyClass } constructor TMyClass.Create; begin inherited; FMaster := TMaster.Create(); end; destructor TMyClass.Destroy; var LDetailsItem: TDetails; begin for LDetailsItem in FDetails do LDetailsItem.free; FMaster.free; inherited; end; end. 

I am using TJson.ObjectToJsonString(TMyClass) and TJson.JsonToObject<TMyClass>(AJsonString) .

My problem is that when serializing like TList<TDetails> a lot of garbage is created. for instance

  { "details":{ "items":[ { "idColisEntreeDetail":0, "codeBarre":"123456789" }, { "idColisEntreeDetail":0, "codeBarre":"234567890" } ], "count":2, "arrayManager":{ } }, "master":{ "version":"" } } 

This is good when using the TArray<TDetails> , but I will lose all TList functions.

How can I use the TList type and get the correct Json output?

+2
source share

Source: https://habr.com/ru/post/1216346/


All Articles