Skip to content

Commit 260e38c

Browse files
committed
Made json::value::as_number and json::value::as_string return by const reference instead of by value.
1 parent d93bc39 commit 260e38c

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

Release/include/cpprest/json.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ namespace json
471471
/// Throws <see cref="json_exception"/> if the value is not a number
472472
/// </summary>
473473
/// <returns>An instance of number class</returns>
474-
_ASYNCRTIMP json::number as_number() const;
474+
_ASYNCRTIMP const json::number& as_number() const;
475475

476476
/// <summary>
477477
/// Converts the JSON value to a C++ bool, if and only if it is a Boolean value.
@@ -509,7 +509,7 @@ namespace json
509509
/// Converts the JSON value to a C++ STL string, if and only if it is a string value.
510510
/// </summary>
511511
/// <returns>A C++ STL string representation of the value</returns>
512-
_ASYNCRTIMP utility::string_t as_string() const;
512+
_ASYNCRTIMP const utility::string_t& as_string() const;
513513

514514
/// <summary>
515515
/// Compares two JSON values for equality.
@@ -644,15 +644,13 @@ namespace json
644644
private:
645645
std::string _message;
646646
public:
647-
json_exception() {}
648647
json_exception(const utility::char_t * const &message) : _message(utility::conversions::to_utf8string(message)) { }
649648

650649
// Must be narrow string because it derives from std::exception
651650
const char* what() const CPPREST_NOEXCEPT
652651
{
653652
return _message.c_str();
654653
}
655-
~json_exception() CPPREST_NOEXCEPT {}
656654
};
657655

658656
namespace details
@@ -1386,15 +1384,15 @@ namespace json
13861384
virtual bool is_integer() const { throw json_exception(_XPLATSTR("not a number")); }
13871385
virtual bool is_double() const { throw json_exception(_XPLATSTR("not a number")); }
13881386

1389-
virtual json::number as_number() { throw json_exception(_XPLATSTR("not a number")); }
1387+
virtual const json::number& as_number() { throw json_exception(_XPLATSTR("not a number")); }
13901388
virtual double as_double() const { throw json_exception(_XPLATSTR("not a number")); }
13911389
virtual int as_integer() const { throw json_exception(_XPLATSTR("not a number")); }
13921390
virtual bool as_bool() const { throw json_exception(_XPLATSTR("not a boolean")); }
13931391
virtual json::array& as_array() { throw json_exception(_XPLATSTR("not an array")); }
13941392
virtual const json::array& as_array() const { throw json_exception(_XPLATSTR("not an array")); }
13951393
virtual json::object& as_object() { throw json_exception(_XPLATSTR("not an object")); }
13961394
virtual const json::object& as_object() const { throw json_exception(_XPLATSTR("not an object")); }
1397-
virtual utility::string_t as_string() const { throw json_exception(_XPLATSTR("not a string")); }
1395+
virtual const utility::string_t& as_string() const { throw json_exception(_XPLATSTR("not a string")); }
13981396

13991397
virtual size_t size() const { return 0; }
14001398

@@ -1464,7 +1462,7 @@ namespace json
14641462
return m_number.to_int32();
14651463
}
14661464

1467-
virtual number as_number() { return m_number; }
1465+
virtual const number& as_number() { return m_number; }
14681466

14691467
protected:
14701468
virtual void format(std::basic_string<char>& stream) const ;
@@ -1554,7 +1552,7 @@ namespace json
15541552

15551553
virtual json::value::value_type type() const { return json::value::String; }
15561554

1557-
virtual utility::string_t as_string() const;
1555+
virtual const utility::string_t & as_string() const;
15581556

15591557
virtual void serialize_impl(std::string& str) const
15601558
{

Release/src/json/json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ web::json::value web::json::value::array(std::vector<value> elements)
262262
);
263263
}
264264

265-
web::json::number web::json::value::as_number() const
265+
const web::json::number& web::json::value::as_number() const
266266
{
267267
return m_value->as_number();
268268
}

Release/src/json/json_serialization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ void web::json::details::_Number::format(std::basic_string<wchar_t>& stream) con
255255

256256
#endif
257257

258-
utility::string_t web::json::details::_String::as_string() const
258+
const utility::string_t & web::json::details::_String::as_string() const
259259
{
260260
return m_string;
261261
}
262262

263-
utility::string_t web::json::value::as_string() const
263+
const utility::string_t & web::json::value::as_string() const
264264
{
265265
return m_value->as_string();
266266
}

0 commit comments

Comments
 (0)