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

com.codepoetics.octarine.json.serialisation.Serialiser Maven / Gradle / Ivy

There is a newer version: 0.18
Show newest version
package com.codepoetics.octarine.json.serialisation;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.function.BiConsumer;

public interface Serialiser extends BiConsumer {

    default String toString(T value) {
        StringWriter writer = new StringWriter();
        try {
            toWriter(writer, value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        writer.flush();
        return writer.toString();
    }

    default void toWriter(Writer writer, T value) throws IOException {
        try (JsonGenerator jsonWriter = new JsonFactory().createGenerator(writer)) {
            accept(jsonWriter, value);
            jsonWriter.flush();
        } catch (SerialisationException e) {
            throw e.getIOExceptionCause();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy