Skip to content

Commit

Permalink
Fix #146.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Sep 18, 2023
1 parent f4e10b6 commit 32f4d55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
15 changes: 9 additions & 6 deletions include/tao/json/binding/internal/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,20 @@ namespace tao::json::binding::internal
}
}

template< typename A, template< typename... > class Traits, typename C >
[[nodiscard]] static bool require_encode( const C& x )
{
return ( N == for_nothing_value::encode ) || ( A::kind == member_kind::required ) || ( !A::template is_nothing< Traits >( x ) );
}

#if defined( _MSC_VER )
#pragma warning( push )
#pragma warning( disable : 4127 )
#endif
template< typename A, template< typename... > class Traits, typename C >
static void assign_member( basic_value< Traits >& v, const C& x )
{
if( ( N == for_nothing_value::encode ) || ( !A::template is_nothing< Traits >( x ) ) ) {
if( require_encode< A, Traits >( x ) ) {
v.try_emplace( A::template key< Traits >(), A::read( x ) );
}
}
Expand Down Expand Up @@ -199,10 +205,7 @@ namespace tao::json::binding::internal
template< template< typename... > class Traits, typename C >
[[nodiscard]] static std::size_t produce_size( const C& x )
{
if constexpr( N == for_nothing_value::encode ) {
return sizeof...( As );
}
return ( std::size_t( !As::template is_nothing< Traits >( x ) ) + ... );
return ( std::size_t( require_encode< As, Traits >( x ) ) + ... );
}

#if defined( _MSC_VER )
Expand All @@ -213,7 +216,7 @@ namespace tao::json::binding::internal
template< typename A, template< typename... > class Traits, typename Consumer, typename C >
static void produce_member( Consumer& consumer, const C& x )
{
if( ( N == for_nothing_value::encode ) || ( !A::template is_nothing< Traits >( x ) ) ) {
if( require_encode< A, Traits >( x ) ) {
A::template produce_key< Traits >( consumer );
A::template produce< Traits >( consumer, x );
consumer.member();
Expand Down
23 changes: 22 additions & 1 deletion src/test/json/binding_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "test.hpp"

#include <tao/json.hpp>
#include <tao/json/contrib/traits.hpp>

namespace tao::json
{
Expand Down Expand Up @@ -212,7 +213,26 @@ namespace tao::json
TEST_ASSERT( s.i == 42 );
}

// TODO: Test with different for_nothing_value (incl. consistency of size to consumer).
struct type_7
{
std::vector< int > a;
std::vector< int > b;
};

template<>
struct traits< type_7 >
: binding::basic_object< binding::for_unknown_key::fail,
binding::for_nothing_value::suppress,
TAO_JSON_BIND_REQUIRED( "a", &type_7::a ),
TAO_JSON_BIND_OPTIONAL( "b", &type_7::b ) >
{};

void unit_test_7()
{
const type_7 t;
const auto s = produce::to_string( t );
TEST_ASSERT( s == "{\"a\":[]}" );
}

void unit_test()
{
Expand All @@ -222,6 +242,7 @@ namespace tao::json
unit_test_4();
unit_test_5();
unit_test_6();
unit_test_7();
}

} // namespace tao::json
Expand Down

0 comments on commit 32f4d55

Please sign in to comment.