Vibe.d - Custom serialization policy

Flying-Toast email at example.org
Wed Jun 26 21:29:38 UTC 2019


On Wednesday, 26 June 2019 at 21:07:34 UTC, Sönke Ludwig wrote:
> Am 26.06.2019 um 21:06 schrieb Flying-Toast:
>> On Wednesday, 26 June 2019 at 18:54:05 UTC, Flying-Toast wrote:
>>> I am using vibe.d's serialization feature for a few classes.
>>>
>>> I have found that immutable/const fields are not serialized, 
>>> though it turns out this is intentional [1].
>>>
>>> In the GitHub issue [1], Sönke mentioned that a solution to 
>>> this would be to use a custom `Policy`. I tried to find 
>>> documentation on vibe's serialization policies, but I 
>>> couldn't find any.
>>>
>>> Could anyone point me to some documentation for serialization 
>>> policies, or explain how to create a policy that allows 
>>> serializing immutable fields (and how to use it with the Json 
>>> serializer)?
>>>
>>> [1](https://github.com/vibe-d/vibe.d/issues/1536)
>>>
>>> Thanks!
>> 
>
> There is an example that applies a custom policy to any struct 
> that has "x"/"y" fields: 
> http://vibed.org/api/vibe.data.serialization/serializeWithPolicy
>
> When working with multiple policies, ChainedPolicy is also 
> handy: 
> http://vibed.org/api/vibe.data.serialization/ChainedPolicy
>
>> Also, how do I make the serializer ignore a single field?
>
> If no policy is used to serialize the particular type, the 
> `@ignore` attribute can be applied to the field: 
> http://vibed.org/api/vibe.data.serialization/ignore

How would I make a new policy to allow immutable/const members to 
be serialized, but leave everything else as it is in 
DefaultPolicy?


More information about the Digitalmars-d mailing list