diff --git a/README.md b/README.md index e8f6cec1..c9c8bce9 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Project is composed of multiple Maven sub-modules, each corresponding to a jar: * [jr-retrofit2](../../tree/master/jr-retrofit2) contains `jackson-jr` - based handlers for [Retrofit 2](https://square.github.io/retrofit/) library * Depends on `jackson-jr` and `Retrofit` API jars, and indirectly on `jackson-core` * [jr-annotation-support](../../tree/master/jr-annotation-support) contains extension with support for a subset of core [Jackson annotations](../../../jackson-annotations) +* [jr-extension-javatime](../../tree/master/jr-extension-javatime) contains extension with support for a subset of Java 8 Date/Time types (e.g. `LocalDateTime`) * jr-all creates an "uber-jar" that contains individual modules along with all their dependencies: * `jr-objects` classes as-is, without relocating * `jr-stree` classes as-is, without relocating diff --git a/jr-extension-javatime/README.md b/jr-extension-javatime/README.md new file mode 100644 index 00000000..f8a6ebd2 --- /dev/null +++ b/jr-extension-javatime/README.md @@ -0,0 +1,54 @@ +## Overview + +This module extends the functionality of jackson-jr by adding support for (a subset of) Java 8 Date/Time types (value types in JDK `java.time` package). + +### Status + +Added in Jackson 2.17. + +### Usage +To be able to use supported annotations, you need to register extension like so: +```java + private static final JSON JACKSON = JSON.builder() + .register(new JacksonJrJavaTimeExtension()) + .build(); +``` +after which you can use normal read and write operations as usual: + +```java +import java.time.LocalDateTime; + +public class Application { + public static void main(String[] args) { + final LocalDateTime now = LocalDateTime.now(); + MyClass myObject = new MyClass(now, 'Some Other Values....'); + String myObjectJsonString = JACKSON.asString(myObject); + MyClass myObjectFromJson = JACKSON.beanFrom(MyClass, myObjectJsonString); + assert myObjectFromJson.getTime().equals(now); + } +} + +// ... + +public class MyClass { + private LocalDateTime time; + private String otherItems; + + public MyClass(LocalDateTime datetime, String others) { + //... + } + + public LocalDateTime getTime() { + return time; + } + // other getters & setters +} +``` + +### Date Classes currently supported by `JacksonJrJavaTimeExtension` + +- `java.util.LocalDateTime` + +### Plans for Future + +- Add support for other Java 8 Date/Time types diff --git a/jr-extension-javatime/pom.xml b/jr-extension-javatime/pom.xml new file mode 100644 index 00000000..2c3c0e4f --- /dev/null +++ b/jr-extension-javatime/pom.xml @@ -0,0 +1,71 @@ + + + + + + + 4.0.0 + + + com.fasterxml.jackson.jr + jackson-jr-parent + 2.17.0-rc1-SNAPSHOT + + + jackson-jr-extension-javatime + bundle + Jackson-jr extension that adds support for Java 8 Date/Time value types such as `java.util.LocalDateTime` + https://github.com/FasterXML/jackson-jr + + + + The Apache Software License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + ${project.groupId}.extension.javatime;version=${project.version} + + + 2023-11-15T22:39:39Z + + + + + com.fasterxml.jackson.jr + jackson-jr-objects + ${project.version} + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${surefire.redirectTestOutputToFile} + + **/failing/*.java + + + + + + org.moditect + moditect-maven-plugin + + + + de.jjohannes + gradle-module-metadata-maven-plugin + + + + + diff --git a/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/JacksonJrJavaTimeExtension.java b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/JacksonJrJavaTimeExtension.java new file mode 100644 index 00000000..a1077d78 --- /dev/null +++ b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/JacksonJrJavaTimeExtension.java @@ -0,0 +1,20 @@ +package com.fasterxml.jackson.jr.extension.javatime; + +import com.fasterxml.jackson.jr.ob.JacksonJrExtension; +import com.fasterxml.jackson.jr.ob.api.ExtensionContext; + +public class JacksonJrJavaTimeExtension extends JacksonJrExtension { + final static JavaTimeReaderWriterProvider DEFAULT_RW_PROVIDER = new JavaTimeReaderWriterProvider(); + + private JavaTimeReaderWriterProvider readerWriterProvider = DEFAULT_RW_PROVIDER; + + @Override + protected void register(ExtensionContext ctxt) { + ctxt.insertProvider(readerWriterProvider); + } + + public JacksonJrJavaTimeExtension with(JavaTimeReaderWriterProvider p) { + readerWriterProvider = p; + return this; + } +} diff --git a/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/JavaTimeReaderWriterProvider.java b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/JavaTimeReaderWriterProvider.java new file mode 100644 index 00000000..bf204791 --- /dev/null +++ b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/JavaTimeReaderWriterProvider.java @@ -0,0 +1,48 @@ +package com.fasterxml.jackson.jr.extension.javatime; + +import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider; +import com.fasterxml.jackson.jr.ob.api.ValueReader; +import com.fasterxml.jackson.jr.ob.api.ValueWriter; +import com.fasterxml.jackson.jr.ob.impl.JSONReader; +import com.fasterxml.jackson.jr.ob.impl.JSONWriter; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Provider for {@link ValueReader}s and {@link ValueWriter}s for Date/Time + * types supported by Java Time Extension. + */ +public class JavaTimeReaderWriterProvider extends ReaderWriterProvider +{ + private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + + public JavaTimeReaderWriterProvider() { } + + @Override + public ValueReader findValueReader(JSONReader readContext, Class type) { + return LocalDateTime.class.isAssignableFrom(type) ? new LocalDateTimeValueReader(dateTimeFormatter) : null; + } + + @Override + public ValueWriter findValueWriter(JSONWriter writeContext, Class type) { + return LocalDateTime.class.isAssignableFrom(type) ? new LocalDateTimeValueWriter(dateTimeFormatter) : null; + } + + /** + * Method for reconfiguring {@link DateTimeFormatter} used for reading/writing + * following Date/Time value types: + * + * + * @param formatter + * + * @return This provider instance for call chaining + */ + public JavaTimeReaderWriterProvider withDateTimeFormatter(DateTimeFormatter formatter) { + dateTimeFormatter = formatter; + return this; + } +} diff --git a/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/LocalDateTimeValueReader.java b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/LocalDateTimeValueReader.java new file mode 100644 index 00000000..cc133327 --- /dev/null +++ b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/LocalDateTimeValueReader.java @@ -0,0 +1,23 @@ +package com.fasterxml.jackson.jr.extension.javatime; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.jr.ob.api.ValueReader; +import com.fasterxml.jackson.jr.ob.impl.JSONReader; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class LocalDateTimeValueReader extends ValueReader { + private final DateTimeFormatter formatter; + + public LocalDateTimeValueReader(DateTimeFormatter formatter) { + super(LocalDateTime.class); + this.formatter = formatter; + } + + @Override + public Object read(JSONReader reader, JsonParser p) throws IOException { + return LocalDateTime.parse(p.getText(), formatter); + } +} diff --git a/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/LocalDateTimeValueWriter.java b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/LocalDateTimeValueWriter.java new file mode 100644 index 00000000..020963f6 --- /dev/null +++ b/jr-extension-javatime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/LocalDateTimeValueWriter.java @@ -0,0 +1,28 @@ +package com.fasterxml.jackson.jr.extension.javatime; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.jr.ob.api.ValueWriter; +import com.fasterxml.jackson.jr.ob.impl.JSONWriter; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class LocalDateTimeValueWriter implements ValueWriter { + private final DateTimeFormatter formatter; + + public LocalDateTimeValueWriter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + @Override + public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException { + String localDateTimeString = ((LocalDateTime) value).format(formatter); + context.writeValue(localDateTimeString); + } + + @Override + public Class valueType() { + return LocalDateTime.class; + } +} diff --git a/jr-extension-javatime/src/main/resources/META-INF/LICENSE b/jr-extension-javatime/src/main/resources/META-INF/LICENSE new file mode 100644 index 00000000..56dcbf42 --- /dev/null +++ b/jr-extension-javatime/src/main/resources/META-INF/LICENSE @@ -0,0 +1,8 @@ +This copy of Jackson-jr library is licensed under the +Apache (Software) License, version 2.0 ("the License"). +See the License for details about distribution rights, and the +specific rights regarding derivative works. + +You may obtain a copy of the License at: + +http://www.apache.org/licenses/LICENSE-2.0 diff --git a/jr-extension-javatime/src/main/resources/META-INF/NOTICE b/jr-extension-javatime/src/main/resources/META-INF/NOTICE new file mode 100644 index 00000000..d55c59a0 --- /dev/null +++ b/jr-extension-javatime/src/main/resources/META-INF/NOTICE @@ -0,0 +1,17 @@ +# Jackson JSON processor + +Jackson is a high-performance, Free/Open Source JSON processing library. +It was originally written by Tatu Saloranta (tatu.saloranta@iki.fi), and has +been in development since 2007. +It is currently developed by a community of developers. + +## Licensing + +Jackson components are licensed under Apache (Software) License, version 2.0, +as per accompanying LICENSE file. + +## Credits + +A list of contributors may be found from CREDITS file, which is included +in some artifacts (usually source distributions); but is always available +from the source code management (SCM) system project uses. diff --git a/jr-extension-javatime/src/moditect/module-info.java b/jr-extension-javatime/src/moditect/module-info.java new file mode 100644 index 00000000..80434722 --- /dev/null +++ b/jr-extension-javatime/src/moditect/module-info.java @@ -0,0 +1,6 @@ +module com.fasterxml.jackson.jr.extension.javatime { + requires com.fasterxml.jackson.core; + requires com.fasterxml.jackson.jr.ob; + + exports com.fasterxml.jackson.jr.extension.javatime; +} diff --git a/pom.xml b/pom.xml index eac0f305..d9347b16 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ jr-retrofit2 jr-stree jr-annotation-support + jr-extension-javatime jr-all diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 67db91bb..d6c182ee 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -39,3 +39,8 @@ Julian Honnen (@jhonnen) * Reported #112: `overrideStandardValueWriter` only applied to first `java.nio.file.Path` valued field of bean (2.17.0) + +@Shounaks + +* Contributed implf ro #100: Add support for `java.time` (Java 8 date/time) types + (2.17.0) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 5873c6db..59199792 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -15,6 +15,9 @@ Modules: #78: Deserializes "null" to "0.0" for `java.lang.Double` (wrapper) (reported by @bill-phast) +#100: Add support for `java.time` (Java 8 date/time) types + (requested by @sebastian-zero) + (contributed by @Shounaks) #112: `overrideStandardValueWriter` only applied to first `java.nio.file.Path` valued field of bean (reported by Julian H)