[Nix#9994] Improve checked JSON casting #58

Open
opened 2024-03-16 06:44:41 +00:00 by lix-bot · 0 comments
Member

Upstream-Issue: NixOS/nix#9994

In #8760, @iFreilicht added an ensureType function to improve our errors for JSON decoding ill-formed values. The errors our indeed better, but using it is cumbersome because has to remember to always first call ensureType with the right type, and then do the actual cast.

To make things a bit easier I propose this interface change:

--- a/src/libutil/json-utils.hh
+++ b/src/libutil/json-utils.hh
@@ -12,7 +12,7 @@ nlohmann::json * get(nlohmann::json & map, const std::string & key);

 /**
  * Get the value of a json object at a key safely, failing
- * with a Nix Error if the key does not exist.
+ * with a nice error if the key does not exist.
  *
  * Use instead of nlohmann::json::at() to avoid ugly exceptions.
  *
@@ -23,14 +23,18 @@ const nlohmann::json & valueAt(
     const std::string & key);

 /**
- * Ensure the type of a json object is what you expect, failing
- * with a Nix Error if it isn't.
+ * Downcast the json object, failing with a nice error if the conversion fails.
  *
- * Use before type conversions and element access to avoid ugly exceptions.
+ * See https://json.nlohmann.me/features/types/
  */
-const nlohmann::json & ensureType(
-    const nlohmann::json & value,
-    nlohmann::json::value_type expectedType);
+std::optional<nlohmann::json> getNullable(const nlohmann::json & value);
+const nlohmann::object_t & getObject(const nlohmann::json & value);
+const nlohmann::array_t & getArray(const nlohmann::json & value);
+const nlohmann::string_t & getString(const nlohmann::json & value);
+const nlohman::number_integer_t & getInteger(const nlohmann::json & value);
+const nlohman::number_unsigned_t & getNatural(const nlohmann::json & value);
+const nlohman::number_float_t & getFloat(const nlohmann::json & value);

 /**
  * For `adl_serializer<std::optional<T>>` below, we need to track what

It would be very nice if someone could change this interface (and update the current usages of ensureType to use these new functions instead).

Upstream-Issue: https://git.lix.systems/NixOS/nix/issues/9994 In #8760, @iFreilicht added an `ensureType` function to improve our errors for JSON decoding ill-formed values. The errors our indeed better, but using it is cumbersome because has to remember to always first call `ensureType` with the right type, and then do the actual cast. To make things a bit easier I propose this interface change: ```diff --- a/src/libutil/json-utils.hh +++ b/src/libutil/json-utils.hh @@ -12,7 +12,7 @@ nlohmann::json * get(nlohmann::json & map, const std::string & key); /** * Get the value of a json object at a key safely, failing - * with a Nix Error if the key does not exist. + * with a nice error if the key does not exist. * * Use instead of nlohmann::json::at() to avoid ugly exceptions. * @@ -23,14 +23,18 @@ const nlohmann::json & valueAt( const std::string & key); /** - * Ensure the type of a json object is what you expect, failing - * with a Nix Error if it isn't. + * Downcast the json object, failing with a nice error if the conversion fails. * - * Use before type conversions and element access to avoid ugly exceptions. + * See https://json.nlohmann.me/features/types/ */ -const nlohmann::json & ensureType( - const nlohmann::json & value, - nlohmann::json::value_type expectedType); +std::optional<nlohmann::json> getNullable(const nlohmann::json & value); +const nlohmann::object_t & getObject(const nlohmann::json & value); +const nlohmann::array_t & getArray(const nlohmann::json & value); +const nlohmann::string_t & getString(const nlohmann::json & value); +const nlohman::number_integer_t & getInteger(const nlohmann::json & value); +const nlohman::number_unsigned_t & getNatural(const nlohmann::json & value); +const nlohman::number_float_t & getFloat(const nlohmann::json & value); /** * For `adl_serializer<std::optional<T>>` below, we need to track what ``` It would be very nice if someone could change this interface (and update the current usages of `ensureType` to use these new functions instead).
lix-bot added the
devx
imported
labels 2024-03-16 06:44:41 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lix-project/lix#58
No description provided.