Skip to content

Commit 2280f05

Browse files
committed
Add web::json::value::string overload with additional 'has_escaped_chars' parameter. New overload has O(1) performance since it does not try to search for to-be-escaped-characters in provided string. Makes huge performance difference for large enough strings (i.e. images encoded as base64)
1 parent 9cc7758 commit 2280f05

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

Release/include/cpprest/json.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ namespace web { namespace json
200200
/// <returns>A JSON string value</returns>
201201
static _ASYNCRTIMP value __cdecl string(utility::string_t value);
202202

203+
/// <summary>
204+
/// Creates a string value
205+
/// </summary>
206+
/// <param name="value">The C++ value to create a JSON value from</param>
207+
/// <param name="has_escape_chars">Whether <paramref name="value" /> contains characters that should be escaped in JSON value</param>
208+
/// <returns>A JSON string value</returns>
209+
static _ASYNCRTIMP value __cdecl string(utility::string_t value, bool has_escape_chars);
210+
203211
#ifdef _MS_WINDOWS
204212
private:
205213
// Only used internally by JSON parser.

Release/src/json/json.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ web::json::value web::json::value::string(utility::string_t value)
184184
);
185185
}
186186

187+
web::json::value web::json::value::string(utility::string_t value, bool has_escape_chars)
188+
{
189+
std::unique_ptr<details::_Value> ptr = utility::details::make_unique<details::_String>(std::move(value), has_escape_chars);
190+
return web::json::value(std::move(ptr)
191+
#ifdef ENABLE_JSON_VALUE_VISUALIZER
192+
,value::String
193+
#endif
194+
);
195+
}
196+
187197
#ifdef _MS_WINDOWS
188198
web::json::value web::json::value::string(const std::string &value)
189199
{

Release/tests/Functional/json/construction_tests.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ TEST(factory_overloads)
186186
json::value v5 = json::value::string(U("Hello Again!"));
187187
json::value v6 = json::value::string(U("Hello!"));
188188
json::value v7 = json::value::string(U("Hello Again!"));
189-
json::value v8 = json::value::object();
190-
json::value v9 = json::value::array();
189+
json::value v8 = json::value::string(U("Hello not-escaped!"), true);
190+
json::value v9 = json::value::string(U("Hello not-escaped!"), false);
191+
json::value v10 = json::value::object();
192+
json::value v11 = json::value::array();
193+
191194

192195
VERIFY_ARE_EQUAL(v0.type(), json::value::Null);
193196
VERIFY_ARE_EQUAL(v1.type(), json::value::Number);
@@ -197,10 +200,12 @@ TEST(factory_overloads)
197200
VERIFY_ARE_EQUAL(v5.type(), json::value::String);
198201
VERIFY_ARE_EQUAL(v6.type(), json::value::String);
199202
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());
203+
VERIFY_ARE_EQUAL(v8.type(), json::value::String);
204+
VERIFY_ARE_EQUAL(v9.type(), json::value::String);
205+
VERIFY_ARE_EQUAL(v10.type(), json::value::Object);
206+
VERIFY_IS_TRUE(v10.is_object());
207+
VERIFY_ARE_EQUAL(v11.type(), json::value::Array);
208+
VERIFY_IS_TRUE(v11.is_array());
204209
}
205210

206211
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)