CampAPI Config API
CampAPI::Config is a config API that stores data as nlohmann::json in memory, and can then be loaded and saved to several file formats.
Supported formats:
- JSON
- YAML (
.yml/.yaml) - INI (
.ini)
Dependencies
Required:
nlohmann/json
Optional:
yaml-cppfor YAML support
If yaml-cpp is not available, YAML files are still recognized in the API, but the YAML load/save functions will throw an error when used.
Provided Files
mcapi/campapi/api/Config.hpp
Namespace
All classes and enums are in the following namespace:
CampAPIMain Class
ConfigFormat
Used to select the file format:
CampAPI::ConfigFormat::Auto
CampAPI::ConfigFormat::Json
CampAPI::ConfigFormat::Yaml
CampAPI::ConfigFormat::IniConfigError
Exceptions for configuration errors, for example:
- file cannot be opened
- format not recognized
- key not found
- YAML support not installed
Config
The main class for reading, writing, and accessing configuration.
Example of use
Load files
#include "Config.hpp"
CampAPI::Config cfg = CampAPI::Config::loadFromFile("config.json");Read value
int port = cfg.get<int>("server.port", 19132);
std::string host = cfg.get<std::string>("server.host", "0.0.0.0");
bool debug = cfg.get<bool>("debug", false);Must have
std::string token = cfg.require<std::string>("auth.token");If the key does not exist, require() will throw CampAPI::ConfigError.
Write value
cfg.set("server.port", 25565);
cfg.set("server.host", "127.0.0.1");
cfg.set("debug", true);Delete value
cfg.remove("debug");Merge JSON patches
cfg.merge({
{"server", {
{"port", 3000}
}}
});Save file
cfg.saveToFile("config.yml");JSON Example
{
"server": {
"host": "0.0.0.0",
"port": 19132
},
"debug": true
}YAML Example
server:
host: 0.0.0.0
port: 19132
debug: trueINI Example
[server]
host="0.0.0.0"
port=19132
debug=trueHow paths work
This API uses paths with periods:
server.portserver.hostdatabase.user
Example:
cfg.set("database.user", "root");
cfg.set("database.pass", "secret");Format Notes
JSON
Safest for complex structures, large arrays, and nested objects.
YAML
Suitable for configs that remain neat and human-readable.
INI
Suitable for simple configs. Nested sections are stored via nested section names.
Important Notes
- All data is still processed as
nlohmann::json. - YAML requires
yaml-cpp. - INI is supported here with a simple subset, suitable for general configs.
- The root INI must be an object when saved.