Skip to content

ArtifactDB/millijson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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>
#include "millijson/millijson.hpp"

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

The same works with a file:

auto ptr = millijson::parse_file("some_json_file.json");

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

millijson::validate_file("some_json_file.json");

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.