File: tt_decoder.cpp

package info (click to toggle)
tomlplusplus 3.3.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,184 kB
  • sloc: cpp: 35,145; ansic: 2,220; python: 983; makefile: 25; sh: 17
file content (113 lines) | stat: -rw-r--r-- 2,540 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//# 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.
// SPDX-License-Identifier: MIT

#include "tt.h"

using nlohmann::json;
using namespace std::string_view_literals;

TOML_NAMESPACE_START
{
	static void to_json(json & j, const value<std::string>& val)
	{
		j["type"]  = "string";
		j["value"] = *val;
	}

	template <typename T>
	static void to_json_via_stream(json & j, const value<T>& val)
	{
		static constexpr auto flags = toml_formatter::default_flags
									& ~(format_flags::allow_binary_integers		   //
										| format_flags::allow_hexadecimal_integers //
										| format_flags::allow_octal_integers);

		std::ostringstream ss;
		ss << toml_formatter{ val, flags };
		j["value"] = ss.str();
	}

	static void to_json(json & j, const value<int64_t>& val)
	{
		j["type"] = "integer";
		to_json_via_stream(j, val);
	}

	static void to_json(json & j, const value<double>& val)
	{
		j["type"] = "float";
		to_json_via_stream(j, val);
	}

	static void to_json(json & j, const value<bool>& val)
	{
		j["type"] = "bool";
		to_json_via_stream(j, val);
	}

	static void to_json(json & j, const value<date>& val)
	{
		j["type"] = "date-local";
		to_json_via_stream(j, val);
	}

	static void to_json(json & j, const value<time>& val)
	{
		j["type"] = "time-local";
		to_json_via_stream(j, val);
	}

	static void to_json(json & j, const value<date_time>& val)
	{
		j["type"] = val->is_local() ? "datetime-local" : "datetime";
		to_json_via_stream(j, val);
	}

	static void to_json(json&, const array&);

	static void to_json(json & j, const table& tbl)
	{
		j = json::object();
		for (auto& [k_, v_] : tbl)
			v_.visit([&, k = &k_](auto& v) { j[std::string{ k->str() }] = v; });
	}

	static void to_json(json & j, const array& arr)
	{
		j = json::array();
		for (auto& v_ : arr)
			v_.visit([&](auto& v) { j.push_back(v); });
	}
}
TOML_NAMESPACE_END;

int main()
{
	try
	{
		const std::string str(std::istreambuf_iterator<char>{ std::cin }, std::istreambuf_iterator<char>{});

		json j = toml::parse(str, "stdin"sv);

		std::cout << j << "\n";
	}
	catch (const toml::parse_error& err)
	{
		std::cerr << "\n\n" << err << "\n";
		return 1;
	}
	catch (const std::exception& exc)
	{
		std::cerr << "\n\n" << exc.what() << "\n";
		return 1;
	}
	catch (...)
	{
		std::cerr << "\n\nAn unspecified error occurred.\n";
		return 1;
	}

	return 0;
}