Skip to content

Commit

Permalink
Using Module for DateTime Extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks committed Feb 11, 2024
1 parent 85ed0e2 commit 7cff9b7
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
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>
<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>

</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 {
@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>
</modules>

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

0 comments on commit 7cff9b7

Please sign in to comment.