Skip to content

Commit 94a607f

Browse files
committed
[simplestreams] Use nlohmann::json
1 parent 5807a73 commit 94a607f

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ find_package(gRPC CONFIG REQUIRED)
186186
find_package(fmt CONFIG REQUIRED)
187187
# targets: fmt::fmt, fmt::fmt-header-only
188188

189+
find_package(nlohmann_json CONFIG REQUIRED)
190+
189191
# scope_guard config
190192
find_path(SCOPE_GUARD_INCLUDE_DIRS "scope_guard.hpp")
191193
add_library(scope_guard INTERFACE)

src/simplestreams/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ target_link_libraries(simplestreams
2323
settings
2424
utils
2525
Qt6::Core
26-
Qt6::Network)
26+
Qt6::Network
27+
nlohmann_json::nlohmann_json)

src/simplestreams/simple_streams_index.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,23 @@
2020

2121
#include <multipass/simple_streams_index.h>
2222

23-
#include <QJsonDocument>
24-
#include <QJsonObject>
23+
#include <nlohmann/json.hpp>
2524

2625
#include <stdexcept>
2726

27+
using json = nlohmann::json;
2828
namespace mp = multipass;
2929

3030
namespace
3131
{
32-
QJsonObject parse_index(const QByteArray& json)
32+
json parse_index(std::string_view json)
3333
{
34-
QJsonParseError parse_error;
35-
auto doc = QJsonDocument::fromJson(json, &parse_error);
36-
if (doc.isNull())
37-
throw std::runtime_error(parse_error.errorString().toStdString());
38-
39-
if (!doc.isObject())
34+
auto doc = json::parse(json);
35+
if (!doc.is_object())
4036
throw std::runtime_error("invalid index object");
4137

42-
auto index = doc.object()["index"].toObject();
43-
if (index.isEmpty())
38+
auto index = doc.at("index");
39+
if (index.empty())
4440
throw std::runtime_error("No index found");
4541

4642
return index;
@@ -49,16 +45,15 @@ QJsonObject parse_index(const QByteArray& json)
4945

5046
mp::SimpleStreamsIndex mp::SimpleStreamsIndex::fromJson(const QByteArray& json)
5147
{
52-
auto index = parse_index(json);
48+
auto index = parse_index(json.toStdString());
5349

54-
for (QJsonValueRef value : index)
50+
for (auto&& [_, value] : index.items())
5551
{
56-
auto entry = value.toObject();
57-
if (entry["datatype"] == "image-downloads")
52+
if (value.at("datatype") == "image-downloads")
5853
{
59-
auto path_entry = entry["path"];
60-
auto date_entry = entry["updated"];
61-
return {path_entry.toString(), date_entry.toString()};
54+
auto path_entry = value.at("path").get<std::string>();
55+
auto date_entry = value.at("updated").get<std::string>();
56+
return {QString::fromStdString(path_entry), QString::fromStdString(date_entry)};
6257
}
6358
}
6459

vcpkg.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"fmt",
66
"scope-guard",
77
"yaml-cpp",
8-
"neargye-semver"
8+
"neargye-semver",
9+
"nlohmann-json"
910
],
1011
"overrides": [
1112
{

0 commit comments

Comments
 (0)