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
26 changes: 26 additions & 0 deletions jr-extension-datetime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<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>jr-extension-datetime</artifactId>

<properties>
<maven.compiler.source>20</maven.compiler.source>
Shounaks marked this conversation as resolved.
Show resolved Hide resolved
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-objects</artifactId>
</dependency>
</dependencies>

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

<url>https://github.com/FasterXML/jackson-jr</url>
Expand Down
Loading