aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/JSONBuilder.h
blob: 1690ae7bb771e8b3f5e3673f291083603e190da1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <type_traits>
#include <vector>
#include <IO/WriteBuffer.h>
#include <Formats/FormatSettings.h>
#include <IO/WriteHelpers.h>

namespace DB::JSONBuilder
{

struct FormatSettings
{
    const DB::FormatSettings & settings;
    size_t indent = 2;
    bool print_simple_arrays_in_single_row = true;
};

struct FormatContext
{
    WriteBuffer & out;
    size_t offset = 0;
};

class IItem
{
public:
    virtual ~IItem() = default;
    virtual void format(const FormatSettings & settings, FormatContext & context) = 0;
};

using ItemPtr = std::unique_ptr<IItem>;

class JSONString : public IItem
{
public:
    explicit JSONString(std::string_view value_) : value(value_) {}
    void format(const FormatSettings & settings, FormatContext & context) override;

private:
    std::string value;
};

template <typename T>
class JSONNumber : public IItem
{
public:
    explicit JSONNumber(T value_) : value(value_)
    {
        static_assert(std::is_arithmetic_v<T>, "JSONNumber support only numeric types");
    }

    void format(const FormatSettings & settings, FormatContext & context) override
    {
        writeJSONNumber(value, context.out, settings.settings);
    }

private:
    T value;
};

class JSONBool : public IItem
{
public:
    explicit JSONBool(bool value_) : value(value_) {}
    void format(const FormatSettings & settings, FormatContext & context) override;

private:
    bool value;
};

class JSONArray : public IItem
{
public:
    void add(ItemPtr value) { values.push_back(std::move(value)); }
    void add(std::string value) { add(std::make_unique<JSONString>(std::move(value))); }
    void add(const char * value) { add(std::make_unique<JSONString>(value)); }
    void add(bool value) { add(std::make_unique<JSONBool>(value)); }

    template <typename T>
    requires std::is_arithmetic_v<T>
    void add(T value) { add(std::make_unique<JSONNumber<T>>(value)); }

    void format(const FormatSettings & settings, FormatContext & context) override;

private:
    std::vector<ItemPtr> values;
};

class JSONMap : public IItem
{
    struct Pair
    {
        std::string key;
        ItemPtr value;
    };

public:
    void add(std::string key, ItemPtr value) { values.emplace_back(Pair{.key = std::move(key), .value = std::move(value)}); }
    void add(std::string key, std::string value) { add(std::move(key), std::make_unique<JSONString>(std::move(value))); }
    void add(std::string key, const char * value) { add(std::move(key), std::make_unique<JSONString>(value)); }
    void add(std::string key, std::string_view value) { add(std::move(key), std::make_unique<JSONString>(value)); }
    void add(std::string key, bool value) { add(std::move(key), std::make_unique<JSONBool>(value)); }

    template <typename T>
    requires std::is_arithmetic_v<T>
    void add(std::string key, T value) { add(std::move(key), std::make_unique<JSONNumber<T>>(value)); }

    void format(const FormatSettings & settings, FormatContext & context) override;

private:
    std::vector<Pair> values;
};

}