Serialization question |
Post Reply ![]() |
Author | |||
Alex Egorov ![]() Member ![]() Joined: 04 May 2017 Posts: 50 |
![]() ![]() ![]() ![]() Posted: 12 Nov 2019 at 8:44am |
||
Hello, I'm trying to test serialization:
Why sJSON is not contained values of fields?
|
|||
![]() |
|||
Bracey Mark ![]() New Member ![]() Joined: 02 Aug 2012 Posts: 17 |
![]() ![]() ![]() ![]() |
||
Since you own Aurelius I would use the JSON serializer supplied with it.
It is located in \TMS Busines Core Library\source\core with units Bcl.Json.Deserializer, Bcl.Json.Serializer, Bcl.Json.Attributes, Your class would look like this using it. [JsonNamingStrategy(TDefaultNamingStrategy)] TRecTest = class private FS1: string; FI1: Integer; public [JSONProperty] property S1: string read FS1 write FS1; [JSONProperty] property I1: Integer read fI1 write fI1; function ToJSON: string; end; function TRecTest.ToJson: String; var JS : TJsonSerializer; var V : TValue; TT : TTypeToken; SS : TStringStream; begin V := TValue.From(Self); TT := TTypeToken.Create( V.TypeInfo); try SS := TStringStream.Create; JS := TJsonSerializer.Create; JS.Write(V,TT, SS); Result := SS.DataString; finally JS.Free; SS.Free; end; |
|||
![]() |
|||
Alex Egorov ![]() Member ![]() Joined: 04 May 2017 Posts: 50 |
![]() ![]() ![]() ![]() |
||
Thank you, your code works! And also my code works too with small additions for class:
|
|||
![]() |
|||
Alex Egorov ![]() Member ![]() Joined: 04 May 2017 Posts: 50 |
![]() ![]() ![]() ![]() |
||
Mark, your code can be shortened to:
|
|||
![]() |
|||
Bracey Mark ![]() New Member ![]() Joined: 02 Aug 2012 Posts: 17 |
![]() ![]() ![]() ![]() |
||
It could, but I have a base class that I can derive from which has implemented in the base
LoadFrom SaveTo Strings, Streams, Files and my method allows for that. I also use a Generic TObjectList to do the same kind of thing. e.g. procedure TBEObjectList<T>.LoadFromStream(const Stream: TStream; const ITS : TInstanceTypeSerialization = TInstanceTypeSerialization.IfNeeded); var V : TValue; JD : TJsonDeserializer; TT : TTypeToken; begin Clear; V := TValue.From(Self); TT := TTypeToken.Create( V.TypeInfo); JD := TJsonDeserializer.Create; JD.Converters.ObjectConverterFactory.InstanceTypeSerialization := ITS; try Stream.Position := 0; JD.Read(Stream, V, TT); finally JD.Free; end; end; |
|||
![]() |
|||
Bracey Mark ![]() New Member ![]() Joined: 02 Aug 2012 Posts: 17 |
![]() ![]() ![]() ![]() |
||
I have two base classes
TBaseEntity and TBEObjectList<T: TBaseEntity> = class(TObjectList<T>) which I use to bind to the TMSAureliusDataset and my generic serialization works well for that. e.g. my TBaseEntity LoadFromJson method.
procedure TBaseEntity.LoadFromJson(const Json: String); var V : TValue; JD : TJsonDeserializer; TT : TTypeToken; begin V := TValue.From(Self); TT := TTypeToken.Create( V.TypeInfo); JD := TJsonDeserializer.Create; try JD.Read(Json,V, TT); Loaded; finally JD.Free; end; end; |
|||
![]() |
|||
Alex Egorov ![]() Member ![]() Joined: 04 May 2017 Posts: 50 |
![]() ![]() ![]() ![]() |
||
Thank you for the code! Mark, can you provide an example for TBEObjectList<T> usage?
|
|||
![]() |
|||
Wagner R. Landgraf ![]() TMS Support ![]() Joined: 18 May 2010 Posts: 2547 |
![]() ![]() ![]() ![]() |
||
It could be even more simplified this way:
Edited by Wagner R. Landgraf - 19 Nov 2019 at 12:52am |
|||
![]() |
Post Reply ![]() |
Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |