Skip to content
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 @@ -2,8 +2,11 @@

import it.aboutbits.springboot.toolbox.jackson.CustomTypeDeserializer;
import it.aboutbits.springboot.toolbox.jackson.CustomTypeSerializer;
import it.aboutbits.springboot.toolbox.jackson.EntityIdDeserializer;
import it.aboutbits.springboot.toolbox.type.CustomType;
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
import it.aboutbits.springboot.toolbox.web.CustomTypePropertyEditor;
import it.aboutbits.springboot.toolbox.web.EntityIdPropertyEditor;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -29,6 +32,7 @@ public void initBinder(WebDataBinder binder) {
for (var clazz : types) {
binder.registerCustomEditor(clazz, new CustomTypePropertyEditor<>(clazz));
}
binder.registerCustomEditor(EntityId.class, new EntityIdPropertyEditor());
}
}

Expand All @@ -41,8 +45,11 @@ public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer(CustomTypeScanner co
.toList()
.toArray(new CustomTypeDeserializer[types.size()]);


return builder -> builder
.serializers(new CustomTypeSerializer())
.deserializers(deserializers);
.deserializers(deserializers)
.deserializerByType(EntityId.class, new EntityIdDeserializer());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package it.aboutbits.springboot.toolbox.jackson;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import it.aboutbits.springboot.toolbox.type.identity.EntityId;

import java.io.IOException;

public class EntityIdDeserializer extends JsonDeserializer<EntityId<?>> {
/*
This is needed because EntityId is just the interface. Jackson does not know what implementation to take.
So we use this generic implementation.
*/
@Override
public EntityId<?> deserialize(
JsonParser jsonParser,
DeserializationContext deserializationContext
) throws IOException {
// We need to do this because due to type erasure we don't actually know the wrapped type. So we try a number first and fallback to a string.
try {
var theValue = jsonParser.getLongValue();
return new EntityId<Long>() {
@Override
public Long value() {
return theValue;
}

@Override
public String toString() {
return String.valueOf(value());
}
};
} catch (JsonParseException e) {
var theValue = jsonParser.getValueAsString();
return new EntityId<String>() {
@Override
public String value() {
return theValue;
}

@Override
public String toString() {
return theValue;
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package it.aboutbits.springboot.toolbox.web;

import it.aboutbits.springboot.toolbox.type.identity.EntityId;
import org.springframework.lang.Nullable;

import java.beans.PropertyEditorSupport;

public final class EntityIdPropertyEditor extends PropertyEditorSupport {
@SuppressWarnings("unchecked")
@Override
@Nullable
public String getAsText() {
var value = (EntityId<?>) getValue();

return value == null ? null : value.toString();
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
EntityId<?> value = null;
try {
var theValue = Long.parseLong(text);
value = new EntityId<Long>() {
@Override
public Long value() {
return theValue;
}

@Override
public String toString() {
return String.valueOf(value());
}
};
} catch (NumberFormatException e) {
value = new EntityId<String>() {
@Override
public String value() {
return text;
}

@Override
public String toString() {
return value();
}
};
}

setValue(
value
);
}
}