uploaded 1 month 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](https://github.com/nlohmann/json).

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`](https://json.nlohmann.me/api/macros/json_no_io/)  
   Prevents including `<iostream>` headers, which increase significantly the size of the app.
2. [`JSON_NOEXCEPTION`](https://json.nlohmann.me/api/macros/json_noexception/)  
   Disables exceptions. By default, these are disabled in ESP-IDF as well.
3. [`JSON_DISABLE_ENUM_SERIALIZATION`](https://json.nlohmann.me/api/macros/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.2"

or download archive

Stats

  • Archive size
    Archive size ~ 3.14 KB
  • Downloaded in total
    Downloaded in total 42 times
  • Downloaded this version
    This version: 2 times

Badge

mittelab/nlohmann-json version: 3.11.2
|