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

net.dongliu.cute.http.json.JacksonMarshaller Maven / Gradle / Ivy

The newest version!
package net.dongliu.cute.http.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.Objects;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

/**
 * JsonMarshaller Jackson implementation
 */
public class JacksonMarshaller implements JsonMarshaller {
    private final ObjectMapper mapper;

    public JacksonMarshaller() {
        this(new ObjectMapper()
                .disable(FAIL_ON_UNKNOWN_PROPERTIES));
    }

    public JacksonMarshaller(ObjectMapper mapper) {
        this.mapper = Objects.requireNonNull(mapper);
    }

    @Override
    public void marshal(@Nullable Object value, Writer writer) throws IOException {
        mapper.writeValue(writer, value);
    }

    @Override
    public  @Nullable T unmarshal(Reader reader, Type type) throws IOException {
        var javaType = mapper.getTypeFactory().constructType(type);
        return mapper.readValue(reader, javaType);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy