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
|
#pragma once
|
||||||
|
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
|
#include <cmath>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
@@ -16,6 +17,11 @@ namespace Void::Json::Strict
|
|||||||
{
|
{
|
||||||
inline constexpr std::size_t default_maximum_depth{64U};
|
inline constexpr std::size_t default_maximum_depth{64U};
|
||||||
|
|
||||||
|
struct ParseOptions {
|
||||||
|
std::size_t maximumDepth{default_maximum_depth};
|
||||||
|
bool allowDecimals{true};
|
||||||
|
};
|
||||||
|
|
||||||
struct ParseError {
|
struct ParseError {
|
||||||
std::size_t offset{};
|
std::size_t offset{};
|
||||||
std::string message;
|
std::string message;
|
||||||
@@ -24,8 +30,8 @@ namespace Void::Json::Strict
|
|||||||
struct Value {
|
struct Value {
|
||||||
using Array = std::vector<Value>;
|
using Array = std::vector<Value>;
|
||||||
using Object = std::map<std::string, Value, std::less<>>;
|
using Object = std::map<std::string, Value, std::less<>>;
|
||||||
std::variant<std::nullptr_t, bool, std::int64_t, std::string, Array,
|
std::variant<std::nullptr_t, bool, std::int64_t, double,
|
||||||
Object>
|
std::string, Array, Object>
|
||||||
value;
|
value;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,8 +46,8 @@ namespace Void::Json::Strict
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
explicit Parser(const std::string_view input,
|
explicit Parser(const std::string_view input,
|
||||||
const std::size_t maximumDepth)
|
const ParseOptions options)
|
||||||
: input_{input}, maximumDepth_{maximumDepth}
|
: 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()
|
[[nodiscard]] Value parseNumber()
|
||||||
{
|
{
|
||||||
const std::size_t start = position_;
|
const std::size_t start = position_;
|
||||||
@@ -242,21 +263,52 @@ namespace Void::Json::Strict
|
|||||||
}
|
}
|
||||||
if (input_[position_] == '0') {
|
if (input_[position_] == '0') {
|
||||||
++position_;
|
++position_;
|
||||||
|
if (position_ < input_.size() &&
|
||||||
|
isDigit(input_[position_])) {
|
||||||
|
fail("leading zero in number");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (input_[position_] < '1' ||
|
if (input_[position_] < '1' ||
|
||||||
input_[position_] > '9') {
|
input_[position_] > '9') {
|
||||||
fail("invalid number");
|
fail("invalid number");
|
||||||
}
|
}
|
||||||
while (position_ < input_.size() &&
|
static_cast<void>(consumeDigits());
|
||||||
input_[position_] >= '0' &&
|
}
|
||||||
input_[position_] <= '9') {
|
bool isDecimal = false;
|
||||||
++position_;
|
if (position_ < input_.size() && input_[position_] == '.') {
|
||||||
|
isDecimal = true;
|
||||||
|
++position_;
|
||||||
|
if (consumeDigits() == 0U) {
|
||||||
|
fail("incomplete fractional part");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (position_ < input_.size() &&
|
if (position_ < input_.size() &&
|
||||||
(input_[position_] == '.' || input_[position_] == 'e' ||
|
(input_[position_] == 'e' || input_[position_] == 'E')) {
|
||||||
input_[position_] == 'E')) {
|
isDecimal = true;
|
||||||
fail("floating-point numbers are not supported");
|
++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{};
|
std::int64_t value{};
|
||||||
const auto result =
|
const auto result =
|
||||||
@@ -330,7 +382,8 @@ namespace Void::Json::Strict
|
|||||||
|
|
||||||
[[nodiscard]] Value parseValue(const std::size_t depth)
|
[[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");
|
fail("nesting limit or unexpected end of input");
|
||||||
}
|
}
|
||||||
switch (input_[position_]) {
|
switch (input_[position_]) {
|
||||||
@@ -351,8 +404,7 @@ namespace Void::Json::Strict
|
|||||||
return Value{nullptr};
|
return Value{nullptr};
|
||||||
default:
|
default:
|
||||||
if (input_[position_] == '-' ||
|
if (input_[position_] == '-' ||
|
||||||
(input_[position_] >= '0' &&
|
isDigit(input_[position_])) {
|
||||||
input_[position_] <= '9')) {
|
|
||||||
return parseNumber();
|
return parseNumber();
|
||||||
}
|
}
|
||||||
fail("invalid value");
|
fail("invalid value");
|
||||||
@@ -360,20 +412,27 @@ namespace Void::Json::Strict
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string_view input_;
|
std::string_view input_;
|
||||||
std::size_t maximumDepth_;
|
ParseOptions options_;
|
||||||
std::size_t position_{};
|
std::size_t position_{};
|
||||||
};
|
};
|
||||||
} // namespace Detail
|
} // namespace Detail
|
||||||
|
|
||||||
[[nodiscard]] inline std::expected<Value, ParseError>
|
[[nodiscard]] inline std::expected<Value, ParseError>
|
||||||
parse(const std::string_view input,
|
parse(const std::string_view input, const ParseOptions options)
|
||||||
const std::size_t maximumDepth = default_maximum_depth)
|
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return Detail::Parser{input, maximumDepth}.parse();
|
return Detail::Parser{input, options}.parse();
|
||||||
} catch (const Detail::ParseFailure& failure) {
|
} catch (const Detail::ParseFailure& failure) {
|
||||||
return std::unexpected{failure.error};
|
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
|
} // namespace Void::Json::Strict
|
||||||
@@ -2,34 +2,147 @@
|
|||||||
#include <Void/Testing/Assert.hpp>
|
#include <Void/Testing/Assert.hpp>
|
||||||
#include <Void/Testing/Test.hpp>
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cmath>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
Void::Testing::Test strict_json_preserves_signed_64_bit_integers()
|
Void::Testing::Test strict_json_preserves_signed_64_bit_integers()
|
||||||
{
|
{
|
||||||
const auto result = Void::Json::Strict::parse(
|
const auto result = Void::Json::Strict::parse(
|
||||||
R"({"minimum":-9223372036854775808,"maximum":9223372036854775807})");
|
R"({"minimum":-9223372036854775808,"maximum":9223372036854775807,"zero":0,"negZero":-0,"answer":42})");
|
||||||
Void::Testing::require(result.has_value());
|
Void::Testing::require(result.has_value());
|
||||||
const auto* object =
|
const auto* object =
|
||||||
std::get_if<Void::Json::Strict::Value::Object>(&result->value);
|
std::get_if<Void::Json::Strict::Value::Object>(&result->value);
|
||||||
Void::Testing::require(object != nullptr);
|
Void::Testing::require(object != nullptr);
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<std::int64_t>(object->at("minimum").value));
|
||||||
Void::Testing::expect_eq(
|
Void::Testing::expect_eq(
|
||||||
std::get<std::int64_t>(object->at("minimum").value), INT64_MIN);
|
std::get<std::int64_t>(object->at("minimum").value), INT64_MIN);
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<std::int64_t>(object->at("maximum").value));
|
||||||
Void::Testing::expect_eq(
|
Void::Testing::expect_eq(
|
||||||
std::get<std::int64_t>(object->at("maximum").value), INT64_MAX);
|
std::get<std::int64_t>(object->at("maximum").value), INT64_MAX);
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<std::int64_t>(object->at("zero").value));
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
std::get<std::int64_t>(object->at("zero").value), std::int64_t{0});
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<std::int64_t>(object->at("negZero").value));
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
std::get<std::int64_t>(object->at("negZero").value), std::int64_t{0});
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<std::int64_t>(object->at("answer").value));
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
std::get<std::int64_t>(object->at("answer").value), std::int64_t{42});
|
||||||
|
|
||||||
|
Void::Testing::expect(
|
||||||
|
!Void::Json::Strict::parse("9223372036854775808").has_value());
|
||||||
|
Void::Testing::expect(
|
||||||
|
!Void::Json::Strict::parse("-9223372036854775809").has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
Void::Testing::Test strict_json_rejects_duplicates_floats_and_deep_inputs()
|
Void::Testing::Test strict_json_rejects_duplicates_floats_when_disabled_and_deep_inputs()
|
||||||
{
|
{
|
||||||
Void::Testing::expect(
|
Void::Testing::expect(
|
||||||
!Void::Json::Strict::parse(R"({"key":1,"key":2})").has_value());
|
!Void::Json::Strict::parse(R"({"key":1,"key":2})").has_value());
|
||||||
|
constexpr Void::Json::Strict::ParseOptions noDecimals{
|
||||||
|
.allowDecimals = false,
|
||||||
|
};
|
||||||
Void::Testing::expect(
|
Void::Testing::expect(
|
||||||
!Void::Json::Strict::parse(R"({"value":1.5})").has_value());
|
!Void::Json::Strict::parse(R"({"value":1.5})", noDecimals).has_value());
|
||||||
|
Void::Testing::expect(
|
||||||
|
!Void::Json::Strict::parse("1e5", noDecimals).has_value());
|
||||||
Void::Testing::expect(
|
Void::Testing::expect(
|
||||||
!Void::Json::Strict::parse("[[[0]]]", 1U).has_value());
|
!Void::Json::Strict::parse("[[[0]]]", 1U).has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test strict_json_parses_lsp_decimals_and_exponents()
|
||||||
|
{
|
||||||
|
constexpr std::string_view kLspMessage =
|
||||||
|
R"({"jsonrpc":"2.0","id":1,"result":{"red":0.5,"green":0.25,"blue":1.0,"alpha":1.0,"offset":-0.125,"tiny":1e-3,"scaled":2.5E+2,"negZero":-0.0}})";
|
||||||
|
const auto parsed = Void::Json::Strict::parse(kLspMessage);
|
||||||
|
Void::Testing::require(parsed.has_value());
|
||||||
|
const auto* root =
|
||||||
|
std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
|
||||||
|
Void::Testing::require(root != nullptr);
|
||||||
|
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
std::get<std::string>(root->at("jsonrpc").value), std::string{"2.0"});
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<std::int64_t>(root->at("id").value));
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
std::get<std::int64_t>(root->at("id").value), std::int64_t{1});
|
||||||
|
|
||||||
|
const auto* color =
|
||||||
|
std::get_if<Void::Json::Strict::Value::Object>(&root->at("result").value);
|
||||||
|
Void::Testing::require(color != nullptr);
|
||||||
|
|
||||||
|
auto expect_decimal = [&](const std::string_view key, const double expected) {
|
||||||
|
const auto it = color->find(key);
|
||||||
|
Void::Testing::require(it != color->end());
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<double>(it->second.value));
|
||||||
|
Void::Testing::expect_eq(std::get<double>(it->second.value), expected);
|
||||||
|
};
|
||||||
|
|
||||||
|
expect_decimal("red", 0.5);
|
||||||
|
expect_decimal("green", 0.25);
|
||||||
|
expect_decimal("blue", 1.0);
|
||||||
|
expect_decimal("alpha", 1.0);
|
||||||
|
expect_decimal("offset", -0.125);
|
||||||
|
expect_decimal("tiny", 1e-3);
|
||||||
|
expect_decimal("scaled", 250.0);
|
||||||
|
|
||||||
|
const auto negZeroIt = color->find("negZero");
|
||||||
|
Void::Testing::require(negZeroIt != color->end());
|
||||||
|
Void::Testing::require(
|
||||||
|
std::holds_alternative<double>(negZeroIt->second.value));
|
||||||
|
const double negZero = std::get<double>(negZeroIt->second.value);
|
||||||
|
Void::Testing::expect_eq(negZero, 0.0);
|
||||||
|
Void::Testing::expect(std::signbit(negZero));
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test strict_json_rejects_malformed_numbers()
|
||||||
|
{
|
||||||
|
constexpr std::array<std::string_view, 20> kMalformed{
|
||||||
|
"1.", "-0.", ".5", "-.5", "1.e5",
|
||||||
|
"1e", "1e+", "1E-", "1.5e", "01",
|
||||||
|
"01.5", "-01.5", "+1", "+1.5", "1e309",
|
||||||
|
"-1e309", "1e-400", "NaN", "Infinity", "-Infinity",
|
||||||
|
};
|
||||||
|
for (const std::string_view input : kMalformed) {
|
||||||
|
Void::Testing::expect(!Void::Json::Strict::parse(input).has_value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test strict_json_value_constructors_select_expected_alternatives()
|
||||||
|
{
|
||||||
|
using Void::Json::Strict::Value;
|
||||||
|
const Value nilVal{nullptr};
|
||||||
|
const Value boolVal{true};
|
||||||
|
const Value intVal{42};
|
||||||
|
const Value i64Val{std::numeric_limits<std::int64_t>::max()};
|
||||||
|
const Value floatVal{1.5F};
|
||||||
|
const Value doubleVal{2.25};
|
||||||
|
const Value strVal{std::string{"hello"}};
|
||||||
|
|
||||||
|
Void::Testing::expect(std::holds_alternative<std::nullptr_t>(nilVal.value));
|
||||||
|
Void::Testing::expect(std::holds_alternative<bool>(boolVal.value));
|
||||||
|
Void::Testing::expect(std::holds_alternative<std::int64_t>(intVal.value));
|
||||||
|
Void::Testing::expect_eq(std::get<std::int64_t>(intVal.value),
|
||||||
|
std::int64_t{42});
|
||||||
|
Void::Testing::expect(std::holds_alternative<std::int64_t>(i64Val.value));
|
||||||
|
Void::Testing::expect(std::holds_alternative<double>(floatVal.value));
|
||||||
|
Void::Testing::expect_eq(std::get<double>(floatVal.value), 1.5);
|
||||||
|
Void::Testing::expect(std::holds_alternative<double>(doubleVal.value));
|
||||||
|
Void::Testing::expect_eq(std::get<double>(doubleVal.value), 2.25);
|
||||||
|
Void::Testing::expect(std::holds_alternative<std::string>(strVal.value));
|
||||||
|
}
|
||||||
|
|
||||||
Void::Testing::Test strict_json_decodes_valid_unicode_pairs()
|
Void::Testing::Test strict_json_decodes_valid_unicode_pairs()
|
||||||
{
|
{
|
||||||
const auto result = Void::Json::Strict::parse(R"("\uD83D\uDE03")");
|
const auto result = Void::Json::Strict::parse(R"("\uD83D\uDE03")");
|
||||||
|
|||||||
Reference in new issue
Block a user