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

fix serialization conditions order #1500

Merged
merged 1 commit into from
May 26, 2016
Merged

Conversation

tchaloupka
Copy link
Contributor

I can't get to work custom policy to deserialize enum values as I described in forum.

So I tried to find out why and found there is a bug in current implementation.

Problem is with the static if conditions order where even if the enum policy condition passed, it is not used as it is too far bottom and other condition gets prioritized before intended function.

With this PR it works ok.

Although as I looked on the other conditions, it seems to me, that they are wrong too, ie isCustomSerializable!T, isISOExtStringSerializable!T should probably also go upper. But I did not dig deeper.

Sample code, which works for me is this:

import std.stdio;
import vibe.d;

enum Color {red, green, blue}

template UpperCasePolicy(T) if (is(T == Color)) {
     import std.conv, std.string : toLower, toUpper;

     Color fromRepresentation(string value) {return value.toLower.to!Color;}

     string toRepresentation(Color value) {return to!string(value).toUpper;}
}

struct Test {
     @byName!UpperCasePolicy Color color;
}

void main()
{
    Test test = {Color.red};
    assert(test.serializeWithPolicy!(JsonSerializer, UpperCasePolicy).toString == `{"color":"RED"}`);
    assert(Json(["color": Json("GREEN")]).deserializeWithPolicy!(JsonSerializer, UpperCasePolicy, Test) == Test(Color.green));
}

I can't get it to compile with JsonStringSerializer - as mentioned on forum (Error: template vibe.data.serialization.serializeWithPolicy cannot deduce function from argument types !(JsonStringSerializer, UpperCasePolicy)(Test))

@s-ludwig
Copy link
Member

Makes sense indeed! The other ones should be fine, though, since they only apply to structs/classes and only built-in types/Nullable are tested in the conditions above. I'll have a look at the remaining issue.

@s-ludwig s-ludwig merged commit 21b8b14 into vibe-d:master May 26, 2016
@tchaloupka tchaloupka deleted the policy_ser_fix branch September 25, 2016 08:03
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.

2 participants