Skip to content

Commit

Permalink
Merge branch 'nlohmann:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lformaggia authored Aug 15, 2023
2 parents ddeddc4 + 5d27543 commit 943b1d3
Show file tree
Hide file tree
Showing 45 changed files with 2,019 additions and 1,397 deletions.
4 changes: 4 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Checks: '*,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-union-access,
-cppcoreguidelines-rvalue-reference-param-not-moved,
-cppcoreguidelines-virtual-class-destructor,
-fuchsia-default-arguments-calls,
-fuchsia-default-arguments-declarations,
Expand All @@ -35,10 +36,13 @@ Checks: '*,
-llvm-header-guard,
-llvm-include-order,
-llvmlibc-*,
-misc-use-anonymous-namespace,
-misc-confusable-identifiers,
-misc-include-cleaner,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-modernize-concat-nested-namespaces,
-modernize-type-traits,
-modernize-use-nodiscard,
-modernize-use-trailing-return-type,
-readability-function-cognitive-complexity,
Expand Down
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To make changes, you need to edit the following files:

1. [`include/nlohmann/*`](https://github.com/nlohmann/json/tree/develop/include/nlohmann) - These files are the sources of the library. Before testing or creating a pull request, execute `make amalgamate` to regenerate `single_include/nlohmann/json.hpp` and `single_include/nlohmann/json_fwd.hpp`.

2. [`tests/src/unit-*.cpp`](https://github.com/nlohmann/json/tree/develop/tests/src) - These files contain the [doctest](https://github.com/onqtam/doctest) unit tests which currently cover [100 %](https://coveralls.io/github/nlohmann/json) of the library's code.
2. [`tests/src/unit-*.cpp`](https://github.com/nlohmann/json/tree/develop/tests/src) - These files contain the [doctest](https://github.com/onqtam/doctest) unit tests which currently cover [100 %](https://coveralls.io/github/nlohmann/json) of the library's code. Before creating a pull request, execute `make pretty` to make sure that the style is correct, as this will be checked by the CI.

If you add or change a feature, please also add a unit test to this file. The unit tests can be compiled and executed with

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
standard: [11, 14, 17, 20]
standard: [11, 14, 17, 20, 23]

steps:
- uses: actions/checkout@v3
Expand Down
24 changes: 20 additions & 4 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,35 @@ jobs:
- name: Build
run: cmake --build build --target ci_test_compiler_${{ matrix.compiler }}

ci_test_standards:
ci_test_standards_gcc:
runs-on: ubuntu-latest
container: ghcr.io/nlohmann/json-ci:v2.4.0
strategy:
matrix:
standard: [11, 14, 17, 20]
compiler: [gcc, clang]
standard: [11, 14, 17, 20, 23]
steps:
- uses: actions/checkout@v3
- name: Run CMake
run: cmake -S . -B build -DJSON_CI=On
- name: Build
run: cmake --build build --target ci_test_${{ matrix.compiler }}_cxx${{ matrix.standard }}
run: cmake --build build --target ci_test_gcc_cxx${{ matrix.standard }}

ci_test_standards_clang:
runs-on: ubuntu-latest
container: silkeh/clang:latest
strategy:
matrix:
standard: [11, 14, 17, 20, 23]
steps:
- name: Install git and unzip
run: apt-get update ; apt-get install -y git unzip
- uses: actions/checkout@v3
- name: Get latest CMake and ninja
uses: lukka/get-cmake@latest
- name: Run CMake
run: cmake -S . -B build -DJSON_CI=On
- name: Build
run: cmake --build build --target ci_test_clang_cxx${{ matrix.standard }}

ci_cuda_example:
runs-on: ubuntu-latest
Expand Down
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[![Documentation](https://img.shields.io/badge/docs-mkdocs-blue.svg)](https://json.nlohmann.me)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nlohmann/json/master/LICENSE.MIT)
[![GitHub Releases](https://img.shields.io/github/release/nlohmann/json.svg)](https://github.com/nlohmann/json/releases)
[![Vcpkg Version](https://img.shields.io/vcpkg/v/nlohmann-json)](https://vcpkg.link/ports/nlohmann-json)
[![Packaging status](https://repology.org/badge/tiny-repos/nlohmann-json.svg)](https://repology.org/project/nlohmann-json/versions)
[![GitHub Downloads](https://img.shields.io/github/downloads/nlohmann/json/total)](https://github.com/nlohmann/json/releases)
[![GitHub Issues](https://img.shields.io/github/issues/nlohmann/json.svg)](https://github.com/nlohmann/json/issues)
Expand Down Expand Up @@ -85,6 +86,7 @@ You can sponsor this library at [GitHub Sponsors](https://github.com/sponsors/nl
- [Steve Sperandeo](https://github.com/homer6)
- [Robert Jefe Lindstädt](https://github.com/eljefedelrodeodeljefe)
- [Steve Wagner](https://github.com/ciroque)
- [Lion Yang](https://github.com/LionNatsu)

Thanks everyone!

Expand Down Expand Up @@ -300,7 +302,7 @@ Note the difference between serialization and assignment:
json j_string = "this is a string";

// retrieve the string value
auto cpp_string = j_string.get<std::string>();
auto cpp_string = j_string.template get<std::string>();
// retrieve the string value (alternative when a variable already exists)
std::string cpp_string2;
j_string.get_to(cpp_string2);
Expand All @@ -309,7 +311,7 @@ j_string.get_to(cpp_string2);
std::string serialized_string = j_string.dump();

// output of original string
std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.get<std::string>() << '\n';
std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.template get<std::string>() << '\n';
// output of serialized value
std::cout << j_string << " == " << serialized_string << std::endl;
```
Expand Down Expand Up @@ -482,7 +484,7 @@ for (auto& element : j) {
}
// getter/setter
const auto tmp = j[0].get<std::string>();
const auto tmp = j[0].template get<std::string>();
j[1] = 42;
bool foo = j.at(2);
Expand Down Expand Up @@ -690,7 +692,7 @@ You can switch off implicit conversions by defining `JSON_USE_IMPLICIT_CONVERSIO
// strings
std::string s1 = "Hello, world!";
json js = s1;
auto s2 = js.get<std::string>();
auto s2 = js.template get<std::string>();
// NOT RECOMMENDED
std::string s3 = js;
std::string s4;
Expand All @@ -699,7 +701,7 @@ s4 = js;
// Booleans
bool b1 = true;
json jb = b1;
auto b2 = jb.get<bool>();
auto b2 = jb.template get<bool>();
// NOT RECOMMENDED
bool b3 = jb;
bool b4;
Expand All @@ -708,7 +710,7 @@ b4 = jb;
// numbers
int i = 42;
json jn = i;
auto f = jn.get<double>();
auto f = jn.template get<double>();
// NOT RECOMMENDED
double f2 = jb;
double f3;
Expand Down Expand Up @@ -751,9 +753,9 @@ j["age"] = p.age;

// convert from JSON: copy each value from the JSON object
ns::person p {
j["name"].get<std::string>(),
j["address"].get<std::string>(),
j["age"].get<int>()
j["name"].template get<std::string>(),
j["address"].template get<std::string>(),
j["age"].template get<int>()
};
```
Expand All @@ -770,7 +772,7 @@ std::cout << j << std::endl;
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
// conversion: json -> person
auto p2 = j.get<ns::person>();
auto p2 = j.template get<ns::person>();
// that's it
assert(p == p2);
Expand All @@ -797,13 +799,13 @@ namespace ns {
```

That's all! When calling the `json` constructor with your type, your custom `to_json` method will be automatically called.
Likewise, when calling `get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.
Likewise, when calling `template get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.

Some important things:

* Those methods **MUST** be in your type's namespace (which can be the global namespace), or the library will not be able to locate them (in this example, they are in namespace `ns`, where `person` is defined).
* Those methods **MUST** be available (e.g., proper headers must be included) everywhere you use these conversions. Look at [issue 1108](https://github.com/nlohmann/json/issues/1108) for errors that may occur otherwise.
* When using `get<your_type>()`, `your_type` **MUST** be [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). (There is a way to bypass this requirement described later.)
* When using `template get<your_type>()`, `your_type` **MUST** be [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). (There is a way to bypass this requirement described later.)
* In function `from_json`, use function [`at()`](https://json.nlohmann.me/api/basic_json/at/) 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.
* You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these.

Expand Down Expand Up @@ -888,8 +890,8 @@ namespace nlohmann {
if (j.is_null()) {
opt = boost::none;
} else {
opt = j.get<T>(); // same as above, but with
// adl_serializer<T>::from_json
opt = j.template get<T>(); // same as above, but with
// adl_serializer<T>::from_json
}
}
};
Expand All @@ -916,7 +918,7 @@ namespace nlohmann {
// note: the return type is no longer 'void', and the method only takes
// one argument
static move_only_type from_json(const json& j) {
return {j.get<int>()};
return {j.template get<int>()};
}

// Here's the catch! You must provide a to_json method! Otherwise, you
Expand Down Expand Up @@ -1020,19 +1022,19 @@ assert(j == "stopped");
// json string to enum
json j3 = "running";
assert(j3.get<TaskState>() == TS_RUNNING);
assert(j3.template get<TaskState>() == TS_RUNNING);
// undefined json value to enum (where the first map entry above is the default)
json jPi = 3.14;
assert(jPi.get<TaskState>() == TS_INVALID );
assert(jPi.template get<TaskState>() == TS_INVALID );
```

Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
- `NLOHMANN_JSON_SERIALIZE_ENUM()` MUST be declared in your enum type's namespace (which can be the global namespace), or the library will not be able to locate it, and it will default to integer serialization.
- It MUST be available (e.g., proper headers must be included) everywhere you use the conversions.

Other Important points:
- When using `get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this default pair carefully.
- When using `template get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this default pair carefully.
- If an enum or JSON value is specified more than once in your map, the first matching occurrence from the top of the map will be returned when converting to or from JSON.

### Binary formats (BSON, CBOR, MessagePack, UBJSON, and BJData)
Expand Down
12 changes: 7 additions & 5 deletions cmake/ci.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ execute_process(COMMAND ${ASTYLE_TOOL} --version OUTPUT_VARIABLE ASTYLE_TOOL_VER
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" ASTYLE_TOOL_VERSION "${ASTYLE_TOOL_VERSION}")
message(STATUS "🔖 Artistic Style ${ASTYLE_TOOL_VERSION} (${ASTYLE_TOOL})")

find_program(CLANG_TOOL NAMES clang++-HEAD clang++-16 clang++-15 clang++-14 clang++-13 clang++-12 clang++-11 clang++)
find_program(CLANG_TOOL NAMES clang++-HEAD clang++ clang++-17 clang++-16 clang++-15 clang++-14 clang++-13 clang++-12 clang++-11 clang++)
execute_process(COMMAND ${CLANG_TOOL} --version OUTPUT_VARIABLE CLANG_TOOL_VERSION ERROR_VARIABLE CLANG_TOOL_VERSION)
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CLANG_TOOL_VERSION "${CLANG_TOOL_VERSION}")
message(STATUS "🔖 Clang ${CLANG_TOOL_VERSION} (${CLANG_TOOL})")

find_program(CLANG_TIDY_TOOL NAMES clang-tidy-15 clang-tidy-14 clang-tidy-13 clang-tidy-12 clang-tidy-11 clang-tidy)
find_program(CLANG_TIDY_TOOL NAMES clang-tidy-17 clang-tidy-16 clang-tidy-15 clang-tidy-14 clang-tidy-13 clang-tidy-12 clang-tidy-11 clang-tidy)
execute_process(COMMAND ${CLANG_TIDY_TOOL} --version OUTPUT_VARIABLE CLANG_TIDY_TOOL_VERSION ERROR_VARIABLE CLANG_TIDY_TOOL_VERSION)
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CLANG_TIDY_TOOL_VERSION "${CLANG_TIDY_TOOL_VERSION}")
message(STATUS "🔖 Clang-Tidy ${CLANG_TIDY_TOOL_VERSION} (${CLANG_TIDY_TOOL})")
Expand All @@ -30,7 +30,7 @@ execute_process(COMMAND ${CPPCHECK_TOOL} --version OUTPUT_VARIABLE CPPCHECK_TOOL
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CPPCHECK_TOOL_VERSION "${CPPCHECK_TOOL_VERSION}")
message(STATUS "🔖 Cppcheck ${CPPCHECK_TOOL_VERSION} (${CPPCHECK_TOOL})")

find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-12 g++-11 g++-10)
find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-13 g++-12 g++-11 g++-10)
execute_process(COMMAND ${GCC_TOOL} --version OUTPUT_VARIABLE GCC_TOOL_VERSION ERROR_VARIABLE GCC_TOOL_VERSION)
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GCC_TOOL_VERSION "${GCC_TOOL_VERSION}")
message(STATUS "🔖 GCC ${GCC_TOOL_VERSION} (${GCC_TOOL})")
Expand Down Expand Up @@ -95,6 +95,7 @@ file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/include/nlohmann/*.hpp)
# -Wno-extra-semi-stmt The library uses std::assert which triggers this warning.
# -Wno-padded We do not care about padding warnings.
# -Wno-covered-switch-default All switches list all cases and a default case.
# -Wno-unsafe-buffer-usage Otherwise Doctest would not compile.
# -Wno-weak-vtables The library is header-only.
# -Wreserved-identifier See https://github.com/onqtam/doctest/issues/536.

Expand All @@ -107,6 +108,7 @@ set(CLANG_CXXFLAGS
-Wno-extra-semi-stmt
-Wno-padded
-Wno-covered-switch-default
-Wno-unsafe-buffer-usage
-Wno-weak-vtables
-Wno-reserved-identifier
)
Expand Down Expand Up @@ -437,7 +439,7 @@ add_custom_target(ci_test_clang
# Different C++ Standards.
###############################################################################

foreach(CXX_STANDARD 11 14 17 20)
foreach(CXX_STANDARD 11 14 17 20 23)
add_custom_target(ci_test_gcc_cxx${CXX_STANDARD}
COMMAND CXX=${GCC_TOOL} CXXFLAGS="${GCC_CXXFLAGS}" ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=Debug -GNinja
Expand Down Expand Up @@ -898,7 +900,7 @@ add_custom_target(ci_cmake_flags
# Use more installed compilers.
###############################################################################

foreach(COMPILER g++-4.8 g++-4.9 g++-5 g++-6 g++-7 g++-8 g++-9 g++-10 g++-11 clang++-3.5 clang++-3.6 clang++-3.7 clang++-3.8 clang++-3.9 clang++-4.0 clang++-5.0 clang++-6.0 clang++-7 clang++-8 clang++-9 clang++-10 clang++-11 clang++-12 clang++-13 clang++-14)
foreach(COMPILER g++-4.8 g++-4.9 g++-5 g++-6 g++-7 g++-8 g++-9 g++-10 g++-11 clang++-3.5 clang++-3.6 clang++-3.7 clang++-3.8 clang++-3.9 clang++-4.0 clang++-5.0 clang++-6.0 clang++-7 clang++-8 clang++-9 clang++-10 clang++-11 clang++-12 clang++-13 clang++-14 clang++-15 clang++-16 clang++-17)
find_program(COMPILER_TOOL NAMES ${COMPILER})
if (COMPILER_TOOL)
unset(ADDITIONAL_FLAGS)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/from_json__default_constructible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main()
j["address"] = "744 Evergreen Terrace";
j["age"] = 60;

auto p = j.get<ns::person>();
auto p = j.template get<ns::person>();

std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
}
2 changes: 1 addition & 1 deletion docs/examples/from_json__non_default_constructible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main()
j["address"] = "744 Evergreen Terrace";
j["age"] = 60;

auto p = j.get<ns::person>();
auto p = j.template get<ns::person>();

std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
}
10 changes: 5 additions & 5 deletions docs/examples/get__PointerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ int main()
json value = 17;

// explicitly getting pointers
auto p1 = value.get<const json::number_integer_t*>();
auto p2 = value.get<json::number_integer_t*>();
auto p3 = value.get<json::number_integer_t* const>();
auto p4 = value.get<const json::number_integer_t* const>();
auto p5 = value.get<json::number_float_t*>();
auto p1 = value.template get<const json::number_integer_t*>();
auto p2 = value.template get<json::number_integer_t*>();
auto p3 = value.template get<json::number_integer_t* const>();
auto p4 = value.template get<const json::number_integer_t* const>();
auto p5 = value.template get<json::number_float_t*>();

// print the pointees
std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n';
Expand Down
16 changes: 8 additions & 8 deletions docs/examples/get__ValueType_const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ int main()
};

// use explicit conversions
auto v1 = json_types["boolean"].get<bool>();
auto v2 = json_types["number"]["integer"].get<int>();
auto v3 = json_types["number"]["integer"].get<short>();
auto v4 = json_types["number"]["floating-point"].get<float>();
auto v5 = json_types["number"]["floating-point"].get<int>();
auto v6 = json_types["string"].get<std::string>();
auto v7 = json_types["array"].get<std::vector<short>>();
auto v8 = json_types.get<std::unordered_map<std::string, json>>();
auto v1 = json_types["boolean"].template get<bool>();
auto v2 = json_types["number"]["integer"].template get<int>();
auto v3 = json_types["number"]["integer"].template get<short>();
auto v4 = json_types["number"]["floating-point"].template get<float>();
auto v5 = json_types["number"]["floating-point"].template get<int>();
auto v6 = json_types["string"].template get<std::string>();
auto v7 = json_types["array"].template get<std::vector<short>>();
auto v8 = json_types.template get<std::unordered_map<std::string, json>>();

// print the conversion results
std::cout << v1 << '\n';
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/nlohmann_define_type_intrusive_explicit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ int main()

// deserialization: json -> person
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json;
auto p2 = j2.get<ns::person>();
auto p2 = j2.template get<ns::person>();

// incomplete deserialization:
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json;
try
{
auto p3 = j3.get<ns::person>();
auto p3 = j3.template get<ns::person>();
}
catch (json::exception& e)
{
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/nlohmann_define_type_intrusive_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ int main()

// deserialization: json -> person
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json;
auto p2 = j2.get<ns::person>();
auto p2 = j2.template get<ns::person>();

// incomplete deserialization:
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json;
try
{
auto p3 = j3.get<ns::person>();
auto p3 = j3.template get<ns::person>();
}
catch (json::exception& e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ int main()

// deserialization: json -> person
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json;
auto p2 = j2.get<ns::person>();
auto p2 = j2.template get<ns::person>();

// incomplete deserialization:
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json;
auto p3 = j3.get<ns::person>();
auto p3 = j3.template get<ns::person>();
std::cout << "roundtrip: " << json(p3) << std::endl;
}
Loading

0 comments on commit 943b1d3

Please sign in to comment.