New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow policy-based serialization #937

Merged
merged 1 commit into from Feb 23, 2015

Conversation

Projects
None yet
3 participants
@lultimouomo
Contributor

lultimouomo commented Dec 20, 2014

An explicit Policy parameter allows to customize serialization of types
without having to modify them, which is useful if the types are supplied
by a library or if different serializations are needed.

The PR is not ready yet, needing documentation and tests, but it's working, so I would like some feedback.

An usage example (that pushed me to write the code) is serializing objects synthesized by dproto, which can be done with such a policy:

template BufferPolicy(Buf) if (__traits(compiles, Buf.init == Buf.init.opGet))
{
    auto toRepresentation(Buf b) {
        return b.opGet;
    }
    Buf fromRepresentation(typeof(Buf.init.opGet) v){
        Buf b;
        b = v;
        return b;
    }
}

which transforms the serialized form from:

{"fieldname":{"opGet":"fieldvalue"}}

to

{"fieldname": "fieldvalue"}

(Actually, serializing dproto objects without a custom policy fails, due to https://issues.dlang.org/show_bug.cgi?id=12363 ; but it would look like the former if it worked)

I also would have liked to add a template parameter (defaulted to DefaultPolicy) to serialize and deserialize, but I don't think it can be done without breaking some existing code.

It should be possible to compose policies with something like

template CompositePolicy(T)
{
    static if (isPolicySerializable!(FirstPolicy, T))
    {
        alias toRepresentation = FirstPolicy!T.toRepresentation;
        alias fromRepresentation = FirstPolicy!T.fromRepresentation;
    } else {
        alias toRepresentation = FallbackPolicy!T.toRepresentation;
        alias fromRepresentation = FallbackPolicy!T.fromRepresentation;
    }
}

but I still have to work on it, and I wonder if it would be possible to create a general policy composer.

@lultimouomo

This comment has been minimized.

Show comment
Hide comment
@lultimouomo

lultimouomo Dec 21, 2014

Contributor

I added a ChainedPolicy template which allows to chain an arbitrary number of policies.

Contributor

lultimouomo commented Dec 21, 2014

I added a ChainedPolicy template which allows to chain an arbitrary number of policies.

Allow policy-based serialization
An explicit Policy parameter allows to customize serialization of types
without having to modify them, which is useful if the types are supplied
by a library or if different serializations are needed.

Signed-off-by: Luca Niccoli <l.niccoli@awtech.it>
@s-ludwig

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig Jan 15, 2015

Member

I like this a lot and it would also be able to handle some of the more specific serialization needs that came up in the past. I'll have to allocate some more time to look at the details of the interface in the next days.

Member

s-ludwig commented Jan 15, 2015

I like this a lot and it would also be able to handle some of the more specific serialization needs that came up in the past. I'll have to allocate some more time to look at the details of the interface in the next days.

@lultimouomo

This comment has been minimized.

Show comment
Hide comment
@lultimouomo

lultimouomo Feb 22, 2015

Contributor

Hi Ludwig,

did you have the chance to look at this?

Contributor

lultimouomo commented Feb 22, 2015

Hi Ludwig,

did you have the chance to look at this?

@s-ludwig

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig Feb 23, 2015

Member

Took the time now finally and all looks good! This is a nice concept for later extension, too (e.g. adding an UDA for customizing fields or maybe supporting more ways to customize the output within the Policy).

Member

s-ludwig commented Feb 23, 2015

Took the time now finally and all looks good! This is a nice concept for later extension, too (e.g. adding an UDA for customizing fields or maybe supporting more ways to customize the output within the Policy).

s-ludwig added a commit that referenced this pull request Feb 23, 2015

@s-ludwig s-ludwig merged commit 50b6ad2 into vibe-d:master Feb 23, 2015

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment