]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/JsonWriter.java
Add CommandException to abstract cli return codes for errors
[signal-cli] / src / main / java / org / asamk / signal / JsonWriter.java
index 3cc875145ec1d4f6b60675fdb6b9d18b077cbd52..e7549adf8bcb4d1c13231adb7a4a201c7e317bf6 100644 (file)
@@ -6,32 +6,38 @@ import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
+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 {
 
-    private final OutputStreamWriter writer;
+    private final Writer writer;
     private final ObjectMapper objectMapper;
 
     public JsonWriter(final OutputStream writer) {
-        this.writer = new OutputStreamWriter(writer, StandardCharsets.UTF_8);
+        this.writer = new BufferedWriter(new OutputStreamWriter(writer, StandardCharsets.UTF_8));
 
         objectMapper = new ObjectMapper();
-        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
         objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
     }
 
-    public void write(final Object object) throws IOException {
+    public void write(final Object object) {
         try {
-            objectMapper.writeValue(writer, object);
-        } catch (JsonProcessingException e) {
-            // Some issue with json serialization, probably caused by a bug
+            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);
         }
-        writer.write(System.lineSeparator());
-        writer.flush();
     }
 }