millijson
Lightweight JSON parser for C++
Loading...
Searching...
No Matches
Yet another JSON parser

Unit tests Documentation codecov

Overview

millijson is a header-only C++ library that provides a lightweight JSON parser. It has no selling points other than being small and simple, if you could call those selling points. I wrote it mostly for fun but also to provide a replacement for the nlohmann/json library - while excellent, its single-header source code is pretty large (around 25 kLoCs, if I remember correctly) and I wanted something slimmer. This is most noticeable when vendoring the file into various modules/packages, where C++ suddenly becomes the main language of the associated repository, and I didn't like that. Yes, I'm that petty.

Quick start

Given a JSON-formatted string:

#include <string>
std::string foo = "[ { \"foo\": \"bar\" }, 1e-2 ]";
auto ptr = millijson::parse_string(foo.c_str(), foo.size());
ptr->type(); // millijson::ARRAY
const auto& array = ptr->get_array(); // vector of pointers
array[0]->type(); // millijson::OBJECT
const auto& mapping = array[0]->get_object(); // unordered map
const auto& value = *(mapping.find("foo"));
value->type(); # millijson::STRING
const auto& string = value->get_string(); // std::string
array[1]->type(); // milljson::NUMBER
auto number = array[1]->get_number(); // double
Header-only library for JSON parsing.
std::shared_ptr< Base > parse_string(const char *ptr, size_t len)
Definition millijson.hpp:789

The same works with a file:

auto ptr = millijson::parse_file("some_json_file.json");
std::shared_ptr< Base > parse_file(const char *path, size_t buffer_size=65536)
Definition millijson.hpp:879

If you just want to validate a file, without using memory to load it:

millijson::validate_file("some_json_file.json");
Type validate_file(const char *path, size_t buffer_size=65536)
Definition millijson.hpp:891

See the reference documentation for more details.

Building projects

CMake with FetchContent

If you're using CMake, you just need to add something like this to your CMakeLists.txt:

include(FetchContent)
FetchContent_Declare(
millijson
GIT_REPOSITORY https://github.com/ArtifactDB/millijson
GIT_TAG master # or any version of interest
)
FetchContent_MakeAvailable(millijson)

Then you can link to millijson to make the headers available during compilation:

# For executables:
target_link_libraries(myexe millijson)
# For libaries
target_link_libraries(mylib INTERFACE millijson)

CMake with find_package()

You can install the library by cloning a suitable version of this repository and running the following commands:

mkdir build && cd build
cmake .. -DMILLIJSON_TESTS=OFF
cmake --build . --target install

Then you can use find_package() as usual:

find_package(ltla_millijson CONFIG REQUIRED)
target_link_libraries(mylib INTERFACE ltla::millijson)

Manual

If you're not using CMake, the simple approach is to just copy the files in the include/ subdirectory - either directly or with Git submodules - and include their path during compilation with, e.g., GCC's -I.

Links

Parsing is based on the JSON standard described here.

This interface was inspired by the implementation in nlohmann/json.