Skip to content

Method: deserialize(JsonValue, DeserializationContext)

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 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.deserialization.DeserializationContext;
21: import cz.cvut.kbss.jsonld.deserialization.ValueDeserializer;
22: import cz.cvut.kbss.jsonld.deserialization.util.ValueUtils;
23: import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
24: import jakarta.json.JsonValue;
25:
26: import java.time.Duration;
27:
28: /**
29: * Deserializes JSON values to {@link Duration}.
30: * <p>
31: * The value is expected to be an ISO 8601-formatted string.
32: */
33: public class DurationDeserializer implements ValueDeserializer<Duration> {
34:
35: @Override
36: public Duration deserialize(JsonValue jsonNode, DeserializationContext<Duration> ctx) {
37: final JsonValue value = ValueUtils.getValue(jsonNode);
38: try {
39: return Duration.parse(ValueUtils.stringValue(value));
40: } catch (RuntimeException e) {
41: throw new JsonLdDeserializationException("Unable to deserialize duration.", e);
42: }
43: }
44: }