Package: TemporalSerializer
TemporalSerializer
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TemporalSerializer() |
|
|
|
|
|
||||||||||||||||||||
TemporalSerializer(DateTimeSerializer, LocalDateSerializer, TimeSerializer) |
|
|
|
|
|
||||||||||||||||||||
configure(Configuration) |
|
|
|
|
|
||||||||||||||||||||
serialize(TemporalAccessor, SerializationContext) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
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.serialization.serializer.compact.datetime;
19:
20: import cz.cvut.kbss.jsonld.ConfigParam;
21: import cz.cvut.kbss.jsonld.Configuration;
22: import cz.cvut.kbss.jsonld.exception.UnsupportedTemporalTypeException;
23: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
24: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
25: import cz.cvut.kbss.jsonld.serialization.serializer.datetime.DateTimeSerializer;
26: import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
27:
28: import java.time.*;
29: import java.time.temporal.TemporalAccessor;
30:
31: /**
32: * Serializes Java 8 date/time values represented by the {@link TemporalAccessor} interface.
33: */
34: public class TemporalSerializer implements ValueSerializer<TemporalAccessor> {
35:
36: protected DateTimeSerializer dateTimeSerializer;
37: private final LocalDateSerializer dateSerializer;
38: private final TimeSerializer timeSerializer;
39:
40: public TemporalSerializer() {
41: this(new IsoDateTimeSerializer(), new LocalDateSerializer(), new TimeSerializer());
42: }
43:
44: protected TemporalSerializer(DateTimeSerializer dateTimeSerializer, LocalDateSerializer dateSerializer, TimeSerializer timeSerializer) {
45: this.dateTimeSerializer = dateTimeSerializer;
46: this.dateSerializer = dateSerializer;
47: this.timeSerializer = timeSerializer;
48: }
49:
50: @Override
51: public JsonNode serialize(TemporalAccessor value, SerializationContext<TemporalAccessor> ctx) {
52:• if (value instanceof LocalDate) {
53: return dateSerializer.serialize((LocalDate) value, ctx);
54:• } else if (value instanceof OffsetTime) {
55: return timeSerializer.serialize((OffsetTime) value, ctx);
56:• } else if (value instanceof LocalTime) {
57: return timeSerializer.serialize((LocalTime) value, ctx);
58:• } else if (value instanceof OffsetDateTime) {
59: return dateTimeSerializer.serialize((OffsetDateTime) value, ctx);
60:• } else if (value instanceof LocalDateTime) {
61: return dateTimeSerializer.serialize((LocalDateTime) value, ctx);
62:• } else if (value instanceof Instant) {
63: return dateTimeSerializer.serialize((Instant) value, ctx);
64:• } else if (value instanceof ZonedDateTime) {
65: return dateTimeSerializer.serialize(((ZonedDateTime) value).toOffsetDateTime(), ctx);
66: }
67: throw new UnsupportedTemporalTypeException("Temporal type " + value.getClass() + " serialization is not supported.");
68: }
69:
70: @Override
71: public void configure(Configuration config) {
72:• assert config != null;
73:• this.dateTimeSerializer = config.is(ConfigParam.SERIALIZE_DATETIME_AS_MILLIS) ?
74: new EpochBasedDateTimeSerializer() : new IsoDateTimeSerializer();
75: dateTimeSerializer.configure(config);
76: }
77: }