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
33 changes: 29 additions & 4 deletions include/jsoncons_ext/jmespath/jmespath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,9 @@ namespace detail {
template <typename Json>
class jmespath_evaluator
{
static constexpr double max_double_to_int64 = 9223372036854775807.0;
static constexpr double min_double_to_int64 = -9223372036854775808.0;

public:
typedef typename Json::char_type char_type;
typedef typename Json::char_traits_type char_traits_type;
Expand Down Expand Up @@ -1387,11 +1390,22 @@ namespace detail {
case json_type::uint64:
case json_type::int64:
{
return *context.create_json(arg0.template as<double>());
return arg0;
}
case json_type::float64:
{
return *context.create_json(std::ceil(arg0.template as<double>()));
auto dbl0 = arg0.template as<double>();
if (!std::isfinite(dbl0)) {
ec = jmespath_errc::invalid_type;
return context.null_value();
}
dbl0 = std::ceil(dbl0);
if (dbl0 >= min_double_to_int64 && dbl0 <= max_double_to_int64) {
return *context.create_json(static_cast<int64_t>(dbl0));
}
else {
return *context.create_json(dbl0);
}
}
default:
ec = jmespath_errc::invalid_type;
Expand Down Expand Up @@ -1523,11 +1537,22 @@ namespace detail {
case json_type::uint64:
case json_type::int64:
{
return *context.create_json(arg0.template as<double>());
return *context.create_json(arg0);
}
case json_type::float64:
{
return *context.create_json(std::floor(arg0.template as<double>()));
auto dbl0 = arg0.template as<double>();
if (!std::isfinite(dbl0)) {
ec = jmespath_errc::invalid_type;
return context.null_value();
}
dbl0 = std::floor(dbl0);
if (dbl0 >= min_double_to_int64 && dbl0 <= max_double_to_int64) {
return *context.create_json(static_cast<int64_t>(dbl0));
}
else {
return *context.create_json(dbl0);
}
}
default:
ec = jmespath_errc::invalid_type;
Expand Down
50 changes: 50 additions & 0 deletions test/jmespath/input/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,56 @@
"result": { "value": false }
}
]
},
{
"given": {},
"cases": [
{
"comment": "Floor function applied to a float should return integer",
"expression": "to_string(floor(`3.7`))",
"result": "3"
},
{
"comment": "Floor function applied to a negative float should return integer",
"expression": "to_string(floor(`-3.7`))",
"result": "-4"
},
{
"comment": "Floor function applied to an integer should return the same integer",
"expression": "to_string(floor(`3`))",
"result": "3"
},
{
"comment": "Floor function applied to a negative integer should return the same integer",
"expression": "to_string(floor(`-3`))",
"result": "-3"
},
{
"comment": "Ceil function applied to a float should return integer",
"expression": "to_string(ceil(`3.7`))",
"result": "4"
},
{
"comment": "Ceil function applied to a negative float should return integer",
"expression": "to_string(ceil(`-3.7`))",
"result": "-3"
},
{
"comment": "Ceil function applied to an integer should return the same integer",
"expression": "to_string(ceil(`3`))",
"result": "3"
},
{
"comment": "Ceil function applied to a negative integer should return the same integer",
"expression": "to_string(ceil(`-3`))",
"result": "-3"
},
{
"comment": "Ceil function applied to a large double not in int64_t range should return as double",
"expression": "ceil(`1e20`)",
"result": 1e20
}
]
}
]