Skip to content

Commit

Permalink
GH-55 - Support for ValueObject marker interface in Jackson integration.
Browse files Browse the repository at this point in the history
We now also detect the newly introduced ValueObject marker interface when detecting value objects to support in our Jackson (de)serialization customization. See xmolecules/jmolecules#55.
  • Loading branch information
odrotbohm committed Aug 5, 2021
1 parent d1a6e8e commit e6db6c5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import com.fasterxml.jackson.databind.module.SimpleModule;

/**
* A Jackson {@link Module} to support JMolecules' {@link ValueObject} and {@link Identifier} types.
* A Jackson {@link Module} to support JMolecules' {@link ValueObject}, {@link org.jmolecules.ddd.types.ValueObject} and
* {@link Identifier} types.
*
* @author Oliver Drotbohm
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, Bean
Class<?> type = descriptor.getBeanClass();

if (DETECTOR.hasAnnotation(type, ValueObject.class)
|| org.jmolecules.ddd.types.ValueObject.class.isAssignableFrom(type)
|| Identifier.class.isAssignableFrom(type)) {

BeanPropertyDefinition definition = properties.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescri
Class<?> type = description.getBeanClass();

if (AnnotatedElementUtils.hasAnnotation(type, ValueObject.class)
|| org.jmolecules.ddd.types.ValueObject.class.isAssignableFrom(type)
|| Identifier.class.isAssignableFrom(type)) {
return new SingleAttributeSerializer(properties.get(0).getAccessor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ void serialize() throws Exception {

SampleIdentifier identifier = SampleIdentifier.of(UUID.randomUUID());
SampleValueObject valueObject = SampleValueObject.of(42L);
ImplementingValueObject implementingValueObject = ImplementingValueObject.of(27L);
Document source = new Document(identifier, valueObject, implementingValueObject);

String result = mapper.writeValueAsString(new Document(identifier, valueObject));
String result = mapper.writeValueAsString(source);

DocumentContext document = JsonPath.parse(result);

assertThat(document.read("$.identifier", String.class)).isEqualTo(identifier.getId().toString());
assertThat(document.read("$.valueObject", Long.class)).isEqualTo(42L);
assertThat(document.read("$.implementingValueObject", Long.class)).isEqualTo(27L);
}

@Test // #19
Expand All @@ -56,12 +59,14 @@ void deserialize() throws Exception {
String uuidSource = "fe6f3370-5551-4251-86d3-b4db049a7ddd";
UUID uuid = UUID.fromString(uuidSource);

Document document = mapper.readValue(
"{ \"identifier\" : \"" + uuidSource + "\", \"valueObject\" : 42 }",
Document document = mapper.readValue("{ \"identifier\" : \"" + uuidSource + "\","
+ " \"valueObject\" : 42,"
+ " \"implementingValueObject\" : 27 }",
Document.class);

assertThat(document.identifier).isEqualTo(SampleIdentifier.of(uuid));
assertThat(document.valueObject).isEqualTo(SampleValueObject.of(42L));
assertThat(document.implementingValueObject).isEqualTo(ImplementingValueObject.of(27L));
}

@Data
Expand All @@ -70,6 +75,7 @@ void deserialize() throws Exception {
static class Document {
SampleIdentifier identifier;
SampleValueObject valueObject;
ImplementingValueObject implementingValueObject;
}

@Value(staticConstructor = "of")
Expand All @@ -82,4 +88,9 @@ static class SampleIdentifier implements Identifier {
static class SampleValueObject {
Long number;
}

@Value(staticConstructor = "of")
static class ImplementingValueObject implements org.jmolecules.ddd.types.ValueObject {
Long value;
}
}

0 comments on commit e6db6c5

Please sign in to comment.