Skip to content

Commit a6f53d9

Browse files
committed
According to code review results, adding new web::json::value constructor overloads with additional has_escaped_chars parameter. New overlods have O(1) performance since they do not try to search for to-be-escaped-characters in provided string.
1 parent 738aad1 commit a6f53d9

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

Release/include/cpprest/json.h

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,56 @@ namespace web { namespace json
131131
/// Constructor creating a JSON string value
132132
/// </summary>
133133
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
134-
_ASYNCRTIMP explicit value(utility::string_t);
134+
/// <remarks>
135+
/// This constructor has O(n) performance because it tries to determine if
136+
/// specified string has characters that should be properly escaped in JSON.
137+
/// <remarks>
138+
_ASYNCRTIMP explicit value(utility::string_t value);
139+
140+
/// <summary>
141+
/// Constructor creating a JSON string value specifying if the string contains characters to escape
142+
/// </summary>
143+
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
144+
/// <param name="has_escape_chars">Whether <paramref name="value" /> contains characters
145+
/// that should be escaped in JSON value</param>
146+
/// <remarks>
147+
/// This constructor has O(1) performance.
148+
/// </remarks>
149+
_ASYNCRTIMP explicit value(utility::string_t value, bool has_escape_chars);
150+
151+
/// <summary>
152+
/// Constructor creating a JSON string value
153+
/// </summary>
154+
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
155+
/// <remarks>
156+
/// <para>
157+
/// This constructor has O(n) performance because it tries to determine if
158+
/// specified string has characters that should be properly escaped in JSON.
159+
/// </para>
160+
/// <para>
161+
/// This constructor exists in order to avoid string literals matching another constructor,
162+
/// as is very likely. For example, conversion to bool does not require a user-defined conversion,
163+
/// and will therefore match first, which means that the JSON value turns up as a boolean.
164+
/// </para>
165+
/// </remarks>
166+
_ASYNCRTIMP explicit value(const utility::char_t* value);
135167

136168
/// <summary>
137169
/// Constructor creating a JSON string value
138170
/// </summary>
139171
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
140-
/// <remarks>This constructor exists in order to avoid string literals matching another constructor,
172+
/// <param name="has_escape_chars">Whether <paramref name="value" /> contains characters
173+
/// <remarks>
174+
/// <para>
175+
/// This overload has O(1) performance.
176+
/// </para>
177+
/// <para>
178+
/// This constructor exists in order to avoid string literals matching another constructor,
141179
/// as is very likely. For example, conversion to bool does not require a user-defined conversion,
142-
/// and will therefore match first, which means that the JSON value turns up as a boolean.</remarks>
143-
_ASYNCRTIMP explicit value(const utility::char_t *);
180+
/// and will therefore match first, which means that the JSON value turns up as a boolean.
181+
/// </para>
182+
/// </remarks>
183+
_ASYNCRTIMP explicit value(const utility::char_t* value, bool has_escape_chars);
144184

145185
/// <summary>
146186
/// Copy constructor

Release/src/json/json.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,27 @@ web::json::value::value(utility::string_t value) :
105105
#endif
106106
{ }
107107

108+
web::json::value::value(utility::string_t value, bool has_escape_chars) :
109+
m_value(utility::details::make_unique<web::json::details::_String>(std::move(value), has_escape_chars))
110+
#ifdef ENABLE_JSON_VALUE_VISUALIZER
111+
, m_kind(value::String)
112+
#endif
113+
{ }
114+
108115
web::json::value::value(const utility::char_t* value) :
109116
m_value(utility::details::make_unique<web::json::details::_String>(utility::string_t(value)))
110117
#ifdef ENABLE_JSON_VALUE_VISUALIZER
111118
,m_kind(value::String)
112119
#endif
113120
{ }
114121

122+
web::json::value::value(const utility::char_t* value, bool has_escape_chars) :
123+
m_value(utility::details::make_unique<web::json::details::_String>(utility::string_t(value), has_escape_chars))
124+
#ifdef ENABLE_JSON_VALUE_VISUALIZER
125+
, m_kind(value::String)
126+
#endif
127+
{ }
128+
115129
web::json::value::value(const value &other) :
116130
m_value(other.m_value->_copy_value())
117131
#ifdef ENABLE_JSON_VALUE_VISUALIZER

Release/tests/Functional/json/construction_tests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ TEST(constructor_overloads)
153153
json::value v5(U("Hello Again!"));
154154
json::value v6(U("YES YOU KNOW IT"));
155155
json::value v7(U("HERE ID IS"));
156+
json::value v8(U("Hello not-escaped!"), true);
157+
158+
const utility::char_t* p9 = U("Hello not-escaped!");
159+
json::value v9(p9, false);
156160

157161
VERIFY_ARE_EQUAL(v0.type(), json::value::Null);
158162
VERIFY_IS_TRUE(v0.is_null());
@@ -174,6 +178,10 @@ TEST(constructor_overloads)
174178
VERIFY_IS_TRUE(v6.is_string());
175179
VERIFY_ARE_EQUAL(v7.type(), json::value::String);
176180
VERIFY_IS_TRUE(v7.is_string());
181+
VERIFY_ARE_EQUAL(v8.type(), json::value::String);
182+
VERIFY_IS_TRUE(v8.is_string());
183+
VERIFY_ARE_EQUAL(v9.type(), json::value::String);
184+
VERIFY_IS_TRUE(v9.is_string());
177185
}
178186

179187
TEST(factory_overloads)

0 commit comments

Comments
 (0)