diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateConverter.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateConverter.java index 00af32ac..5e8b4342 100644 --- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateConverter.java +++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateConverter.java @@ -19,36 +19,40 @@ package org.apache.johnzon.mapper.converter; import org.apache.johnzon.mapper.Converter; +import org.apache.johnzon.mapper.util.DateUtil; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; +import java.util.Locale; public class DateConverter implements Converter { - // TODO: see if we can clean it - private final ThreadLocal format; + // Almost ISO 8601 basic, but zone is defined by generic name instead of zone-offset + public static final DateConverter ISO_8601_SHORT = new DateConverter("yyyyMMddHHmmssz"); + + private static final ZoneId UTC = ZoneId.of("UTC"); + + private final DateTimeFormatter formatter; public DateConverter(final String pattern) { - format = new ThreadLocal() { - @Override - protected DateFormat initialValue() { - return new SimpleDateFormat(pattern); - } - }; + this.formatter = DateTimeFormatter.ofPattern(pattern, Locale.ROOT); } @Override public String toString(final Date instance) { - return format.get().format(instance); + return formatter.format(ZonedDateTime.ofInstant(instance.toInstant(), UTC)); } @Override public Date fromString(final String text) { try { - return format.get().parse(text); - } catch (final ParseException e) { - throw new IllegalArgumentException(e); + return Date.from(DateUtil.parseZonedDateTime(text, formatter, UTC).toInstant()); + } catch (final DateTimeParseException dpe) { + return Date.from(LocalDateTime.parse(text).toInstant(ZoneOffset.UTC)); } } } diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateWithCopyConverter.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateWithCopyConverter.java index 85abb26b..2114b800 100644 --- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateWithCopyConverter.java +++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/DateWithCopyConverter.java @@ -28,7 +28,7 @@ public class DateWithCopyConverter implements Adapter { private final Adapter delegate; public DateWithCopyConverter(final Adapter delegate) { - this.delegate = delegate == null ? new ConverterAdapter<>(new DateConverter("yyyyMMddHHmmssZ"), Date.class) : delegate; + this.delegate = delegate == null ? new ConverterAdapter<>(DateConverter.ISO_8601_SHORT, Date.class) : delegate; } @Override diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/UUIDConverter.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/UUIDConverter.java new file mode 100644 index 00000000..c6a724a6 --- /dev/null +++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/UUIDConverter.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.johnzon.mapper.converter; + +import org.apache.johnzon.mapper.Converter; + +import java.util.UUID; + +public class UUIDConverter implements Converter { + @Override + public String toString(UUID instance) { + return instance.toString(); + } + + @Override + public UUID fromString(String text) { + return UUID.fromString(text); + } +} diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java index f1a4e429..943a1f1b 100644 --- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java +++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java @@ -29,8 +29,10 @@ import org.apache.johnzon.mapper.converter.StringConverter; import org.apache.johnzon.mapper.converter.URIConverter; import org.apache.johnzon.mapper.converter.URLConverter; +import org.apache.johnzon.mapper.converter.UUIDConverter; import org.apache.johnzon.mapper.internal.AdapterKey; import org.apache.johnzon.mapper.internal.ConverterAdapter; +import org.apache.johnzon.mapper.util.DateUtil; import java.lang.reflect.Type; import java.math.BigDecimal; @@ -50,28 +52,19 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; -import java.time.temporal.TemporalAccessor; -import java.time.temporal.TemporalQueries; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; -import java.util.Objects; import java.util.Set; import java.util.SimpleTimeZone; import java.util.TimeZone; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Stream; -import static java.time.temporal.ChronoField.DAY_OF_MONTH; -import static java.time.temporal.ChronoField.HOUR_OF_DAY; -import static java.time.temporal.ChronoField.MILLI_OF_SECOND; -import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; -import static java.time.temporal.ChronoField.MONTH_OF_YEAR; -import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; -import static java.time.temporal.ChronoField.YEAR; import static java.util.stream.Collectors.toSet; // important: override all usages, @@ -297,6 +290,9 @@ public OffsetTime fromString(final String text) { } }, OffsetTime.class)); } + if (from == UUID.class) { + return add(key, new ConverterAdapter<>(new UUIDConverter(), UUID.class)); + } return null; } @@ -312,7 +308,7 @@ public String toString(final OffsetDateTime instance) { @Override public OffsetDateTime fromString(final String text) { try { - return parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toOffsetDateTime(); + return DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toOffsetDateTime(); } catch (final DateTimeParseException dpe) { return OffsetDateTime.parse(text); } @@ -344,7 +340,7 @@ public String toString(final ZonedDateTime instance) { @Override public ZonedDateTime fromString(final String text) { try { - return parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC); + return DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC); } catch (final DateTimeParseException dpe) { return ZonedDateTime.parse(text); } @@ -377,7 +373,7 @@ public String toString(final LocalDateTime instance) { @Override public LocalDateTime fromString(final String text) { try { - return parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toLocalDateTime(); + return DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toLocalDateTime(); } catch (final DateTimeParseException dpe) { return LocalDateTime.parse(text); } @@ -409,7 +405,7 @@ public String toString(final LocalDate instance) { @Override public LocalDate fromString(final String text) { try { - return parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toLocalDate(); + return DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toLocalDate(); } catch (final DateTimeParseException dpe) { return LocalDate.parse(text); } @@ -440,7 +436,7 @@ public String toString(final Instant instance) { @Override public Instant fromString(final String text) { - return parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toInstant(); + return DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toInstant(); } }, Instant.class)); } @@ -468,7 +464,7 @@ public String toString(final GregorianCalendar instance) { @Override public GregorianCalendar fromString(final String text) { - final ZonedDateTime zonedDateTime = parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC); + final ZonedDateTime zonedDateTime = DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC); final Calendar instance = GregorianCalendar.getInstance(); instance.setTimeZone(TimeZone.getTimeZone(zonedDateTime.getZone())); instance.setTime(Date.from(zonedDateTime.toInstant())); @@ -500,7 +496,7 @@ public String toString(final Calendar instance) { @Override public Calendar fromString(final String text) { - final ZonedDateTime zonedDateTime = parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC); + final ZonedDateTime zonedDateTime = DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC); final Calendar instance = Calendar.getInstance(); instance.setTimeZone(TimeZone.getTimeZone(zonedDateTime.getZone())); instance.setTime(Date.from(zonedDateTime.toInstant())); @@ -529,7 +525,7 @@ public Calendar fromString(final String text) { private Adapter addDateConverter(final AdapterKey key) { if (useShortISO8601Format) { - return add(key, new ConverterAdapter<>(new DateConverter("yyyyMMddHHmmssZ"), Date.class)); + return add(key, new ConverterAdapter<>(DateConverter.ISO_8601_SHORT, Date.class)); } final ZoneId zoneIDUTC = ZoneId.of("UTC"); if (dateTimeFormatter != null) { @@ -543,7 +539,7 @@ public String toString(final Date instance) { @Override public Date fromString(final String text) { try { - return Date.from(parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toInstant()); + return Date.from(DateUtil.parseZonedDateTime(text, dateTimeFormatter, zoneIDUTC).toInstant()); } catch (final DateTimeParseException dpe) { return Date.from(LocalDateTime.parse(text).toInstant(ZoneOffset.UTC)); } @@ -568,22 +564,6 @@ public Date fromString(final String text) { }, Date.class)); } - private static ZonedDateTime parseZonedDateTime(final String text, final DateTimeFormatter formatter, final ZoneId defaultZone) { - final TemporalAccessor parse = formatter.parse(text); - ZoneId zone = parse.query(TemporalQueries.zone()); - if (Objects.isNull(zone)) { - zone = defaultZone; - } - final int year = parse.isSupported(YEAR) ? parse.get(YEAR) : 0; - final int month = parse.isSupported(MONTH_OF_YEAR) ? parse.get(MONTH_OF_YEAR) : 0; - final int day = parse.isSupported(DAY_OF_MONTH) ? parse.get(DAY_OF_MONTH) : 0; - final int hour = parse.isSupported(HOUR_OF_DAY) ? parse.get(HOUR_OF_DAY) : 0; - final int minute = parse.isSupported(MINUTE_OF_HOUR) ? parse.get(MINUTE_OF_HOUR) : 0; - final int second = parse.isSupported(SECOND_OF_MINUTE) ? parse.get(SECOND_OF_MINUTE) : 0; - final int millisecond = parse.isSupported(MILLI_OF_SECOND) ? parse.get(MILLI_OF_SECOND) : 0; - return ZonedDateTime.of(year, month, day, hour, minute, second, millisecond, zone); - } - private static void checkForDeprecatedTimeZone(final String text) { switch (text) { case "CST": // really for TCK, this sucks for end users so we don't fail for all deprecated zones diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/util/DateUtil.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/util/DateUtil.java new file mode 100644 index 00000000..e963d44c --- /dev/null +++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/util/DateUtil.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.mapper.util; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.TemporalQueries; + +public final class DateUtil { + private DateUtil() { + + } + + public static ZonedDateTime parseZonedDateTime(final String text, final DateTimeFormatter formatter, ZoneId defaultZone) { + final TemporalAccessor parse = formatter.parse(text); + ZoneId zone = parse.query(TemporalQueries.zone()); + if (zone == null) { + zone = defaultZone; + } + + return ZonedDateTime.of( + ifSupported(parse, ChronoField.YEAR), + ifSupported(parse, ChronoField.MONTH_OF_YEAR), + ifSupported(parse, ChronoField.DAY_OF_MONTH), + ifSupported(parse, ChronoField.HOUR_OF_DAY), + ifSupported(parse, ChronoField.MINUTE_OF_HOUR), + ifSupported(parse, ChronoField.SECOND_OF_MINUTE), + ifSupported(parse, ChronoField.MILLI_OF_SECOND), + zone); + } + + public static int ifSupported(TemporalAccessor temporal, ChronoField unit) { + if (temporal.isSupported(unit)) { + return temporal.get(unit); + } + + return 0; + } +} diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/DeserializationExceptionMessagesTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/DeserializationExceptionMessagesTest.java index ac6fec9c..de0dd22f 100644 --- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/DeserializationExceptionMessagesTest.java +++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/DeserializationExceptionMessagesTest.java @@ -394,7 +394,7 @@ public void dateFromObject() throws Exception { public void dateFromString() throws Exception { assertMessage("{ \"date\" : \"Supercalifragilisticexpialidocious\" }", "Widget property 'date' of type Date cannot be mapped to json string value: \"Supercalifragilisti...\n" + - "java.text.ParseException: Unparseable date: \"Supercalifragilisticexpialidocious\""); + "Text 'Supercalifragilisticexpialidocious' could not be parsed at index 0"); } @Test diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/UUIDConverterTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/UUIDConverterTest.java new file mode 100644 index 00000000..36137f87 --- /dev/null +++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/UUIDConverterTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.johnzon.mapper.converter; + +import org.apache.johnzon.mapper.Mapper; +import org.apache.johnzon.mapper.MapperBuilder; +import org.junit.Test; + +import java.util.UUID; + +import static org.junit.Assert.assertEquals; + +public class UUIDConverterTest { + private static final UUID THE_UUID = UUID.randomUUID(); + + @Test + public void serialize() { + Mapper mapper = new MapperBuilder().build(); + String json = mapper.writeObjectAsString(THE_UUID); + + assertEquals("\"" + THE_UUID + "\"", json); + } + + @Test + public void deserialize() { + Mapper mapper = new MapperBuilder().build(); + UUID uuid = mapper.readObject("\"" + THE_UUID + "\"", UUID.class); + + assertEquals(THE_UUID, uuid); + } +}