package lc.eo.json;

import lc.eo.EO;
import lc.eo.EOJsonSerializer;
import lc.eo.io.EOOutputStream;
import lc.mecha.json.JSONObject;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class JsonOutput implements EOOutputStream {
    private final OutputStream os;

    public JsonOutput(OutputStream outputStream) {
        this.os = outputStream;
    }

    public void writeEO(EO obj) throws IOException {
        JSONObject json = EOJsonSerializer.toJson(obj);
        os.write(json.toString().getBytes(StandardCharsets.UTF_8));
        os.write("\r\n".getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public void close() throws IOException {
        os.close();
    }

    public void flush() throws IOException {
        os.flush();
    }

}
