package leigh.eo.json;

import leigh.eo.EO;
import leigh.eo.EOJsonSerializer;
import leigh.eo.io.EOOutputStream;
import leigh.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();
    }

}
