NeBuild dev
Loading...
Searching...
No Matches
json_formatter.inl
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// # {{
9#if !TOML_IMPLEMENTATION
10#error This is an implementation-only header.
11#endif
12// # }}
13#if TOML_ENABLE_FORMATTERS
14
15#include "array.hpp"
16#include "header_start.hpp"
17#include "json_formatter.hpp"
18#include "print_to_stream.hpp"
19#include "table.hpp"
20
23 void json_formatter::print(const toml::table& tbl) {
24 if (tbl.empty()) {
25 print_unformatted("{}"sv);
26 return;
27 }
28
29 print_unformatted('{');
30
31 if (indent_sub_tables()) increase_indent();
32 bool first = false;
33 for (auto&& [k, v] : tbl) {
34 if (first) print_unformatted(',');
35 first = true;
36 print_newline(true);
37 print_indent();
38
39 print_string(k.str(), false);
40 if (terse_kvps())
41 print_unformatted(":"sv);
42 else
43 print_unformatted(" : "sv);
44
45 const auto type = v.type();
46 TOML_ASSUME(type != node_type::none);
47 switch (type) {
48 case node_type::table:
49 print(*reinterpret_cast<const table*>(&v));
50 break;
51 case node_type::array:
52 print(*reinterpret_cast<const array*>(&v));
53 break;
54 default:
55 print_value(v, type);
56 }
57 }
58 if (indent_sub_tables()) decrease_indent();
59 print_newline(true);
60 print_indent();
61
62 print_unformatted('}');
63 }
64
66 void json_formatter::print(const toml::array& arr) {
67 if (arr.empty()) {
68 print_unformatted("[]"sv);
69 return;
70 }
71
72 print_unformatted('[');
73 if (indent_array_elements()) increase_indent();
74 for (size_t i = 0; i < arr.size(); i++) {
75 if (i > 0u) print_unformatted(',');
76 print_newline(true);
77 print_indent();
78
79 auto& v = arr[i];
80 const auto type = v.type();
81 TOML_ASSUME(type != node_type::none);
82 switch (type) {
83 case node_type::table:
84 print(*reinterpret_cast<const table*>(&v));
85 break;
86 case node_type::array:
87 print(*reinterpret_cast<const array*>(&v));
88 break;
89 default:
90 print_value(v, type);
91 }
92 }
93 if (indent_array_elements()) decrease_indent();
94 print_newline(true);
95 print_indent();
96 print_unformatted(']');
97 }
98
100 void json_formatter::print() {
101 if (dump_failed_parse_result()) return;
102
103 switch (auto source_type = source().type()) {
104 case node_type::table:
105 print(*reinterpret_cast<const table*>(&source()));
106 break;
107 case node_type::array:
108 print(*reinterpret_cast<const array*>(&source()));
109 break;
110 default:
111 print_value(source(), source_type);
112 }
113 }
114}
116
117#include "header_end.hpp"
118#endif // TOML_ENABLE_FORMATTERS
A TOML array.
Definition array.hpp:285
A TOML table.
Definition table.hpp:220
enum TOML_CLOSED_FLAGS_ENUM indent_sub_tables
Apply indentation to tables nested within other tables/arrays.
Definition forward_declarations.hpp:330
enum TOML_CLOSED_FLAGS_ENUM indent_array_elements
Apply indentation to array elements when the array is forced to wrap over multiple lines.
Definition forward_declarations.hpp:333
TOML_NAMESPACE_START
Definition json_formatter.inl:21
#define TOML_EXTERNAL_LINKAGE
Definition preprocessor.hpp:1339
#define TOML_NAMESPACE_END
Definition preprocessor.hpp:1320
#define TOML_ASSUME(expr)
Definition preprocessor.hpp:506