Skip to content

Commit d8adc4e

Browse files
committed
Merge branch 'development' of https://git01.codeplex.com/casablanca into 4gb_file_fix
2 parents e3edeb6 + e800120 commit d8adc4e

File tree

5 files changed

+129
-10
lines changed

5 files changed

+129
-10
lines changed

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Brian Wengert (bwengert79)
99
Leslie Brody (Les1966)
1010
Michael M (M1xa)
1111
Matt Peterson (MattPeterson1)
12+
Dmitry Kolomiets (kolomiets)
1213

1314
Illumina Inc.
1415
Gery Vessere ([email protected])

Release/include/cpprest/json.h

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,56 @@ namespace json
150150
/// Constructor creating a JSON string value
151151
/// </summary>
152152
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
153-
_ASYNCRTIMP explicit value(utility::string_t);
153+
/// <remarks>
154+
/// This constructor has O(n) performance because it tries to determine if
155+
/// specified string has characters that should be properly escaped in JSON.
156+
/// <remarks>
157+
_ASYNCRTIMP explicit value(utility::string_t value);
158+
159+
/// <summary>
160+
/// Constructor creating a JSON string value specifying if the string contains characters to escape
161+
/// </summary>
162+
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
163+
/// <param name="has_escape_chars">Whether <paramref name="value" /> contains characters
164+
/// that should be escaped in JSON value</param>
165+
/// <remarks>
166+
/// This constructor has O(1) performance.
167+
/// </remarks>
168+
_ASYNCRTIMP explicit value(utility::string_t value, bool has_escape_chars);
154169

155170
/// <summary>
156171
/// Constructor creating a JSON string value
157172
/// </summary>
158173
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
159-
/// <remarks>This constructor exists in order to avoid string literals matching another constructor,
174+
/// <remarks>
175+
/// <para>
176+
/// This constructor has O(n) performance because it tries to determine if
177+
/// specified string has characters that should be properly escaped in JSON.
178+
/// </para>
179+
/// <para>
180+
/// This constructor exists in order to avoid string literals matching another constructor,
160181
/// as is very likely. For example, conversion to bool does not require a user-defined conversion,
161-
/// and will therefore match first, which means that the JSON value turns up as a boolean.</remarks>
162-
_ASYNCRTIMP explicit value(const utility::char_t *);
182+
/// and will therefore match first, which means that the JSON value turns up as a boolean.
183+
/// </para>
184+
/// </remarks>
185+
_ASYNCRTIMP explicit value(const utility::char_t* value);
186+
187+
/// <summary>
188+
/// Constructor creating a JSON string value
189+
/// </summary>
190+
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the platform-native character width</param>
191+
/// <param name="has_escape_chars">Whether <paramref name="value" /> contains characters
192+
/// <remarks>
193+
/// <para>
194+
/// This overload has O(1) performance.
195+
/// </para>
196+
/// <para>
197+
/// This constructor exists in order to avoid string literals matching another constructor,
198+
/// as is very likely. For example, conversion to bool does not require a user-defined conversion,
199+
/// and will therefore match first, which means that the JSON value turns up as a boolean.
200+
/// </para>
201+
/// </remarks>
202+
_ASYNCRTIMP explicit value(const utility::char_t* value, bool has_escape_chars);
163203

164204
/// <summary>
165205
/// Copy constructor
@@ -217,8 +257,24 @@ namespace json
217257
/// </summary>
218258
/// <param name="value">The C++ value to create a JSON value from</param>
219259
/// <returns>A JSON string value</returns>
260+
/// <remarks>
261+
/// This overload has O(n) performance because it tries to determine if
262+
/// specified string has characters that should be properly escaped in JSON.
263+
/// <remarks>
220264
static _ASYNCRTIMP value __cdecl string(utility::string_t value);
221265

266+
/// <summary>
267+
/// Creates a string value specifying if the string contains characters to escape
268+
/// </summary>
269+
/// <param name="value">The C++ value to create a JSON value from</param>
270+
/// <param name="has_escape_chars">Whether <paramref name="value" /> contains characters
271+
/// that should be escaped in JSON value</param>
272+
/// <returns>A JSON string value</returns>
273+
/// <remarks>
274+
/// This overload has O(1) performance.
275+
/// </remarks>
276+
static _ASYNCRTIMP value __cdecl string(utility::string_t value, bool has_escape_chars);
277+
222278
#ifdef _MS_WINDOWS
223279
private:
224280
// Only used internally by JSON parser.

Release/src/json/json.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,27 @@ web::json::value::value(utility::string_t value) :
103103
#endif
104104
{ }
105105

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

120+
web::json::value::value(const utility::char_t* value, bool has_escape_chars) :
121+
m_value(utility::details::make_unique<web::json::details::_String>(utility::string_t(value), has_escape_chars))
122+
#ifdef ENABLE_JSON_VALUE_VISUALIZER
123+
, m_kind(value::String)
124+
#endif
125+
{ }
126+
113127
web::json::value::value(const value &other) :
114128
m_value(other.m_value->_copy_value())
115129
#ifdef ENABLE_JSON_VALUE_VISUALIZER
@@ -177,6 +191,16 @@ web::json::value web::json::value::string(utility::string_t value)
177191
);
178192
}
179193

194+
web::json::value web::json::value::string(utility::string_t value, bool has_escape_chars)
195+
{
196+
std::unique_ptr<details::_Value> ptr = utility::details::make_unique<details::_String>(std::move(value), has_escape_chars);
197+
return web::json::value(std::move(ptr)
198+
#ifdef ENABLE_JSON_VALUE_VISUALIZER
199+
,value::String
200+
#endif
201+
);
202+
}
203+
180204
#ifdef _MS_WINDOWS
181205
web::json::value web::json::value::string(const std::string &value)
182206
{

Release/tests/Functional/json/construction_tests.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ TEST(constructor_overloads)
154154
json::value v6(U("YES YOU KNOW IT"));
155155
json::value v7(U("HERE ID IS"));
156156

157+
const utility::char_t* p9 = U("Hello not-escaped!");
158+
json::value v8(p9, true);
159+
json::value v9(p9, false);
160+
157161
VERIFY_ARE_EQUAL(v0.type(), json::value::Null);
158162
VERIFY_IS_TRUE(v0.is_null());
159163
VERIFY_ARE_EQUAL(v1.type(), json::value::Number);
@@ -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)
@@ -186,8 +194,11 @@ TEST(factory_overloads)
186194
json::value v5 = json::value::string(U("Hello Again!"));
187195
json::value v6 = json::value::string(U("Hello!"));
188196
json::value v7 = json::value::string(U("Hello Again!"));
189-
json::value v8 = json::value::object();
190-
json::value v9 = json::value::array();
197+
json::value v8 = json::value::string(U("Hello not-escaped!"), true);
198+
json::value v9 = json::value::string(U("Hello not-escaped!"), false);
199+
json::value v10 = json::value::object();
200+
json::value v11 = json::value::array();
201+
191202

192203
VERIFY_ARE_EQUAL(v0.type(), json::value::Null);
193204
VERIFY_ARE_EQUAL(v1.type(), json::value::Number);
@@ -197,10 +208,12 @@ TEST(factory_overloads)
197208
VERIFY_ARE_EQUAL(v5.type(), json::value::String);
198209
VERIFY_ARE_EQUAL(v6.type(), json::value::String);
199210
VERIFY_ARE_EQUAL(v7.type(), json::value::String);
200-
VERIFY_ARE_EQUAL(v8.type(), json::value::Object);
201-
VERIFY_IS_TRUE(v8.is_object());
202-
VERIFY_ARE_EQUAL(v9.type(), json::value::Array);
203-
VERIFY_IS_TRUE(v9.is_array());
211+
VERIFY_ARE_EQUAL(v8.type(), json::value::String);
212+
VERIFY_ARE_EQUAL(v9.type(), json::value::String);
213+
VERIFY_ARE_EQUAL(v10.type(), json::value::Object);
214+
VERIFY_IS_TRUE(v10.is_object());
215+
VERIFY_ARE_EQUAL(v11.type(), json::value::Array);
216+
VERIFY_IS_TRUE(v11.is_array());
204217
}
205218

206219
TEST(object_construction)

Release/tests/Functional/json/to_as_and_operators_tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ void verify_escaped_chars(const utility::string_t& str1, const utility::string_t
124124
VERIFY_ARE_EQUAL(str2, j1.serialize());
125125
}
126126

127+
void verify_unescaped_chars(const utility::string_t& str1, const utility::string_t& str2)
128+
{
129+
json::value j1 = json::value::string(str1, false);
130+
VERIFY_ARE_EQUAL(str2, j1.serialize());
131+
}
132+
127133
TEST(to_string_escaped_chars)
128134
{
129135
verify_escaped_chars(U(" \" "), U("\" \\\" \""));
@@ -148,6 +154,25 @@ TEST(to_string_escaped_chars)
148154
VERIFY_ARE_EQUAL(str, obj2.serialize());
149155
}
150156

157+
TEST(to_string_unescaped_chars)
158+
{
159+
verify_unescaped_chars(U(" \" "), U("\" \" \""));
160+
verify_unescaped_chars(U(" \b "), U("\" \b \""));
161+
verify_unescaped_chars(U(" \f "), U("\" \f \""));
162+
verify_unescaped_chars(U(" \n "), U("\" \n \""));
163+
verify_unescaped_chars(U(" \r "), U("\" \r \""));
164+
verify_unescaped_chars(U(" \t "), U("\" \t \""));
165+
166+
json::value obj = json::value::object();
167+
obj[U(" \t ")] = json::value::string(U(" \b "), false);
168+
169+
json::value arr = json::value::array();
170+
arr[0] = json::value::string(U(" \f "), false);
171+
172+
VERIFY_ARE_EQUAL(U("{\" \\t \":\" \b \"}"), obj.serialize());
173+
VERIFY_ARE_EQUAL(U("[\" \f \"]"), arr.serialize());
174+
}
175+
151176
TEST(as_string)
152177
{
153178
json::value b(false);

0 commit comments

Comments
 (0)