errors 0.6.3
Loading...
Searching...
No Matches
json.hpp
1#pragma once
2
3#include <optional>
4
5#include "errors/error_ptr.hpp"
6#include "errors/source_location.hpp"
7#include "nlohmann/json.hpp"
8
9#if defined(NLOHMANN_JSON_NAMESPACE_BEGIN) && \
10 defined(NLOHMANN_JSON_NAMESPACE_END)
11NLOHMANN_JSON_NAMESPACE_BEGIN
12#else
13namespace nlohmann
14{
15#endif
16
17template <>
18struct adl_serializer< ::errors::source_location> {
19 static void to_json(::nlohmann::json &j,
20 const ::errors::source_location &loc)
21 {
22 j["file_name"] = loc.file_name();
23 j["function_name"] = loc.function_name();
24 j["line"] = loc.line();
25 j["column"] = loc.column();
26 }
27};
28
29template <>
30struct adl_serializer< ::errors::error_ptr> {
31 static void to_json(::nlohmann::json &j, const ::errors::error_ptr &err)
32 {
33 if (!err) {
34 j = {};
35 assert(j.is_null());
36 return;
37 }
38
39 if (err->location()) {
40 j["location"] = err->location().value();
41 }
42
43 j["message"] = err->what();
44
45 j["caused_by"] = err->cause();
46 }
47};
48
49#if defined(NLOHMANN_JSON_NAMESPACE_BEGIN) && \
50 defined(NLOHMANN_JSON_NAMESPACE_END)
51NLOHMANN_JSON_NAMESPACE_END
52#else
53}
54#endif
Smart pointer to errors::error with some utility member functions.
Definition error_ptr.hpp:13
The source location.
Definition source_location.hpp:46
constexpr const char * file_name() const noexcept
Returns the file name, or "unsupported" if it is not supported by the compiler.
Definition source_location.hpp:85