Skip to content
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

lultimouomo
Copy link
Contributor

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 lultimouomo force-pushed the policySerialization branch 3 times, most recently from 800497b to 46d4ae8 Compare December 21, 2014 16:32
@lultimouomo
Copy link
Contributor Author

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

@lultimouomo lultimouomo force-pushed the policySerialization branch 2 times, most recently from 7a2c7c4 to 6784f50 Compare January 5, 2015 17:05
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 <[email protected]>
@s-ludwig
Copy link
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.

@lultimouomo
Copy link
Contributor Author

Hi Ludwig,

did you have the chance to look at this?

@s-ludwig
Copy link
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).

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants