NeBuild dev
Loading...
Searching...
No Matches
json_formatter.hpp
Go to the documentation of this file.
1//# This file is a part of toml++ and is subject to the the terms of the MIT license.
2//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7#include "preprocessor.hpp"
8#if TOML_ENABLE_FORMATTERS
9
10#include "formatter.hpp"
11#include "header_start.hpp"
12
14{
46 class TOML_EXPORTED_CLASS json_formatter : impl::formatter
47 {
48 private:
50
51 using base = impl::formatter;
52
54 void print(const toml::table&);
55
57 void print(const toml::array&);
58
60 void print();
61
62 static constexpr impl::formatter_constants constants = {
63 format_flags::quote_dates_and_times, // mandatory
64 format_flags::allow_literal_strings | format_flags::allow_multi_line_strings, // ignored
65 "Infinity"sv,
66 "-Infinity"sv,
67 "NaN"sv,
68 "true"sv,
69 "false"sv
70 };
71
73
74 public:
76 static constexpr format_flags default_flags = constants.mandatory_flags //
77 | format_flags::quote_infinities_and_nans //
78 | format_flags::allow_unicode_strings //
79 | format_flags::indentation;
80
86 explicit json_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
87 : base{ &source, nullptr, constants, { flags, " "sv } }
88 {}
89
90#if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
91
117 explicit json_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
118 : base{ nullptr, &result, constants, { flags, " "sv } }
119 {}
120
121#endif
122
124 friend std::ostream& operator<<(std::ostream& lhs, json_formatter& rhs)
125 {
126 rhs.attach(lhs);
127 rhs.print();
128 rhs.detach();
129 return lhs;
130 }
131
133 friend std::ostream& operator<<(std::ostream& lhs, json_formatter&& rhs)
134 {
135 return lhs << rhs; // as lvalue
136 }
137 };
138}
140
141#include "header_end.hpp"
142#endif // TOML_ENABLE_FORMATTERS
enum TOML_CLOSED_FLAGS_ENUM format_flags
Format flags for modifying how TOML data is printed to streams.
Definition forward_declarations.hpp:297
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &lhs, node_type rhs)
Pretty-prints the value of a node_type to a stream.
Definition forward_declarations.hpp:256
#define TOML_EXPORTED_CLASS
An 'export' annotation to add to classes. \detail Not defined by default.
Definition preprocessor.hpp:979
#define TOML_EXPORTED_MEMBER_FUNCTION
An 'export' annotation to add to non-static class member functions. \detail Not defined by default.
Definition preprocessor.hpp:982
TOML_NAMESPACE_START
Definition json_formatter.hpp:14
TOML_NAMESPACE_END
Definition json_formatter.hpp:139
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446