Skip to content

Commit e082289

Browse files
committed
Fixed issue with json_type_traits specialization for optional
1 parent 9349dd2 commit e082289

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
v0.145.2
2+
--------
3+
4+
Defect fixes:
5+
6+
- Fixed issue with `json_type_traits` specialization for optional
7+
18
v0.145.1
29
--------
310

include/jsoncons/config/version.hpp

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

1212
#define JSONCONS_VERSION_MAJOR 0
1313
#define JSONCONS_VERSION_MINOR 145
14-
#define JSONCONS_VERSION_PATCH 1
14+
#define JSONCONS_VERSION_PATCH 2
1515

1616
namespace jsoncons {
1717

include/jsoncons/json_type_traits.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ struct json_type_traits<Json, jsoncons::optional<T>,
886886

887887
static Json to_json(const jsoncons::optional<T>& val)
888888
{
889-
return val.has_value() ? Json::null() : Json(val.value());
889+
return val.has_value() ? Json(val.value()) : Json::null();
890890
}
891891
};
892892

tests/src/json_type_traits_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ TEST_CASE("test_byte_string_as_vector")
131131
CHECK('l' == bytes[3]);
132132
CHECK('o' == bytes[4]);
133133
}
134+
135+
TEST_CASE("jsoncons::json_type_traits<optional>")
136+
{
137+
std::vector<jsoncons::optional<int>> v = { 0,1,jsoncons::optional<int>{} };
138+
json j = v;
139+
std::cout << j << "\n";
140+
141+
REQUIRE(j.size() == 3);
142+
CHECK(j[0].as<int>() == 0);
143+
CHECK(j[1].as<int>() == 1);
144+
j[2].is_null();
145+
146+
CHECK(j[0].is<jsoncons::optional<int>>());
147+
CHECK_FALSE(j[0].is<jsoncons::optional<double>>());
148+
CHECK(j[1].is<jsoncons::optional<int>>());
149+
CHECK_FALSE(j[1].is<jsoncons::optional<double>>());
150+
CHECK(j[2].is<jsoncons::optional<int>>()); // null can be any optinal
151+
}
134152
/*
135153
TEST_CASE("test_own_vector")
136154
{

0 commit comments

Comments
 (0)