Skip to content

Commit

Permalink
Move SimpleBuilderDeserializer into SpecificObjectDeserializer (#583)
Browse files Browse the repository at this point in the history
This adds support for builders on subtypes.
  • Loading branch information
yawkat authored Sep 27, 2023
1 parent 7ca4093 commit 9ca5d18
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ class BuilderSpec extends Specification {
then:
result == json
}

void "test deserialize builder on subtype"() {
given:
def json = '{"sub":{"foo":"fizz","bar":"buzz"}}'

when:
def value = objectMapper.readValue(json, TestBuildSupertype)

then:
value.foo == 'fizz'
value.bar == 'buzz'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.micronaut.serde.jackson.builder;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(builder = TestBuildSubtype.Builder.class)
public class TestBuildSubtype extends TestBuildSupertype {
private final String bar;

private TestBuildSubtype(String foo, String bar) {
super(foo);
this.bar = bar;
}

public String getBar() {
return bar;
}

public static class Builder {
private String foo;
private String bar;

public Builder foo(String foo) {
this.foo = foo;
return this;
}

public Builder bar(String bar) {
this.bar = bar;
return this;
}

public TestBuildSubtype build() {
return new TestBuildSubtype(foo, bar);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.serde.jackson.builder;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonSubTypes({
@JsonSubTypes.Type(value = TestBuildSubtype.class, name = "sub")
})
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT
)
public abstract class TestBuildSupertype {
private final String foo;

TestBuildSupertype(String foo) {
this.foo = foo;
}

public String getFoo() {
return foo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ public Deserializer<Object> createSpecific(DecoderContext context, Argument<? su
return (Decoder decoder, DecoderContext context1, Argument<? super Object> type1) -> decoder.decodeArbitrary();
}
DeserBean<? super Object> deserBean = getDeserializableBean(type, context);
if (deserBean.hasBuilder) {
return new SimpleBuilderDeserializer(
deserBean.readProperties,
deserBean.introspection,
preInstantiateCallback,
ignoreUnknown,
strictNullable
);
}
if (deserBean.simpleBean) {
return new SimpleObjectDeserializer(ignoreUnknown, strictNullable, deserBean, preInstantiateCallback);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.reflect.exception.InstantiationException;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArrayUtils;
Expand Down Expand Up @@ -328,6 +329,42 @@ public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argume
decoderContext
);
}
} else if (db.hasBuilder) {
BeanIntrospection.Builder<? super Object> builder;
try {
if (preInstantiateCallback != null) {
preInstantiateCallback.preInstantiate(db.introspection);
}
builder = db.introspection.builder();
} catch (InstantiationException e) {
throw new SerdeException(PREFIX_UNABLE_TO_DESERIALIZE_TYPE + type + "]: " + e.getMessage(), e);
}
if (hasProperties) {
while (true) {
final String prop = objectDecoder.decodeKey();
if (prop == null) {
break;
}
final DeserBean.DerProperty<Object, Object> property = readProperties.consume(prop);
if (property != null) {
property.deserializeAndCallBuilder(objectDecoder, decoderContext, builder);
} else {
skipOrSetAny(
decoderContext,
objectDecoder,
prop,
anyValues,
ignoreUnknown,
type
);
}
}
}
try {
obj = builder.build();
} catch (InstantiationException e) {
throw new SerdeException(PREFIX_UNABLE_TO_DESERIALIZE_TYPE + type + "]: " + e.getMessage(), e);
}
} else {
try {
if (preInstantiateCallback != null) {
Expand Down

0 comments on commit 9ca5d18

Please sign in to comment.