Add PlainTextWriter interface

This commit is contained in:
AsamK 2021-08-09 19:02:24 +02:00
parent 641dc7577c
commit 15e8029715
16 changed files with 69 additions and 66 deletions

View file

@ -1,43 +1,6 @@
package org.asamk.signal;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public interface JsonWriter extends OutputWriter {
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class JsonWriter implements OutputWriter {
private final Writer writer;
private final ObjectMapper objectMapper;
public JsonWriter(final OutputStream outputStream) {
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}
public void write(final Object object) {
try {
try {
objectMapper.writeValue(writer, object);
} catch (JsonProcessingException e) {
// Some issue with json serialization, probably caused by a bug
throw new AssertionError(e);
}
writer.write(System.lineSeparator());
writer.flush();
} catch (IOException e) {
throw new AssertionError(e);
}
}
void write(final Object object);
}