2020-03-12 15:23:25 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
2020-04-10 16:46:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-03-12 15:23:25 +00:00
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
#pragma once
|
|
|
|
#include "toml_formatter.h"
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_SWITCH_WARNINGS
|
2020-08-09 09:27:14 +00:00
|
|
|
TOML_DISABLE_SPAM_WARNINGS
|
2020-04-13 20:23:11 +00:00
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_START
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief A wrapper for printing TOML objects out to a stream as formatted JSON.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto some_toml = toml::parse(R"(
|
|
|
|
/// [fruit]
|
|
|
|
/// apple.color = "red"
|
|
|
|
/// apple.taste.sweet = true
|
|
|
|
///
|
|
|
|
/// [fruit.apple.texture]
|
|
|
|
/// smooth = true
|
|
|
|
/// )"sv);
|
|
|
|
/// std::cout << toml::json_formatter{ some_toml } << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// {
|
|
|
|
/// "fruit" : {
|
|
|
|
/// "apple" : {
|
|
|
|
/// "color" : "red",
|
|
|
|
/// "taste" : {
|
|
|
|
/// "sweet" : true
|
|
|
|
/// },
|
|
|
|
/// "texture" : {
|
|
|
|
/// "smooth" : true
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-04-02 21:39:21 +00:00
|
|
|
/// \tparam Char The underlying character type of the output stream. Must be 1 byte in size.
|
|
|
|
template <typename Char = char>
|
|
|
|
class TOML_API json_formatter final : impl::formatter<Char>
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
private:
|
2020-04-02 21:39:21 +00:00
|
|
|
using base = impl::formatter<Char>;
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-04-07 19:34:54 +00:00
|
|
|
void print(const toml::table& tbl);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
void print(const array& arr)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
if (arr.empty())
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream("[]"sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
else
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream('[', base::stream());
|
|
|
|
base::increase_indent();
|
2020-01-06 18:21:16 +00:00
|
|
|
for (size_t i = 0; i < arr.size(); i++)
|
|
|
|
{
|
|
|
|
if (i > 0_sz)
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(',', base::stream());
|
|
|
|
base::print_newline(true);
|
|
|
|
base::print_indent();
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
auto& v = arr[i];
|
|
|
|
const auto type = v.type();
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_ASSUME(type != node_type::none);
|
2020-01-06 18:21:16 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
|
|
|
|
case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
|
2020-01-06 18:21:16 +00:00
|
|
|
default:
|
2020-04-13 20:23:11 +00:00
|
|
|
base::print_value(v, type);
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
base::decrease_indent();
|
|
|
|
base::print_newline(true);
|
|
|
|
base::print_indent();
|
|
|
|
impl::print_to_stream(']', base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
base::clear_naked_newline();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
void print()
|
2020-01-12 15:37:02 +00:00
|
|
|
{
|
|
|
|
switch (auto source_type = base::source().type())
|
|
|
|
{
|
2020-07-21 13:11:32 +00:00
|
|
|
case node_type::table:
|
|
|
|
print(*reinterpret_cast<const table*>(&base::source()));
|
|
|
|
base::print_newline();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::array:
|
|
|
|
print(*reinterpret_cast<const array*>(&base::source()));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
base::print_value(base::source(), source_type);
|
2020-01-12 15:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
public:
|
|
|
|
|
2020-07-13 18:18:04 +00:00
|
|
|
/// \brief The default flags for a json_formatter.
|
|
|
|
static constexpr format_flags default_flags = format_flags::quote_dates_and_times;
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Constructs a JSON formatter and binds it to a TOML object.
|
|
|
|
///
|
|
|
|
/// \param source The source TOML object.
|
|
|
|
/// \param flags Format option flags.
|
2020-01-06 18:21:16 +00:00
|
|
|
TOML_NODISCARD_CTOR
|
2020-07-13 18:18:04 +00:00
|
|
|
explicit json_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
|
|
|
|
: base{ source, flags }
|
2020-01-06 18:21:16 +00:00
|
|
|
{}
|
|
|
|
|
2020-03-03 08:10:07 +00:00
|
|
|
template <typename T, typename U>
|
2020-03-28 16:56:59 +00:00
|
|
|
friend std::basic_ostream<T>& operator << (std::basic_ostream<T>&, json_formatter<U>&);
|
2020-03-03 08:10:07 +00:00
|
|
|
template <typename T, typename U>
|
2020-03-28 16:56:59 +00:00
|
|
|
friend std::basic_ostream<T>& operator << (std::basic_ostream<T>&, json_formatter<U>&&);
|
2020-01-06 18:21:16 +00:00
|
|
|
};
|
2020-04-02 21:39:21 +00:00
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
#if !defined(DOXYGEN) && !TOML_HEADER_ONLY
|
2020-04-02 21:39:21 +00:00
|
|
|
extern template class TOML_API json_formatter<char>;
|
|
|
|
#endif
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-03-18 13:28:00 +00:00
|
|
|
json_formatter(const table&) -> json_formatter<char>;
|
|
|
|
json_formatter(const array&) -> json_formatter<char>;
|
|
|
|
template <typename T> json_formatter(const value<T>&) -> json_formatter<char>;
|
|
|
|
|
2020-03-03 08:10:07 +00:00
|
|
|
/// \brief Prints the bound TOML object out to the stream as JSON.
|
|
|
|
template <typename T, typename U>
|
2020-07-18 12:10:19 +00:00
|
|
|
inline std::basic_ostream<T>& operator << (std::basic_ostream<T>& lhs, json_formatter<U>& rhs)
|
2020-03-03 08:10:07 +00:00
|
|
|
{
|
|
|
|
rhs.attach(lhs);
|
|
|
|
rhs.print();
|
|
|
|
rhs.detach();
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Prints the bound TOML object out to the stream as JSON (rvalue overload).
|
|
|
|
template <typename T, typename U>
|
2020-07-18 12:10:19 +00:00
|
|
|
inline std::basic_ostream<T>& operator << (std::basic_ostream<T>& lhs, json_formatter<U>&& rhs)
|
2020-03-03 08:10:07 +00:00
|
|
|
{
|
|
|
|
return lhs << rhs; //as lvalue
|
|
|
|
}
|
2020-04-02 21:39:21 +00:00
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
#if !defined(DOXYGEN) && !TOML_HEADER_ONLY
|
2020-04-02 21:39:21 +00:00
|
|
|
extern template TOML_API std::ostream& operator << (std::ostream&, json_formatter<char>&);
|
|
|
|
extern template TOML_API std::ostream& operator << (std::ostream&, json_formatter<char>&&);
|
|
|
|
#endif
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_END
|
2020-03-28 16:56:59 +00:00
|
|
|
|
2020-08-09 09:27:14 +00:00
|
|
|
TOML_POP_WARNINGS // TOML_DISABLE_SWITCH_WARNINGS, TOML_DISABLE_SPAM_WARNINGS
|