Skip to content

Package: AbstractDateSerializer

AbstractDateSerializer

nameinstructionbranchcomplexitylinemethod
AbstractDateSerializer(TypeSerializerBuilder)
M: 38 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
formatStrictIJson(Object)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
formatWithFormatter(Object, DateTimeFormatter)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getJsonbDateFormatter(JsonbConfigProperties, Customization)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getZonedFormatter(DateTimeFormatter)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
lambda$new$0(Object, JsonGenerator)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$new$1(Object, JsonGenerator)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$valueSerializer$2(Object)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$valueSerializer$3(DateTimeFormatter, Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$valueSerializer$4(DateTimeFormatter, Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$valueSerializer$5(Locale, Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
serializeKey(Object, JsonGenerator, SerializationContextImpl)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
serializeValue(Object, JsonGenerator, SerializationContextImpl)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toTemporalAccessor(Object)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
valueSerializer(TypeSerializerBuilder)
M: 55 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0 which is available at
6: * http://www.eclipse.org/legal/epl-2.0,
7: * or the Eclipse Distribution License v. 1.0 which is available at
8: * http://www.eclipse.org/org/documents/edl-v10.php.
9: *
10: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11: */
12:
13: package org.eclipse.yasson.internal.serializer.types;
14:
15: import java.time.Instant;
16: import java.time.ZoneId;
17: import java.time.format.DateTimeFormatter;
18: import java.time.temporal.TemporalAccessor;
19: import java.util.Locale;
20: import java.util.Optional;
21: import java.util.function.BiConsumer;
22: import java.util.function.Function;
23:
24: import jakarta.json.bind.annotation.JsonbDateFormat;
25: import jakarta.json.stream.JsonGenerator;
26:
27: import org.eclipse.yasson.internal.JsonbConfigProperties;
28: import org.eclipse.yasson.internal.JsonbDateFormatter;
29: import org.eclipse.yasson.internal.SerializationContextImpl;
30: import org.eclipse.yasson.internal.model.customization.Customization;
31:
32: /**
33: * Base for all date related serializers.
34: */
35: abstract class AbstractDateSerializer<T> extends TypeSerializer<T> {
36:
37: static final ZoneId UTC = ZoneId.of("UTC");
38:
39: private final Function<T, String> toStringSerializer;
40: private final BiConsumer<T, JsonGenerator> valueWriter;
41:
42: AbstractDateSerializer(TypeSerializerBuilder serializerBuilder) {
43: super(serializerBuilder);
44: Customization customization = serializerBuilder.getCustomization();
45: JsonbConfigProperties properties = serializerBuilder.getJsonbContext().getConfigProperties();
46: final JsonbDateFormatter formatter = getJsonbDateFormatter(properties, customization);
47: toStringSerializer = valueSerializer(serializerBuilder);
48:• if (JsonbDateFormat.TIME_IN_MILLIS.equals(formatter.getFormat()) && !properties.isDateInMillisecondsAsString()) {
49: valueWriter = (value, generator) -> generator.write(toInstant(value).toEpochMilli());
50: } else {
51: valueWriter = (value, generator) -> generator.write(toStringSerializer.apply(value));
52: }
53: }
54:
55: private Function<T, String> valueSerializer(TypeSerializerBuilder serializerBuilder) {
56: Customization customization = serializerBuilder.getCustomization();
57: JsonbConfigProperties properties = serializerBuilder.getJsonbContext().getConfigProperties();
58: final JsonbDateFormatter formatter = getJsonbDateFormatter(properties, customization);
59:• if (JsonbDateFormat.TIME_IN_MILLIS.equals(formatter.getFormat())) {
60: return value -> String.valueOf(toInstant(value).toEpochMilli());
61:• } else if (formatter.getDateTimeFormatter() != null) {
62: DateTimeFormatter dateTimeFormatter = formatter.getDateTimeFormatter();
63: return value -> formatWithFormatter(value, dateTimeFormatter);
64: } else {
65: DateTimeFormatter configDateTimeFormatter = properties.getConfigDateFormatter().getDateTimeFormatter();
66:• if (configDateTimeFormatter != null) {
67: return value -> formatWithFormatter(value, configDateTimeFormatter);
68: }
69: }
70:• if (properties.isStrictIJson()) {
71: return this::formatStrictIJson;
72: }
73: Locale locale = properties.getLocale(formatter.getLocale());
74: return value -> formatDefault(value, locale);
75: }
76:
77: private JsonbDateFormatter getJsonbDateFormatter(JsonbConfigProperties properties, Customization customization) {
78: return Optional.ofNullable(customization.getSerializeDateFormatter())
79: .orElse(properties.getConfigDateFormatter());
80: }
81:
82: /**
83: * Convert date object to {@link TemporalAccessor}
84: *
85: * Only for legacy dates.
86: *
87: * @param value date object
88: * @return converted {@link TemporalAccessor}
89: */
90: protected TemporalAccessor toTemporalAccessor(T value) {
91: return (TemporalAccessor) value;
92: }
93:
94: /**
95: * Convert java.time object to epoch milliseconds instant. Discards zone offset and zone id information.
96: *
97: * @param value date object to convert
98: * @return instant
99: */
100: protected abstract Instant toInstant(T value);
101:
102: /**
103: * Format with default formatter for a given java.time date object.
104: * Different default formatter for each date object type is used.
105: *
106: * @param value date object
107: * @param locale locale from annotation / default not null
108: * @return formatted date obj as string
109: */
110: protected abstract String formatDefault(T value, Locale locale);
111:
112: /**
113: * Format date object with given formatter.
114: *
115: * @param value date object to format
116: * @param formatter formatter to format with
117: * @return formatted result
118: */
119: protected String formatWithFormatter(T value, DateTimeFormatter formatter) {
120: return formatter.format(toTemporalAccessor(value));
121: }
122:
123: /**
124: * Format date object as strict IJson date format.
125: *
126: * @param value value to format
127: * @return formatted result
128: */
129: protected String formatStrictIJson(T value) {
130: return JsonbDateFormatter.IJSON_DATE_FORMATTER.format(toTemporalAccessor(value));
131: }
132:
133: /**
134: * Append UTC zone in case zone is not set on formatter.
135: *
136: * @param formatter formatter
137: * @return zoned formatter
138: */
139: protected DateTimeFormatter getZonedFormatter(DateTimeFormatter formatter) {
140:• return formatter.getZone() != null
141: ? formatter
142: : formatter.withZone(UTC);
143: }
144:
145: @Override
146: void serializeValue(T value, JsonGenerator generator, SerializationContextImpl context) {
147: valueWriter.accept(value, generator);
148: }
149:
150: @Override
151: void serializeKey(T key, JsonGenerator generator, SerializationContextImpl context) {
152: generator.writeKey(toStringSerializer.apply(key));
153: }
154: }