feat(data): support RFC 8259 decimal numbers in StrictJson
This commit is contained in:
1 parent
9931c5ffc4
commit
acd77ee67a
2 files changed
+193
-21
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
@@ -16,6 +17,11 @@ namespace Void::Json::Strict
|
||||
{
|
||||
inline constexpr std::size_t default_maximum_depth{64U};
|
||||
|
||||
struct ParseOptions {
|
||||
std::size_t maximumDepth{default_maximum_depth};
|
||||
bool allowDecimals{true};
|
||||
};
|
||||
|
||||
struct ParseError {
|
||||
std::size_t offset{};
|
||||
std::string message;
|
||||
@@ -24,8 +30,8 @@ namespace Void::Json::Strict
|
||||
struct Value {
|
||||
using Array = std::vector<Value>;
|
||||
using Object = std::map<std::string, Value, std::less<>>;
|
||||
std::variant<std::nullptr_t, bool, std::int64_t, std::string, Array,
|
||||
Object>
|
||||
std::variant<std::nullptr_t, bool, std::int64_t, double,
|
||||
std::string, Array, Object>
|
||||
value;
|
||||
};
|
||||
|
||||
@@ -40,8 +46,8 @@ namespace Void::Json::Strict
|
||||
public:
|
||||
|
||||
explicit Parser(const std::string_view input,
|
||||
const std::size_t maximumDepth)
|
||||
: input_{input}, maximumDepth_{maximumDepth}
|
||||
const ParseOptions options)
|
||||
: input_{input}, options_{options}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -231,6 +237,21 @@ namespace Void::Json::Strict
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] static bool isDigit(const char ch) noexcept
|
||||
{
|
||||
return ch >= '0' && ch <= '9';
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t consumeDigits() noexcept
|
||||
{
|
||||
const std::size_t begin = position_;
|
||||
while (position_ < input_.size() &&
|
||||
isDigit(input_[position_])) {
|
||||
++position_;
|
||||
}
|
||||
return position_ - begin;
|
||||
}
|
||||
|
||||
[[nodiscard]] Value parseNumber()
|
||||
{
|
||||
const std::size_t start = position_;
|
||||
@@ -242,21 +263,52 @@ namespace Void::Json::Strict
|
||||
}
|
||||
if (input_[position_] == '0') {
|
||||
++position_;
|
||||
if (position_ < input_.size() &&
|
||||
isDigit(input_[position_])) {
|
||||
fail("leading zero in number");
|
||||
}
|
||||
} else {
|
||||
if (input_[position_] < '1' ||
|
||||
input_[position_] > '9') {
|
||||
fail("invalid number");
|
||||
}
|
||||
while (position_ < input_.size() &&
|
||||
input_[position_] >= '0' &&
|
||||
input_[position_] <= '9') {
|
||||
++position_;
|
||||
static_cast<void>(consumeDigits());
|
||||
}
|
||||
bool isDecimal = false;
|
||||
if (position_ < input_.size() && input_[position_] == '.') {
|
||||
isDecimal = true;
|
||||
++position_;
|
||||
if (consumeDigits() == 0U) {
|
||||
fail("incomplete fractional part");
|
||||
}
|
||||
}
|
||||
if (position_ < input_.size() &&
|
||||
(input_[position_] == '.' || input_[position_] == 'e' ||
|
||||
input_[position_] == 'E')) {
|
||||
fail("floating-point numbers are not supported");
|
||||
(input_[position_] == 'e' || input_[position_] == 'E')) {
|
||||
isDecimal = true;
|
||||
++position_;
|
||||
if (position_ < input_.size() &&
|
||||
(input_[position_] == '+' ||
|
||||
input_[position_] == '-')) {
|
||||
++position_;
|
||||
}
|
||||
if (consumeDigits() == 0U) {
|
||||
fail("incomplete exponent");
|
||||
}
|
||||
}
|
||||
if (isDecimal) {
|
||||
if (!options_.allowDecimals) {
|
||||
fail("floating-point numbers are not supported");
|
||||
}
|
||||
double value{};
|
||||
const auto result = std::from_chars(
|
||||
input_.data() + start, input_.data() + position_,
|
||||
value, std::chars_format::general);
|
||||
if (result.ec != std::errc{} ||
|
||||
result.ptr != input_.data() + position_ ||
|
||||
!std::isfinite(value)) {
|
||||
fail("number is out of range");
|
||||
}
|
||||
return Value{value};
|
||||
}
|
||||
std::int64_t value{};
|
||||
const auto result =
|
||||
@@ -330,7 +382,8 @@ namespace Void::Json::Strict
|
||||
|
||||
[[nodiscard]] Value parseValue(const std::size_t depth)
|
||||
{
|
||||
if (depth > maximumDepth_ || position_ >= input_.size()) {
|
||||
if (depth > options_.maximumDepth ||
|
||||
position_ >= input_.size()) {
|
||||
fail("nesting limit or unexpected end of input");
|
||||
}
|
||||
switch (input_[position_]) {
|
||||
@@ -351,8 +404,7 @@ namespace Void::Json::Strict
|
||||
return Value{nullptr};
|
||||
default:
|
||||
if (input_[position_] == '-' ||
|
||||
(input_[position_] >= '0' &&
|
||||
input_[position_] <= '9')) {
|
||||
isDigit(input_[position_])) {
|
||||
return parseNumber();
|
||||
}
|
||||
fail("invalid value");
|
||||
@@ -360,20 +412,27 @@ namespace Void::Json::Strict
|
||||
}
|
||||
|
||||
std::string_view input_;
|
||||
std::size_t maximumDepth_;
|
||||
ParseOptions options_;
|
||||
std::size_t position_{};
|
||||
};
|
||||
} // namespace Detail
|
||||
|
||||
[[nodiscard]] inline std::expected<Value, ParseError>
|
||||
parse(const std::string_view input,
|
||||
const std::size_t maximumDepth = default_maximum_depth)
|
||||
parse(const std::string_view input, const ParseOptions options)
|
||||
{
|
||||
try {
|
||||
return Detail::Parser{input, maximumDepth}.parse();
|
||||
return Detail::Parser{input, options}.parse();
|
||||
} catch (const Detail::ParseFailure& failure) {
|
||||
return std::unexpected{failure.error};
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::expected<Value, ParseError>
|
||||
parse(const std::string_view input,
|
||||
const std::size_t maximumDepth = default_maximum_depth)
|
||||
{
|
||||
return parse(input, ParseOptions{.maximumDepth = maximumDepth,
|
||||
.allowDecimals = true});
|
||||
}
|
||||
|
||||
} // namespace Void::Json::Strict
|
||||
Reference in new issue
Block a user