diff --git a/include/xrpl/json/json_value.h b/include/xrpl/json/json_value.h index 9c31af037e..ccae3154d4 100644 --- a/include/xrpl/json/json_value.h +++ b/include/xrpl/json/json_value.h @@ -217,6 +217,7 @@ public: Value(UInt value); Value(double value); Value(const char* value); + Value(ripple::Number const& value); /** \brief Constructs a value from a static string. * Like other value string constructor but do not duplicate the string for @@ -240,9 +241,15 @@ public: operator=(Value&& other); template - requires(!std::convertible_to) Value& - operator=(T const& rhs); + operator=(T const& rhs) + requires( + !std::is_convertible_v && + std::is_convertible_v())), Value>) + { + *this = to_json(rhs); + return *this; + } Value(Value&& other) noexcept; @@ -444,6 +451,12 @@ private: int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. }; +inline Value +to_json(ripple::Number const& number) +{ + return to_string(number); +} + bool operator==(const Value&, const Value&); @@ -689,48 +702,6 @@ public: } }; -// https://ericniebler.com/2014/10/21/customization-point-design-in-c11-and-beyond/ -namespace detail { - -inline Value -to_json(ripple::Number const& number) -{ - return to_string(number); -} - -struct to_json_fn -{ - template - Value - operator()(T&& t) const - { - return to_json(std::forward(t)); - } -}; - -template -struct static_const -{ - static constexpr T value = {}; -}; - -} // namespace detail - -namespace { - -constexpr auto const& to_json = detail::static_const::value; - -} - -template - requires(!std::convertible_to) -Value& -Value::operator=(T const& rhs) -{ - *this = to_json(rhs); - return *this; -} - } // namespace Json #endif // CPPTL_JSON_H_INCLUDED diff --git a/src/libxrpl/json/json_value.cpp b/src/libxrpl/json/json_value.cpp index 6145f0e640..69ec2eace7 100644 --- a/src/libxrpl/json/json_value.cpp +++ b/src/libxrpl/json/json_value.cpp @@ -231,6 +231,13 @@ Value::Value(const char* value) : type_(stringValue), allocated_(true) value_.string_ = valueAllocator()->duplicateStringValue(value); } +Value::Value(ripple::Number const& value) : type_(stringValue), allocated_(true) +{ + auto const tmp = to_string(value); + value_.string_ = + valueAllocator()->duplicateStringValue(tmp.c_str(), tmp.length()); +} + Value::Value(std::string const& value) : type_(stringValue), allocated_(true) { value_.string_ = valueAllocator()->duplicateStringValue( diff --git a/src/test/jtx/basic_prop.h b/src/test/jtx/basic_prop.h index 742b8744ef..a8daafba41 100644 --- a/src/test/jtx/basic_prop.h +++ b/src/test/jtx/basic_prop.h @@ -20,6 +20,8 @@ #ifndef RIPPLE_TEST_JTX_BASIC_PROP_H_INCLUDED #define RIPPLE_TEST_JTX_BASIC_PROP_H_INCLUDED +#include + namespace ripple { namespace test { namespace jtx {