The Art of C++ / Config is a C++ header-only library that reads config files in a format based on JSON and JAXN and produces a single JSON Value as result.
- Changelog
- Version 1.x (requires C++17)
For questions and suggestions regarding The Art of C++ / Config, success or failure stories, and any other kind of feedback, please feel free to open a discussion, an issue or a pull request on GitHub or contact the authors at taocpp(at)icemx.net
.
- JAXN syntax with extensions (backward compatible with JSON).
- JAXN data model (JSON extended with binary data and non-finites).
- Meta data; all sub-values are annotated with filename and position.
- Copy via reference, overwrite and delete anything in the data structure.
- Include other config files into any position in the data structure.
- And more, the complete list of features is documented on the Writing Config Files page.
- The function
tao::config::from_file()
is all you need to get going.
Every JSON file with a top-level object can be used as config file.
{
"ip": "127.0.0.2",
"port": 27960,
"maps": [ "ztn", "dm13", "t9" ]
}
This small example can be rendered differently using some of the additional syntactic possibilities of the config file format.
#!/usr/local/bin/q3s
ip = "127.0.0.2"
port = 27960
maps = [ "ztn" "dm13" "t9" ] // Add dm6 or t4?
Semantic features like deleting and referencing values, or including files and reading environment variables, usually only make sense with larger, non-trivial real-world examples.
These features can be used to manage situations that go beyond single deployments with a single config, for example providing the tools to manage configuration templates that are adapted to different environments.
Parsing a config file is as simple as calling the appropriate from_file()
function with the filename.
#include <tao/config.hpp>
const tao::config::value config = tao::config::from_file( "foo.cfg" );
The resulting value is a JSON Value from taoJSON with a custom taoCONFIG traits class that annotates every sub-value with the filename and position it was parsed from. It can be inspected -- and manipulated -- using all the facilities of the taoJSON library.
The Art of C++ / Config is certified Open Source software. It may be used for any purpose, including commercial purposes, at absolutely no cost. It is distributed under the terms of the MIT license reproduced here.
Copyright (c) 2018-2024 Dr. Colin Hirsch and Daniel Frey
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.