Skip to content

Commit e22bb68

Browse files
committed
Towards #108.
1 parent f16f59e commit e22bb68

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

include/tao/json/binding/element.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ namespace tao::json::binding
8282
using class_t = C;
8383
using value_t = T;
8484

85+
using internal_t = std::decay_t< decltype( P( std::declval< const C >() ) ) >;
86+
8587
[[nodiscard]] static decltype( auto ) read( const C& v )
8688
{
8789
return P( v );
@@ -103,6 +105,8 @@ namespace tao::json::binding
103105
using class_t = A;
104106
using value_t = std::decay_t< R >;
105107

108+
using internal_t = value_t;
109+
106110
[[nodiscard]] static decltype( auto ) read( const A& v ) noexcept
107111
{
108112
return CP( v );
@@ -139,6 +143,8 @@ namespace tao::json::binding
139143
using class_t = A;
140144
using value_t = std::decay_t< R >;
141145

146+
using internal_t = value_t;
147+
142148
[[nodiscard]] static decltype( auto ) read( const A& v ) noexcept
143149
{
144150
return CP( v );

include/tao/json/binding/member.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ namespace tao::json::binding
2727
}
2828
};
2929

30+
template< member_kind R, typename K, auto CP, auto P >
31+
struct member2
32+
: element2< CP, P >,
33+
internal::type_key< K, typename binding::element2< CP, P >::internal_t >
34+
{
35+
static constexpr member_kind kind = R;
36+
37+
template< template< typename... > class Traits, typename C >
38+
[[nodiscard]] static bool is_nothing( const C& x )
39+
{
40+
return json::internal::is_nothing< Traits >( binding::element2< CP, P >::read( x ) );
41+
}
42+
};
43+
3044
} // namespace tao::json::binding
3145

3246
#endif

src/test/json/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(testsources
44
big_list_of_naughty_strings.cpp
55
binding_array.cpp
66
binding_factory.cpp
7+
binding_function.cpp
78
binding_object.cpp
89
binding_versions.cpp
910
cbor.cpp

src/test/json/binding_function.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2018-2021 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#include "test.hpp"
5+
6+
#include <tao/json.hpp>
7+
8+
namespace tao::json
9+
{
10+
struct type_f
11+
{
12+
int private_a;
13+
int private_b;
14+
};
15+
16+
[[nodiscard]] int get_a( const type_f& f ) noexcept
17+
{
18+
return f.private_a + 10;
19+
}
20+
21+
[[nodiscard]] int& set_a( type_f& f ) noexcept
22+
{
23+
return f.private_a;
24+
}
25+
26+
[[nodiscard]] int get_b( const type_f& f )
27+
{
28+
return f.private_b + 20;
29+
}
30+
31+
// void set_b( type_f& f, int& b ) noexcept
32+
// {
33+
// f.private_b = b;
34+
// }
35+
36+
template<>
37+
struct traits< type_f >
38+
: binding::object< binding::member2< binding::member_kind::required, TAO_JSON_STRING_T( "a" ), &get_a, &set_a >,
39+
binding::member< binding::member_kind::required, TAO_JSON_STRING_T( "b" ), &get_b > >
40+
{
41+
TAO_JSON_DEFAULT_KEY( "f" );
42+
};
43+
44+
void unit_test()
45+
{
46+
const type_f f{ 41, 42 };
47+
const value v = produce::to_value( f );
48+
TEST_ASSERT( v.get_object().size() == 2 );
49+
TEST_ASSERT( v.get_object().at( "a" ).as< int >() == 51 );
50+
TEST_ASSERT( v.get_object().at( "b" ).as< int >() == 62 );
51+
}
52+
53+
} // namespace tao::json
54+
55+
#include "../../test/json/main.hpp"

0 commit comments

Comments
 (0)