Skip to content

Commit a895ce1

Browse files
xpahosmiloyip
andauthored
Allow escaped apostrophe in values (Tencent#1639)
* Allow escaped apostrophe in values * Allow escaped apostrophe in values * Canonical flag name * Add translation for escaped apostrophe Co-authored-by: Milo Yip <[email protected]>
1 parent 418331e commit a895ce1

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

doc/dom.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Parse flags | Meaning
119119
`kParseNumbersAsStringsFlag` | Parse numerical type values as strings.
120120
`kParseTrailingCommasFlag` | Allow trailing commas at the end of objects and arrays (relaxed JSON syntax).
121121
`kParseNanAndInfFlag` | Allow parsing `NaN`, `Inf`, `Infinity`, `-Inf` and `-Infinity` as `double` values (relaxed JSON syntax).
122+
`kParseEscapedApostropheFlag` | Allow escaped apostrophe `\'` in strings (relaxed JSON syntax).
122123
123124
By using a non-type template parameter, instead of a function parameter, C++ compiler can generate code which is optimized for specified combinations, improving speed, and reducing code size (if only using a single specialization). The downside is the flags needed to be determined in compile-time.
124125

doc/dom.zh-cn.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ GenericDocument& GenericDocument::Parse(const Ch* str);
119119
`kParseNumbersAsStringsFlag` | 把数字类型解析成字符串。
120120
`kParseTrailingCommasFlag` | 容许在对象和数组结束前含有逗号(放宽的 JSON 语法)。
121121
`kParseNanAndInfFlag` | 容许 `NaN`、`Inf`、`Infinity`、`-Inf` 及 `-Infinity` 作为 `double` 值(放宽的 JSON 语法)。
122+
`kParseEscapedApostropheFlag` | 容许字符串中转义单引号 `\'` (放宽的 JSON 语法)。
122123
123124
由于使用了非类型模板参数,而不是函数参数,C++ 编译器能为个别组合生成代码,以改善性能及减少代码尺寸(当只用单种特化)。缺点是需要在编译期决定标志。
124125

include/rapidjson/reader.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ enum ParseFlag {
154154
kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings.
155155
kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays.
156156
kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles.
157+
kParseEscapedApostropheFlag = 512, //!< Allow escaped apostrophe in strings.
157158
kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS
158159
};
159160

@@ -991,7 +992,7 @@ class GenericReader {
991992
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
992993
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
993994
static const char escape[256] = {
994-
Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/',
995+
Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '/',
995996
Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0,
996997
0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0,
997998
0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -1014,6 +1015,10 @@ class GenericReader {
10141015
is.Take();
10151016
os.Put(static_cast<typename TEncoding::Ch>(escape[static_cast<unsigned char>(e)]));
10161017
}
1018+
else if ((parseFlags & kParseEscapedApostropheFlag) && RAPIDJSON_LIKELY(e == '\'')) { // Allow escaped apostrophe
1019+
is.Take();
1020+
os.Put('\'');
1021+
}
10171022
else if (RAPIDJSON_LIKELY(e == 'u')) { // Unicode
10181023
is.Take();
10191024
unsigned codepoint = ParseHex4(is, escapeOffset);

test/unittest/readertest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,4 +2198,28 @@ TEST(Reader, ParseNanAndInfinity) {
21982198
#undef TEST_NAN_INF
21992199
}
22002200

2201+
TEST(Reader, EscapedApostrophe) {
2202+
const char json[] = " { \"foo\": \"bar\\'buzz\" } ";
2203+
2204+
BaseReaderHandler<> h;
2205+
2206+
{
2207+
StringStream s(json);
2208+
Reader reader;
2209+
ParseResult r = reader.Parse<kParseNoFlags>(s, h);
2210+
EXPECT_TRUE(reader.HasParseError());
2211+
EXPECT_EQ(kParseErrorStringEscapeInvalid, r.Code());
2212+
EXPECT_EQ(14u, r.Offset());
2213+
}
2214+
2215+
{
2216+
StringStream s(json);
2217+
Reader reader;
2218+
ParseResult r = reader.Parse<kParseEscapedApostropheFlag>(s, h);
2219+
EXPECT_FALSE(reader.HasParseError());
2220+
EXPECT_EQ(kParseErrorNone, r.Code());
2221+
EXPECT_EQ(0u, r.Offset());
2222+
}
2223+
}
2224+
22012225
RAPIDJSON_DIAG_POP

0 commit comments

Comments
 (0)