json-schema-validator/src/json-patch.hpp
Sylvain Joubert 79535fe0b6
Fix performance regression from logical combination patch discard (#288)
A recent fix to discard patch from invalid logical combination
introduced a lot (for large json instance and/or schema) of copies to
make a patch backup. On some case, this introduced up to a 100x slower
validation time.

Resizing the patch object to its previous size avoid unnecessary
temporary allocations from the backup object.
2023-09-22 16:03:55 +02:00

42 lines
868 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(const json::json_pointer &, json value);
json_patch &replace(const json::json_pointer &, json value);
json_patch &remove(const json::json_pointer &);
json &get_json() { return j_; }
const json &get_json() const { return j_; }
operator json() const { return j_; }
private:
json j_ = nlohmann::json::array();
static void validateJsonPatch(json const &patch);
};
} // namespace nlohmann