mittelab/nlohmann-json

uploaded 3 months ago
JSON for Modern C++ (ESP-IDF wrapper)

readme

JSON for Modern C++ (ESP-IDF wrapper)

This is a lightweight component that wraps Niels Lohmann's excellent JSON library.

It downloads the single-header version of the library with CMake (using FetchContent). It works for ESP-IDF but can be used for desktop C++ development too.

How to use (ESP-IDF)

Add a dependency with idf.py add-dependency mittelab/nlohmann-json, and then you can use it with

C++

#include <nlohmann/json.hpp>

This is a header wrapper that defines some custom macros (see below). If you need the original header for some reason, include

C++

#include <nlohmann/json_impl.hpp>

How to use (ESP-IDF, git submodule)

Assume you have checked out this repo at $PROJ/thirdparty/nlohmann-json. Then in your root CMakeLists.txt, add

Cmake

list(APPEND EXTRA_COMPONENT_DIRS "$PROJ/thirdparty/nlohmann-json")
# include($ENV{IDF_PATH}/tools/cmake/project.cmake)
# project(...)

In the components that need to use this library, make sure to specify nlohmann-json as CMake dependency:

Cmake

idf_component_register(
    SRCS ${SOURCES}
    REQUIRES
        nlohmann-json
)

How to use (desktop development, git submodule)

Assume you have checked out this repo at $PROJ/thirdparty/nlohmann-json. Then in your CMakeLists.txt, add

Cmake

add_subdirectory($PROJ/thirdparty/nlohmann-json/nlohmann-json)

# Your target setup goes here, then

target_link_libraries($YOUR_TARGET nlohmann-json)
target_include_directories($YOUR_TARGET PRIVATE $<TARGET_PROPERTY:nlohmann-json,INTERFACE_INCLUDE_DIRECTORIES>)

Custom feature macros

By default, including <nlohmann/json.hpp> sets the following macros:

  1. JSON_NO_IO
    Prevents including <iostream> headers, which increase significantly the size of the app.
  2. JSON_NOEXCEPTION
    Disables exceptions. By default, these are disabled in ESP-IDF as well.
  3. JSON_DISABLE_ENUM_SERIALIZATION
    This prevents automatic serialization of enums. This is is included because in some scenarios we needed custom enum serialization.

If you need to change the macro configurations, define your own json.hpp header with your custom macros and include directly <nlohmann/json_impl.hpp>.

Links

Supports all targets

Maintainer

  • Pietro Saccardi <lizardm4@gmail.com>

License: MIT

Tags

To add this component to your project, run:

idf.py add-dependency "mittelab/nlohmann-json^3.11.3"

or download archive

Stats

  • Archive size
    Archive size ~ 136.99 KB
  • Downloaded in total
    Downloaded in total 74 times
  • Downloaded this version
    This version: 71 times

Badge

mittelab/nlohmann-json version: 3.11.3
|