Skip to content

Package: SqlDateSerializer

SqlDateSerializer

nameinstructionbranchcomplexitylinemethod
SqlDateSerializer(TypeSerializerBuilder)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
formatDefault(Date, Locale)
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
formatWithFormatter(Date, DateTimeFormatter)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
toInstant(Date)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2018, 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.format.DateTimeFormatter;
17: import java.util.Date;
18: import java.util.Locale;
19:
20: /**
21: * Common serializer for {@link Date} and {@link java.sql.Date} types.
22: */
23: class SqlDateSerializer extends DateSerializer<Date> {
24:
25: SqlDateSerializer(TypeSerializerBuilder serializerBuilder) {
26: super(serializerBuilder);
27: }
28:
29: @Override
30: protected Instant toInstant(Date value) {
31:• if (value instanceof java.sql.Date) {
32: // java.sql.Date doesn't have a time component, so do our best if TIME_IN_MILLIS is requested
33: // In the future (at a breaking change boundary) we should probably reject this code path
34: return Instant.ofEpochMilli(value.getTime());
35: } else {
36: return super.toInstant(value);
37: }
38: }
39:
40: @Override
41: protected String formatDefault(Date value, Locale locale) {
42:• if (value instanceof java.sql.Date) {
43: return value.toString() + 'Z'; // Z is the UTC timezone indicator
44: } else {
45: return super.formatDefault(value, locale);
46: }
47: }
48:
49: @Override
50: protected String formatWithFormatter(Date value, DateTimeFormatter formatter) {
51:• if (value instanceof java.sql.Date) {
52: return ((java.sql.Date) value).toLocalDate().format(formatter);
53: } else {
54: return super.formatWithFormatter(value, formatter);
55: }
56: }
57: }