Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit fc83e91

Browse files
committed
Remove nlohmann::json
1 parent 3dba0ce commit fc83e91

File tree

12 files changed

+56
-74
lines changed

12 files changed

+56
-74
lines changed

engine/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ find_package(jsoncpp CONFIG REQUIRED)
7373
find_package(Drogon CONFIG REQUIRED)
7474
find_package(yaml-cpp CONFIG REQUIRED)
7575
find_package(httplib CONFIG REQUIRED)
76-
find_package(nlohmann_json CONFIG REQUIRED)
7776
find_package(unofficial-minizip CONFIG REQUIRED)
7877
find_package(LibArchive REQUIRED)
7978
find_package(CURL REQUIRED)
@@ -149,7 +148,6 @@ add_executable(${TARGET_NAME} main.cc
149148
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
150149

151150
target_link_libraries(${TARGET_NAME} PRIVATE httplib::httplib)
152-
target_link_libraries(${TARGET_NAME} PRIVATE nlohmann_json::nlohmann_json)
153151
target_link_libraries(${TARGET_NAME} PRIVATE unofficial::minizip::minizip)
154152
target_link_libraries(${TARGET_NAME} PRIVATE LibArchive::LibArchive)
155153
target_link_libraries(${TARGET_NAME} PRIVATE CURL::libcurl)

engine/cli/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ add_compile_definitions(CORTEX_CONFIG_FILE_PATH="${CORTEX_CONFIG_FILE_PATH}")
6363
find_package(jsoncpp CONFIG REQUIRED)
6464
find_package(yaml-cpp CONFIG REQUIRED)
6565
find_package(httplib CONFIG REQUIRED)
66-
find_package(nlohmann_json CONFIG REQUIRED)
6766
find_package(CLI11 CONFIG REQUIRED)
6867
find_package(unofficial-minizip CONFIG REQUIRED)
6968
find_package(LibArchive REQUIRED)
@@ -83,7 +82,6 @@ add_executable(${TARGET_NAME} main.cc
8382
)
8483

8584
target_link_libraries(${TARGET_NAME} PRIVATE httplib::httplib)
86-
target_link_libraries(${TARGET_NAME} PRIVATE nlohmann_json::nlohmann_json)
8785
target_link_libraries(${TARGET_NAME} PRIVATE CLI11::CLI11)
8886
target_link_libraries(${TARGET_NAME} PRIVATE unofficial::minizip::minizip)
8987
target_link_libraries(${TARGET_NAME} PRIVATE LibArchive::LibArchive)
@@ -123,4 +121,4 @@ set_target_properties(${TARGET_NAME} PROPERTIES
123121
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}
124122
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
125123
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
126-
)
124+
)

engine/cli/commands/chat_completion_cmd.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ struct ChunkParser {
2929
is_done = true;
3030
} else {
3131
try {
32-
content = nlohmann::json::parse(s)["choices"][0]["delta"]["content"];
33-
} catch (const nlohmann::json::parse_error& e) {
32+
content =
33+
json_helper::ParseJsonString(s)["choices"][0]["delta"]["content"]
34+
.asString();
35+
} catch (const std::exception& e) {
3436
CTL_WRN("JSON parse error: " << e.what());
3537
}
3638
}

engine/cli/commands/cortex_upd_cmd.cc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "cortex_upd_cmd.h"
22
#include "httplib.h"
3-
#include "nlohmann/json.hpp"
43
#include "server_stop_cmd.h"
54
#include "utils/archive_utils.h"
65
#include "utils/file_manager_utils.h"
6+
#include "utils/json_helper.h"
77
#include "utils/logging_utils.h"
88
#include "utils/scope_exit.h"
99
#include "utils/system_info_utils.h"
@@ -144,26 +144,26 @@ std::optional<std::string> CheckNewUpdate(
144144
if (auto res = cli.Get(release_path)) {
145145
if (res->status == httplib::StatusCode::OK_200) {
146146
try {
147-
auto get_latest = [](const nlohmann::json& data) -> std::string {
147+
auto get_latest = [](const Json::Value& data) -> std::string {
148148
if (data.empty()) {
149149
return "";
150150
}
151151

152152
if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) {
153153
for (auto& d : data) {
154-
if (auto tag = d["tag_name"].get<std::string>();
154+
if (auto tag = d["tag_name"].asString();
155155
tag.find(kBetaComp) != std::string::npos) {
156156
return tag;
157157
}
158158
}
159-
return data[0]["tag_name"].get<std::string>();
159+
return data[0]["tag_name"].asString();
160160
} else {
161-
return data["tag_name"].get<std::string>();
161+
return data["tag_name"].asString();
162162
}
163163
return "";
164164
};
165165

166-
auto json_res = nlohmann::json::parse(res->body);
166+
auto json_res = json_helper::ParseJsonString(res->body);
167167
std::string latest_version = get_latest(json_res);
168168
if (latest_version.empty()) {
169169
CTL_WRN("Release not found!");
@@ -178,7 +178,7 @@ std::optional<std::string> CheckNewUpdate(
178178
if (current_version != latest_version) {
179179
return latest_version;
180180
}
181-
} catch (const nlohmann::json::parse_error& e) {
181+
} catch (const std::exception& e) {
182182
CTL_INF("JSON parse error: " << e.what());
183183
return std::nullopt;
184184
}
@@ -321,7 +321,7 @@ bool CortexUpdCmd::GetStable(const std::string& v) {
321321
if (auto res = cli.Get(release_path)) {
322322
if (res->status == httplib::StatusCode::OK_200) {
323323
try {
324-
auto json_data = nlohmann::json::parse(res->body);
324+
auto json_data = json_helper::ParseJsonString(res->body);
325325
if (json_data.empty()) {
326326
CLI_LOG("Version not found: " << v);
327327
return false;
@@ -333,7 +333,7 @@ bool CortexUpdCmd::GetStable(const std::string& v) {
333333
!downloaded_exe_path) {
334334
return false;
335335
}
336-
} catch (const nlohmann::json::parse_error& e) {
336+
} catch (const std::exception& e) {
337337
CLI_LOG_ERROR("JSON parse error: " << e.what());
338338
return false;
339339
}
@@ -377,12 +377,12 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
377377
if (auto res = cli.Get(release_path)) {
378378
if (res->status == httplib::StatusCode::OK_200) {
379379
try {
380-
auto json_res = nlohmann::json::parse(res->body);
380+
auto json_res = json_helper::ParseJsonString(res->body);
381381

382-
nlohmann::json json_data;
382+
Json::Value json_data;
383383
for (auto& jr : json_res) {
384384
// Get the latest beta or match version
385-
if (auto tag = jr["tag_name"].get<std::string>();
385+
if (auto tag = jr["tag_name"].asString();
386386
(v.empty() && tag.find(kBetaComp) != std::string::npos) ||
387387
(tag == v)) {
388388
json_data = jr;
@@ -401,7 +401,7 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
401401
!downloaded_exe_path) {
402402
return false;
403403
}
404-
} catch (const nlohmann::json::parse_error& e) {
404+
} catch (const std::exception& e) {
405405
CLI_LOG_ERROR("JSON parse error: " << e.what());
406406
return false;
407407
}
@@ -433,10 +433,10 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
433433
}
434434

435435
std::optional<std::string> CortexUpdCmd::HandleGithubRelease(
436-
const nlohmann::json& assets, const std::string& os_arch) {
436+
const Json::Value& assets, const std::string& os_arch) {
437437
std::string matched_variant = "";
438438
for (auto& asset : assets) {
439-
auto asset_name = asset["name"].get<std::string>();
439+
auto asset_name = asset["name"].asString();
440440
if (asset_name.find(kCortexBinary) != std::string::npos &&
441441
asset_name.find(os_arch) != std::string::npos &&
442442
asset_name.find(kReleaseFormat) != std::string::npos) {
@@ -452,10 +452,10 @@ std::optional<std::string> CortexUpdCmd::HandleGithubRelease(
452452
CTL_INF("Matched variant: " << matched_variant);
453453

454454
for (auto& asset : assets) {
455-
auto asset_name = asset["name"].get<std::string>();
455+
auto asset_name = asset["name"].asString();
456456
if (asset_name == matched_variant) {
457-
auto download_url = asset["browser_download_url"].get<std::string>();
458-
auto file_name = asset["name"].get<std::string>();
457+
auto download_url = asset["browser_download_url"].asString();
458+
auto file_name = asset["name"].asString();
459459
CTL_INF("Download url: " << download_url);
460460

461461
auto local_path =

engine/cli/commands/cortex_upd_cmd.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ inline std::string GetCortexServerBinary() {
5757
return has_exe ? kCortexServerBinary + "-nightly.exe"
5858
: kCortexServerBinary + "-nightly";
5959
} else if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) {
60-
return has_exe ? kCortexServerBinary + "-beta.exe" : kCortexServerBinary + "-beta";
60+
return has_exe ? kCortexServerBinary + "-beta.exe"
61+
: kCortexServerBinary + "-beta";
6162
} else {
6263
return has_exe ? kCortexServerBinary + ".exe" : kCortexServerBinary;
6364
}
@@ -104,8 +105,8 @@ class CortexUpdCmd {
104105

105106
bool GetStable(const std::string& v);
106107
bool GetBeta(const std::string& v);
107-
std::optional<std::string> HandleGithubRelease(const nlohmann::json& assets,
108-
const std::string& os_arch);
108+
std::optional<std::string> HandleGithubRelease(const Json::Value& assets,
109+
const std::string& os_arch);
109110
bool GetNightly(const std::string& v);
110111
};
111112
} // namespace commands

engine/cli/commands/ps_cmd.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#include <httplib.h>
33
#include <string>
44
#include <tabulate/table.hpp>
5-
#include "nlohmann/json.hpp"
65
#include "utils/engine_constants.h"
76
#include "utils/format_utils.h"
7+
#include "utils/json_helper.h"
88
#include "utils/logging_utils.h"
99
#include "utils/string_utils.h"
1010

@@ -20,18 +20,17 @@ void PsCmd::Exec(const std::string& host, int port) {
2020
return;
2121
}
2222

23-
auto body = nlohmann::json::parse(res->body);
24-
auto data = body["data"];
23+
auto data = json_helper::ParseJsonString(res->body)["data"];
2524
std::vector<ModelLoadedStatus> model_status_list;
2625
try {
2726
for (const auto& item : data) {
2827
ModelLoadedStatus model_status;
2928
// TODO(sang) hardcode for now
3029
model_status.engine = kLlamaEngine;
31-
model_status.model = item["id"];
32-
model_status.ram = item["ram"];
33-
model_status.start_time = item["start_time"];
34-
model_status.vram = item["vram"];
30+
model_status.model = item["id"].asString();
31+
model_status.ram = item["ram"].asUInt64();
32+
model_status.start_time = item["start_time"].asUInt64();
33+
model_status.vram = item["vram"].asUInt64();
3534
model_status_list.push_back(model_status);
3635
}
3736
} catch (const std::exception& e) {

engine/common/download_task.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
#include <json/json.h>
44
#include <filesystem>
5-
#include <nlohmann/json.hpp>
65
#include <sstream>
76
#include <string>
87
#include <unordered_map>
98

109
enum class DownloadType { Model, Engine, Miscellaneous, CudaToolkit, Cortex };
1110

12-
using namespace nlohmann;
13-
1411
struct DownloadItem {
1512

1613
std::string id;
@@ -101,21 +98,4 @@ struct DownloadTask {
10198

10299
return root;
103100
}
104-
105-
json ToJson() const {
106-
json dl_items = json::array();
107-
108-
for (const auto& item : items) {
109-
json dl_item{{"id", item.id},
110-
{"downloadUrl", item.downloadUrl},
111-
{"localPath", item.localPath},
112-
{"checksum", item.checksum.value_or("N/A")},
113-
{"bytes", item.bytes.value_or(0)},
114-
{"downloadedBytes", item.downloadedBytes.value_or(0)}};
115-
dl_items.push_back(dl_item);
116-
}
117-
118-
return json{
119-
{"id", id}, {"type", DownloadTypeToString(type)}, {"items", dl_items}};
120-
}
121101
};

engine/common/event.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#pragma once
22

33
#include <eventpp/eventqueue.h>
4-
#include <nlohmann/json.hpp>
54
#include <string>
65
#include "common/download_task.h"
76
#include "eventpp/utilities/anydata.h"
7+
#include "utils/json_helper.h"
88

99
namespace cortex::event {
10-
using namespace nlohmann;
1110

1211
enum class EventType {
1312
DownloadEvent,
@@ -48,16 +47,17 @@ std::string DownloadEventTypeToString(DownloadEventType type) {
4847
} // namespace
4948

5049
struct DownloadEvent : public cortex::event::Event {
51-
std::string ToJsonString() const {
52-
json json{{"type", DownloadEventTypeToString(type_)},
53-
{"task", download_task_.ToJson()}};
54-
return json.dump();
55-
}
56-
5750
DownloadEventType type_;
5851
DownloadTask download_task_;
52+
53+
std::string ToJsonString() const {
54+
Json::Value root;
55+
root["type"] = DownloadEventTypeToString(type_);
56+
root["task"] = download_task_.ToJsonCpp();
57+
return json_helper::DumpJsonString(root);
58+
}
5959
};
60-
} // namespace cortex::event
60+
}; // namespace cortex::event
6161

6262
constexpr std::size_t eventMaxSize =
6363
eventpp::maxSizeOf<cortex::event::Event, cortex::event::DownloadEvent,

engine/controllers/engines.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ void Engines::GetEngineVersions(
136136
Json::Value res;
137137
res["message"] = "Engine name is required";
138138
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
139-
resp->setStatusCode(k409Conflict);
139+
resp->setStatusCode(k400BadRequest);
140140
callback(resp);
141-
LOG_WARN << "No engine field in path param";
142141
return;
143142
}
144143

@@ -148,9 +147,8 @@ void Engines::GetEngineVersions(
148147
Json::Value res;
149148
res["message"] = "Failed to get engine releases: " + result.error();
150149
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
151-
resp->setStatusCode(k409Conflict);
150+
resp->setStatusCode(k400BadRequest);
152151
callback(resp);
153-
LOG_WARN << "No engine field in path param";
154152
return;
155153
}
156154

engine/services/model_service.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "utils/engine_constants.h"
1212
#include "utils/file_manager_utils.h"
1313
#include "utils/huggingface_utils.h"
14+
#include "utils/json_helper.h"
1415
#include "utils/logging_utils.h"
1516
#include "utils/result.hpp"
1617
#include "utils/string_utils.h"
@@ -63,7 +64,6 @@ void ParseGguf(const DownloadItem& ggufDownloadItem,
6364

6465
cpp::result<DownloadTask, std::string> GetDownloadTask(
6566
const std::string& modelId, const std::string& branch = "main") {
66-
using namespace nlohmann;
6767
url_parser::Url url = {
6868
.protocol = "https",
6969
.host = ModelService::kHuggingFaceHost,
@@ -701,11 +701,12 @@ cpp::result<bool, std::string> ModelService::GetModelStatus(
701701
auto mc = yaml_handler.GetModelConfig();
702702

703703
httplib::Client cli(host + ":" + std::to_string(port));
704-
nlohmann::json json_data;
705-
json_data["model"] = model_handle;
706-
json_data["engine"] = mc.engine;
707704

708-
auto data_str = json_data.dump();
705+
Json::Value root;
706+
root["model"] = model_handle;
707+
root["engine"] = mc.engine;
708+
709+
auto data_str = json_helper::DumpJsonString(root);
709710

710711
auto res = cli.Post("/inferences/server/modelstatus", httplib::Headers(),
711712
data_str.data(), data_str.size(), "application/json");

0 commit comments

Comments
 (0)