JSON parse errors with byte offsets
C++
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>
nlohmann::json parseJson(const std::string& text) {
try {
return nlohmann::json::parse(text);
} catch (const nlohmann::json::parse_error& e) {
throw std::runtime_error("invalid json at byte " + std::to_string(e.byte) + ": " + e.what());
}
}
nlohmann::json expectObject(const nlohmann::json& j) {
if (!j.is_object()) throw std::runtime_error("expected object, got " + std::string(j.type_name()));
return j;
}