Skip to content

Add helper types to make it easier to create a basic_json type with modified template parameters - #3898

Closed
barcode wants to merge 1 commit into
nlohmann:developfrom
barcode:make_specifying_adapted_json_type_easier
Closed

barcode wants to merge 1 commit into
nlohmann:developfrom
barcode:make_specifying_adapted_json_type_easier

Conversation

@barcode

@barcode barcode commented Dec 26, 2022 •

Copy link
Copy Markdown
Contributor

(First part of the points listed in #3110 (comment), the rest is done in a different PR to prevent mixing of separate issues / features)

Make specifying a base class easier/less verbose.

This PR adds member templates to nlohmann::json which can be used to create a basic_json type with one replaced template parameter (e.g. nlohmann::json::with_changed_json_serializer_t<T>).

Tests and examples were modified to use those templates (except for regression tests).

Open questions from my side:

  • Should we add something like this? (I think so, but what is your opinion?)
  • Should we go with the current names (with_changed_*) or rather use something else (e.g. with_*)?

In case this will be added, i will write the documentation.


Pull request checklist

Read the Contribution Guidelines for detailed information.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header files single_include/nlohmann/json.hpp and single_include/nlohmann/json_fwd.hpp. The whole process is described here.

@coveralls

coveralls commented Dec 26, 2022 •

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same when pulling 0d9f46f on barcode:make_specifying_adapted_json_type_easier into 5d27543 on nlohmann:develop.

@barcode
barcode force-pushed the make_specifying_adapted_json_type_easier branch from dc4517a to 445e5ff Compare December 26, 2022 13:17
@barcode
barcode force-pushed the make_specifying_adapted_json_type_easier branch from 445e5ff to 134929c Compare January 31, 2023 18:28
@barcode

barcode commented Feb 1, 2023

Copy link
Copy Markdown
Contributor Author

Work on this PR is done and it only needs a review.

@raphael-grimm
raphael-grimm force-pushed the make_specifying_adapted_json_type_easier branch from 134929c to 0d9f46f Compare August 4, 2023 07:26
@github-actions

github-actions Bot commented Nov 2, 2025

Copy link
Copy Markdown

This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!

@github-actions github-actions Bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Nov 2, 2025
@nlohmann

Copy link
Copy Markdown
Owner

Sorry for letting this sit for so long, @barcode — this fell through the cracks, and it's not for lack of merit.

To answer your open questions:

  1. Yes, we'd like to add this. Spelling out all template parameters just to swap one of them is error-prone, and the aliases make these definitions much more readable.
  2. Naming: let's go with the shorter with_* form (e.g., json::with_string_t<alt_string>, json::with_json_serializer_t<my_serializer>).

If you're still interested, could you:

  • rebase onto develop (the header has changed quite a bit since 2022) and re-run make amalgamate,
  • rename the aliases as above, and
  • add documentation under docs/mkdocs/docs/api/basic_json/ (a single page covering all aliases is fine) and link it from the basic_json index and mkdocs.yml?

If you no longer have time for this, just let us know and we'll take it from here.

(This comment was written by Claude Code on behalf of @nlohmann.)

@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Sep 27, 2026

@gregmarr gregmarr left a comment

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.

I also wonder if there are any other common cases that replace two or three elements. I don't think we'd want to have ALL of those, but maybe add them as we find them actually useful.

Comment thread include/nlohmann/json.hpp

/// Json type using a different type for storing unsigned integers
template<class NumberUnsignedType2>
using with_changed_unsigned_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,

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.

I wonder how often someone changes integer but not unsigned or vice-versa. Should these two be combined into a single version that takes both?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What about with_integers_t<I, U>?

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.

Yes, I was thinking something like that.

@nlohmann

nlohmann commented Oct 4, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for this, @barcode! Since we didn't hear back, we continued in #5758. Your commit is included there with you as author. On top of it, the aliases were renamed to with_*_t, the integer aliases were merged into with_integers_t<I, U>, and docs and tests were added.

This comment was written by Claude Code on behalf of @nlohmann.

@nlohmann nlohmann closed this Oct 4, 2026
nlohmann added a commit that referenced this pull request Oct 4, 2026
…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>
nlohmann added a commit that referenced this pull request Oct 6, 2026
…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>
nlohmann added a commit that referenced this pull request Oct 6, 2026
…template parameters (#5758)

* Add helper types to make it easier to create a basic_json type with modified template parameters

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

* Rename with_changed_*_t aliases to with_*_t and merge integer/unsigned 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 documentation for the with_*_t member alias templates

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>

* Add tests for the with_*_t member alias templates

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>

* Fix with_t nav entry and document chaining of the with_*_t aliases

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>

* Add docset entry for basic_json::with_t

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

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Co-authored-by: barcode <barcode@example.com>
Co-authored-by: Raphael Grimm <1005058+barcode@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants