Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension jackson-jr-extension-javatime to support (some) Java 8 date/time types #111

Merged
merged 13 commits into from
Feb 19, 2024
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-datetime](../../tree/master/jr-extension-datetime) contains extension with support 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
Expand Down
34 changes: 34 additions & 0 deletions jr-extension-datetime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Overview

This module extends the functionalities of jackson-jr adding support for a subset of Java 8 Date & Time objects such as `LocalDateTime` & `DateTime`.

### Example

```java
import com.fasterxml.jackson.jr.extension.datetime.JacksonLocalDateTimeExtension;
import com.fasterxml.jackson.jr.ob.JSON;

import java.time.LocalDateTime;

class Application {
private static final JSON JACKSON = JSON.builder()
.register(new JacksonLocalDateTimeExtension())
.build();

public static void main(String[] args) {
// Create the class that we need to
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.time == now;
}

}
```

### Extension List
- LocalDateTime (`JacksonLocalDateTimeExtension`)

### Plans for Future
- Other Java 8 DateTime classes
71 changes: 71 additions & 0 deletions jr-extension-datetime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-parent</artifactId>
<version>2.17.0-rc1-SNAPSHOT</version>
</parent>

<artifactId>jackson-jr-extension-datetime</artifactId>
<packaging>bundle</packaging>
<description>Additional package that adds support for LocalDate time and other Time Related Fields</description>
<url>https://github.com/FasterXML/jackson-jr</url>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<!-- Looks like we need to be bit careful on OSGi exports, to avoid
accidentally double-exporting jr-objects types
-->
<osgi.export>${project.groupId}.extension.datetime;version=${project.version}</osgi.export>

<!-- for Reproducible Builds -->
<project.build.outputTimestamp>2023-11-15T22:39:39Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-objects</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

Shounaks marked this conversation as resolved.
Show resolved Hide resolved
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>${surefire.redirectTestOutputToFile}</redirectTestOutputToFile>
<excludes>
<exclude>**/failing/*.java</exclude>
</excludes>
</configuration>
</plugin>
<!-- 11-Mar-2019, tatu: Add basic JDK8-includable module-info, generated by Moditect -->
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
</plugin>
<!-- 05-Jul-2020, tatu: Add generation of Gradle Module Metadata -->
<plugin>
<groupId>de.jjohannes</groupId>
<artifactId>gradle-module-metadata-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fasterxml.jackson.jr.extension.datetime;

import com.fasterxml.jackson.jr.ob.JacksonJrExtension;
import com.fasterxml.jackson.jr.ob.api.ExtensionContext;

public class JacksonLocalDateTimeExtension extends JacksonJrExtension {
Shounaks marked this conversation as resolved.
Show resolved Hide resolved
@Override
protected void register(ExtensionContext ctxt) {
ctxt.insertProvider(new LocalDateTimeReaderWriterProvider());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fasterxml.jackson.jr.extension.datetime;

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;

public class LocalDateTimeReaderWriterProvider extends ReaderWriterProvider {
@Override
public ValueReader findValueReader(JSONReader readContext, Class<?> type) {
return type.equals(LocalDateTime.class) ? new LocalDateTimeValueReader() : null;
}

@Override
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
return type.equals(LocalDateTime.class) ? new LocalDateTimeValueWriter() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fasterxml.jackson.jr.extension.datetime;

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 {
public LocalDateTimeValueReader() {
super(LocalDateTime.class);
}

@Override
public Object read(JSONReader reader, JsonParser p) throws IOException {
return LocalDateTime.parse(p.getText(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.fasterxml.jackson.jr.extension.datetime;

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 {
@Override
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException {
String localDateTimeString = ((LocalDateTime) value).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
context.writeValue(localDateTimeString);
}

@Override
public Class<?> valueType() {
return LocalDateTime.class;
}
}
8 changes: 8 additions & 0 deletions jr-extension-datetime/src/main/resources/META-INF/LICENSE
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions jr-extension-datetime/src/main/resources/META-INF/NOTICE
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions jr-extension-datetime/src/moditect/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module com.fasterxml.jackson.jr.extension.datetime {
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.jr.ob;

exports com.fasterxml.jackson.jr.extension.datetime;
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>jr-retrofit2</module>
<module>jr-stree</module>
<module>jr-annotation-support</module>
<module>jr-extension-datetime</module>
Shounaks marked this conversation as resolved.
Show resolved Hide resolved
<module>jr-all</module>
</modules>

Expand Down
Loading