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

Builder token buffer support #584

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ class BuilderSpec extends Specification {
value.foo == 'fizz'
value.bar == 'buzz'
}

void "test deserialize builder on supertype"() {
given:
def json = '{"foo":"fizz"}'

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

then:
value.getClass() == TestBuildSupertype2
value.foo == 'fizz'
}
}
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 = TestBuildSubtype2.Builder.class)
public class TestBuildSubtype2 extends TestBuildSupertype2 {
private final String bar;

private TestBuildSubtype2(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 TestBuildSubtype2 build() {
return new TestBuildSubtype2(foo, bar);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.micronaut.serde.jackson.builder;

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

@JsonSubTypes({
@JsonSubTypes.Type(value = TestBuildSubtype2.class, name = "sub")
})
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
defaultImpl = TestBuildSupertype2.class
)
@JsonDeserialize(builder = TestBuildSupertype2.Builder.class)
public class TestBuildSupertype2 {
private final String foo;

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

public String getFoo() {
return foo;
}

public static class Builder {
private String foo;

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

public TestBuildSupertype2 build() {
return new TestBuildSupertype2(foo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,23 @@ public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argume
throw new SerdeException(PREFIX_UNABLE_TO_DESERIALIZE_TYPE + type + "]: " + e.getMessage(), e);
}
if (hasProperties) {
if (tokenBuffer != null) {
for (TokenBuffer buffer : tokenBuffer) {
final DeserBean.DerProperty<Object, Object> property = readProperties.consume(buffer.name);
if (property != null) {
property.deserializeAndCallBuilder(buffer.decoder, decoderContext, builder);
} else {
skipOrSetAny(
decoderContext,
buffer.decoder,
buffer.name,
anyValues,
ignoreUnknown,
type
);
}
}
}
while (true) {
final String prop = objectDecoder.decodeKey();
if (prop == null) {
Expand Down