Skip to content

Commit 32f4d55

Browse files
committed
Fix #146.
1 parent f4e10b6 commit 32f4d55

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

include/tao/json/binding/internal/object.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,20 @@ namespace tao::json::binding::internal
136136
}
137137
}
138138

139+
template< typename A, template< typename... > class Traits, typename C >
140+
[[nodiscard]] static bool require_encode( const C& x )
141+
{
142+
return ( N == for_nothing_value::encode ) || ( A::kind == member_kind::required ) || ( !A::template is_nothing< Traits >( x ) );
143+
}
144+
139145
#if defined( _MSC_VER )
140146
#pragma warning( push )
141147
#pragma warning( disable : 4127 )
142148
#endif
143149
template< typename A, template< typename... > class Traits, typename C >
144150
static void assign_member( basic_value< Traits >& v, const C& x )
145151
{
146-
if( ( N == for_nothing_value::encode ) || ( !A::template is_nothing< Traits >( x ) ) ) {
152+
if( require_encode< A, Traits >( x ) ) {
147153
v.try_emplace( A::template key< Traits >(), A::read( x ) );
148154
}
149155
}
@@ -199,10 +205,7 @@ namespace tao::json::binding::internal
199205
template< template< typename... > class Traits, typename C >
200206
[[nodiscard]] static std::size_t produce_size( const C& x )
201207
{
202-
if constexpr( N == for_nothing_value::encode ) {
203-
return sizeof...( As );
204-
}
205-
return ( std::size_t( !As::template is_nothing< Traits >( x ) ) + ... );
208+
return ( std::size_t( require_encode< As, Traits >( x ) ) + ... );
206209
}
207210

208211
#if defined( _MSC_VER )
@@ -213,7 +216,7 @@ namespace tao::json::binding::internal
213216
template< typename A, template< typename... > class Traits, typename Consumer, typename C >
214217
static void produce_member( Consumer& consumer, const C& x )
215218
{
216-
if( ( N == for_nothing_value::encode ) || ( !A::template is_nothing< Traits >( x ) ) ) {
219+
if( require_encode< A, Traits >( x ) ) {
217220
A::template produce_key< Traits >( consumer );
218221
A::template produce< Traits >( consumer, x );
219222
consumer.member();

src/test/json/binding_object.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "test.hpp"
55

66
#include <tao/json.hpp>
7+
#include <tao/json/contrib/traits.hpp>
78

89
namespace tao::json
910
{
@@ -212,7 +213,26 @@ namespace tao::json
212213
TEST_ASSERT( s.i == 42 );
213214
}
214215

215-
// TODO: Test with different for_nothing_value (incl. consistency of size to consumer).
216+
struct type_7
217+
{
218+
std::vector< int > a;
219+
std::vector< int > b;
220+
};
221+
222+
template<>
223+
struct traits< type_7 >
224+
: binding::basic_object< binding::for_unknown_key::fail,
225+
binding::for_nothing_value::suppress,
226+
TAO_JSON_BIND_REQUIRED( "a", &type_7::a ),
227+
TAO_JSON_BIND_OPTIONAL( "b", &type_7::b ) >
228+
{};
229+
230+
void unit_test_7()
231+
{
232+
const type_7 t;
233+
const auto s = produce::to_string( t );
234+
TEST_ASSERT( s == "{\"a\":[]}" );
235+
}
216236

217237
void unit_test()
218238
{
@@ -222,6 +242,7 @@ namespace tao::json
222242
unit_test_4();
223243
unit_test_5();
224244
unit_test_6();
245+
unit_test_7();
225246
}
226247

227248
} // namespace tao::json

0 commit comments

Comments
 (0)