summaryrefslogtreecommitdiff
path: root/tomlplusplus/toml-test/tt_decoder.cpp
diff options
context:
space:
mode:
authorMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 18:44:05 +0300
committerMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 18:44:05 +0300
commit0b24459ac12b6cf9fd5a401d647796ca254a8fa8 (patch)
treef2fd66e2476976a51e2a51330fd95dc6e87b24c1 /tomlplusplus/toml-test/tt_decoder.cpp
parentb85e90fc3480da0e6a48da73201a0b22488cc650 (diff)
parent1c8b7466e4946fcc3bf20484c0e1d001202cca5a (diff)
downloadProject-Tick-0b24459ac12b6cf9fd5a401d647796ca254a8fa8.tar.gz
Project-Tick-0b24459ac12b6cf9fd5a401d647796ca254a8fa8.zip
Add 'tomlplusplus/' from commit '1c8b7466e4946fcc3bf20484c0e1d001202cca5a'
git-subtree-dir: tomlplusplus git-subtree-mainline: b85e90fc3480da0e6a48da73201a0b22488cc650 git-subtree-split: 1c8b7466e4946fcc3bf20484c0e1d001202cca5a
Diffstat (limited to 'tomlplusplus/toml-test/tt_decoder.cpp')
-rw-r--r--tomlplusplus/toml-test/tt_decoder.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/tomlplusplus/toml-test/tt_decoder.cpp b/tomlplusplus/toml-test/tt_decoder.cpp
new file mode 100644
index 0000000000..0426ea994b
--- /dev/null
+++ b/tomlplusplus/toml-test/tt_decoder.cpp
@@ -0,0 +1,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.hpp"
+
+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;
+}