Skip to content

Clean up JSON utils in a few ways#14351

Merged
Ericson2314 merged 1 commit intoNixOS:masterfrom
obsidiansystems:json-project-reference
Oct 25, 2025
Merged

Clean up JSON utils in a few ways#14351
Ericson2314 merged 1 commit intoNixOS:masterfrom
obsidiansystems:json-project-reference

Conversation

@Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Oct 24, 2025

Motivation

In particular

  • Remove get, it is redundant with valueAt and the get in util.hh.

  • Remove nullableValueAt. It is morally just the function composition getNullable . valueAt, not an orthogonal combinator like the others.

  • optionalValueAt return a pointer, not std::optional. This also expresses optionality, but without creating a needless copy. This brings it in line with the other combinators which also return references.

  • Delete valueAt and optionalValueAt taking the map by value, as we did for get in 408c09a, which prevents bugs / unnecessary copies.

Context

adl_serializer<DerivationOptions::OutputChecks>::from_json was the one use of getNullable. I give it a little static function for the ultimate creation of a std::optional it does need to do (after switching it to using getNullable . valueAt. That could go in json-utils.hh eventually, but I didn't bother for now since only one things needs it.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@Ericson2314 Ericson2314 requested a review from edolstra as a code owner October 24, 2025 22:07
@Ericson2314 Ericson2314 marked this pull request as draft October 24, 2025 22:16
@Ericson2314 Ericson2314 force-pushed the json-project-reference branch 2 times, most recently from 21a27c3 to b0b9739 Compare October 25, 2025 00:45
@Ericson2314 Ericson2314 marked this pull request as ready for review October 25, 2025 00:45
@github-actions github-actions bot added the fetching Networking with the outside (non-Nix) world, input locking label Oct 25, 2025
Comment on lines 33 to 36
/**
* Prevents bugs; see `get` for the same trick.
*/
const nlohmann::json * optionalValueAt(const nlohmann::json::object_t && value, const std::string & key) = delete;
Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, it is the same problem as before. This allowed me to fix it.

(Really, this whole function is just an alias for get, and could be deleted.)

@Ericson2314 Ericson2314 force-pushed the json-project-reference branch from b0b9739 to e11f331 Compare October 25, 2025 16:33
Comment on lines 36 to 37
const nlohmann::json & valueAt(const nlohmann::json::object_t && map, std::string_view key) = delete;
const nlohmann::json * optionalValueAt(const nlohmann::json::object_t && value, std::string_view key) = delete;
Copy link
Member Author

@Ericson2314 Ericson2314 Oct 25, 2025

Choose a reason for hiding this comment

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

Did the delete trick for both of them now

Comment on lines +25 to +31
const nlohmann::json & valueAt(const nlohmann::json::object_t & map, std::string_view key);

std::optional<nlohmann::json> optionalValueAt(const nlohmann::json::object_t & value, const std::string & key);
std::optional<nlohmann::json> nullableValueAt(const nlohmann::json::object_t & value, const std::string & key);
/**
* @return A pointer to the value assiocated with `key` if `value`
* contains `key`, otherwise return `nullptr` (not JSON `null`!).
*/
const nlohmann::json * optionalValueAt(const nlohmann::json::object_t & value, std::string_view key);
Copy link
Member Author

Choose a reason for hiding this comment

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

Using std::string_view is just better, and made GCC happy

@Ericson2314 Ericson2314 force-pushed the json-project-reference branch from 668470a to 88a34c9 Compare October 25, 2025 18:46
In particular

- Remove `get`, it is redundant with `valueAt` and the `get` in
  `util.hh`.

- Remove `nullableValueAt`. It is morally just the function composition
  `getNullable . valueAt`, not an orthogonal combinator like the others.

- `optionalValueAt` return a pointer, not `std::optional`. This also
  expresses optionality, but without creating a needless copy. This
  brings it in line with the other combinators which also return
  references.

- Delete `valueAt` and `optionalValueAt` taking the map by value, as we
  did for `get` in 408c09a, which
  prevents bugs / unnecessary copies.

`adl_serializer<DerivationOptions::OutputChecks>::from_json` was the one
use of `getNullable`. I give it a little static function for the
ultimate creation of a `std::optional` it does need to do (after
switching it to using `getNullable . valueAt`. That could go in
`json-utils.hh` eventually, but I didn't bother for now since only one
things needs it.

Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
@Ericson2314 Ericson2314 force-pushed the json-project-reference branch from 88a34c9 to 0f0d925 Compare October 25, 2025 18:50
@Ericson2314 Ericson2314 enabled auto-merge October 25, 2025 19:21
@Ericson2314 Ericson2314 added this pull request to the merge queue Oct 25, 2025
Merged via the queue into NixOS:master with commit bef3c37 Oct 25, 2025
16 checks passed
@Ericson2314 Ericson2314 deleted the json-project-reference branch October 25, 2025 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fetching Networking with the outside (non-Nix) world, input locking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants