-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature #10] Implement and use custom (de)serializers for TemporalAm…
…ount values. Add integration test ensuring serialization and deserialization are compatible.
- Loading branch information
Showing
16 changed files
with
315 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/cz/cvut/kbss/jsonld/deserialization/datetime/DurationDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cz.cvut.kbss.jsonld.deserialization.datetime; | ||
|
||
import cz.cvut.kbss.jsonld.deserialization.DeserializationContext; | ||
import cz.cvut.kbss.jsonld.deserialization.ValueDeserializer; | ||
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException; | ||
|
||
import java.time.Duration; | ||
import java.util.Map; | ||
|
||
import static cz.cvut.kbss.jsonld.deserialization.datetime.OffsetDateTimeDeserializer.getLiteralValue; | ||
|
||
/** | ||
* Deserializes JSON values to {@link Duration}. | ||
* <p> | ||
* The value is expected to be an ISO 8601-formatted string. | ||
*/ | ||
public class DurationDeserializer implements ValueDeserializer<Duration> { | ||
|
||
@Override | ||
public Duration deserialize(Map<?, ?> jsonNode, DeserializationContext<Duration> ctx) { | ||
final Object value = getLiteralValue(jsonNode); | ||
try { | ||
return Duration.parse(value.toString()); | ||
} catch (RuntimeException e) { | ||
throw new JsonLdDeserializationException("Unable to deserialize duration.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/cz/cvut/kbss/jsonld/deserialization/datetime/PeriodDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cz.cvut.kbss.jsonld.deserialization.datetime; | ||
|
||
import cz.cvut.kbss.jsonld.deserialization.DeserializationContext; | ||
import cz.cvut.kbss.jsonld.deserialization.ValueDeserializer; | ||
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException; | ||
|
||
import java.time.Period; | ||
import java.util.Map; | ||
|
||
import static cz.cvut.kbss.jsonld.deserialization.datetime.OffsetDateTimeDeserializer.getLiteralValue; | ||
|
||
/** | ||
* Deserializes JSON values to {@link Period}. | ||
* <p> | ||
* The value is expected to be an ISO 8601-formatted string. | ||
*/ | ||
public class PeriodDeserializer implements ValueDeserializer<Period> { | ||
|
||
@Override | ||
public Period deserialize(Map<?, ?> jsonNode, DeserializationContext<Period> ctx) { | ||
final Object value = getLiteralValue(jsonNode); | ||
try { | ||
return Period.parse(value.toString()); | ||
} catch (RuntimeException e) { | ||
throw new JsonLdDeserializationException("Unable to deserialize duration.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/cz/cvut/kbss/jsonld/serialization/datetime/TemporalAmountSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cz.cvut.kbss.jsonld.serialization.datetime; | ||
|
||
import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory; | ||
import cz.cvut.kbss.jsonld.serialization.ValueSerializer; | ||
import cz.cvut.kbss.jsonld.serialization.model.JsonNode; | ||
import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext; | ||
|
||
import java.time.temporal.TemporalAmount; | ||
|
||
/** | ||
* Serializes {@link TemporalAmount} instances ({@link java.time.Duration}, {@link java.time.Period}) to JSON string in | ||
* the ISO 8601 format. | ||
*/ | ||
public class TemporalAmountSerializer implements ValueSerializer<TemporalAmount> { | ||
|
||
@Override | ||
public JsonNode serialize(TemporalAmount value, SerializationContext<TemporalAmount> ctx) { | ||
return JsonNodeFactory.createLiteralNode(ctx.getAttributeId(), value.toString()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/cz/cvut/kbss/jsonld/deserialization/datetime/DurationDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cz.cvut.kbss.jsonld.deserialization.datetime; | ||
|
||
import cz.cvut.kbss.jsonld.JsonLd; | ||
import cz.cvut.kbss.jsonld.environment.Generator; | ||
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static cz.cvut.kbss.jsonld.deserialization.datetime.OffsetDateTimeDeserializerTest.deserializationContext; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DurationDeserializerTest { | ||
|
||
private final DurationDeserializer sut = new DurationDeserializer(); | ||
|
||
@Test | ||
void deserializeDeserializesIsoStringToDuration() { | ||
final Duration value = Duration.ofSeconds(Generator.randomCount(5, 10000)); | ||
final Map<String, Object> input = Collections.singletonMap(JsonLd.VALUE, value.toString()); | ||
final Duration result = sut.deserialize(input, deserializationContext(Duration.class)); | ||
assertEquals(value, result); | ||
} | ||
|
||
@Test | ||
void deserializeThrowsJsonLdDeserializationExceptionWhenInputIsMissingValueAttribute() { | ||
final Map<String, Object> input = Collections.singletonMap("notValue", Duration.ofSeconds(100)); | ||
final JsonLdDeserializationException ex = assertThrows(JsonLdDeserializationException.class, | ||
() -> sut.deserialize(input, deserializationContext( | ||
Duration.class))); | ||
assertThat(ex.getMessage(), containsString(JsonLd.VALUE)); | ||
assertThat(ex.getMessage(), containsString("missing")); | ||
} | ||
|
||
@Test | ||
void deserializeThrowsJsonLdDeserializationExceptionWhenInputIsNotInIsoFormat() { | ||
final Map<String, Object> input = Collections.singletonMap(JsonLd.VALUE, "NotValid"); | ||
final JsonLdDeserializationException ex = assertThrows(JsonLdDeserializationException.class, | ||
() -> sut.deserialize(input, deserializationContext( | ||
Duration.class))); | ||
assertInstanceOf(DateTimeParseException.class, ex.getCause()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/cz/cvut/kbss/jsonld/deserialization/datetime/PeriodDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cz.cvut.kbss.jsonld.deserialization.datetime; | ||
|
||
import cz.cvut.kbss.jsonld.JsonLd; | ||
import cz.cvut.kbss.jsonld.environment.Generator; | ||
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Period; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static cz.cvut.kbss.jsonld.deserialization.datetime.OffsetDateTimeDeserializerTest.deserializationContext; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class PeriodDeserializerTest { | ||
|
||
private final PeriodDeserializer sut = new PeriodDeserializer(); | ||
|
||
@Test | ||
void deserializeDeserializesIsoStringToPeriod() { | ||
final Period value = | ||
Period.of(Generator.randomCount(5, 100), Generator.randomCount(1, 12), Generator.randomCount(1, 28)); | ||
final Map<String, Object> input = Collections.singletonMap(JsonLd.VALUE, value.toString()); | ||
final Period result = sut.deserialize(input, deserializationContext(Period.class)); | ||
assertEquals(value, result); | ||
} | ||
|
||
@Test | ||
void deserializeThrowsJsonLdDeserializationExceptionWhenInputIsMissingValueAttribute() { | ||
final Map<String, Object> input = Collections.singletonMap("notValue", Period.ofMonths(8)); | ||
final JsonLdDeserializationException ex = assertThrows(JsonLdDeserializationException.class, | ||
() -> sut.deserialize(input, deserializationContext( | ||
Period.class))); | ||
assertThat(ex.getMessage(), containsString(JsonLd.VALUE)); | ||
assertThat(ex.getMessage(), containsString("missing")); | ||
} | ||
|
||
@Test | ||
void deserializeThrowsJsonLdDeserializationExceptionWhenInputIsNotInIsoFormat() { | ||
final Map<String, Object> input = Collections.singletonMap(JsonLd.VALUE, "NotValid"); | ||
final JsonLdDeserializationException ex = assertThrows(JsonLdDeserializationException.class, | ||
() -> sut.deserialize(input, deserializationContext( | ||
Period.class))); | ||
assertInstanceOf(DateTimeParseException.class, ex.getCause()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.