ClickHouse/src/Common/JSONBuilder.h

115 lines
3.0 KiB
C++
Raw Normal View History

2021-04-13 17:31:43 +00:00
#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;
2021-04-13 17:31:43 +00:00
};
struct FormatContext
{
WriteBuffer & out;
size_t offset = 0;
2021-04-13 17:31:43 +00:00
};
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_) {}
2021-04-13 17:31:43 +00:00
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_)
{
2021-04-20 08:26:54 +00:00
static_assert(std::is_arithmetic_v<T>, "JSONNumber support only numeric types");
2021-04-13 17:31:43 +00:00
}
void format(const FormatSettings & settings, FormatContext & context) override
{
writeJSONNumber(value, context.out, settings.settings);
2021-04-13 17:31:43 +00:00
}
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;
};
2021-04-13 17:31:43 +00:00
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)); }
2021-04-13 17:31:43 +00:00
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:
2021-10-11 11:28:46 +00:00
void add(std::string key, ItemPtr value) { values.emplace_back(Pair{.key = std::move(key), .value = std::move(value)}); } //-V1030
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)); }
2021-04-13 17:31:43 +00:00
void format(const FormatSettings & settings, FormatContext & context) override;
private:
std::vector<Pair> values;
};
}