tqcq 585bca14e3
Some checks failed
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 50s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 54s
linux-x64-gcc / linux-gcc (push) Successful in 58s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m5s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 1m21s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m7s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Failing after 46s
feat add jsoncpp
2023-12-26 20:58:36 +08:00

39 lines
1.1 KiB
C++

#include "json/json.h"
#include <iostream>
#include <memory>
/**
* \brief Parse a raw string into Value object using the CharReaderBuilder
* class, or the legacy Reader class.
* Example Usage:
* $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
* $./readFromString
* colin
* 20
*/
int main() {
const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
const auto rawJsonLength = static_cast<int>(rawJson.length());
constexpr bool shouldUseOldWay = false;
JSONCPP_STRING err;
Json::Value root;
if (shouldUseOldWay) {
Json::Reader reader;
reader.parse(rawJson, root);
} else {
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
&err)) {
std::cout << "error" << std::endl;
return EXIT_FAILURE;
}
}
const std::string name = root["Name"].asString();
const int age = root["Age"].asInt();
std::cout << name << std::endl;
std::cout << age << std::endl;
return EXIT_SUCCESS;
}