1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9 
10 #include <boost/json/parse.hpp>
11 #include <boost/json/value.hpp>
12 #include <boost/json/value_to.hpp>
13 
14 #include "test_suite.hpp"
15 
16 #ifdef _MSC_VER
17 # pragma warning(push)
18 # pragma warning(disable: 4101)
19 # pragma warning(disable: 4189) // local variable is initialized but not referenced
20 #elif defined(__clang__)
21 # pragma clang diagnostic push
22 # pragma clang diagnostic ignored "-Wunused"
23 # pragma clang diagnostic ignored "-Wunused-variable"
24 #elif defined(__GNUC__)
25 # pragma GCC diagnostic push
26 # pragma GCC diagnostic ignored "-Wunused"
27 # pragma GCC diagnostic ignored "-Wunused-variable"
28 #endif
29 
30 BOOST_JSON_NS_BEGIN
31 
32 //----------------------------------------------------------
33 
set1()34 static void set1() {
35 
36 //----------------------------------------------------------
37 {
38 //[doc_using_numbers_1
39 // construction from int
40 value jv1 = 1;
41 
42 assert( jv1.is_int64() );
43 
44 // construction from unsigned int
45 value jv2 = 2u;
46 
47 assert( jv2.is_uint64() );
48 
49 // construction from double
50 value jv3 = 3.0;
51 
52 assert( jv3.is_double() );
53 //]
54 }
55 //----------------------------------------------------------
56 {
57 //[doc_using_numbers_2
58 
59 value jv = 1;
60 
61 assert( jv.is_int64() );
62 
63 // jv.kind() != kind::uint64; throws
64 std::uint64_t r1 = jv.as_uint64();
65 
66 // jv.kind() != kind::uint64; the behavior is undefined
67 std::uint64_t r2 = jv.get_uint64();
68 
69 // if_double will always return nullptr, branch is not taken
70 if(double* d = jv.if_double())
71     assert( false );
72 
73 //]
74 };
75 //----------------------------------------------------------
76 {
77 //[doc_using_numbers_3
78 struct convert_int64
79 {
80     value jv;
81 
82     operator int() const
83     {
84         return value_to< int >( this->jv );
85     }
86 };
87 //]
88 }
89 //----------------------------------------------------------
90 try
91 {
92 //[doc_using_numbers_4
93     value jv1 = 404;
94 
95     assert( jv1.is_int64() );
96 
97     // ok, identity conversion
98     std::int64_t r1 = value_to< std::int64_t >( jv1 );
99 
100     // loss of data, throws system_error
101     char r2 = value_to< char >( jv1 );
102 
103     // ok, no loss of data
104     double r3 = value_to< double >( jv1 );
105 
106     value jv2 = 1.23;
107 
108     assert( jv1.is_double() );
109 
110     // ok, same as static_cast<float>( jv2.get_double() )
111     float r4 = value_to< float >( jv2 );
112 
113     // not exact, throws system_error
114     int r5 = value_to< int >( jv2 );
115 
116     value jv3 = {1, 2, 3};
117 
118     assert( ! jv3.is_number() );
119 
120     // not a number, throws system_error
121     int r6 = value_to< int >( jv3 );
122 //]
123 }
124 catch(...)
125 {
126 }
127 //----------------------------------------------------------
128 {
129 //[doc_using_numbers_5
130 
131 value jv = 10.5;
132 
133 error_code ec;
134 
135 // ok, conversion is exact
136 float r1 = jv.to_number< float >( ec );
137 
138 assert( ! ec );
139 
140 // error, conversion is non-exact
141 int r2 = jv.to_number< int >( ec );
142 
143 assert( ec == error::not_exact );
144 
145 //]
146 }
147 //----------------------------------------------------------
148 {
149 //[doc_using_numbers_6
150 value jv = parse("[-42, 100, 10.25, -299999999999999999998, 2e32]");
151 
152 array ja = jv.as_array();
153 
154 // represented by std::int64_t
155 assert( ja[0].is_int64() );
156 
157 // represented by std::int64_t
158 assert( ja[1].is_int64() );
159 
160 // contains decimal point, represented as double
161 assert( ja[2].is_double() );
162 
163 // less than INT64_MIN, represented as double
164 assert( ja[3].is_double() );
165 
166 // contains exponent, represented as double
167 assert( ja[4].is_double() );
168 //]
169 }
170 //----------------------------------------------------------
171 {
172 //[doc_using_numbers_7
173 //]
174 }
175 //----------------------------------------------------------
176 
177 } // set1
178 
179 //----------------------------------------------------------
180 
181 class doc_using_numbers_test
182 {
183 public:
184     void
run()185     run()
186     {
187         (void)&set1;
188     }
189 };
190 
191 TEST_SUITE(doc_using_numbers_test, "boost.json.doc_using_numbers");
192 
193 BOOST_JSON_NS_END
194