Orange serializer/deserializer

Jacob Carlborg doob at me.com
Sat Jun 2 20:11:17 UTC 2018


On 2018-06-02 03:30, IntegratedDimensions wrote:
> How can I modify the pre serialization and post serialization values? I 
> need to transform some variables that are stored but I would like to do 
> this easily "inline"(would be cool to be able to provide a delegate to 
> do the transformations at the site of definition of the fields).

Use the "onSerializing" and "onSerialized" UDAs on a method. 
"onSerializing" will be called before serializing and "onSerialized" 
after serializing. Have a look at the unit tests [1].

> Also, how does orange handle properties? Seems it just deals with fields 
> and ignores all functions(does not use getter and setter of properties). 
> This is valid, of course, just want to make sure. I still need to be 
> able to transform values pre and post though.

That is correct, it only (de)serializes fields. If you want to 
(de)serialize proprieties, implement the "toData" and "fromData". See 
the example in the wiki [2]. Note, by implementing these methods none of 
the standard serialization will occur. If you want to serialize the 
fields as well, you need to do that as well when implementing "toData" 
and "fromData".

It's also possible to implement these "methods" in a non-intrusive way, 
i.e. for customizing serialization of third party type [3].

[1] 
https://github.com/jacob-carlborg/orange/blob/master/tests/Events.d#L39-L54

[2] https://github.com/jacob-carlborg/orange/wiki/Custom-Serialization

[3] 
https://github.com/jacob-carlborg/orange/blob/master/tests/NonIntrusive.d

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list