Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2842a3a
Update customers and add Cloudback (#4797)
nlohmann May 27, 2025
6036609
Add proper C++20 module support
mikomikotaishi May 29, 2025
3ded69d
Should be OFF by default
mikomikotaishi May 29, 2025
8ffa701
Missing template parameters
mikomikotaishi May 29, 2025
ee2b417
Change acknowledgement description in README
mikomikotaishi May 29, 2025
bf2ff16
Should be OFF again
mikomikotaishi May 29, 2025
a027a27
Should have D in front of it
mikomikotaishi May 29, 2025
3bd7d0b
Remove acknowledgement
mikomikotaishi May 29, 2025
69ac4d1
Revert aliasing changes
mikomikotaishi May 29, 2025
1c0201a
Forgot to revert this one
mikomikotaishi May 29, 2025
a324ce4
Attempt to fix ci_module_cpp20 test
mikomikotaishi May 29, 2025
2f1c908
Attempt 2 at fix ci_module_cpp20 test
mikomikotaishi May 29, 2025
18a207b
This should fix the problems with testing modules
mikomikotaishi May 30, 2025
8fa4eae
Wrote build files to wrong target
mikomikotaishi May 30, 2025
1c4c535
This should be the fix, the module test works
mikomikotaishi May 31, 2025
8b857e4
Make changes as requested
mikomikotaishi May 31, 2025
964de69
Adjust warnings flags (#4801)
nlohmann May 31, 2025
92d0699
Update cmake/ci.cmake
mikomikotaishi Jun 1, 2025
d0898d5
Requested changes
mikomikotaishi Jun 1, 2025
bead746
Requested changes
mikomikotaishi Jun 1, 2025
3ed466f
Use CMAKE_CURRENT_BINARY_DIR
mikomikotaishi Jun 2, 2025
651390d
:arrow_up: Bump mkdocs-git-revision-date-localized-plugin (#4800)
dependabot[bot] Jun 1, 2025
7785bfb
Add Xcode 16.3 (#4803)
nlohmann Jun 1, 2025
9a1d51a
Encode infinity and NaN as float for MsgPack and CBOR (#4802)
nlohmann Jun 1, 2025
e90a3f5
Use binary_t::value_type (#4805)
nlohmann Jun 2, 2025
0617521
:arrow_up: Bump ossf/scorecard-action from 2.4.1 to 2.4.2 (#4807)
dependabot[bot] Jun 2, 2025
572a257
:arrow_up: Bump step-security/harden-runner from 2.12.0 to 2.12.1 (#4…
dependabot[bot] Jun 11, 2025
728cc78
:arrow_up: Bump lukka/get-cmake from 4.0.2 to 4.0.3 (#4820)
dependabot[bot] Jun 16, 2025
33eee5a
:bug: add missing header to input_adapters.hpp (#4830)
ddkilzer Jun 25, 2025
b590847
Simpler using statement for cleaner module API
mikomikotaishi Jun 30, 2025
0834e67
Merge branch 'develop' into develop
mikomikotaishi Jun 30, 2025
e421af4
Missing newline in list
mikomikotaishi Jun 30, 2025
2afacab
Merge branch 'nlohmann:develop' into develop
mikomikotaishi Jul 1, 2025
a27148f
Add proper C++20 module support
mikomikotaishi May 29, 2025
f585024
Revert aliasing changes
mikomikotaishi May 29, 2025
6523eef
Attempt to fix ci_module_cpp20 test
mikomikotaishi May 29, 2025
ed664ee
Attempt 2 at fix ci_module_cpp20 test
mikomikotaishi May 29, 2025
6920e07
This should fix the problems with testing modules
mikomikotaishi May 30, 2025
892c21c
Wrote build files to wrong target
mikomikotaishi May 30, 2025
bb9f397
This should be the fix, the module test works
mikomikotaishi May 31, 2025
9e95ec2
Make changes as requested
mikomikotaishi May 31, 2025
ce9e815
Requested changes
mikomikotaishi Jun 1, 2025
6ea1762
Encode infinity and NaN as float for MsgPack and CBOR (#4802)
nlohmann Jun 1, 2025
3da5741
:arrow_up: Bump step-security/harden-runner from 2.12.0 to 2.12.1 (#4…
dependabot[bot] Jun 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
<<<<<<< HEAD
uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 # v2.12.2
=======
uses: step-security/harden-runner@002fdce3c6a235733a90a27c80493a3241e56863 # v2.12.1
>>>>>>> 572a2572 (:arrow_up: Bump step-security/harden-runner from 2.12.0 to 2.12.1 (#4815))
with:
egress-policy: audit

Expand Down Expand Up @@ -252,7 +256,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
<<<<<<< HEAD
uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 # v2.12.2
=======
uses: step-security/harden-runner@002fdce3c6a235733a90a27c80493a3241e56863 # v2.12.1
>>>>>>> 572a2572 (:arrow_up: Bump step-security/harden-runner from 2.12.0 to 2.12.1 (#4815))
with:
egress-policy: audit

Expand All @@ -273,7 +281,11 @@ jobs:
target: [ci_test_examples, ci_test_build_documentation]
steps:
- name: Harden Runner
<<<<<<< HEAD
uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 # v2.12.2
=======
uses: step-security/harden-runner@002fdce3c6a235733a90a27c80493a3241e56863 # v2.12.1
>>>>>>> 572a2572 (:arrow_up: Bump step-security/harden-runner from 2.12.0 to 2.12.1 (#4815))
with:
egress-policy: audit

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ If using modules (enabled with `NLOHMANN_JSON_BUILD_MODULES`), this example beco
import std;
import nlohmann.json;

using nlohmann::json;

// ...

std::ifstream f("example.json");
json data = json::parse(f);
```

If using modules (enabled with `NLOHMANN_JSON_BUILD_MODULES`), this example becomes:
```cpp
import std;
import nlohmann.json;

using json = nlohmann::json;

// ...
Expand Down Expand Up @@ -1758,6 +1771,7 @@ I deeply appreciate the help of the following people.
382. [bitFiedler](https://github.com/bitFiedler) made GDB pretty printer work with Python 3.8.
383. [Gianfranco Costamagna](https://github.com/LocutusOfBorg) fixed a compiler warning.
384. [risa2000](https://github.com/risa2000) made `std::filesystem::path` conversion to/from UTF-8 encoded string explicit.
385. [mikomikotaishi](https://github.com/mikomikotaishi) adding module support.

Thanks a lot for helping out! Please [let me know](mailto:mail@nlohmann.me) if I forgot someone.

Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs/docs/features/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ json data = json::parse(f);

## Modules do not export macros
It should be noted that as modules do not export macros, the `nlohmann.json` module will not export any macros, but rather only the following symbols:

- `nlohmann::adl_serializer`
- `nlohmann::basic_json`
- `nlohmann::json`
Expand Down
14 changes: 14 additions & 0 deletions modules/json.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module;

#include <nlohmann/json.hpp>

export module nlohmann.json;

export namespace nlohmann {
using ::nlohmann::adl_serializer;
using ::nlohmann::basic_json;
using ::nlohmann::json;
using ::nlohmann::json_pointer;
using ::nlohmann::ordered_json;
using ::nlohmann::ordered_map;
} // namespace nlohmann
18 changes: 5 additions & 13 deletions src/modules/json.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ module;
export module nlohmann.json;

export namespace nlohmann {
template <typename T = void, typename SFINAE = void>
using adl_serializer = ::nlohmann::adl_serializer<T, SFINAE>;

using basic_json = ::nlohmann::basic_json<>;

using json = ::nlohmann::json;

template <typename RefStringType>
using json_pointer = ::nlohmann::json_pointer<RefStringType>;

using ::nlohmann::adl_serializer;
using ::nlohmann::basic_json;
using ::nlohmann::json;
using ::nlohmann::json_pointer;
using ::nlohmann::ordered_json;

template <class Key, class T, class IgnoredLess, class Allocator>
using ordered_map = ::nlohmann::ordered_map<Key, T, IgnoredLess, Allocator>;
using ::nlohmann::ordered_map;
} // namespace nlohmann
67 changes: 67 additions & 0 deletions tests/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,4 +1188,71 @@ TEST_CASE_TEMPLATE("issue #4798 - nlohmann::json::to_msgpack() encode float NaN
CHECK(json::from_cbor(cbor_z_3).get<T>() == -std::numeric_limits<T>::infinity());
}

TEST_CASE_TEMPLATE("issue #4798 - nlohmann::json::to_msgpack() encode float NaN as double", T, double, float) // NOLINT(readability-math-missing-parentheses)
{
// With issue #4798, we encode NaN, infinity, and -infinity as float instead
// of double to allow for smaller encodings.
const json jx = std::numeric_limits<T>::quiet_NaN();
const json jy = std::numeric_limits<T>::infinity();
const json jz = -std::numeric_limits<T>::infinity();

/////////////////////////////////////////////////////////////////////////
// MessagePack
/////////////////////////////////////////////////////////////////////////

// expected MessagePack values
const std::vector<std::uint8_t> msgpack_x = {{0xCA, 0x7F, 0xC0, 0x00, 0x00}};
const std::vector<std::uint8_t> msgpack_y = {{0xCA, 0x7F, 0x80, 0x00, 0x00}};
const std::vector<std::uint8_t> msgpack_z = {{0xCA, 0xFF, 0x80, 0x00, 0x00}};

CHECK(json::to_msgpack(jx) == msgpack_x);
CHECK(json::to_msgpack(jy) == msgpack_y);
CHECK(json::to_msgpack(jz) == msgpack_z);

CHECK(std::isnan(json::from_msgpack(msgpack_x).get<T>()));
CHECK(json::from_msgpack(msgpack_y).get<T>() == std::numeric_limits<T>::infinity());
CHECK(json::from_msgpack(msgpack_z).get<T>() == -std::numeric_limits<T>::infinity());

// Make sure the other MessagePakc encodings for NaN, infinity, and
// -infinity are still supported.
const std::vector<std::uint8_t> msgpack_x_2 = {{0xCB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
const std::vector<std::uint8_t> msgpack_y_2 = {{0xCB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
const std::vector<std::uint8_t> msgpack_z_2 = {{0xCB, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
CHECK(std::isnan(json::from_msgpack(msgpack_x_2).get<T>()));
CHECK(json::from_msgpack(msgpack_y_2).get<T>() == std::numeric_limits<T>::infinity());
CHECK(json::from_msgpack(msgpack_z_2).get<T>() == -std::numeric_limits<T>::infinity());

/////////////////////////////////////////////////////////////////////////
// CBOR
/////////////////////////////////////////////////////////////////////////

// expected CBOR values
const std::vector<std::uint8_t> cbor_x = {{0xF9, 0x7E, 0x00}};
const std::vector<std::uint8_t> cbor_y = {{0xF9, 0x7C, 0x00}};
const std::vector<std::uint8_t> cbor_z = {{0xF9, 0xfC, 0x00}};

CHECK(json::to_cbor(jx) == cbor_x);
CHECK(json::to_cbor(jy) == cbor_y);
CHECK(json::to_cbor(jz) == cbor_z);

CHECK(std::isnan(json::from_cbor(cbor_x).get<T>()));
CHECK(json::from_cbor(cbor_y).get<T>() == std::numeric_limits<T>::infinity());
CHECK(json::from_cbor(cbor_z).get<T>() == -std::numeric_limits<T>::infinity());

// Make sure the other CBOR encodings for NaN, infinity, and -infinity are
// still supported.
const std::vector<std::uint8_t> cbor_x_2 = {{0xFA, 0x7F, 0xC0, 0x00, 0x00}};
const std::vector<std::uint8_t> cbor_y_2 = {{0xFA, 0x7F, 0x80, 0x00, 0x00}};
const std::vector<std::uint8_t> cbor_z_2 = {{0xFA, 0xFF, 0x80, 0x00, 0x00}};
const std::vector<std::uint8_t> cbor_x_3 = {{0xFB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
const std::vector<std::uint8_t> cbor_y_3 = {{0xFB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
const std::vector<std::uint8_t> cbor_z_3 = {{0xFB, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
CHECK(std::isnan(json::from_cbor(cbor_x_2).get<T>()));
CHECK(json::from_cbor(cbor_y_2).get<T>() == std::numeric_limits<T>::infinity());
CHECK(json::from_cbor(cbor_z_2).get<T>() == -std::numeric_limits<T>::infinity());
CHECK(std::isnan(json::from_cbor(cbor_x_3).get<T>()));
CHECK(json::from_cbor(cbor_y_3).get<T>() == std::numeric_limits<T>::infinity());
CHECK(json::from_cbor(cbor_z_3).get<T>() == -std::numeric_limits<T>::infinity());
}

DOCTEST_CLANG_SUPPRESS_WARNING_POP