Skip to content

Commit 9c21285

Browse files
committed
Merge branch 'develop' of https://github.com/nlohmann/json into issue2179
� Conflicts: � single_include/nlohmann/json.hpp
2 parents 5f146cb + cbafed3 commit 9c21285

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1769
-868
lines changed

.clang-tidy

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
Checks: '-*,
2-
bugprone-*,
3-
cert-*,
4-
clang-analyzer-*,
5-
google-*,
6-
-google-runtime-references,
1+
Checks: '*,
2+
-cppcoreguidelines-avoid-goto,
3+
-cppcoreguidelines-avoid-magic-numbers,
4+
-cppcoreguidelines-macro-usage,
5+
-fuchsia-default-arguments-calls,
6+
-fuchsia-default-arguments-declarations,
7+
-fuchsia-overloaded-operator,
78
-google-explicit-constructor,
8-
hicpp-*,
9+
-google-runtime-references,
10+
-hicpp-avoid-goto,
11+
-hicpp-explicit-conversions,
912
-hicpp-no-array-decay,
1013
-hicpp-uppercase-literal-suffix,
11-
-hicpp-explicit-conversions,
12-
misc-*,
13-
-misc-non-private-member-variables-in-classes,
14-
llvm-*,
1514
-llvm-header-guard,
16-
modernize-*,
15+
-llvm-include-order,
16+
-misc-non-private-member-variables-in-classes,
1717
-modernize-use-trailing-return-type,
18-
performance-*,
19-
portability-*,
20-
readability-*,
2118
-readability-magic-numbers,
2219
-readability-uppercase-literal-suffix'
2320

.github/ISSUE_TEMPLATE/Feature_request.md

-16
This file was deleted.

.github/ISSUE_TEMPLATE/config.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
blank_issues_enabled: false
2+
contact_links:
3+
- name: Ask a question
4+
url: https://github.com/nlohmann/json/discussions
5+
about: Ask questions and discuss with other community members

.github/ISSUE_TEMPLATE/question.md

-40
This file was deleted.

.github/workflows/codeql-analysis.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: "Code scanning - action"
2+
3+
on:
4+
push:
5+
branches: [develop, ]
6+
pull_request:
7+
# The branches below must be a subset of the branches above
8+
branches: [develop]
9+
schedule:
10+
- cron: '0 19 * * 1'
11+
12+
jobs:
13+
CodeQL-Build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v2
20+
with:
21+
# We must fetch at least the immediate parents so that if this is
22+
# a pull request then we can checkout the head.
23+
fetch-depth: 2
24+
25+
# If this run was triggered by a pull request event, then checkout
26+
# the head of the pull request instead of the merge commit.
27+
- run: git checkout HEAD^2
28+
if: ${{ github.event_name == 'pull_request' }}
29+
30+
# Initializes the CodeQL tools for scanning.
31+
- name: Initialize CodeQL
32+
uses: github/codeql-action/init@v1
33+
# Override language selection by uncommenting this and choosing your languages
34+
# with:
35+
# languages: go, javascript, csharp, python, cpp, java
36+
37+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
38+
# If this step fails, then you should remove it and run the build manually (see below)
39+
- name: Autobuild
40+
uses: github/codeql-action/autobuild@v1
41+
42+
# ℹ️ Command-line programs to run using the OS shell.
43+
# 📚 https://git.io/JvXDl
44+
45+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
46+
# and modify them (or add more) to build your code if your project
47+
# uses a compiled language
48+
49+
#- run: |
50+
# make bootstrap
51+
# make release
52+
53+
- name: Perform CodeQL Analysis
54+
uses: github/codeql-action/analyze@v1

CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ include(ExternalProject)
1515
##
1616
## OPTIONS
1717
##
18+
19+
if (POLICY CMP0077)
20+
# Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory.
21+
cmake_policy(SET CMP0077 NEW)
22+
endif ()
23+
1824
option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ON)
1925
option(JSON_Install "Install CMake targets during install step." ON)
2026
option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF)
@@ -73,6 +79,12 @@ if (MSVC)
7379
)
7480
endif()
7581

82+
# Install a pkg-config file, so other tools can find this.
83+
CONFIGURE_FILE(
84+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in"
85+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
86+
)
87+
7688
##
7789
## TESTS
7890
## create and configure the unit test target
@@ -133,4 +145,8 @@ endif()
133145
NAMESPACE ${PROJECT_NAME}::
134146
DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
135147
)
148+
install(
149+
FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc"
150+
DESTINATION lib/pkgconfig
151+
)
136152
endif()

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ doctest:
9797
# -Wno-exit-time-destructors: warning in json code triggered by NLOHMANN_JSON_SERIALIZE_ENUM
9898
# -Wno-float-equal: not all comparisons in the tests can be replaced by Approx
9999
# -Wno-keyword-macro: unit-tests use "#define private public"
100+
# -Wno-missing-prototypes: for NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
100101
# -Wno-padded: padding is nothing to warn about
101102
# -Wno-range-loop-analysis: items tests "for(const auto i...)"
102103
# -Wno-switch-enum -Wno-covered-switch-default: pedantic/contradicting warnings about switches
@@ -113,6 +114,7 @@ pedantic_clang:
113114
-Wno-exit-time-destructors \
114115
-Wno-float-equal \
115116
-Wno-keyword-macro \
117+
-Wno-missing-prototypes \
116118
-Wno-padded \
117119
-Wno-range-loop-analysis \
118120
-Wno-switch-enum -Wno-covered-switch-default \
@@ -253,7 +255,7 @@ pedantic_gcc:
253255
-Wmismatched-tags \
254256
-Wmissing-attributes \
255257
-Wmissing-braces \
256-
-Wmissing-declarations \
258+
-Wno-missing-declarations \
257259
-Wmissing-field-initializers \
258260
-Wmissing-include-dirs \
259261
-Wmissing-profile \

README.md

+57-8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [Integration](#integration)
2828
- [CMake](#cmake)
2929
- [Package Managers](#package-managers)
30+
- [Pkg-config](#pkg-config)
3031
- [Examples](#examples)
3132
- [JSON as first-class data type](#json-as-first-class-data-type)
3233
- [Serialization / Deserialization](#serialization--deserialization)
@@ -230,6 +231,20 @@ Please file issues [here](https://github.com/build2-packaging/nlohmann-json) if
230231

231232
If you are using [`wsjcpp`](https://wsjcpp.org), you can use the command `wsjcpp install "https://github.com/nlohmann/json:develop"` to get the latest version. Note you can change the branch ":develop" to an existing tag or another branch.
232233

234+
### Pkg-config
235+
236+
If you are using bare Makefiles, you can use `pkg-config` to generate the include flags that point to where the library is installed:
237+
238+
```sh
239+
pkg-config nlohmann_json --cflags
240+
```
241+
242+
Users of the Meson build system will also be able to use a system wide library, which will be found by `pkg-config`:
243+
244+
```meson
245+
json = dependency('nlohmann_json', required: true)
246+
```
247+
233248
## Examples
234249

235250
Beside the examples below, you may want to check the [documentation](https://nlohmann.github.io/json/) where each function contains a separate code example (e.g., check out [`emplace()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a5338e282d1d02bed389d852dd670d98d.html#a5338e282d1d02bed389d852dd670d98d)). All [example files](https://github.com/nlohmann/json/tree/develop/doc/examples) can be compiled and executed on their own (e.g., file [emplace.cpp](https://github.com/nlohmann/json/blob/develop/doc/examples/emplace.cpp)).
@@ -869,6 +884,42 @@ Some important things:
869884
* In function `from_json`, use function [`at()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a93403e803947b86f4da2d1fb3345cf2c.html#a93403e803947b86f4da2d1fb3345cf2c) to access the object values rather than `operator[]`. In case a key does not exist, `at` throws an exception that you can handle, whereas `operator[]` exhibits undefined behavior.
870885
* You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these.
871886

887+
#### Simplify your life with macros
888+
889+
If you just want to serialize/deserialize some structs, the `to_json`/`from_json` functions can be a lot of boilerplate.
890+
891+
There are two macros to make your life easier as long as you (1) want to use a JSON object as serialization and (2) want to use the member variable names as object keys in that object:
892+
893+
- `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(name, member1, member2, ...)` is to be defined inside of the namespace of the class/struct to create code for.
894+
- `NLOHMANN_DEFINE_TYPE_INTRUSIVE(name, member1, member2, ...)` is to be defined inside of the class/struct to create code for. This macro can also access private members.
895+
896+
In both macros, the first parameter is the name of the class/struct, and all remaining parameters name the members.
897+
898+
##### Examples
899+
900+
The `to_json`/`from_json` functions for the `person` struct above can be created with:
901+
902+
```cpp
903+
namespace ns {
904+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, address, age)
905+
}
906+
```
907+
908+
Here is an example with private members, where `NLOHMANN_DEFINE_TYPE_INTRUSIVE` is needed:
909+
910+
```cpp
911+
namespace ns {
912+
class address {
913+
private:
914+
std::string street;
915+
int housenumber;
916+
int postcode;
917+
918+
public:
919+
NLOHMANN_DEFINE_TYPE_INTRUSIVE(address, street, housenumber, postcode)
920+
};
921+
}
922+
```
872923

873924
#### How do I convert third-party types?
874925

@@ -1508,7 +1559,7 @@ The library supports **Unicode input** as follows:
15081559
15091560
### Comments in JSON
15101561
1511-
This library does not support comments. It does so for three reasons:
1562+
This library does not support comments by default. It does so for three reasons:
15121563
15131564
1. Comments are not part of the [JSON specification](https://tools.ietf.org/html/rfc8259). You may argue that `//` or `/* */` are allowed in JavaScript, but JSON is not JavaScript.
15141565
2. This was not an oversight: Douglas Crockford [wrote on this](https://plus.google.com/118095276221607585885/posts/RK8qyGVaGSr) in May 2012:
@@ -1519,11 +1570,7 @@ This library does not support comments. It does so for three reasons:
15191570
15201571
3. It is dangerous for interoperability if some libraries would add comment support while others don't. Please check [The Harmful Consequences of the Robustness Principle](https://tools.ietf.org/html/draft-iab-protocol-maintenance-01) on this.
15211572
1522-
This library will not support comments in the future. If you wish to use comments, I see three options:
1523-
1524-
1. Strip comments before using this library.
1525-
2. Use a different JSON library with comment support.
1526-
3. Use a format that natively supports comments (e.g., YAML or JSON5).
1573+
However, you can pass set parameter `ignore_comments` to true in the `parse` function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace.
15271574
15281575
### Order of object keys
15291576
@@ -1542,7 +1589,7 @@ Here is a related issue [#1924](https://github.com/nlohmann/json/issues/1924).
15421589
15431590
### Further notes
15441591
1545-
- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](https://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a233b02b0839ef798942dd46157cc0fe6.html#a233b02b0839ef798942dd46157cc0fe6) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a73ae333487310e3302135189ce8ff5d8.html#a73ae333487310e3302135189ce8ff5d8).
1592+
- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](https://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a233b02b0839ef798942dd46157cc0fe6.html#a233b02b0839ef798942dd46157cc0fe6) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a73ae333487310e3302135189ce8ff5d8.html#a73ae333487310e3302135189ce8ff5d8). Furthermore, you can define `JSON_ASSERT(x)` to replace calls to `assert(x)`.
15461593
- As the exact type of a number is not defined in the [JSON specification](https://tools.ietf.org/html/rfc8259.html), this library tries to choose the best fitting C++ number type automatically. As a result, the type `double` may be used to store numbers which may yield [**floating-point exceptions**](https://github.com/nlohmann/json/issues/181) in certain rare situations if floating-point exceptions have been unmasked in the calling code. These exceptions are not caused by the library and need to be fixed in the calling code, such as by re-masking the exceptions prior to calling library functions.
15471594
- The code can be compiled without C++ **runtime type identification** features; that is, you can use the `-fno-rtti` compiler flag.
15481595
- **Exceptions** are used widely within the library. They can, however, be switched off with either using the compiler flag `-fno-exceptions` or by defining the symbol `JSON_NOEXCEPTION`. In this case, exceptions are replaced by `abort()` calls. You can further control this behavior by defining `JSON_THROW_USER´` (overriding `throw`), `JSON_TRY_USER` (overriding `try`), and `JSON_CATCH_USER` (overriding `catch`). Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior.
@@ -1559,4 +1606,6 @@ $ cmake --build .
15591606
$ ctest --output-on-failure
15601607
```
15611608

1562-
For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml).
1609+
Note that during the `ctest` stage, several JSON test files are downloaded from an [external repository](https://github.com/nlohmann/json_test_data). If policies forbid downloading artifacts during testing, you can download the files yourself and pass the directory with the test files via `-DJSON_TestDataDirectory=path` to CMake. Then, no Internet connectivity is required. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information.
1610+
1611+
In case you have downloaded the library rather than checked out the code via Git, test `cmake_fetch_content_configure`. Please execute `ctest -LE git_required` to skip these tests. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information.

cmake/download_test_data.cmake

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
find_package(Git)
2-
31
set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data)
4-
set(JSON_TEST_DATA_VERSION 2.0.0)
5-
6-
# target to download test data
7-
add_custom_target(download_test_data
8-
COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1
9-
COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})"
10-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
11-
)
2+
set(JSON_TEST_DATA_VERSION 3.0.0)
123

13-
# create a header with the path to the downloaded test data
14-
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n")
4+
# if variable is set, use test data from given directory rather than downloading them
5+
if(JSON_TestDataDirectory)
6+
message(STATUS "Using test data in ${JSON_TestDataDirectory}.")
7+
add_custom_target(download_test_data)
8+
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TestDataDirectory}\"\n")
9+
else()
10+
find_package(Git)
11+
# target to download test data
12+
add_custom_target(download_test_data
13+
COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1
14+
COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})"
15+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
16+
)
17+
# create a header with the path to the downloaded test data
18+
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n")
19+
endif()
1520

1621
# determine the operating system (for debug and support purposes)
1722
find_program(UNAME_COMMAND uname)

cmake/pkg-config.pc.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Name: ${PROJECT_NAME}
2+
Description: JSON for Modern C++
3+
Version: ${PROJECT_VERSION}
4+
Cflags: -I${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}

0 commit comments

Comments
 (0)