toml++ works whether you have exceptions enabled or not. For the most part the usage is the same, the main difference being how parsing errors are reported to the caller. When exceptions are enabled a successful call to a parsing function simply returns a toml::table, whereas a failed call sees a toml::parse_error thrown directly from the site of the error:
#include <iostream>
+- TOML v1.0.0-rc.1, plus optional support for some unreleased TOML features
- C++17 (plus some C++20 features where available, e.g. experimental support for char8_t strings)
- Proper UTF-8 handling (incl. BOM)
- Works with or without exceptions
- Doesn't require RTTI
- First-class support for serializing to JSON
- Tested on Clang, GCC and MSVC (VS2019)
- Tested on x64, x86 and ARM
It's header-only library so really all you have to do is clone the repository from GitHub and set your include paths. There's some minor configuration you can do to customize some basic library functionality, but that's totally optional. See the README for more info.
You're looking at it! Browse the docs using the links at the top of the page. You can search from anywhere by pressing the TAB key.
toml++ works whether you have exceptions enabled or not. For the most part the usage is the same, the main difference being how parsing errors are reported to the caller. When exceptions are enabled a successful call to a parsing function simply returns a toml::table, whereas a failed call sees a toml::parse_error thrown directly from the site of the error:
#include <iostream>
#include <fstream> //required for parse_file()
#include <toml++/toml.h>
using namespace std::string_view_literals;
@@ -98,10 +97,9 @@ C++
catch (const toml::parse_error& err)
{
std::cerr
- << "Error parsing file '"sv << *err.source().path
- << "':\n"sv << err.description()
- << "\n ("sv << err.source().begin << ")"sv
- << std::endl;
+ << "Error parsing file '" << *err.source().path
+ << "':\n" << err.description()
+ << "\n (" << err.source().begin << ")\n";
return 1;
}
@@ -118,10 +116,9 @@ C++
if (!tbl)
{
std::cerr
- << "Error parsing file '"sv << *tbl.error().source().path
- << "':\n"sv << tbl.error().description()
- << "\n ("sv << tbl.error().source().begin << ")"sv
- << std::endl;
+ << "Error parsing file '" << *tbl.error().source().path
+ << "':\n" << tbl.error().description()
+ << "\n (" << tbl.error().source().begin << ")\n";
return 1;
}
@@ -133,11 +130,11 @@ C++
}
catch (const toml::parse_error & err)
{
- std::cerr << "Parsing failed:\n"sv << err << std::endl;
+ std::cerr << "Parsing failed:\n" << err << "\n";
return 1;
}Parsing failed:
Encountered unexpected character while parsing boolean; expected 'true', saw 'trU'
- (error occurred at line 1, column 13)
If the default error formatting is not be suitable for your use-case you can access the error's toml::source_region and description directly from the error object (as in the examples above).
Strings and std::istreams can be read directly using toml::parse():
#include <iostream>
+ (error occurred at line 1, column 13)
If the default error formatting is not be suitable for your use-case you can access the error's toml::source_region and description directly from the error object (as in the examples above).
Strings and std::istreams can be read directly using toml::parse():
#include <iostream>
#include <sstream>
#include <toml++/toml.h>
using namespace std::string_view_literals;
@@ -147,7 +144,7 @@ C++
static constexpr auto source = R"(
[library]
name = "toml++"
- authors = ["Mark Gillard <mark@notarealwebsite.com>"]
+ authors = ["Mark Gillard <mark.gillard@outlook.com.au>"]
[dependencies]
cpp = 17
@@ -156,14 +153,14 @@ C++
// parse directly from a string view:
{
auto tbl = toml::parse(source);
- std::cout << tbl << std::endl;
+ std::cout << tbl << "\n";
}
// parse from a string stream:
{
std::stringstream ss{ std::string{ source } };
auto tbl = toml::parse(ss);
- std::cout << tbl << std::endl;
+ std::cout << tbl << "\n";
}
return 0;
@@ -195,9 +192,9 @@ C++
// get a view of the element 'numbers'
auto numbers = tbl["numbers"];
- std::cout << "table has 'numbers': "sv << !!numbers << std::endl;
- std::cout << "numbers is a: "sv << numbers.type() << std::endl;
- std::cout << "numbers: "sv << numbers << std::endl;
+ std::cout << "table has 'numbers': " << !!numbers << "\n";
+ std::cout << "numbers is a: " << numbers.type() << "\n";
+ std::cout << "numbers: " << numbers << "\n";
// get the underlying array object to do some more advanced stuff
if (auto arr = numbers.as_array())
@@ -217,15 +214,15 @@ C++
// arrays are very similar to std::vector
arr->push_back(7);
arr->emplace_back<toml::array>(8, 9);
- std::cout << "numbers: "sv << numbers << std::endl;
+ std::cout << "numbers: " << numbers << "\n";
}
// node-views can be chained to quickly query deeper
- std::cout << "cats: "sv << tbl["animals"]["cats"] << std::endl;
- std::cout << "fish[1]: "sv << tbl["animals"]["fish"][1] << std::endl;
+ std::cout << "cats: " << tbl["animals"]["cats"] << "\n";
+ std::cout << "fish[1]: " << tbl["animals"]["fish"][1] << "\n";
// ...even if the element doesn't exist
- std::cout << "dinosaurs: "sv << tbl["animals"]["dinosaurs"] << std::endl; //no dinosaurs :(
+ std::cout << "dinosaurs: " << tbl["animals"]["dinosaurs"] << "\n"; //no dinosaurs :(
return 0;
}table has 'numbers': true
@@ -242,7 +239,7 @@ C++
auto tbl = toml::table{{
{ "lib", "toml++" },
{ "cpp", toml::array{ 17, 20, "and beyond" } },
- { "toml", toml::array{ "0.5.0", "and beyond" } },
+ { "toml", toml::array{ "1.0.0", "and beyond" } },
{ "repo", "https://github.com/marzer/tomlplusplus/" },
{ "author", toml::table{{
{ "name", "Mark Gillard" },
@@ -253,18 +250,19 @@ C++
}};
// serializing as TOML is just writing it to a stream
- std::cout << "###### TOML ######"sv << std::endl;
- std::cout << tbl << std::endl << std::endl;
+ std::cout << "###### TOML ######" << "\n\n";
+ std::cout << tbl << "\n\n";
// serializing as JSON is _also_ just writing it to a stream, but via a json_formatter:
- std::cout << "###### JSON ######"sv << std::endl;
- std::cout << toml::json_formatter{ tbl } << std::endl;
+ std::cout << "###### JSON ######" << "\n\n";
+ std::cout << toml::json_formatter{ tbl } << "\n\n";
return 0;
}###### TOML ######
+
cpp = [17, 20, "and beyond"]
lib = "toml++"
repo = "https://github.com/marzer/tomlplusplus/"
-toml = ["0.5.0", "and beyond"]
+toml = ["1.0.0", "and beyond"]
[author]
github = "https://github.com/marzer"
@@ -272,6 +270,7 @@ C++
twitter = "https://twitter.com/marzer8789"
###### JSON ######
+
{
"author" : {
"github" : "https://github.com/marzer",
@@ -286,7 +285,7 @@ C++
"lib" : "toml++",
"repo" : "https://github.com/marzer/tomlplusplus/",
"toml" : [
- "0.5.0",
+ "1.0.0",
"and beyond"
]
}Because toml++ is a header-only library of nontrivial size you might find that compilation times noticeably increase after you add it to your project, especially if you add the library's header somewhere that's visible from a large number of translation units. You can counter this by disabling 'all inline' mode and explicitly controlling where the library's implementation is compiled.
Step 1: Set TOML_ALL_INLINE to 0 before including toml++
This must be the same everywhere, so either set it as a global #define in your build system, or do it manually before including toml++ in some global header that's used everywhere in your project:
// global_header_that_includes_toml++.h
@@ -295,7 +294,7 @@ C++
#include <toml.hpp>
Step 2: Define TOML_IMPLEMENTATION before including toml++ in one specific translation unit
// some_code_file.cpp
#define TOML_IMPLEMENTATION
-#include "global_header_that_includes_toml++.h"
Your project may already have a specific header/source file pair configured as a 'precompiled header'; if so, that's the ideal place to put this machinery.
Contributions are very welcome! Either by reporting issues or submitting pull requests. If you wish to submit a pull request, please see CONTRIBUTING for all the details you need to get going.
toml++ is licensed under the terms of the MIT license - see LICENSE.
If you're using the single-header version of the library you don't need to explicitly distribute the license file; it is embedded in the preamble at the top of the header.
For bug reports and feature requests please consider using the GitHub issues system. For anything else though you're welcome to reach out via other means. In order of likely response speed:
+#include "global_header_that_includes_toml++.h"