Skip to content

Commit

Permalink
add support for JsonValue annotations
Browse files Browse the repository at this point in the history
When constructing a JacksonBasedProperty, the nestedContainer is checked if any property has a JsonValue annotation. If such property exists, the typeInformation and the embeddedContainer are passed directly to the property with the JsonValue annotation (skipping the current property).
  • Loading branch information
NielsCW committed Dec 18, 2024
1 parent b940931 commit 7f8069b
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,38 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.fasterxml.jackson.annotation.JsonValue;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Predicate;
import lombok.RequiredArgsConstructor;
import org.springframework.data.util.TypeInformation;

@RequiredArgsConstructor
public class JacksonBasedProperty implements Property {
private final Property delegate;
private final JacksonBasedProperty value;

public JacksonBasedProperty(Property delegate) {
this.delegate = delegate;
this.value = findJsonValue().orElse(null);
}

protected Optional<JacksonBasedProperty> findJsonValue() {
var values = new ArrayList<Property>();
delegate.nestedContainer()
.ifPresent(container -> {
container.doWithProperties(property -> {
var jsonValue = property.findAnnotation(JsonValue.class)
.map(JsonValue::value)
.orElse(false);
if (jsonValue) {
values.add(property);
}
});
});
return values.stream().findFirst()
.map(JacksonBasedProperty::new);
}

protected Optional<String> preferredName() {
return delegate.findAnnotation(JsonProperty.class)
Expand All @@ -28,6 +51,9 @@ public String getName() {

@Override
public TypeInformation<?> getTypeInformation() {
if (value != null) {
return value.getTypeInformation();
}
return delegate.getTypeInformation();
}

Expand Down Expand Up @@ -55,6 +81,9 @@ public boolean isReadOnly() {

@Override
public Optional<Container> nestedContainer() {
if (value != null) {
return value.nestedContainer();
}
return delegate.nestedContainer()
.map(JacksonBasedContainer::new);
}
Expand Down

0 comments on commit 7f8069b

Please sign in to comment.