Skip to content

Commit f0c5d7c

Browse files
committed
Adding test for json strings with escaped unicode, code points above 127.
1 parent ae63e64 commit f0c5d7c

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

Release/src/json/json_parsing.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/***
22
* ==++==
33
*
4-
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Copyright (c) Microsoft Corporation. All rights reserved.
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,14 +31,14 @@
3131
#include <array>
3232

3333
#if defined(_MSC_VER)
34-
#pragma warning(disable : 4127) // allow expressions like while(true) pass
34+
#pragma warning(disable : 4127) // allow expressions like while(true) pass
3535
#endif
3636
using namespace web;
3737
using namespace web::json;
3838
using namespace utility;
3939
using namespace utility::conversions;
4040

41-
std::array<signed char,128> _hexval = {{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41+
std::array<signed char,128> _hexval = {{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4242
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4343
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4444
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
@@ -74,7 +74,7 @@ template <typename CharType>
7474
class JSON_Parser
7575
{
7676
public:
77-
JSON_Parser()
77+
JSON_Parser()
7878
: m_currentLine(1),
7979
m_eof(std::char_traits<CharType>::eof()),
8080
m_currentColumn(1),
@@ -171,15 +171,15 @@ class JSON_Parser
171171
void CreateToken(typename JSON_Parser<CharType>::Token& tk, typename Token::Kind kind, Location &start)
172172
{
173173
tk.kind = kind;
174-
tk.start = start;
174+
tk.start = start;
175175
tk.string_val.clear();
176176
}
177177

178178
void CreateToken(typename JSON_Parser<CharType>::Token& tk, typename Token::Kind kind)
179179
{
180180
tk.kind = kind;
181-
tk.start.m_line = m_currentLine;
182-
tk.start.m_column = m_currentColumn;
181+
tk.start.m_line = m_currentLine;
182+
tk.start.m_column = m_currentColumn;
183183
tk.string_val.clear();
184184
}
185185

@@ -400,16 +400,16 @@ namespace
400400
}
401401
#endif
402402

403-
static double anystod(const char* str)
403+
static double anystod(const char* str)
404404
{
405405
#ifdef _MS_WINDOWS
406406
return _strtod_l(str, nullptr, utility::details::scoped_c_thread_locale::c_locale());
407407
#else
408-
return strtod(str, nullptr);
408+
return strtod(str, nullptr);
409409
#endif
410410
}
411-
static double anystod(const wchar_t* str)
412-
{
411+
static double anystod(const wchar_t* str)
412+
{
413413
#ifdef _MS_WINDOWS
414414
return _wcstod_l(str, nullptr, utility::details::scoped_c_thread_locale::c_locale());
415415
#else
@@ -726,7 +726,7 @@ inline bool JSON_Parser<CharType>::handle_unescape_char(Token &token)
726726
return true;
727727
case 'u':
728728
{
729-
// A four-hexdigit unicode character
729+
// A four-hexdigit Unicode character
730730
int decoded = 0;
731731
for (int i = 0; i < 4; ++i)
732732
{
@@ -961,14 +961,14 @@ std::unique_ptr<web::json::details::_Object> JSON_Parser<CharType>::_ParseObject
961961
auto obj = utility::details::make_unique<web::json::details::_Object>(g_keep_json_object_unsorted);
962962
auto& elems = obj->m_object.m_elements;
963963

964-
if ( tkn.kind != JSON_Parser<CharType>::Token::TKN_CloseBrace )
964+
if ( tkn.kind != JSON_Parser<CharType>::Token::TKN_CloseBrace )
965965
{
966966
while ( true )
967967
{
968968
// State 1: New field or end of object, looking for field name or closing brace
969969

970970
std::basic_string<CharType> fieldName;
971-
971+
972972
switch ( tkn.kind )
973973
{
974974
case JSON_Parser<CharType>::Token::TKN_StringLiteral:
@@ -1033,7 +1033,7 @@ std::unique_ptr<web::json::details::_Array> JSON_Parser<CharType>::_ParseArray(t
10331033

10341034
auto result = utility::details::make_unique<web::json::details::_Array>();
10351035

1036-
if ( tkn.kind != JSON_Parser<CharType>::Token::TKN_CloseBracket )
1036+
if ( tkn.kind != JSON_Parser<CharType>::Token::TKN_CloseBracket )
10371037
{
10381038
while ( true )
10391039
{
@@ -1091,7 +1091,7 @@ std::unique_ptr<web::json::details::_Value> JSON_Parser<CharType>::_ParseValue(t
10911091
return std::move(value);
10921092
}
10931093

1094-
case JSON_Parser<CharType>::Token::TKN_NumberLiteral:
1094+
case JSON_Parser<CharType>::Token::TKN_NumberLiteral:
10951095
{
10961096
auto value = utility::details::make_unique<web::json::details::_Number>(tkn.double_val);
10971097
GetNextToken(tkn);

Release/tests/Functional/json/parsing_tests.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/***
22
* ==++==
33
*
4-
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Copyright (c) Microsoft Corporation. All rights reserved.
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -202,13 +202,20 @@ TEST(string_t)
202202

203203
str = json::value::parse(U("\"\\t\""));
204204
VERIFY_ARE_EQUAL(U("\t"), str.as_string());
205+
}
205206

206-
str = json::value::parse(U("\"\\u0041\""));
207+
TEST(escaped_unicode_string)
208+
{
209+
auto str = json::value::parse(U("\"\\u0041\""));
207210
VERIFY_ARE_EQUAL(U("A"), str.as_string());
208211

209212
str = json::value::parse(U("\"\\u004B\""));
210213
VERIFY_ARE_EQUAL(U("K"), str.as_string());
211214

215+
str = json::value::parse(U("\"\\u20AC\""));
216+
const auto euro = to_string_t("\xE2\x82\xAC");
217+
VERIFY_ARE_EQUAL(euro, str.as_string());
218+
212219
VERIFY_PARSING_THROW(json::value::parse(U("\"\\u0klB\"")));
213220
}
214221

0 commit comments

Comments
 (0)