Skip to content

Commit

Permalink
Minor renaming, bit more configurability
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 19, 2024
1 parent 5a11de7 commit 7676b74
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
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(new JavaTimeReaderWriterProvider());
ctxt.insertProvider(readerWriterProvider);
}

public JacksonJrJavaTimeExtension with(JavaTimeReaderWriterProvider p) {
readerWriterProvider = p;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,40 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JavaTimeReaderWriterProvider extends ReaderWriterProvider {
private DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
/**
* 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 type.equals(LocalDateTime.class) ? new JavaTimeValueReader(formatter) : null;
return LocalDateTime.class.isAssignableFrom(type) ? new LocalDateTimeValueReader(dateTimeFormatter) : null;
}

@Override
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
return type.equals(LocalDateTime.class) ? new JavaTimeValueWriter(formatter) : null;
return LocalDateTime.class.isAssignableFrom(type) ? new LocalDateTimeValueWriter(dateTimeFormatter) : null;
}

public JavaTimeReaderWriterProvider withFormatter(DateTimeFormatter formatter) {
this.formatter = formatter;
/**
* Method for reconfiguring {@link DateTimeFormatter} used for reading/writing
* following Date/Time value types:
*<ul>
* <li>{@code java.time.LocalDateTime}
* </li>
*</ul>
*
* @param formatter
*
* @return This provider instance for call chaining
*/
public JavaTimeReaderWriterProvider withDateTimeFormatter(DateTimeFormatter formatter) {
dateTimeFormatter = formatter;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JavaTimeValueReader extends ValueReader {
public class LocalDateTimeValueReader extends ValueReader {
private final DateTimeFormatter formatter;

public JavaTimeValueReader(DateTimeFormatter formatter) {
public LocalDateTimeValueReader(DateTimeFormatter formatter) {
super(LocalDateTime.class);
this.formatter = formatter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JavaTimeValueWriter implements ValueWriter {
public class LocalDateTimeValueWriter implements ValueWriter {
private final DateTimeFormatter formatter;

public JavaTimeValueWriter(DateTimeFormatter formatter) {
public LocalDateTimeValueWriter(DateTimeFormatter formatter) {
this.formatter = formatter;
}

Expand Down

0 comments on commit 7676b74

Please sign in to comment.