Skip to content

Package: AbstractDateDeserializer

AbstractDateDeserializer

nameinstructionbranchcomplexitylinemethod
AbstractDateDeserializer(Class)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
AbstractDateDeserializer(TypeDeserializerBuilder)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
actualDeserializer(JsonbConfigProperties, Customization)
M: 45 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
deserializeStringValue(String, DeserializationContextImpl, Type)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 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$actualDeserializer$0(String, DeserializationContextImpl)
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$actualDeserializer$1(JsonbDateFormatter, String, DeserializationContextImpl)
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$actualDeserializer$2(DateTimeFormatter, String, DeserializationContextImpl)
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$actualDeserializer$3(String, DeserializationContextImpl)
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$actualDeserializer$4(Locale, String, DeserializationContextImpl)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
parseWithFormatterInternal(String, DateTimeFormatter)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 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%

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.deserializer.types;
14:
15: import java.lang.reflect.Type;
16: import java.sql.Date;
17: import java.time.DateTimeException;
18: import java.time.Instant;
19: import java.time.ZoneId;
20: import java.time.format.DateTimeFormatter;
21: import java.util.Locale;
22: import java.util.Optional;
23:
24: import jakarta.json.bind.JsonbException;
25: import jakarta.json.bind.annotation.JsonbDateFormat;
26:
27: import org.eclipse.yasson.internal.DeserializationContextImpl;
28: import org.eclipse.yasson.internal.JsonbConfigProperties;
29: import org.eclipse.yasson.internal.JsonbDateFormatter;
30: import org.eclipse.yasson.internal.deserializer.JustReturn;
31: import org.eclipse.yasson.internal.deserializer.ModelDeserializer;
32: import org.eclipse.yasson.internal.model.customization.Customization;
33: import org.eclipse.yasson.internal.properties.MessageKeys;
34: import org.eclipse.yasson.internal.properties.Messages;
35:
36: /**
37: * Base deserializer for all the date related types.
38: */
39: abstract class AbstractDateDeserializer<T> extends TypeDeserializer {
40:
41: static final ZoneId UTC = ZoneId.of("UTC");
42:
43: private ModelDeserializer<String> actualDeserializer;
44:
45: AbstractDateDeserializer(TypeDeserializerBuilder builder) {
46: super(builder);
47: this.actualDeserializer = actualDeserializer(builder.getConfigProperties(), builder.getCustomization());
48: }
49:
50: AbstractDateDeserializer(Class<Date> clazz) {
51: super(new TypeDeserializerBuilder(clazz, null, null, JustReturn.instance()));
52: this.actualDeserializer = null;
53: }
54:
55: private ModelDeserializer<String> actualDeserializer(JsonbConfigProperties properties, Customization customization) {
56: final JsonbDateFormatter formatter = getJsonbDateFormatter(properties, customization);
57:• if (JsonbDateFormat.TIME_IN_MILLIS.equals(formatter.getFormat())) {
58: return (value, context) -> fromInstant(Instant.ofEpochMilli(Long.parseLong(value)));
59:• } else if (formatter.getDateTimeFormatter() != null) {
60: return (value, context) -> parseWithFormatterInternal(value, formatter.getDateTimeFormatter());
61: } else {
62: DateTimeFormatter configDateTimeFormatter = properties.getConfigDateFormatter().getDateTimeFormatter();
63:• if (configDateTimeFormatter != null) {
64: return (value, context) -> parseWithFormatterInternal(value, configDateTimeFormatter);
65: }
66: }
67:• if (properties.isStrictIJson()) {
68: return (value, context) -> parseWithFormatterInternal(value, JsonbDateFormatter.IJSON_DATE_FORMATTER);
69: }
70: Locale locale = properties.getLocale(formatter.getLocale());
71: return (value, context) -> {
72: try {
73: return parseDefault(value, locale);
74: } catch (DateTimeException e) {
75: throw new JsonbException(Messages.getMessage(MessageKeys.DATE_PARSE_ERROR, value, getType()), e);
76: }
77: };
78: }
79:
80: private JsonbDateFormatter getJsonbDateFormatter(JsonbConfigProperties properties, Customization customization) {
81: return Optional.ofNullable(customization.getDeserializeDateFormatter())
82: .orElse(properties.getConfigDateFormatter());
83: }
84:
85: @Override
86: public Object deserializeStringValue(String value, DeserializationContextImpl context, Type rType) {
87:• if (actualDeserializer == null) {
88: actualDeserializer = actualDeserializer(context.getJsonbContext().getConfigProperties(), context.getCustomization());
89: }
90: return actualDeserializer.deserialize(value, context);
91: }
92:
93: /**
94: * Construct date object from an instant containing epoch millisecond.
95: * If date object supports zone offset / zone id, system default is used and warning is logged.
96: *
97: * @param instant instant to construct from
98: * @return date object
99: */
100: abstract T fromInstant(Instant instant);
101:
102: /**
103: * Parse java.time date object with default formatter.
104: * Different default formatter for each date object type is used.
105: *
106: * @param jsonValue string value to parse from
107: * @param locale annotated locale or default
108: * @return parsed date object
109: */
110: abstract T parseDefault(String jsonValue, Locale locale);
111:
112: /**
113: * Parse java.time date object with provided formatter.
114: *
115: * @param jsonValue string value to parse from
116: * @param formatter a formatter to use
117: * @return parsed date object
118: */
119: abstract T parseWithFormatter(String jsonValue, DateTimeFormatter formatter);
120:
121: private T parseWithFormatterInternal(String jsonValue, DateTimeFormatter formatter) {
122: try {
123: return parseWithFormatter(jsonValue, formatter);
124: } catch (DateTimeException e) {
125: throw new JsonbException(Messages.getMessage(MessageKeys.DATE_PARSE_ERROR, jsonValue, getType()), e);
126: }
127: }
128:
129: protected DateTimeFormatter getZonedFormatter(DateTimeFormatter formatter) {
130:• return formatter.getZone() != null
131: ? formatter
132: : formatter.withZone(UTC);
133: }
134:
135: }