Simplify conversions to Json::Value

Also remove potential ODR violation from json_value.h
This commit is contained in:
Bronek Kozicki
2024-12-18 14:34:57 +00:00
parent b9f1200652
commit 241429ba32
3 changed files with 24 additions and 44 deletions

View File

@@ -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 <typename T>
requires(!std::convertible_to<T, Value>)
Value&
operator=(T const& rhs);
operator=(T const& rhs)
requires(
!std::is_convertible_v<T, Value> &&
std::is_convertible_v<decltype(to_json(std::declval<T>())), 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 <typename T>
Value
operator()(T&& t) const
{
return to_json(std::forward<T>(t));
}
};
template <typename T>
struct static_const
{
static constexpr T value = {};
};
} // namespace detail
namespace {
constexpr auto const& to_json = detail::static_const<detail::to_json_fn>::value;
}
template <typename T>
requires(!std::convertible_to<T, Value>)
Value&
Value::operator=(T const& rhs)
{
*this = to_json(rhs);
return *this;
}
} // namespace Json
#endif // CPPTL_JSON_H_INCLUDED

View File

@@ -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(

View File

@@ -20,6 +20,8 @@
#ifndef RIPPLE_TEST_JTX_BASIC_PROP_H_INCLUDED
#define RIPPLE_TEST_JTX_BASIC_PROP_H_INCLUDED
#include <memory>
namespace ripple {
namespace test {
namespace jtx {