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

ca.ibodrov.mica.server.YamlMapper Maven / Gradle / Ivy

There is a newer version: 0.0.25
Show newest version
package ca.ibodrov.mica.server;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import static com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.*;
import static java.util.Objects.requireNonNull;

public class YamlMapper {

    private final ObjectMapper delegate;
    private final ObjectWriter prettyPrinter;

    public YamlMapper(ObjectMapper delegate) {
        this.delegate = requireNonNull(delegate).copyWith(YAMLFactory.builder()
                .disable(MINIMIZE_QUOTES)
                .disable(SPLIT_LINES)
                .disable(WRITE_DOC_START_MARKER)
                .enable(LITERAL_BLOCK_STYLE)
                .build());

        this.prettyPrinter = this.delegate.writerWithDefaultPrettyPrinter();
    }

    public  T readValue(String src, Class valueType) throws JsonProcessingException {
        return delegate.readValue(src, valueType);
    }

    public  T readValue(InputStream src, Class valueType) throws IOException {
        return delegate.readValue(src, valueType);
    }

    public  T readValue(Reader src, Class valueType) throws IOException {
        return delegate.readValue(src, valueType);
    }

    public  T convertValue(Object fromValue, Class valueType) {
        return delegate.convertValue(fromValue, valueType);
    }

    public String prettyPrint(Object value) throws IOException {
        return prettyPrinter.writeValueAsString(value);
    }

    public byte[] prettyPrintAsBytes(Object value) throws IOException {
        return prettyPrinter.writeValueAsBytes(value);
    }

    public JsonNode readTree(String in) throws JsonProcessingException {
        return delegate.readTree(in);
    }

    public ObjectNode createObjectNode() {
        return delegate.createObjectNode();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy