2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/WriteBufferValidUTF8.h>
|
|
|
|
#include <DataStreams/JSONEachRowRowOutputStream.h>
|
2016-02-18 11:44:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2018-06-08 01:51:55 +00:00
|
|
|
JSONEachRowRowOutputStream::JSONEachRowRowOutputStream(WriteBuffer & ostr_, const Block & sample, const FormatSettings & settings)
|
|
|
|
: ostr(ostr_), settings(settings)
|
2016-02-18 11:44:50 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t columns = sample.columns();
|
|
|
|
fields.resize(columns);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
WriteBufferFromString out(fields[i]);
|
|
|
|
writeJSONString(sample.getByPosition(i).name, out);
|
|
|
|
}
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeField(const IColumn & column, const IDataType & type, size_t row_num)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(fields[field_number], ostr);
|
|
|
|
writeChar(':', ostr);
|
2017-07-05 16:28:57 +00:00
|
|
|
type.serializeTextJSON(column, row_num, ostr, settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
++field_number;
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeFieldDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar(',', ostr);
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeRowStartDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('{', ostr);
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeRowEndDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeCString("}\n", ostr);
|
|
|
|
field_number = 0;
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|