You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
@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.
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).
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.
The solution wouldn't necessarily need to be in the mixin but maybe could be a special kind of deserializer?
I hope my explanation is clear.
The text was updated successfully, but these errors were encountered: