All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fasterxml.jackson.jr.extension.javatime.LocalDateTimeValueWriter Maven / Gradle / Ivy

Go to download

Jackson-jr extension that adds support for Java 8 Date/Time value types such as `java.time.LocalDateTime`

There is a newer version: 2.18.2
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy