Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/sonic/dom/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ enum class ParseFlags : uint32_t {
// native numeric representation. In-range floating-point numbers stay
// double; in-range integers stay int64/uint64.
kParseOverflowNumAsNumStr = 1 << 3,
// Use the legacy number-string policy: preserve floating-point and overflow
// numbers as NumStr while keeping in-range integers as int64/uint64.
kParseFloatAndOverflowNumAsNumStr = 1 << 4,
Comment thread
howz97 marked this conversation as resolved.
Resolved
};

// Compatibility layer for downstream users.
Expand Down
4 changes: 4 additions & 0 deletions include/sonic/dom/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ class Parser {

template <typename SAX>
sonic_force_inline bool parseNumber(SAX &sax) {
if constexpr (parseFlags & ParseFlags::kParseFloatAndOverflowNumAsNumStr) {
return parseNumberAsString(sax);
Comment thread
howz97 marked this conversation as resolved.
Resolved
}

// These helper macros are used only within this function.
// Define/undefine them locally to avoid leaking into includers.
#undef FLOATING_LONGEST_DIGITS
Expand Down
25 changes: 25 additions & 0 deletions tests/jsonpath_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,4 +893,29 @@ TEST(JsonPathDump, SerializeAcceptsConstResult) {
EXPECT_EQ(std::get<1>(dumped), kErrorNone);
EXPECT_EQ(std::get<0>(dumped), "abc");
}

TEST(JsonPathDump, NumericValuesPreserveOriginalText) {
const std::vector<std::string> numbers = {
"221.5222225222225222",
"1e10",
"1e400",
};
for (const auto& number : numbers) {
SCOPED_TRACE(number);
Document doc;
doc.Parse<ParseFlags::kParseFloatAndOverflowNumAsNumStr>(
"{\"v\":" + number + "}");
ASSERT_FALSE(doc.HasParseError());

Document& cdoc = doc;
const auto result = cdoc.AtJsonPath("$.v");
ASSERT_EQ(result.error, kErrorNone);
ASSERT_EQ(result.nodes.size(), 1);

sonic_json::WriteBuffer wb;
const auto err = result.nodes[0]->Serialize(wb);
ASSERT_EQ(err, kErrorNone);
EXPECT_EQ(wb.ToStringView(), number);
}
}
} // namespace
32 changes: 32 additions & 0 deletions tests/parsenumber_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,38 @@ TEST(ParserTest, ParseOverflowNumAsNumStr_InRangeIntegerKeepsType) {
}
}

TEST(ParserTest, ParseNumAsStrUsesLegacyPolicy) {
const std::vector<std::string> string_numbers = {
"1.5", "1e10", "18446744073709551616", "1e400"};

for (const auto& input : string_numbers) {
SCOPED_TRACE(input);
Document doc;
doc.Parse<ParseFlags::kParseFloatAndOverflowNumAsNumStr>(input.data(),
input.size());
ASSERT_FALSE(doc.HasParseError());
ASSERT_TRUE(doc.IsStringNumber());
EXPECT_EQ(doc.GetStringView(), input);
EXPECT_EQ(doc.Dump(), input);
}

{
Document doc;
doc.Parse<ParseFlags::kParseFloatAndOverflowNumAsNumStr>("42");
ASSERT_FALSE(doc.HasParseError());
ASSERT_TRUE(doc.IsUint64());
EXPECT_EQ(doc.GetUint64(), 42U);
}

{
Document doc;
doc.Parse<ParseFlags::kParseFloatAndOverflowNumAsNumStr>("-42");
ASSERT_FALSE(doc.HasParseError());
ASSERT_TRUE(doc.IsInt64());
EXPECT_EQ(doc.GetInt64(), -42);
}
}

TEST(ParserTest, ParseStringNumber_ZeroExponentStaysDouble) {
Document doc;
std::string input = "0e+123";
Expand Down
10 changes: 10 additions & 0 deletions tests/parser_oom_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,16 @@ TEST(Parser, NumberOverflowAsNumStrRejectionReportsSaxTermination) {
EXPECT_EQ(kSaxTermination, res.Error());
}

TEST(Parser, NumberAsNumStrRejectionReportsSaxTermination) {
const char* json = "1e10";
auto buf = pad_json_for_parser(json, std::strlen(json));
RejectingSAX sax;
sax.reject_numstr = true;
Parser<ParseFlags::kParseFloatAndOverflowNumAsNumStr> p;
auto res = p.Parse(buf.data(), std::strlen(json), sax);
EXPECT_EQ(kSaxTermination, res.Error());
}

TEST(Parser, NullRejectionReportsSaxTermination) {
const char* json = "null";
auto buf = pad_json_for_parser(json, std::strlen(json));
Expand Down
Loading