Skip to content

Commit 07cf589

Browse files
authored
Merge pull request #1429 from NN---/fix/value_int_ctor
Add constructor from all integer types. Previously one was lacking for `long` which has identical conversion rank to both long long and unsigned long long.
2 parents bfe3487 + 3eac925 commit 07cf589

File tree

3 files changed

+130
-24
lines changed

3 files changed

+130
-24
lines changed

Release/include/cpprest/json.h

+46-16
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,37 @@ class value
100100
/// Constructor creating a JSON number value
101101
/// </summary>
102102
/// <param name="value">The C++ value to create a JSON value from</param>
103-
_ASYNCRTIMP value(int32_t value);
103+
_ASYNCRTIMP value(int value);
104104

105105
/// <summary>
106106
/// Constructor creating a JSON number value
107107
/// </summary>
108108
/// <param name="value">The C++ value to create a JSON value from</param>
109-
_ASYNCRTIMP value(uint32_t value);
109+
_ASYNCRTIMP value(unsigned value);
110110

111111
/// <summary>
112112
/// Constructor creating a JSON number value
113113
/// </summary>
114114
/// <param name="value">The C++ value to create a JSON value from</param>
115-
_ASYNCRTIMP value(int64_t value);
115+
_ASYNCRTIMP value(long value);
116116

117117
/// <summary>
118118
/// Constructor creating a JSON number value
119119
/// </summary>
120120
/// <param name="value">The C++ value to create a JSON value from</param>
121-
_ASYNCRTIMP value(uint64_t value);
121+
_ASYNCRTIMP value(unsigned long value);
122+
123+
/// <summary>
124+
/// Constructor creating a JSON number value
125+
/// </summary>
126+
/// <param name="value">The C++ value to create a JSON value from</param>
127+
_ASYNCRTIMP value(long long value);
128+
129+
/// <summary>
130+
/// Constructor creating a JSON number value
131+
/// </summary>
132+
/// <param name="value">The C++ value to create a JSON value from</param>
133+
_ASYNCRTIMP value(unsigned long long value);
122134

123135
/// <summary>
124136
/// Constructor creating a JSON number value
@@ -222,28 +234,42 @@ class value
222234
/// </summary>
223235
/// <param name="value">The C++ value to create a JSON value from</param>
224236
/// <returns>A JSON number value</returns>
225-
static _ASYNCRTIMP value __cdecl number(int32_t value);
237+
static _ASYNCRTIMP value __cdecl number(int value);
238+
239+
/// <summary>
240+
/// Creates a number value
241+
/// </summary>
242+
/// <param name="value">The C++ value to create a JSON value from</param>
243+
/// <returns>A JSON number value</returns>
244+
static _ASYNCRTIMP value __cdecl number(unsigned value);
245+
246+
/// <summary>
247+
/// Creates a number value
248+
/// </summary>
249+
/// <param name="value">The C++ value to create a JSON value from</param>
250+
/// <returns>A JSON number value</returns>
251+
static _ASYNCRTIMP value __cdecl number(long value);
226252

227253
/// <summary>
228254
/// Creates a number value
229255
/// </summary>
230256
/// <param name="value">The C++ value to create a JSON value from</param>
231257
/// <returns>A JSON number value</returns>
232-
static _ASYNCRTIMP value __cdecl number(uint32_t value);
258+
static _ASYNCRTIMP value __cdecl number(unsigned long value);
233259

234260
/// <summary>
235261
/// Creates a number value
236262
/// </summary>
237263
/// <param name="value">The C++ value to create a JSON value from</param>
238264
/// <returns>A JSON number value</returns>
239-
static _ASYNCRTIMP value __cdecl number(int64_t value);
265+
static _ASYNCRTIMP value __cdecl number(long long value);
240266

241267
/// <summary>
242268
/// Creates a number value
243269
/// </summary>
244270
/// <param name="value">The C++ value to create a JSON value from</param>
245271
/// <returns>A JSON number value</returns>
246-
static _ASYNCRTIMP value __cdecl number(uint64_t value);
272+
static _ASYNCRTIMP value __cdecl number(unsigned long long value);
247273

248274
/// <summary>
249275
/// Creates a Boolean value
@@ -1218,10 +1244,12 @@ class number
12181244
// convert to unsigned int64). This helps handling number objects e.g. comparing two numbers.
12191245

12201246
number(double value) : m_value(value), m_type(double_type) {}
1221-
number(int32_t value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
1222-
number(uint32_t value) : m_intval(value), m_type(unsigned_type) {}
1223-
number(int64_t value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
1224-
number(uint64_t value) : m_uintval(value), m_type(unsigned_type) {}
1247+
number(int value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
1248+
number(unsigned value) : m_intval(value), m_type(unsigned_type) {}
1249+
number(long value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
1250+
number(unsigned long value) : m_uintval(value), m_type(unsigned_type) {}
1251+
number(long long value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
1252+
number(unsigned long long value) : m_uintval(value), m_type(unsigned_type) {}
12251253

12261254
public:
12271255
/// <summary>
@@ -1438,10 +1466,12 @@ class _Number : public _Value
14381466
{
14391467
public:
14401468
_Number(double value) : m_number(value) {}
1441-
_Number(int32_t value) : m_number(value) {}
1442-
_Number(uint32_t value) : m_number(value) {}
1443-
_Number(int64_t value) : m_number(value) {}
1444-
_Number(uint64_t value) : m_number(value) {}
1469+
_Number(int value) : m_number(value) {}
1470+
_Number(unsigned value) : m_number(value) {}
1471+
_Number(long value) : m_number(value) {}
1472+
_Number(unsigned long value) : m_number(value) {}
1473+
_Number(long long value) : m_number(value) {}
1474+
_Number(unsigned long long value) : m_number(value) {}
14451475

14461476
virtual std::unique_ptr<_Value> _copy_value() { return utility::details::make_unique<_Number>(*this); }
14471477

Release/src/json/json.cpp

+29-8
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,48 @@ web::json::value::value()
3838
{
3939
}
4040

41-
web::json::value::value(int32_t value)
41+
web::json::value::value(int value)
4242
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
4343
#ifdef ENABLE_JSON_VALUE_VISUALIZER
4444
, m_kind(value::Number)
4545
#endif
4646
{
4747
}
4848

49-
web::json::value::value(uint32_t value)
49+
web::json::value::value(unsigned value)
5050
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
5151
#ifdef ENABLE_JSON_VALUE_VISUALIZER
5252
, m_kind(value::Number)
5353
#endif
5454
{
5555
}
5656

57-
web::json::value::value(int64_t value)
57+
58+
web::json::value::value(long value)
5859
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
5960
#ifdef ENABLE_JSON_VALUE_VISUALIZER
6061
, m_kind(value::Number)
6162
#endif
6263
{
6364
}
6465

65-
web::json::value::value(uint64_t value)
66+
web::json::value::value(unsigned long value)
67+
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
68+
#ifdef ENABLE_JSON_VALUE_VISUALIZER
69+
, m_kind(value::Number)
70+
#endif
71+
{
72+
}
73+
74+
web::json::value::value(long long value)
75+
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
76+
#ifdef ENABLE_JSON_VALUE_VISUALIZER
77+
, m_kind(value::Number)
78+
#endif
79+
{
80+
}
81+
82+
web::json::value::value(unsigned long long value)
6683
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
6784
#ifdef ENABLE_JSON_VALUE_VISUALIZER
6885
, m_kind(value::Number)
@@ -162,13 +179,17 @@ web::json::value web::json::value::null() { return web::json::value(); }
162179

163180
web::json::value web::json::value::number(double value) { return web::json::value(value); }
164181

165-
web::json::value web::json::value::number(int32_t value) { return web::json::value(value); }
182+
web::json::value web::json::value::number(int value) { return web::json::value(value); }
183+
184+
web::json::value web::json::value::number(unsigned value) { return web::json::value(value); }
185+
186+
web::json::value web::json::value::number(long value) { return web::json::value(value); }
166187

167-
web::json::value web::json::value::number(uint32_t value) { return web::json::value(value); }
188+
web::json::value web::json::value::number(unsigned long value) { return web::json::value(value); }
168189

169-
web::json::value web::json::value::number(int64_t value) { return web::json::value(value); }
190+
web::json::value web::json::value::number(long long value) { return web::json::value(value); }
170191

171-
web::json::value web::json::value::number(uint64_t value) { return web::json::value(value); }
192+
web::json::value web::json::value::number(unsigned long long value) { return web::json::value(value); }
172193

173194
web::json::value web::json::value::boolean(bool value) { return web::json::value(value); }
174195

Release/tests/functional/json/construction_tests.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,61 @@ SUITE(construction_tests)
5050
VERIFY_ARE_EQUAL(U("null"), arr[1].serialize());
5151
}
5252

53+
TEST(int_assignment_op)
54+
{
55+
json::value v;
56+
v = static_cast<int>(1);
57+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
58+
59+
v = static_cast<unsigned>(1);
60+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
61+
62+
v = static_cast<long>(1);
63+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
64+
65+
v = static_cast<unsigned long>(1);
66+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
67+
68+
v = static_cast<long long >(1);
69+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
70+
71+
v = static_cast<unsigned long long>(1);
72+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
73+
}
74+
75+
TEST(int_ctor)
76+
{
77+
{
78+
json::value v(static_cast<int>(1));
79+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
80+
}
81+
82+
{
83+
json::value v(static_cast<unsigned>(1));
84+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
85+
}
86+
87+
{
88+
json::value v(static_cast<long>(1));
89+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
90+
}
91+
92+
{
93+
json::value v(static_cast<unsigned long>(1));
94+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
95+
}
96+
97+
{
98+
json::value v(static_cast<long long>(1));
99+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
100+
}
101+
102+
{
103+
json::value v(static_cast<unsigned long long>(1));
104+
VERIFY_ARE_EQUAL(U("1"), v.serialize());
105+
}
106+
}
107+
53108
TEST(copy_ctor_array)
54109
{
55110
json::value arr = json::value::array();

0 commit comments

Comments
 (0)