[5분 따라하기]
비주얼 스튜디오 C++에서 JSON
파서 설치하기
박재호(jrogue@gmail.com)
참고 자료
• <컴퓨터 vs 책> 블로그
• http://jhrogue.blogspot.com/
• OKdevTV 유튜브 방송
• 오늘자 방송
• https://www.youtube.com/watch?v=KH3mirEkZKU&list=PLdntWJk2tJPLKNNYBVCxn
de2PEB6dzbSL&index=14
• 5분 따라하기 리스트:
• https://www.youtube.com/playlist?list=PLdntWJk2tJPLKNNYBVCxnde2PEB6dzbSL
• 슬라이드 셰어
• https://www.slideshare.net/jrogue/presentations
• ASCIINEMA
• https://asciinema.org/~jrogue
선행 조건
• Visual Studio에 영문 언어팩 설치
오늘 소개할 내용
• 자바스크립트와 파이썬과는 달리 C/++에서는 JSON을 언어 차
원에서 지원하지 않음
• 이런 상황에서 비주얼 스튜디오에서 C++ 프로그램을 개발할
때 어떻게 해야할까?
패키지로 설치하는 방법
• vcpkg!
• https://docs.microsoft.com/ko-kr/cpp/build/vcpkg?view=vs-2019
• C++용 명령행 패키지 관리자
• 교차 플랫폼 지원: 윈도우, 리눅스, MacOS X
• 윈도우 카탈로그에 등록된 경우 소스 코드를 다운로드해서 빌드 → 컴
파일러 버전 문제 해소
설치
• $ git clone https://github.com/Microsoft/vcpkg.git
• $ cd vcpkg
• $ ./bootstrap-vcpkg.sh
• $ ./vcpkg integrate install
• $ ./vcpkg search | grep json
• $ ./vcpkg install nlohmann-json
실행 결과(예시)
$ git clone https://github.com/Microsoft/vcpkg.git
Cloning into 'vcpkg'...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 86762 (delta 0), reused 0 (delta 0), pack-reused
86761
Receiving objects: 100% (86762/86762), 25.61 MiB | 8.41 MiB/s,
done.
Resolving deltas: 100% (53902/53902), done.
Updating files: 100% (5288/5288), done.
$ ./bootstrap-vcpkg.sh
Building vcpkg.exe ...
pch.cpp
archives.cpp
checks.cpp
chrono.cpp
...
vcpkgpaths.cpp
versiont.cpp
visualstudio.cpp
vcpkg.cpp
...
Building vcpkg.exe... done.
$ ./vcpkg integrate install
Applied user-wide integration for this vcpkg root.
All MSBuild C++ projects can now #include any installed libraries.
Linking will be handled automatically.
Installing new libraries will make them instantly available.
CMake projects should use: "-
DCMAKE_TOOLCHAIN_FILE=C:/Users/jaypark/vcpkg/scripts/buildsystems/vcpkg.
cmake"
실행 결과(예시)
$ ./vcpkg search | grep json
arrow[json] JSON file support
bitserializer[cpprestjson-archive] Module for support JSON (implementation based on the CppRestSDK library)
bitserializer[rapidjson-archive] Module for support JSON (implementation based on the RapidJson library)
bitserializer-cpp... alias Deprecated alias for bitserializer-cpprestjson
bitserializer-rap... alias Deprecated alias for bitserializer-rapidjson
cjson 2019-11-30-1 Ultralightweight JSON parser in ANSI C
cjson[utils] Enable building the cJSON_Utils library
json-c 2019-09-10 A JSON implementation in C
json-dto 0.2.9.2 A small header-only library for converting data between json representation an...
json-schema-valid... 2.1.0 This is a C++ library for validating JSON documents based on a JSON Schema. Th...
json-spirit 4.1.0-1 json parser using boost library
json11 2017-06-20-2 json11 is a tiny JSON library for C++11, providing JSON parsing and serializat...
json5-parser 1.0.0-1 An enhancement of the JSON Spirit C++ library to understand json5.
jsoncons 0.155.1 A C++, header-only library for constructing JSON and JSON-like text and binary...
jsoncpp 1.9.2 jsoncpp is an implementation of a JSON reader and writer in C++. JSON (JavaScr...
jsonnet 0.14.0-1 Jsonnet - The data templating language
jwt-cpp 0.4.0 A header only library for creating and validating json web tokens in c++
nlohmann-json 3.9.1 JSON for Modern C++
parson 2019-07-11 a lighweight json library written in C
picojson 1.3.0-1 A header-file-only, JSON parser serializer in C++.
rapidjson 2020-02-08 A fast JSON parser/generator for C++ with both SAX/DOM style API <http://rapid...
sajson 2018-09-21 Lightweight, extremely high-performance JSON parser for C++11
simdjson 0.4.6 A extremely fast JSON library that can parse gigabytes of JSON per second
sqlite3[json1] enable JSON functionality for sqlite3
taocpp-json 2019-07-11 C++ header-only JSON library
valijson 2018-11-17-1 Header-only C++ library for JSON Schema validation
실행 결과(예시)
$ ./vcpkg install nlohmann-json
Computing installation plan...
A suitable version of cmake was not found (required v3.17.2). Downloading portable cmake v3.17.2...
Downloading cmake...
https://github.com/Kitware/CMake/releases/download/v3.17.2/cmake-3.17.2-win32-x86.zip ->
C:Usersjayparkdocumentsvcpkgdownloadscmake-3.17.2-win32-x86.zip
Extracting cmake...
A suitable version of 7zip was not found (required v18.1.0). Downloading portable 7zip v18.1.0...
Downloading 7zip...
https://www.nuget.org/api/v2/package/7-Zip.CommandLine/18.1.0 -> C:Usersjayparkdocumentsvcpkgdownloads7-
zip.commandline.18.1.0.nupkg
Extracting 7zip...
A suitable version of nuget was not found (required v5.5.1). Downloading portable nuget v5.5.1...
Downloading nuget...
https://dist.nuget.org/win-x86-commandline/v5.5.1/nuget.exe -> C:Usersjayparkdocumentsvcpkgdownloads22ea847d-nuget.exe
The following packages will be built and installed:
nlohmann-json[core]:x86-windows
Detecting compiler hash for triplet x86-windows...
A suitable version of powershell-core was not found (required v6.2.1). Downloading portable powershell-core v6.2.1...
Downloading powershell-core...
https://github.com/PowerShell/PowerShell/releases/download/v6.2.1/PowerShell-6.2.1-win-x86.zip ->
C:UsersjayparkvcpkgdownloadsPowerShell-6.2.1-win-x86.zip
Extracting powershell-core...
실행 결과(예시)
Starting package 1/1: nlohmann-json:x86-windows
Building package nlohmann-json[core]:x86-windows...
Could not locate cached archive:
C:UsersjayparkAppDataLocalvcpkg/archives1e1e699d54ec05be35d7ea0e7c4de7a1672bd0b286.zip
-- Downloading https://github.com/nlohmann/json/raw/v3.9.1/CMakeLists.txt...
-- Downloading https://github.com/nlohmann/json/raw/v3.9.1/LICENSE.MIT...
-- Downloading https://github.com/nlohmann/json/raw/v3.9.1/nlohmann_json.natvis...
-- Downloading https://github.com/nlohmann/json/raw/v3.9.1/cmake/config.cmake.in...
-- Downloading https://github.com/nlohmann/json/raw/v3.9.1/cmake/pkg-config.pc.in...
-- Downloading https://github.com/nlohmann/json/raw/v3.9.1/cmake/nlohmann_jsonConfigVersion.cmake.in...
-- Downloading https://github.com/nlohmann/json/releases/download/v3.9.1/include.zip...
-- Extracting source C:/Users/jaypark/documents/vcpkg/downloads/nlohmann-json-v3.9.1-include.zip
-- Using source at C:/Users/jaypark/documents/vcpkg/buildtrees/nlohmann-json/3.9.1-74ced61d52.clean
-- Configuring x86-windows
-- Building x86-windows-dbg
-- Building x86-windows-rel
-- Installing: C:/Users/jaypark/documents/vcpkg/packages/nlohmann-json_x86-windows/share/nlohmann-json/copyright
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: C:UsersjayparkAppDataLocalvcpkg/archives1e1e699d54ec05be35d7ea0e7c4de7a1672bd0b286.zip
Building package nlohmann-json[core]:x86-windows... done
Installing package nlohmann-json[core]:x86-windows...
Installing package nlohmann-json[core]:x86-windows... done
Elapsed time for package nlohmann-json:x86-windows: 12.56 s
Total elapsed time: 30.43 s
The package nlohmann-json:x86-windows provides CMake targets:
find_package(nlohmann_json CONFIG REQUIRED)
target_link_libraries(main PRIVATE nlohmann_json nlohmann_json::nlohmann_json)
실제 사용(예시)
#include <nlohmann/json.hpp>
using nlohmann::json;
json j1 = json::parse(json_string); // 파싱
// 1st class data type
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
json j3 = "{ "happy": true, "pi": 3.141 }"_json; // serialize

[5분 따라하기] 비주얼 스튜디오 C++에서 JSON 파서 설치하기

  • 1.
    [5분 따라하기] 비주얼 스튜디오C++에서 JSON 파서 설치하기 박재호(jrogue@gmail.com)
  • 2.
    참고 자료 • <컴퓨터vs 책> 블로그 • http://jhrogue.blogspot.com/ • OKdevTV 유튜브 방송 • 오늘자 방송 • https://www.youtube.com/watch?v=KH3mirEkZKU&list=PLdntWJk2tJPLKNNYBVCxn de2PEB6dzbSL&index=14 • 5분 따라하기 리스트: • https://www.youtube.com/playlist?list=PLdntWJk2tJPLKNNYBVCxnde2PEB6dzbSL • 슬라이드 셰어 • https://www.slideshare.net/jrogue/presentations • ASCIINEMA • https://asciinema.org/~jrogue
  • 3.
    선행 조건 • VisualStudio에 영문 언어팩 설치
  • 4.
    오늘 소개할 내용 •자바스크립트와 파이썬과는 달리 C/++에서는 JSON을 언어 차 원에서 지원하지 않음 • 이런 상황에서 비주얼 스튜디오에서 C++ 프로그램을 개발할 때 어떻게 해야할까?
  • 5.
    패키지로 설치하는 방법 •vcpkg! • https://docs.microsoft.com/ko-kr/cpp/build/vcpkg?view=vs-2019 • C++용 명령행 패키지 관리자 • 교차 플랫폼 지원: 윈도우, 리눅스, MacOS X • 윈도우 카탈로그에 등록된 경우 소스 코드를 다운로드해서 빌드 → 컴 파일러 버전 문제 해소
  • 6.
    설치 • $ gitclone https://github.com/Microsoft/vcpkg.git • $ cd vcpkg • $ ./bootstrap-vcpkg.sh • $ ./vcpkg integrate install • $ ./vcpkg search | grep json • $ ./vcpkg install nlohmann-json
  • 7.
    실행 결과(예시) $ gitclone https://github.com/Microsoft/vcpkg.git Cloning into 'vcpkg'... remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 86762 (delta 0), reused 0 (delta 0), pack-reused 86761 Receiving objects: 100% (86762/86762), 25.61 MiB | 8.41 MiB/s, done. Resolving deltas: 100% (53902/53902), done. Updating files: 100% (5288/5288), done. $ ./bootstrap-vcpkg.sh Building vcpkg.exe ... pch.cpp archives.cpp checks.cpp chrono.cpp ... vcpkgpaths.cpp versiont.cpp visualstudio.cpp vcpkg.cpp ... Building vcpkg.exe... done. $ ./vcpkg integrate install Applied user-wide integration for this vcpkg root. All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available. CMake projects should use: "- DCMAKE_TOOLCHAIN_FILE=C:/Users/jaypark/vcpkg/scripts/buildsystems/vcpkg. cmake"
  • 8.
    실행 결과(예시) $ ./vcpkgsearch | grep json arrow[json] JSON file support bitserializer[cpprestjson-archive] Module for support JSON (implementation based on the CppRestSDK library) bitserializer[rapidjson-archive] Module for support JSON (implementation based on the RapidJson library) bitserializer-cpp... alias Deprecated alias for bitserializer-cpprestjson bitserializer-rap... alias Deprecated alias for bitserializer-rapidjson cjson 2019-11-30-1 Ultralightweight JSON parser in ANSI C cjson[utils] Enable building the cJSON_Utils library json-c 2019-09-10 A JSON implementation in C json-dto 0.2.9.2 A small header-only library for converting data between json representation an... json-schema-valid... 2.1.0 This is a C++ library for validating JSON documents based on a JSON Schema. Th... json-spirit 4.1.0-1 json parser using boost library json11 2017-06-20-2 json11 is a tiny JSON library for C++11, providing JSON parsing and serializat... json5-parser 1.0.0-1 An enhancement of the JSON Spirit C++ library to understand json5. jsoncons 0.155.1 A C++, header-only library for constructing JSON and JSON-like text and binary... jsoncpp 1.9.2 jsoncpp is an implementation of a JSON reader and writer in C++. JSON (JavaScr... jsonnet 0.14.0-1 Jsonnet - The data templating language jwt-cpp 0.4.0 A header only library for creating and validating json web tokens in c++ nlohmann-json 3.9.1 JSON for Modern C++ parson 2019-07-11 a lighweight json library written in C picojson 1.3.0-1 A header-file-only, JSON parser serializer in C++. rapidjson 2020-02-08 A fast JSON parser/generator for C++ with both SAX/DOM style API <http://rapid... sajson 2018-09-21 Lightweight, extremely high-performance JSON parser for C++11 simdjson 0.4.6 A extremely fast JSON library that can parse gigabytes of JSON per second sqlite3[json1] enable JSON functionality for sqlite3 taocpp-json 2019-07-11 C++ header-only JSON library valijson 2018-11-17-1 Header-only C++ library for JSON Schema validation
  • 9.
    실행 결과(예시) $ ./vcpkginstall nlohmann-json Computing installation plan... A suitable version of cmake was not found (required v3.17.2). Downloading portable cmake v3.17.2... Downloading cmake... https://github.com/Kitware/CMake/releases/download/v3.17.2/cmake-3.17.2-win32-x86.zip -> C:Usersjayparkdocumentsvcpkgdownloadscmake-3.17.2-win32-x86.zip Extracting cmake... A suitable version of 7zip was not found (required v18.1.0). Downloading portable 7zip v18.1.0... Downloading 7zip... https://www.nuget.org/api/v2/package/7-Zip.CommandLine/18.1.0 -> C:Usersjayparkdocumentsvcpkgdownloads7- zip.commandline.18.1.0.nupkg Extracting 7zip... A suitable version of nuget was not found (required v5.5.1). Downloading portable nuget v5.5.1... Downloading nuget... https://dist.nuget.org/win-x86-commandline/v5.5.1/nuget.exe -> C:Usersjayparkdocumentsvcpkgdownloads22ea847d-nuget.exe The following packages will be built and installed: nlohmann-json[core]:x86-windows Detecting compiler hash for triplet x86-windows... A suitable version of powershell-core was not found (required v6.2.1). Downloading portable powershell-core v6.2.1... Downloading powershell-core... https://github.com/PowerShell/PowerShell/releases/download/v6.2.1/PowerShell-6.2.1-win-x86.zip -> C:UsersjayparkvcpkgdownloadsPowerShell-6.2.1-win-x86.zip Extracting powershell-core...
  • 10.
    실행 결과(예시) Starting package1/1: nlohmann-json:x86-windows Building package nlohmann-json[core]:x86-windows... Could not locate cached archive: C:UsersjayparkAppDataLocalvcpkg/archives1e1e699d54ec05be35d7ea0e7c4de7a1672bd0b286.zip -- Downloading https://github.com/nlohmann/json/raw/v3.9.1/CMakeLists.txt... -- Downloading https://github.com/nlohmann/json/raw/v3.9.1/LICENSE.MIT... -- Downloading https://github.com/nlohmann/json/raw/v3.9.1/nlohmann_json.natvis... -- Downloading https://github.com/nlohmann/json/raw/v3.9.1/cmake/config.cmake.in... -- Downloading https://github.com/nlohmann/json/raw/v3.9.1/cmake/pkg-config.pc.in... -- Downloading https://github.com/nlohmann/json/raw/v3.9.1/cmake/nlohmann_jsonConfigVersion.cmake.in... -- Downloading https://github.com/nlohmann/json/releases/download/v3.9.1/include.zip... -- Extracting source C:/Users/jaypark/documents/vcpkg/downloads/nlohmann-json-v3.9.1-include.zip -- Using source at C:/Users/jaypark/documents/vcpkg/buildtrees/nlohmann-json/3.9.1-74ced61d52.clean -- Configuring x86-windows -- Building x86-windows-dbg -- Building x86-windows-rel -- Installing: C:/Users/jaypark/documents/vcpkg/packages/nlohmann-json_x86-windows/share/nlohmann-json/copyright -- Performing post-build validation -- Performing post-build validation done Stored binary cache: C:UsersjayparkAppDataLocalvcpkg/archives1e1e699d54ec05be35d7ea0e7c4de7a1672bd0b286.zip Building package nlohmann-json[core]:x86-windows... done Installing package nlohmann-json[core]:x86-windows... Installing package nlohmann-json[core]:x86-windows... done Elapsed time for package nlohmann-json:x86-windows: 12.56 s Total elapsed time: 30.43 s The package nlohmann-json:x86-windows provides CMake targets: find_package(nlohmann_json CONFIG REQUIRED) target_link_libraries(main PRIVATE nlohmann_json nlohmann_json::nlohmann_json)
  • 11.
    실제 사용(예시) #include <nlohmann/json.hpp> usingnlohmann::json; json j1 = json::parse(json_string); // 파싱 // 1st class data type json j2 = { {"pi", 3.141}, {"happy", true}, {"name", "Niels"}, {"nothing", nullptr}, {"answer", { {"everything", 42} }}, {"list", {1, 0, 2}}, {"object", { {"currency", "USD"}, {"value", 42.99} }} }; json j3 = "{ "happy": true, "pi": 3.141 }"_json; // serialize