Skip to content

Add with_*_t alias templates to create basic_json types with changed template parameters - #5758

Merged
nlohmann merged 6 commits into
developfrom
feature/with-aliases-3898
Oct 6, 2026
Merged

nlohmann merged 6 commits into
developfrom
feature/with-aliases-3898

Conversation

@nlohmann

@nlohmann nlohmann commented Oct 4, 2026

Copy link
Copy Markdown
Owner

Adds member alias templates to basic_json that create a basic_json type with one of its template parameters replaced, so you don't have to spell out all eleven:

using my_json = nlohmann::json::with_string_t<my_string>;
// instead of
using my_json = nlohmann::basic_json<std::map, std::vector, my_string, bool, std::int64_t, std::uint64_t,
                                     double, std::allocator, nlohmann::adl_serializer, std::vector<std::uint8_t>>;

Aliases: with_object_t, with_array_t, with_string_t, with_boolean_t, with_integers_t<I, U>, with_float_t, with_allocator_t, with_json_serializer_t, with_binary_t, with_base_class_t. Together they cover all template parameters. They are members, so they also work from ordered_json or any other specialization and keep its other parameters.

This takes over #3898 by @barcode, which went stale. Their commit is included with them as author. On top of it, following the review in #3898:

  • with_changed_*_t is renamed to the shorter with_*_t.
  • The separate signed and unsigned integer aliases are merged into with_integers_t<I, U> (@gregmarr's suggestion), because the two are usually changed together.
  • New documentation page api/basic_json/with_t.md with an example, linked from the basic_json index and mkdocs.yml.
  • std::is_same tests for every alias in unit-udt.cpp, including one that checks ordered_json::with_string_t keeps ordered_map.
  • Tests and examples that spelled out the full parameter list now use the aliases.

Supersedes #3898. Thanks @barcode!

Public API

Only additions: ten new public member alias templates on basic_json. Nothing existing changes. No breaking changes.

🤖 Generated with Claude Code

@nlohmann
nlohmann force-pushed the feature/with-aliases-3898 branch from 126637c to ea72ffe Compare October 4, 2026 20:17
@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label Oct 4, 2026
Comment thread docs/mkdocs/mkdocs.yml Outdated
- 'update': api/basic_json/update.md
- 'value': api/basic_json/value.md
- 'value_t': api/basic_json/value_t.md
- 'with_t': api/basic_json/with_t.md

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems weird that this isn't indented. Intentional?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional, thanks — the entry ended up at the top level of nav. Fixed in 8ab8fa9.

(Written by Claude Code.)

@gregmarr

gregmarr commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Because this is a nested template type, these can chain. This is not obvious and should probably be called out with an example. Also, that it can start with ordered_json as well.

  using my_json = nlohmann::json::with_integers_t<int, unsigned int>::with_float_t<float>;

I did verify that nlohmann::json::with_object_t<nlohmann::ordered_map> and nlohmann::ordered_json are the same type, as least as far as typeid().name() is concerned. That's what I would expect.

@nlohmann

nlohmann commented Oct 5, 2026

Copy link
Copy Markdown
Owner Author

Good point, thanks for checking. 8ab8fa9 adds a paragraph to with_t.md explaining that the aliases are members of every basic_json specialization (including ordered_json), so they can be chained:

using my_json = nlohmann::json::with_integers_t<int, unsigned int>::with_float_t<float>;

It also notes that the order of chained aliases does not matter and that json::with_object_t<nlohmann::ordered_map> is ordered_json. These are now checked with std::is_same in unit-udt.cpp (both chaining orders, and json ↔ ordered_json in both directions).

(Written by Claude Code.)

barcode and others added 5 commits October 6, 2026 07:25
…odified template parameters

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
…d aliases

Per review discussion on #3898 between gregmarr and nlohmann:
- rename with_changed_X_t to with_X_t for brevity
- replace the separate with_changed_integer_t/with_changed_unsigned_t
  aliases with a single with_integers_t<NumberIntegerType2, NumberUnsignedType2>
- add @sa doc comment links for the upcoming documentation page

Co-authored-by: Raphael Grimm <1005058+barcode@users.noreply.github.com>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Add docs/mkdocs/docs/api/basic_json/with_t.md documenting with_object_t,
with_array_t, with_string_t, with_boolean_t, with_integers_t, with_float_t,
with_allocator_t, with_json_serializer_t, with_binary_t and with_base_class_t,
with an accompanying example, and link the page from the basic_json member
types list and the mkdocs navigation.

Co-authored-by: Raphael Grimm <1005058+barcode@users.noreply.github.com>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Check with std::is_same that each with_*_t alias produces the expected
basic_json type, and that with_string_t keeps nlohmann::ordered_map as
the object type when used on ordered_json.

Co-authored-by: Raphael Grimm <1005058+barcode@users.noreply.github.com>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Indent the with_t entry in mkdocs.yml so it is listed under basic_json,
explain that the aliases can be chained and work on ordered_json, and
test both, including json::with_object_t<ordered_map> == ordered_json.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
@nlohmann nlohmann added 🚀 ready to merge Ready to merge - just waiting for CI to complete. and removed review needed It would be great if someone could review the proposed changes. labels Oct 6, 2026
@nlohmann nlohmann added this to the Release 3.13.0 milestone Oct 6, 2026
@nlohmann
nlohmann merged commit 162e13b into develop Oct 6, 2026
22 of 168 checks passed
@nlohmann
nlohmann deleted the feature/with-aliases-3898 branch October 6, 2026 20:33
@gregmarr

gregmarr commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

@nlohmann Would you want to update the tests and examples to use this new functionality? If so, would you prefer nlohmann::basic_json::with_... or nlohmann::json::with_... and would you want to change ...basic_json<my_object_type to ...json::with_object_t<my_object_type>.

In looking at this, I found a few places where only integer_t was changed, and a few places that all three numbers were changed. There might be a case to have with_integer_t and with_numbers_t as well.

@nlohmann

nlohmann commented Oct 8, 2026

Copy link
Copy Markdown
Owner Author

Thanks! Yes, done in #5787. It uses nlohmann::json::with_… (basic_json would need <>), and that includes the short basic_json<my_object_type> forms. I left out with_numbers_t / with_integer_t for now: the five int32/uint32/float sites read fine as json::with_integers_t<std::int32_t, std::uint32_t>::with_float_t<float>, and the signed-only ones as with_integers_t<std::int32_t, std::uint64_t>. If more call sites show up, we can still add a with_numbers_t.

(Written by Claude Code on behalf of @nlohmann.)

@gregmarr

gregmarr commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

I left out with_numbers_t / with_integer_t for now

Sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation L 🚀 ready to merge Ready to merge - just waiting for CI to complete. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants