-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathJSON.cpp
More file actions
25 lines (22 loc) · 811 Bytes
/
Copy pathJSON.cpp
File metadata and controls
25 lines (22 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "JSON.h"
#include <format>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <stdexcept>
namespace Abyss::Common {
nlohmann::json parseJson(std::string_view json) {
rapidjson::Document d;
d.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(json.data(), json.length());
if (d.HasParseError()) {
throw std::runtime_error(
std::format("JSON parse error ({}) at offset {}", rapidjson::GetParseError_En(d.GetParseError()), d.GetErrorOffset()));
}
rapidjson::StringBuffer sb;
rapidjson::Writer writer(sb);
d.Accept(writer);
return nlohmann::json::parse(sb.GetString());
}
} // namespace Abyss::Common