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

Add support for static @JsonCreator factory methods in Mixin classes #10

Open
loesak opened this issue Sep 29, 2016 · 3 comments
Open

Comments

@loesak
Copy link

loesak commented Sep 29, 2016

There are many situations where creating a simple mixin for a class is not feasable and creating a (de)serializer is a lot of effort. It would be nice if it were possible to create a static factory method annotated with @JsonCreator in a mixin class.

For example, take the class below (actually ran into this situation trying to create a mixin for UsernamePasswordAuthenticationToken from Spring security when trying to persist all the authentication objects needed for Spring Security OAuth).

public class Foo {

    private String field1;
    private Integer field2;
    private Double field3;
    private Boolean field4;

    public Foo(String field1, Integer field2) {
        this.field1 = field1;
        this.field2 = field2;
        this.field4 = false;
    }

    public Foo(String field1, Integer field2, Double field3) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
        this.field4 = true;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public String getField1() {
        return this.field1;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    public String getField2() {
        return this.field2;
    }

    public void setField3(String field3) {
        this.field3 = field3;
    }

    public String getField3() {
        return this.field3;
    }

    public void setField4(String field4) {
        if (field4) {
            throw new IllegalArgumentException("not allowed");
        }
        this.field4 = false;
    }

    public String getField4() {
        return this.field4;
    }

}

As you can see, the first constructor always sets the value for field4 to false and the second constructor sets the value to true. In addition, trying to set the value to true using the setter results in an exception. This particular use case isn't difficult to create a (de)serializer for but in the case of creating ones for the Spring Security OAuth classes () it is quite difficult. It would have been much easier if I could have created a factory class in the mixin to handle basic logic.

public abstract class FooMixin {

    @JsonCreator
    public static Foo factory(
            @JsonProperty("field1") String field1,
            @JsonProperty("field2") Integer field2,
            @JsonProperty("field3") Double field3,
            @JsonProperty("field4") Boolean field4) {

        if (field4 != null && field4) {
            return new Foo(field1, field2, field3);
        } else {
            return new Foo(field2, field2);
        }
    }

}

The solution wouldn't necessarily need to be in the mixin but maybe could be a special kind of deserializer?

public class MyDeserializer extends SomeKindOfBaseDeserializer {

    @JsonCreator
    public static Foo factory(
            @JsonProperty("field1") String field1,
            @JsonProperty("field2") Integer field2,
            @JsonProperty("field3") Double field3,
            @JsonProperty("field4") Boolean field4) {

        if (field4 != null && field4) {
            return new Foo(field1, field2, field3);
        } else {
            return new Foo(field2, field2);
        }
    }

}

I hope my explanation is clear.

@cowtowncoder
Copy link
Member

@loesak Since this is a concrete RFE for jackson-databind, please re-create issue over there?
I think something like this has been suggested before, but not sure if there is yet an issue.

@loganmzz
Copy link

@loesak @cowtowncoder
Was the jackson-databind created ? If so, can you link to it ?

@loesak
Copy link
Author

loesak commented Nov 12, 2017

@cowtowncoder @loganmzz
Just created the RFE. FasterXML/jackson-databind#1820

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

No branches or pull requests

3 participants