ademyankov / mini_json

Minimalistic JSON parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C++17 Minimalistic Json parser.

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.

Mini Json parser supports:

  • strings
  • arrays (no nested arrays supported)
  • objects
{
    "k1": "",
    "a1": [],
    "o1": {}
}

Simple usage of the parser:

#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;
}

Files:

  • The header is here
  • The hello sample application is here
  • Catch2 unit tests are here

Build

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

Microsoft Visual Studio

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

Linux

To build on Linux using make:

mkdir -p build/x86_x64_linux
cd build/x86_x64_linux
cmake ../..
make

Include mjson into your cmake project

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)

About

Minimalistic JSON parser

License:MIT License


Languages

Language:C++ 94.8%Language:CMake 5.2%