Study: Software(SW)/SW: Opensource

[C++] C++ Json 라이브러리 변경: JsonCpp to Nlohmann/json...

DrawingProcess 2022. 8. 10. 11:04
반응형
참고: JsonCpp로 적용되어 있는 기존의 Json library를 Nlohman/Json으로 바꾸자를 제안이 있어서 조사하였으며, 조사의 결과는
[C++] C++ Json 라이브러리 성능비교(벤치마크): JsonCpp, Nlohmann/json 에 정리해놓았습니다.
이 문서는 조사의 결과, 성능과 편의성 측면에서 우수한 Nlohman/Json으로 교체하기 위한 과정을 기록한 문서이니 참고 부탁드립니다.

JsonCpp to Nlohmann/json

Generate json

JsonCpp

Json::Value json;

Nlohmann/json

nlohmann::json json;
nlohmann::ordered_json ordered_json; // 넣는 순서대로 프린트되는 json 형태

 

Export json

JsonCpp

std::string content = json["source"].asString()
uint64_t content = json["source"].asUInt64()

Nlohmann/json

std::string content = json["source"].get<std::string>()
uint64_t content = json["source"].get<uint64_t>()

 

Null Check

JsonCpp

if (json["source"] == Json::nullValue)

Nlohmann/json

if (json["source"].is_null())
if (json["source"] == nullptr)

 

Field/Colume Check

JsonCpp

if (json.isObject() && json.isMember("source"))

Nlohmann/json

if (json["key"].find("source") != json["key"].end())
if (json["key"].contains("source"))

 

json to std::string

JsonCpp

// stringstream
Json::StreamWriterBuilder wbuilder;
wbuilder["commentStyle"] = "None";
wbuilder["indentation"] = "";

std::unique_ptr<Json::StreamWriter> jsonStreamWriter;
jsonStreamWriter.reset(wbuilder.newStreamWriter());

std::string content = Json::writeString(wbuilder, json);
std::string content = json.toStyledString()

Nlohmann/json

std::string content = json.dump()

 

About nlohmann::json

 

참고



반응형