The code is just a header under 200 lines of code. There is no particular reason why I wrote it. It was just a weekend fun project to practice finite state machines.
- strings
- arrays (no nested arrays supported)
- objects
{
"k1": "",
"a1": [],
"o1": {}
}
#include <mjson/mjson.hpp>
void foo() {
const auto cfg = R"(
{
"key" : "value"
}
)";
mjson::json js(cfg);
if (!js.is_valid()) {
std::cout << "FAILURE: Format error!" << std::endl;
return;
}
if (!js.has("key")) {
std::cout << "No 'key' field found" << std::endl;
return;
}
const auto& key = js["key"];
std::cout << "key: " << key << std::endl;
}
The code has been built and tested on Windows and Linux using MS Visual Studio 2019 and gcc 9.3.0 on Ubuntu 18.4
To create Microsoft Visual Studio solution and project files, and build the solution from PowerShell or CMD:
mkdir -p build/msvs
cd build/msvs
cmake ../..
cmake --build .
Microsoft Visual Studio solution file will be /build/msvs/mjson.sln
To build on Linux using make:
mkdir -p build/x86_x64_linux
cd build/x86_x64_linux
cmake ../..
make
The below cmake code creates mjson interface library and downloads only mjson.hpp file.
ExternalProject_Add(mjson_download
PREFIX ${FETCHCONTENT_BASE_DIR}/mjson
URL https://github.com/ademyankov/mini_json/releases/download/v1.1/mjson.hpp
DOWNLOAD_NO_EXTRACT 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
add_library(mjson INTERFACE)
target_include_directories(mjson INTERFACE ${FETCHCONTENT_BASE_DIR}/mjson/src)
add_dependencies(mjson mjson_download)