Skip to content

Commit 36addc7

Browse files
committed
JSON object model should use UTF-8 internally on windows.
The JSON spec specifies UTF-8 as the wire format for JSON, so storing UTF-16 internally causes unneeded conversions during parsing and (potentially) from user code.
1 parent 45ff2f6 commit 36addc7

File tree

20 files changed

+896
-1160
lines changed

20 files changed

+896
-1160
lines changed

Release/include/cpprest/asyncrt_utils.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,32 @@ namespace conversions
198198
}
199199

200200
template <typename Target>
201-
Target scan_string(const utility::string_t &str, const std::locale &loc)
201+
Target scan_string(const std::string &str, const std::locale &loc = std::locale())
202202
{
203203
Target t;
204-
utility::istringstream_t iss(str);
204+
std::istringstream iss(str);
205205
iss.imbue(loc);
206206
iss >> t;
207-
if (iss.bad())
207+
if (iss.bad() || iss.fail())
208208
{
209209
throw std::bad_cast();
210210
}
211211
return t;
212212
}
213-
214213
template <typename Target>
215-
Target scan_string(const utility::string_t &str)
214+
Target scan_string(const utf16string &str, const std::locale &loc = std::locale())
216215
{
217-
return scan_string<Target>(str, std::locale());
216+
Target t;
217+
utf16istringstream iss(str);
218+
iss.imbue(loc);
219+
iss >> t;
220+
if (iss.bad() || iss.fail())
221+
{
222+
throw std::bad_cast();
223+
}
224+
return t;
218225
}
226+
219227
}
220228

221229
namespace details

Release/include/cpprest/json.h

Lines changed: 359 additions & 481 deletions
Large diffs are not rendered by default.

Release/include/cpprest/oauth2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace experimental
7272
class oauth2_exception : public std::exception
7373
{
7474
public:
75-
oauth2_exception(utility::string_t msg) : m_msg(utility::conversions::to_utf8string(std::move(msg))) {}
75+
oauth2_exception(std::string msg) : m_msg(std::move(msg)) {}
7676
~oauth2_exception() CPPREST_NOEXCEPT {}
7777
const char* what() const CPPREST_NOEXCEPT { return m_msg.c_str(); }
7878

Release/samples/BlackJack/BlackJack_Client/BlackJackClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ void PrintTable(const http_response &response, bool &refresh)
125125
bool suppressMoney = iter == players.as_array().begin();
126126

127127
if ( suppressMoney )
128-
ucout << "'" << name.as_string() << "'" ;
128+
std::cout << "'" << name.as_string() << "'" ;
129129
else
130-
ucout << "'" << name.as_string() << "' Balance = $" << bet.as_double() << " ";
130+
std::cout << "'" << name.as_string() << "' Balance = $" << bet.as_double() << " ";
131131

132132
PrintHand(suppressMoney, BJHand::FromJSON(player[HAND].as_object()));
133133
ucout << std::endl;

Release/samples/BlackJack/BlackJack_Server/messagetypes.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,25 @@ struct BJHand
162162
auto iState = object.find(STATE);
163163
if (iState == object.end())
164164
{
165-
throw web::json::json_exception(U("STATE key not found"));
165+
throw web::json::json_exception("STATE key not found");
166166
}
167167
res.state = (BJHandState)iState->second.as_integer();
168168
auto iBet = object.find(BET);
169169
if (iBet == object.end())
170170
{
171-
throw web::json::json_exception(U("BET key not found"));
171+
throw web::json::json_exception("BET key not found");
172172
}
173173
res.bet = iBet->second.as_double();
174174
auto iInsurance = object.find(INSURANCE);
175175
if (iInsurance == object.end())
176176
{
177-
throw web::json::json_exception(U("INSURANCE key not found"));
177+
throw web::json::json_exception("INSURANCE key not found");
178178
}
179179
res.insurance = iInsurance->second.as_double();
180180
auto iResult = object.find(RESULT);
181181
if (iResult == object.end())
182182
{
183-
throw web::json::json_exception(U("RESULT key not found"));
183+
throw web::json::json_exception("RESULT key not found");
184184
}
185185
res.result = (BJHandResult)object.find(RESULT)->second.as_integer();
186186
return res;
@@ -234,23 +234,23 @@ struct Player
234234
auto iName = object.find(NAME);
235235
if (iName == object.end())
236236
{
237-
throw web::json::json_exception(U("NAME key not found"));
237+
throw web::json::json_exception("NAME key not found");
238238
}
239239
const web::json::value &name = iName->second;
240240
auto iBalance = object.find(BALANCE);
241241
if (iBalance == object.end())
242242
{
243-
throw web::json::json_exception(U("BALANCE key not found"));
243+
throw web::json::json_exception("BALANCE key not found");
244244
}
245245
const web::json::value &balance = iBalance->second;
246246
auto iHand = object.find(HAND);
247247
if (iHand == object.end())
248248
{
249-
throw web::json::json_exception(U("HAND key not found"));
249+
throw web::json::json_exception("HAND key not found");
250250
}
251251
const web::json::value &hand = iHand->second;
252252

253-
result.Name = name.as_string();
253+
result.Name = utility::conversions::to_string_t(name.as_string());
254254
result.Balance = balance.as_double();
255255
result.Hand = BJHand::FromJSON(hand.as_object());
256256
return result;
@@ -281,21 +281,21 @@ struct BJTable
281281
auto iID = object.find(ID);
282282
if (iID == object.end())
283283
{
284-
throw web::json::json_exception(U("ID key not found"));
284+
throw web::json::json_exception("ID key not found");
285285
}
286286
result.Id = (int)iID->second.as_double();
287287
auto iCapacity = object.find(CAPACITY);
288288
if (iCapacity == object.end())
289289
{
290-
throw web::json::json_exception(U("CAPACITY key not found"));
290+
throw web::json::json_exception("CAPACITY key not found");
291291
}
292292
result.Capacity = (size_t)iCapacity->second.as_double();
293293

294294

295295
auto iPlayers = object.find(PLAYERS);
296296
if (iPlayers == object.end())
297297
{
298-
throw web::json::json_exception(U("PLAYTERS key not found"));
298+
throw web::json::json_exception("PLAYTERS key not found");
299299
}
300300
web::json::value players = iPlayers->second;
301301
int i = 0;

Release/samples/BlackJack/BlackJack_UIClient/messagetypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct Player
173173
const web::json::value &balance = object[BALANCE];
174174
const web::json::value &hand = object[HAND];
175175

176-
result.Name = name.as_string();
176+
result.Name = utility::conversions::to_utf16string(name.as_string());
177177
result.Balance = balance.as_double();
178178
result.Hand = BJHand(hand);
179179

Release/src/http/oauth/oauth2.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ pplx::task<void> oauth2_config::token_from_redirected_uri(const web::http::uri&
7979
auto state_param = query.find(oauth2_strings::state);
8080
if (state_param == query.end())
8181
{
82-
return pplx::task_from_exception<void>(oauth2_exception(U("parameter 'state' missing from redirected URI.")));
82+
return pplx::task_from_exception<void>(oauth2_exception("parameter 'state' missing from redirected URI."));
8383
}
8484
if (state() != state_param->second)
8585
{
86-
utility::ostringstream_t err;
86+
std::ostringstream err;
8787
err.imbue(std::locale::classic());
88-
err << U("redirected URI parameter 'state'='") << state_param->second
89-
<< U("' does not match state='") << state() << U("'.");
88+
err << "redirected URI parameter 'state'='" << utility::conversions::to_utf8string(state_param->second)
89+
<< "' does not match state='" << utility::conversions::to_utf8string(state()) << "'.";
9090
return pplx::task_from_exception<void>(oauth2_exception(err.str()));
9191
}
9292

@@ -101,7 +101,7 @@ pplx::task<void> oauth2_config::token_from_redirected_uri(const web::http::uri&
101101
auto token_param = query.find(oauth2_strings::access_token);
102102
if (token_param == query.end())
103103
{
104-
return pplx::task_from_exception<void>(oauth2_exception(U("either 'code' or 'access_token' parameter must be in the redirected URI.")));
104+
return pplx::task_from_exception<void>(oauth2_exception("either 'code' or 'access_token' parameter must be in the redirected URI."));
105105
}
106106

107107
set_token(token_param->second);
@@ -158,16 +158,16 @@ oauth2_token oauth2_config::_parse_token_from_json(const json::value& token_json
158158

159159
if (token_json.has_field(oauth2_strings::access_token))
160160
{
161-
result.set_access_token(token_json.at(oauth2_strings::access_token).as_string());
161+
result.set_access_token(utility::conversions::to_string_t(token_json.at(oauth2_strings::access_token).as_string()));
162162
}
163163
else
164164
{
165-
throw oauth2_exception(U("response json contains no 'access_token': ") + token_json.serialize());
165+
throw oauth2_exception("response json contains no 'access_token': " + token_json.serialize());
166166
}
167167

168168
if (token_json.has_field(oauth2_strings::token_type))
169169
{
170-
result.set_token_type(token_json.at(oauth2_strings::token_type).as_string());
170+
result.set_token_type(utility::conversions::to_string_t(token_json.at(oauth2_strings::token_type).as_string()));
171171
}
172172
else
173173
{
@@ -178,12 +178,12 @@ oauth2_token oauth2_config::_parse_token_from_json(const json::value& token_json
178178
}
179179
if (!utility::details::str_icmp(result.token_type(), oauth2_strings::bearer))
180180
{
181-
throw oauth2_exception(U("only 'token_type=bearer' access tokens are currently supported: ") + token_json.serialize());
181+
throw oauth2_exception("only 'token_type=bearer' access tokens are currently supported: " + token_json.serialize());
182182
}
183183

184184
if (token_json.has_field(oauth2_strings::refresh_token))
185185
{
186-
result.set_refresh_token(token_json.at(oauth2_strings::refresh_token).as_string());
186+
result.set_refresh_token(utility::conversions::to_string_t(token_json.at(oauth2_strings::refresh_token).as_string()));
187187
}
188188
else
189189
{
@@ -200,10 +200,7 @@ oauth2_token oauth2_config::_parse_token_from_json(const json::value& token_json
200200
{
201201
// Handle the case of a number as a JSON "string".
202202
// Using streams because std::stoll isn't avaliable on Android.
203-
int64_t expires;
204-
utility::istringstream_t iss(json_expires_in_val.as_string());
205-
iss.exceptions(std::ios::badbit | std::ios::failbit);
206-
iss >> expires;
203+
int64_t expires = utility::conversions::scan_string<int64_t>(json_expires_in_val.as_string());
207204
result.set_expires_in(expires);
208205
}
209206
}
@@ -217,7 +214,7 @@ oauth2_token oauth2_config::_parse_token_from_json(const json::value& token_json
217214
// The authorization server may return different scope from the one requested.
218215
// This however doesn't necessarily mean the token authorization scope is different.
219216
// See: http://tools.ietf.org/html/rfc6749#section-3.3
220-
result.set_scope(token_json.at(oauth2_strings::scope).as_string());
217+
result.set_scope(utility::conversions::to_string_t(token_json.at(oauth2_strings::scope).as_string()));
221218
}
222219
else
223220
{

0 commit comments

Comments
 (0)