Uses json patch format for defaults: json patch (RFC 6902) makes it clearer in case of array contents. It is supported by nlohmann::json and can be applied with `patch` to fill up the json with defaults.
39 lines
733 B
C++
39 lines
733 B
C++
#pragma once
|
|
|
|
#include <nlohmann/json.hpp>
|
|
#include <string>
|
|
|
|
namespace nlohmann
|
|
{
|
|
class JsonPatchFormatException : public std::exception
|
|
{
|
|
public:
|
|
explicit JsonPatchFormatException(std::string msg)
|
|
: ex_{std::move(msg)} {}
|
|
|
|
inline const char *what() const noexcept override final { return ex_.c_str(); }
|
|
|
|
private:
|
|
std::string ex_;
|
|
};
|
|
|
|
class json_patch
|
|
{
|
|
public:
|
|
json_patch() = default;
|
|
json_patch(json &&patch);
|
|
json_patch(const json &patch);
|
|
|
|
json_patch &add(std::string path, json value);
|
|
json_patch &replace(std::string path, json value);
|
|
json_patch &remove(std::string path);
|
|
|
|
operator json() const { return j_; }
|
|
|
|
private:
|
|
json j_;
|
|
|
|
static void validateJsonPatch(json const &patch);
|
|
};
|
|
} // namespace nlohmann
|