| 1 | #include <Processors/Formats/Impl/JSONCompactRowOutputFormat.h> |
| 2 | #include <Formats/FormatFactory.h> |
| 3 | |
| 4 | #include <IO/WriteHelpers.h> |
| 5 | |
| 6 | |
| 7 | namespace DB |
| 8 | { |
| 9 | |
| 10 | JSONCompactRowOutputFormat::JSONCompactRowOutputFormat( |
| 11 | WriteBuffer & out_, const Block & , FormatFactory::WriteCallback callback, const FormatSettings & settings_) |
| 12 | : JSONRowOutputFormat(out_, header, callback, settings_) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | |
| 17 | void JSONCompactRowOutputFormat::writeField(const IColumn & column, const IDataType & type, size_t row_num) |
| 18 | { |
| 19 | type.serializeAsTextJSON(column, row_num, *ostr, settings); |
| 20 | ++field_number; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | void JSONCompactRowOutputFormat::writeFieldDelimiter() |
| 25 | { |
| 26 | writeCString(", " , *ostr); |
| 27 | } |
| 28 | |
| 29 | void JSONCompactRowOutputFormat::writeTotalsFieldDelimiter() |
| 30 | { |
| 31 | writeCString("," , *ostr); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | void JSONCompactRowOutputFormat::writeRowStartDelimiter() |
| 36 | { |
| 37 | writeCString("\t\t[" , *ostr); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void JSONCompactRowOutputFormat::writeRowEndDelimiter() |
| 42 | { |
| 43 | writeChar(']', *ostr); |
| 44 | field_number = 0; |
| 45 | ++row_count; |
| 46 | } |
| 47 | |
| 48 | void JSONCompactRowOutputFormat::writeBeforeTotals() |
| 49 | { |
| 50 | writeCString(",\n" , *ostr); |
| 51 | writeChar('\n', *ostr); |
| 52 | writeCString("\t\"totals\": [" , *ostr); |
| 53 | } |
| 54 | |
| 55 | void JSONCompactRowOutputFormat::writeAfterTotals() |
| 56 | { |
| 57 | writeChar(']', *ostr); |
| 58 | } |
| 59 | |
| 60 | void JSONCompactRowOutputFormat::writeExtremesElement(const char * title, const Columns & columns, size_t row_num) |
| 61 | { |
| 62 | writeCString("\t\t\"" , *ostr); |
| 63 | writeCString(title, *ostr); |
| 64 | writeCString("\": [" , *ostr); |
| 65 | |
| 66 | size_t extremes_columns = columns.size(); |
| 67 | for (size_t i = 0; i < extremes_columns; ++i) |
| 68 | { |
| 69 | if (i != 0) |
| 70 | writeTotalsFieldDelimiter(); |
| 71 | |
| 72 | writeField(*columns[i], *types[i], row_num); |
| 73 | } |
| 74 | |
| 75 | writeChar(']', *ostr); |
| 76 | } |
| 77 | |
| 78 | void registerOutputFormatProcessorJSONCompact(FormatFactory & factory) |
| 79 | { |
| 80 | factory.registerOutputFormatProcessor("JSONCompact" , []( |
| 81 | WriteBuffer & buf, |
| 82 | const Block & sample, |
| 83 | FormatFactory::WriteCallback callback, |
| 84 | const FormatSettings & format_settings) |
| 85 | { |
| 86 | return std::make_shared<JSONCompactRowOutputFormat>(buf, sample, callback, format_settings); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |