mirror of
https://github.com/marzer/tomlplusplus.git
synced 2025-02-24 03:40:13 +00:00
also: - added `toml_version.h` - added version number to toml.hpp - added `node::visit()` - added `table::empty()` - added `array::empty()` - added version number to `toml.hpp` - added ostream operator overload for `source_position` - added tests for string escape sequences - added tests for + in bare keys (toml/issues/687) - added tests for hexfloat literals (toml/issues/562) - added c++20 char8_t detection to meson - moved third party submodules to `/extern` - refactored noexcept version of `parse_result` to be more useful - refactored all code to 'mostly' stick to a 120 column limit - fixed some minor stuff picked up by gcc9
123 lines
2.5 KiB
C++
123 lines
2.5 KiB
C++
#pragma once
|
|
#include "toml_formatter.h"
|
|
|
|
namespace toml
|
|
{
|
|
class json_formatter final : impl::formatter
|
|
{
|
|
private:
|
|
using base = impl::formatter;
|
|
|
|
inline void write(const toml::table& tbl) TOML_MAY_THROW;
|
|
|
|
void write(const array& arr) TOML_MAY_THROW
|
|
{
|
|
if (arr.empty())
|
|
base::write("[]"sv);
|
|
else
|
|
{
|
|
base::write('[');
|
|
indent_level++;
|
|
for (size_t i = 0; i < arr.size(); i++)
|
|
{
|
|
if (i > 0_sz)
|
|
base::write(',');
|
|
write_newline(true);
|
|
write_indent();
|
|
|
|
auto v = arr.get(i);
|
|
const auto type = v->type();
|
|
switch (type)
|
|
{
|
|
case node_type::table: write(*reinterpret_cast<const table*>(v)); break;
|
|
case node_type::array: write(*reinterpret_cast<const array*>(v)); break;
|
|
default:
|
|
write_value(v, type);
|
|
}
|
|
|
|
}
|
|
indent_level--;
|
|
write_newline(true);
|
|
write_indent();
|
|
base::write(']');
|
|
}
|
|
naked_newline = false;
|
|
}
|
|
|
|
void write() TOML_MAY_THROW
|
|
{
|
|
write(source);
|
|
}
|
|
|
|
public:
|
|
|
|
TOML_NODISCARD_CTOR
|
|
json_formatter(const toml::table& source_, toml::string_view indent_string = {}) noexcept
|
|
: formatter{
|
|
source_,
|
|
impl::formatter_options{
|
|
indent_string,
|
|
true //quote_dates_and_times
|
|
}
|
|
}
|
|
{}
|
|
|
|
template <typename CHAR>
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, json_formatter& rhs)
|
|
TOML_MAY_THROW
|
|
{
|
|
auto fw = impl::formatter_writer{ lhs };
|
|
rhs.attach(fw);
|
|
rhs.write();
|
|
rhs.detach();
|
|
return lhs;
|
|
}
|
|
|
|
template <typename CHAR>
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, json_formatter&& rhs)
|
|
TOML_MAY_THROW
|
|
{
|
|
return lhs << rhs; //as lvalue
|
|
}
|
|
};
|
|
|
|
inline void json_formatter::write(const toml::table& tbl) TOML_MAY_THROW
|
|
{
|
|
if (tbl.empty())
|
|
base::write("{}"sv);
|
|
else
|
|
{
|
|
base::write('{');
|
|
indent_level++;
|
|
bool first = false;
|
|
for (auto& [k, v] : tbl.values)
|
|
{
|
|
if (first)
|
|
base::write(", "sv);
|
|
first = true;
|
|
write_newline(true);
|
|
write_indent();
|
|
|
|
base::write_quoted_string(k);
|
|
base::write(" : "sv);
|
|
|
|
const auto type = v->type();
|
|
switch (type)
|
|
{
|
|
case node_type::table: write(*reinterpret_cast<const table*>(v.get())); break;
|
|
case node_type::array: write(*reinterpret_cast<const array*>(v.get())); break;
|
|
default:
|
|
write_value(v.get(), type);
|
|
}
|
|
|
|
}
|
|
indent_level--;
|
|
write_newline(true);
|
|
write_indent();
|
|
base::write('}');
|
|
}
|
|
naked_newline = false;
|
|
}
|
|
}
|
|
|