1 | #pragma once |
2 | |
3 | #include <Core/Block.h> |
4 | #include <IO/Progress.h> |
5 | #include <IO/WriteBuffer.h> |
6 | #include <Common/Stopwatch.h> |
7 | #include <Processors/Formats/IRowOutputFormat.h> |
8 | #include <Formats/FormatSettings.h> |
9 | |
10 | |
11 | namespace DB |
12 | { |
13 | |
14 | /** Stream for output data in JSON format. |
15 | */ |
16 | class JSONRowOutputFormat : public IRowOutputFormat |
17 | { |
18 | public: |
19 | JSONRowOutputFormat(WriteBuffer & out_, const Block & , FormatFactory::WriteCallback callback, const FormatSettings & settings_); |
20 | |
21 | String getName() const override { return "JSONRowOutputFormat" ; } |
22 | |
23 | void writeField(const IColumn & column, const IDataType & type, size_t row_num) override; |
24 | void writeFieldDelimiter() override; |
25 | void writeRowStartDelimiter() override; |
26 | void writeRowEndDelimiter() override; |
27 | void writeRowBetweenDelimiter() override; |
28 | void writePrefix() override; |
29 | void writeSuffix() override; |
30 | |
31 | void writeMinExtreme(const Columns & columns, size_t row_num) override; |
32 | void writeMaxExtreme(const Columns & columns, size_t row_num) override; |
33 | void writeTotals(const Columns & columns, size_t row_num) override; |
34 | |
35 | void writeBeforeTotals() override; |
36 | void writeAfterTotals() override; |
37 | void writeBeforeExtremes() override; |
38 | void writeAfterExtremes() override; |
39 | |
40 | void writeLastSuffix() override; |
41 | |
42 | void flush() override |
43 | { |
44 | ostr->next(); |
45 | |
46 | if (validating_ostr) |
47 | out.next(); |
48 | } |
49 | |
50 | void setRowsBeforeLimit(size_t rows_before_limit_) override |
51 | { |
52 | applied_limit = true; |
53 | rows_before_limit = rows_before_limit_; |
54 | } |
55 | |
56 | void onProgress(const Progress & value) override; |
57 | |
58 | String getContentType() const override { return "application/json; charset=UTF-8" ; } |
59 | |
60 | protected: |
61 | virtual void writeTotalsField(const IColumn & column, const IDataType & type, size_t row_num); |
62 | virtual void writeExtremesElement(const char * title, const Columns & columns, size_t row_num); |
63 | virtual void writeTotalsFieldDelimiter() { writeFieldDelimiter(); } |
64 | |
65 | void writeRowsBeforeLimitAtLeast(); |
66 | void writeStatistics(); |
67 | |
68 | |
69 | std::unique_ptr<WriteBuffer> validating_ostr; /// Validates UTF-8 sequences, replaces bad sequences with replacement character. |
70 | WriteBuffer * ostr; |
71 | |
72 | size_t field_number = 0; |
73 | size_t row_count = 0; |
74 | bool applied_limit = false; |
75 | size_t rows_before_limit = 0; |
76 | NamesAndTypes fields; |
77 | |
78 | Progress progress; |
79 | Stopwatch watch; |
80 | FormatSettings settings; |
81 | }; |
82 | |
83 | } |
84 | |