今天來介紹C++中用於解析跟序列化Json格式資料的兩個套件。
環境就延續Day4的Dockerfile,重啟一個新的Container來做Demo。
docker run -v D:/dvt:/home --name=cpp_dev --tty cpp_dev
首先介紹jsoncpp,是一個能夠解析Json文件的函式庫,甚至能解析文件中的註解。
安裝的部分,一樣是靠vcpkg來搞定。
vcpkg install jsoncpp
成功的話,應該會看到以下信息。
jsoncpp provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(jsoncpp CONFIG REQUIRED)
    target_link_libraries(main PRIVATE JsonCpp::JsonCpp)
然後再透過CMake的CLI設定專案,如果不想用CLI,也可以參考Day2中提到,在VS Code的settings.json加入相對應的欄位設定。
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=/opt/vcpkg/scripts/buildsystems/vcpkg.cmake
這邊就拿官方的example展示。
#include "json/json.h"
#include <fstream>
#include <iostream>
 
int main(int argc, char* argv[]) {
  Json::Value root;
  std::ifstream ifs;
  ifs.open(argv[1]);
  Json::CharReaderBuilder builder;
  builder["collectComments"] = true;
  JSONCPP_STRING errs;
  if (!parseFromStream(builder, ifs, &root, &errs)) {
    std::cout << errs << std::endl;
    return EXIT_FAILURE;
  }
  std::cout << root << std::endl;
  return EXIT_SUCCESS;
}
json file:
// comment head
{
    // comment before
    "key" : "value"
    // comment after
}// comment tail
cd build/
make
./jsoncpp_demo ../demo.json
# output
// comment head
{
        // comment before
        "key" : "value"
} // comment tail
// comment after
ajson也是一個能夠解析json格式的函式庫,但他更特別的地方在於他能將json物件轉成C++中的結構體(struct)。
ajson安裝的話,就不能透過vcpkg了,因為vcpkg中目前並沒有包含ajson,但ajson其實只需要有頭文件就可以使用,所以我們只需要從github抓下來即可使用。
git clone https://github.com/lordoffox/ajson.git
這邊也是一樣使用官方的example。
#include <iostream>
#include <string>
#include "ajson.hpp"
using namespace std;
using namespace ajson;
struct demo
{
  string hello;
  string world;
};
AJSON(demo,hello,world)
int main(int argc, char * argv[])
{
 char * buff = "{\"hello\" : \"Hello\", \"world\" : \"world.\"}";
 demo the_demo;
 load_from_buff(the_demo,buff);
 cout << the_demo.hello << " " << the_demo.world << std::endl;
 cin.get();
 return 0;
}
cd build/
make
./ajson_demo
# output
Hello world.
以上代碼都同步到我的github上,有需要都可以自己抓下來試試。