Skip to content

Commit 2aae6c9

Browse files
authored
Merge branch 'nlohmann:develop' into develop
2 parents 53a7a02 + 2134cb9 commit 2aae6c9

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

include/nlohmann/detail/macro_scope.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@
395395

396396
#define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
397397
#define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
398-
#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1);
398+
#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = !nlohmann_json_j.is_null() ? nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1) : nlohmann_json_default_obj.v1;
399399

400400
/*!
401401
@brief macro

single_include/nlohmann/json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2761,7 +2761,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP
27612761

27622762
#define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
27632763
#define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
2764-
#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1);
2764+
#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = !nlohmann_json_j.is_null() ? nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1) : nlohmann_json_default_obj.v1;
27652765

27662766
/*!
27672767
@brief macro

tests/src/unit-udt_macro.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,13 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU
551551
CHECK(p3.getName() == "");
552552
CHECK(p3.getAge() == 0);
553553
CHECK(p3.getMetadata() == nullptr);
554+
555+
// check default value in case of empty json
556+
const json j4;
557+
T p4 = j4.get<T>();
558+
CHECK(p4.getName() == "");
559+
CHECK(p4.getAge() == 0);
560+
CHECK(p4.getMetadata() == nullptr);
554561
}
555562
}
556563

0 commit comments

Comments
 (0)