Skip to content

Package: OffsetDateTimeDeserializer

OffsetDateTimeDeserializer

nameinstructionbranchcomplexitylinemethod
OffsetDateTimeDeserializer()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
configure(Configuration)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
deserialize(JsonValue, DeserializationContext)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.deserialization.datetime;
19:
20: import cz.cvut.kbss.jsonld.Configuration;
21: import cz.cvut.kbss.jsonld.deserialization.DeserializationContext;
22: import cz.cvut.kbss.jsonld.deserialization.ValueDeserializer;
23: import cz.cvut.kbss.jsonld.deserialization.util.ValueUtils;
24: import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
25: import jakarta.json.JsonNumber;
26: import jakarta.json.JsonValue;
27:
28: import java.time.OffsetDateTime;
29:
30: /**
31: * Deserializes values to {@link OffsetDateTime}.
32: * <p>
33: * If the value is a number, it is taken as the number of milliseconds since the Unix Epoch. Otherwise, it is parsed as
34: * a string.
35: * <p>
36: * If a datetime pattern is configured ({@link cz.cvut.kbss.jsonld.ConfigParam#DATE_TIME_FORMAT}), it is used to parse
37: * the value. Otherwise, the default ISO-based pattern is used.
38: */
39: public class OffsetDateTimeDeserializer implements ValueDeserializer<OffsetDateTime> {
40:
41: private final StringBasedDateTimeResolver stringResolver = new StringBasedDateTimeResolver();
42:
43: private final EpochBasedDateTimeResolver epochResolver = new EpochBasedDateTimeResolver();
44:
45: @Override
46: public OffsetDateTime deserialize(JsonValue jsonNode, DeserializationContext<OffsetDateTime> ctx) {
47: final JsonValue value = ValueUtils.getValue(jsonNode);
48: try {
49:• return value.getValueType() == JsonValue.ValueType.NUMBER ? epochResolver.resolve((JsonNumber) value) :
50: stringResolver.resolve(ValueUtils.stringValue(value));
51: } catch (RuntimeException e) {
52: throw new JsonLdDeserializationException("Unable to deserialize datetime value.", e);
53: }
54: }
55:
56: @Override
57: public void configure(Configuration config) {
58: stringResolver.configure(config);
59: }
60: }