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

how to use custom serializer AND retain model version #9

Open
conor-roche-rockalltech opened this issue May 31, 2017 · 0 comments
Open

Comments

@conor-roche-rockalltech
Copy link

conor-roche-rockalltech commented May 31, 2017

I have a versioned model class which has a field that i would like to use a custom serializer on:

@JsonVersionedModel(currentVersion = "1", toCurrentConverterClass = ToCurrentLoanPolicyOverrideConverter.class)
public class LoanPolicyOverride extends LaserData {

    @JsonSerialize(using = PolicyIdSerializer.class)
    private Policy policy;

}

But when I do the modelVersion attribute is lost in the serialized json.

Here the serializer should only outut the id of the Policy object, i have tried two approaches for the serializer:

  1. Simple approach like this:
public void serialize(
        Policy value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {

    jgen.writeStartObject();
    jgen.writeStringField("id", value.getId());
    jgen.writeEndObject();
}
  1. approach that modifies the object so only the id field is set:
public void serialize(
        Policy value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    if (value == null) {
        jgen.writeNull();
    } else {
        Policy simple = Policy.builder(value.getClassification().getClassificationCode().getCode()).id(value.getId()).build();
        BeanSerializerFactory.instance.createSerializer(provider, SimpleType.construct(Policy.class)).serialize(simple, jgen, provider);
    }
}

Any ideas on how to use a custom serializer AND keep the model version?

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

1 participant