Skip to content

Commit 789b489

Browse files
committed
New
1 parent 320c40d commit 789b489

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/test/json/user.cc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#include "test.hh"
5+
6+
#include <tao/json/value.hh>
7+
#include <tao/json/to_string.hh>
8+
#include <tao/json/stream.hh>
9+
10+
namespace tao
11+
{
12+
namespace json
13+
{
14+
struct user
15+
{
16+
bool is_human;
17+
std::string name;
18+
unsigned age;
19+
20+
user( const bool h, const std::string & n, const unsigned a )
21+
: is_human( h ), name( n ), age( a )
22+
{}
23+
};
24+
25+
template<>
26+
struct traits< user >
27+
{
28+
static const char* default_key;
29+
30+
static void assign( value & v, const user & u )
31+
{
32+
v = {
33+
{ "is_human", u.is_human },
34+
{ "name", u.name },
35+
{ "age", u.age }
36+
};
37+
}
38+
39+
static user as( const value & v )
40+
{
41+
return user( v.at( "is_human" ).get_boolean(),
42+
v.at( "name" ).get_string(),
43+
v.at( "age" ).as< unsigned >() );
44+
}
45+
};
46+
47+
const char* traits< user >::default_key = "user";
48+
49+
void unit_test()
50+
{
51+
user u( true, "Daniel", 44 );
52+
53+
user u2( false, "Rusty", 3 );
54+
user u3( false, "Amelie", 3 );
55+
user u4( false, "Milow", 2 );
56+
user u5( false, "Jessy", 1 );
57+
58+
value v = {
59+
u,
60+
{ "cats", value::array( { u2, u3, u4, u5 } ) }
61+
};
62+
63+
std::cout << std::setw( 2 ) << v << std::endl;
64+
65+
user r = v.at( "user" ).as< user >();
66+
std::cout << std::setw( 2 ) << value( r ) << std::endl;
67+
}
68+
69+
} // json
70+
71+
} // tao
72+
73+
#include "main.hh"

0 commit comments

Comments
 (0)