2020-05-20 10:27:06 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
2021-01-02 15:48:47 +00:00
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
2020-05-20 10:27:06 +00:00
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
#include "preprocessor.h"
|
2021-10-30 12:56:14 +00:00
|
|
|
//# {{
|
2020-06-23 10:04:05 +00:00
|
|
|
#if !TOML_IMPLEMENTATION
|
2021-10-24 22:04:23 +00:00
|
|
|
#error This is an implementation-only header.
|
2020-05-20 10:27:06 +00:00
|
|
|
#endif
|
|
|
|
//# }}
|
2021-11-02 20:13:09 +00:00
|
|
|
#if TOML_ENABLE_FORMATTERS
|
2020-05-20 10:27:06 +00:00
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
#include "json_formatter.h"
|
2021-10-24 22:04:23 +00:00
|
|
|
#include "print_to_stream.h"
|
|
|
|
#include "table.h"
|
|
|
|
#include "array.h"
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_start.h"
|
2020-06-23 10:04:05 +00:00
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_START
|
2020-05-20 10:27:06 +00:00
|
|
|
{
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
void json_formatter::print(const toml::table& tbl)
|
2020-05-20 10:27:06 +00:00
|
|
|
{
|
|
|
|
if (tbl.empty())
|
|
|
|
{
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted("{}"sv);
|
2021-11-02 20:13:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-20 10:27:06 +00:00
|
|
|
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted('{');
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2021-11-03 16:19:48 +00:00
|
|
|
if (indent_sub_tables())
|
|
|
|
increase_indent();
|
2021-11-02 20:13:09 +00:00
|
|
|
bool first = false;
|
|
|
|
for (auto&& [k, v] : tbl)
|
|
|
|
{
|
|
|
|
if (first)
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted(',');
|
2021-11-02 20:13:09 +00:00
|
|
|
first = true;
|
2021-11-03 16:19:48 +00:00
|
|
|
print_newline(true);
|
|
|
|
print_indent();
|
2021-11-02 20:13:09 +00:00
|
|
|
|
2021-11-10 09:17:15 +00:00
|
|
|
print_string(k.str(), false);
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted(" : "sv);
|
2021-11-02 20:13:09 +00:00
|
|
|
|
|
|
|
const auto type = v.type();
|
|
|
|
TOML_ASSUME(type != node_type::none);
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
|
|
|
|
case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
|
2021-11-03 16:19:48 +00:00
|
|
|
default: print_value(v, type);
|
2021-11-02 20:13:09 +00:00
|
|
|
}
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
2021-11-03 16:19:48 +00:00
|
|
|
if (indent_sub_tables())
|
|
|
|
decrease_indent();
|
|
|
|
print_newline(true);
|
|
|
|
print_indent();
|
2021-11-02 20:13:09 +00:00
|
|
|
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted('}');
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
void json_formatter::print(const toml::array& arr)
|
|
|
|
{
|
|
|
|
if (arr.empty())
|
|
|
|
{
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted("[]"sv);
|
2021-11-02 20:13:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-20 10:27:06 +00:00
|
|
|
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted('[');
|
|
|
|
if (indent_array_elements())
|
|
|
|
increase_indent();
|
2021-11-02 20:13:09 +00:00
|
|
|
for (size_t i = 0; i < arr.size(); i++)
|
|
|
|
{
|
|
|
|
if (i > 0u)
|
2021-11-03 16:19:48 +00:00
|
|
|
print_unformatted(',');
|
|
|
|
print_newline(true);
|
|
|
|
print_indent();
|
2021-11-02 20:13:09 +00:00
|
|
|
|
|
|
|
auto& v = arr[i];
|
|
|
|
const auto type = v.type();
|
|
|
|
TOML_ASSUME(type != node_type::none);
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
|
|
|
|
case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
|
2021-11-03 16:19:48 +00:00
|
|
|
default: print_value(v, type);
|
2021-11-02 20:13:09 +00:00
|
|
|
}
|
2020-05-20 10:27:06 +00:00
|
|
|
}
|
2021-11-03 16:19:48 +00:00
|
|
|
if (indent_array_elements())
|
|
|
|
decrease_indent();
|
|
|
|
print_newline(true);
|
|
|
|
print_indent();
|
|
|
|
print_unformatted(']');
|
2020-05-20 10:27:06 +00:00
|
|
|
}
|
2021-10-24 22:04:23 +00:00
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
void json_formatter::print()
|
|
|
|
{
|
2021-11-03 16:19:48 +00:00
|
|
|
if (dump_failed_parse_result())
|
2021-10-24 22:04:23 +00:00
|
|
|
return;
|
|
|
|
|
2021-11-03 16:19:48 +00:00
|
|
|
switch (auto source_type = source().type())
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
2021-11-03 16:19:48 +00:00
|
|
|
case node_type::table: print(*reinterpret_cast<const table*>(&source())); break;
|
|
|
|
case node_type::array: print(*reinterpret_cast<const array*>(&source())); break;
|
|
|
|
default: print_value(source(), source_type);
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 10:27:06 +00:00
|
|
|
}
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_NAMESPACE_END;
|
2020-05-20 10:27:06 +00:00
|
|
|
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_end.h"
|
2021-11-02 20:13:09 +00:00
|
|
|
#endif // TOML_ENABLE_FORMATTERS
|