Skip to content

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-cpp for 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:

cpp
CampAPI

Main Class

ConfigFormat

Used to select the file format:

cpp
CampAPI::ConfigFormat::Auto
CampAPI::ConfigFormat::Json
CampAPI::ConfigFormat::Yaml
CampAPI::ConfigFormat::Ini

ConfigError

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

cpp
#include "Config.hpp"

CampAPI::Config cfg = CampAPI::Config::loadFromFile("config.json");

Read value

cpp
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

cpp
std::string token = cfg.require<std::string>("auth.token");

If the key does not exist, require() will throw CampAPI::ConfigError.

Write value

cpp
cfg.set("server.port", 25565);
cfg.set("server.host", "127.0.0.1");
cfg.set("debug", true);

Delete value

cpp
cfg.remove("debug");

Merge JSON patches

cpp
cfg.merge({ 
{"server", { 
{"port", 3000} 
}}
});

Save file

cpp
cfg.saveToFile("config.yml");

JSON Example

json
{
"server": {
"host": "0.0.0.0",
"port": 19132
},
"debug": true
}

YAML Example

yml
server:
host: 0.0.0.0
port: 19132
debug: true

INI Example

ini
[server]
host="0.0.0.0"
port=19132

debug=true

How paths work

This API uses paths with periods:

  • server.port
  • server.host
  • database.user

Example:

cpp
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.