Here's the custom deserializer: https://github.com/jonpeterson/jackson-module-model-versioning/blob/master/src/main/java/com/github/jonpeterson/jackson/module/versioning/VersionedModelDeserializer.java
Some more details here: https://github.com/immutables/immutables/issues/461 and here: https://github.com/jonpeterson/jackson-module-model-versioning/issues/6 Basically when calling readValue like this deserialize() gets called 2 times. ObjectMapper m = new ObjectMapper(); m.registerModule(new VersioningModule()); String json = "{\"field\":\"f\",\"anotherField\":\"a\",\"modelVersion\":\"1\"}"; Entity pe = m.readValue(json, Entity.class); The ImmutableEntity that gets generated by annotation processor is the following: /** * Immutable implementation of {@link Entity}. * <p> * Use the builder to create immutable instances: * {@code ImmutableEntity.builder()}. */ @SuppressWarnings({"all"}) @SuppressFBWarnings @ParametersAreNonnullByDefault @Generated({"Immutables.generator", "Entity"}) @Immutable @CheckReturnValue @JsonVersionedModel(currentVersion = "1", defaultDeserializeToVersion = "0", versionToSuppressPropertySerialization = "0") public final class ImmutableEntity implements Entity { private final @Nullable String serializeToVersion; private final String field; private final String anotherField; private ImmutableEntity( @Nullable String serializeToVersion, String field, String anotherField) { this.serializeToVersion = serializeToVersion; this.field = field; this.anotherField = anotherField; } /** * @return The value of the {@code serializeToVersion} attribute */ @JsonProperty("serializeToVersion") @JsonSerializeToVersion(defaultToSource = true) @Override public @Nullable String getSerializeToVersion() { return serializeToVersion; } /** * @return The field */ @JsonProperty("field") @Override public String field() { return field; } /** * @return another field */ @JsonProperty("anotherField") @Override public String getAnotherField() { return anotherField; } /** * Copy the current immutable object by setting a value for the {@link Entity#getSerializeToVersion() serializeToVersion} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for serializeToVersion (can be {@code null}) * @return A modified copy of the {@code this} object */ public final ImmutableEntity withSerializeToVersion(@Nullable String value) { if (Objects.equals(this.serializeToVersion, value)) return this; return new ImmutableEntity(value, this.field, this.anotherField); } /** * Copy the current immutable object by setting a value for the {@link Entity#field() field} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for field * @return A modified copy of the {@code this} object */ public final ImmutableEntity withField(String value) { if (this.field.equals(value)) return this; String newValue = Objects.requireNonNull(value, "field"); return new ImmutableEntity(this.serializeToVersion, newValue, this.anotherField); } /** * Copy the current immutable object by setting a value for the {@link Entity#getAnotherField() anotherField} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for anotherField * @return A modified copy of the {@code this} object */ public final ImmutableEntity withAnotherField(String value) { if (this.anotherField.equals(value)) return this; String newValue = Objects.requireNonNull(value, "anotherField"); return new ImmutableEntity(this.serializeToVersion, this.field, newValue); } /** * This instance is equal to all instances of {@code ImmutableEntity} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { if (this == another) return true; return another instanceof ImmutableEntity && equalTo((ImmutableEntity) another); } private boolean equalTo(ImmutableEntity another) { return Objects.equals(serializeToVersion, another.serializeToVersion) && field.equals(another.field) && anotherField.equals(another.anotherField); } /** * Computes a hash code from attributes: {@code serializeToVersion}, {@code field}, {@code anotherField}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + Objects.hashCode(serializeToVersion); h = h * 17 + field.hashCode(); h = h * 17 + anotherField.hashCode(); return h; } /** * Prints the immutable value {@code Entity} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "Entity{" + "serializeToVersion=" + serializeToVersion + ", field=" + field + ", anotherField=" + anotherField + "}"; } /** * Utility type used to correctly read immutable object from JSON representation. * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure */ @Deprecated @JsonDeserialize @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE) static final class Json implements Entity { @Nullable String serializeToVersion; @Nullable String field; @Nullable String anotherField; @JsonProperty("serializeToVersion") @JsonSerializeToVersion(defaultToSource = true) public void setSerializeToVersion(@Nullable String serializeToVersion) { this.serializeToVersion = serializeToVersion; } @JsonProperty("field") public void setField(String field) { this.field = field; } @JsonProperty("anotherField") public void setAnotherField(String anotherField) { this.anotherField = anotherField; } @Override public String getSerializeToVersion() { throw new UnsupportedOperationException(); } @Override public String field() { throw new UnsupportedOperationException(); } @Override public String getAnotherField() { throw new UnsupportedOperationException(); } } /** * @param json A JSON-bindable data structure * @return An immutable value type * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure */ @Deprecated @JsonCreator(mode = JsonCreator.Mode.DEFAULT) static ImmutableEntity fromJson(Json json) { ImmutableEntity.Builder builder = ImmutableEntity.builder(); if (json.serializeToVersion != null) { builder.serializeToVersion(json.serializeToVersion); } if (json.field != null) { builder.field(json.field); } if (json.anotherField != null) { builder.anotherField(json.anotherField); } return builder.build(); } /** * Creates an immutable copy of a {@link Entity} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable Entity instance */ public static ImmutableEntity copyOf(Entity instance) { if (instance instanceof ImmutableEntity) { return (ImmutableEntity) instance; } return ImmutableEntity.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableEntity ImmutableEntity}. * @return A new ImmutableEntity builder */ public static ImmutableEntity.Builder builder() { return new ImmutableEntity.Builder(); } /** * Builds instances of type {@link ImmutableEntity ImmutableEntity}. * Initialize attributes and then invoke the {@link #build()} method to create an * immutable instance. * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances.</em> */ @NotThreadSafe public static final class Builder { private static final long INIT_BIT_FIELD = 0x1L; private static final long INIT_BIT_ANOTHER_FIELD = 0x2L; private long initBits = 0x3L; private @Nullable String serializeToVersion; private @Nullable String field; private @Nullable String anotherField; private Builder() { } /** * Fill a builder with attribute values from the provided {@code Entity} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(Entity instance) { Objects.requireNonNull(instance, "instance"); @Nullable String serializeToVersionValue = instance.getSerializeToVersion(); if (serializeToVersionValue != null) { serializeToVersion(serializeToVersionValue); } field(instance.field()); anotherField(instance.getAnotherField()); return this; } /** * Initializes the value for the {@link Entity#getSerializeToVersion() serializeToVersion} attribute. * @param serializeToVersion The value for serializeToVersion (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ public final Builder serializeToVersion(@Nullable String serializeToVersion) { this.serializeToVersion = serializeToVersion; return this; } /** * Initializes the value for the {@link Entity#field() field} attribute. * @param field The value for field * @return {@code this} builder for use in a chained invocation */ public final Builder field(String field) { this.field = Objects.requireNonNull(field, "field"); initBits &= ~INIT_BIT_FIELD; return this; } /** * Initializes the value for the {@link Entity#getAnotherField() anotherField} attribute. * @param anotherField The value for anotherField * @return {@code this} builder for use in a chained invocation */ public final Builder anotherField(String anotherField) { this.anotherField = Objects.requireNonNull(anotherField, "anotherField"); initBits &= ~INIT_BIT_ANOTHER_FIELD; return this; } /** * Builds a new {@link ImmutableEntity ImmutableEntity}. * @return An immutable instance of Entity * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableEntity build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableEntity(serializeToVersion, field, anotherField); } private String formatRequiredAttributesMessage() { List<String> attributes = new ArrayList<String>(); if ((initBits & INIT_BIT_FIELD) != 0) attributes.add("field"); if ((initBits & INIT_BIT_ANOTHER_FIELD) != 0) attributes.add("anotherField"); return "Cannot build Entity, some of required attributes are not set " + attributes; } } } Any ideas? -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
