Skip to content

Commit 730cd5c

Browse files
authored
fix: Handle null values in GetJsonValue and its friends (#258)
The current JSON parsing logic in GetJsonValueOrDefault does not correctly handle cases where an optional field is present with an explicit null value. Now, if a key is present but its value is null, the function correctly returns the default value, treating it the same as a missing key.
1 parent b2b38af commit 730cd5c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/iceberg/util/json_util_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Result<T> GetJsonValueImpl(const nlohmann::json& json, std::string_view key) {
5757
template <typename T>
5858
Result<std::optional<T>> GetJsonValueOptional(const nlohmann::json& json,
5959
std::string_view key) {
60-
if (!json.contains(key)) {
60+
if (!json.contains(key) || json.at(key).is_null()) {
6161
return std::nullopt;
6262
}
6363
ICEBERG_ASSIGN_OR_RAISE(auto value, GetJsonValueImpl<T>(json, key));
@@ -66,7 +66,7 @@ Result<std::optional<T>> GetJsonValueOptional(const nlohmann::json& json,
6666

6767
template <typename T>
6868
Result<T> GetJsonValue(const nlohmann::json& json, std::string_view key) {
69-
if (!json.contains(key)) {
69+
if (!json.contains(key) || json.at(key).is_null()) {
7070
return JsonParseError("Missing '{}' in {}", key, SafeDumpJson(json));
7171
}
7272
return GetJsonValueImpl<T>(json, key);
@@ -75,7 +75,7 @@ Result<T> GetJsonValue(const nlohmann::json& json, std::string_view key) {
7575
template <typename T>
7676
Result<T> GetJsonValueOrDefault(const nlohmann::json& json, std::string_view key,
7777
T default_value = T{}) {
78-
if (!json.contains(key)) {
78+
if (!json.contains(key) || json.at(key).is_null()) {
7979
return default_value;
8080
}
8181
return GetJsonValueImpl<T>(json, key);

0 commit comments

Comments
 (0)