Skip to content

Commit

Permalink
fix: build error of custom json conversion (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: zzyyyl <[email protected]>
  • Loading branch information
MistEO and zzyyyl authored Oct 26, 2023
1 parent f3a8f21 commit 2f4d842
Showing 1 changed file with 34 additions and 59 deletions.
93 changes: 34 additions & 59 deletions include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ class basic_value
template <typename... args_t>
basic_value(value_type type, args_t&&... args);

// Prohibit conversion of other types to basic_value
template <typename value_t>
template <typename value_t, typename _ = std::enable_if_t<!std::is_convertible_v<value_t, basic_value<string_t>>>>
basic_value(value_t) = delete;

// I don't know if you want to convert char to string or number, so I delete these constructors.
basic_value(char) = delete;
basic_value(wchar_t) = delete;
basic_value(char16_t) = delete;
basic_value(char32_t) = delete;

~basic_value();

bool valid() const noexcept { return _type != value_type::invalid; }
Expand Down Expand Up @@ -261,19 +266,18 @@ class basic_array
basic_array() = default;
basic_array(const basic_array<string_t>& rhs) = default;
basic_array(basic_array<string_t>&& rhs) noexcept = default;
basic_array(const raw_array& arr);
basic_array(raw_array&& arr) noexcept;
basic_array(std::initializer_list<value_type> init_list);
basic_array(typename raw_array::size_type size);

explicit basic_array(const basic_value<string_t>& val);
explicit basic_array(basic_value<string_t>&& val);
template <typename array_t, typename _ = std::enable_if_t<
std::is_constructible_v<value_type, typename std::decay_t<array_t>::value_type>>>
basic_array(array_t arr)
{
_array_data.assign(std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()));
}

template <typename collection_t,
typename _ = std::enable_if_t<std::is_constructible_v<
value_type, typename std::iterator_traits<typename collection_t::iterator>::value_type>>>
basic_array(collection_t arr)
: _array_data(std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()))
{}

~basic_array() noexcept = default;

Expand Down Expand Up @@ -377,17 +381,13 @@ class basic_object
basic_object() = default;
basic_object(const basic_object<string_t>& rhs) = default;
basic_object(basic_object<string_t>&& rhs) noexcept = default;
basic_object(const raw_object& raw_obj);
basic_object(raw_object&& raw_obj);
basic_object(std::initializer_list<value_type> init_list);
explicit basic_object(const basic_value<string_t>& val);
explicit basic_object(basic_value<string_t>&& val);
template <typename map_t, typename _ = std::enable_if_t<
std::is_constructible_v<value_type, typename std::decay_t<map_t>::value_type>>>
basic_object(map_t map)
{
_object_data.insert(std::make_move_iterator(map.begin()), std::make_move_iterator(map.end()));
}
template <typename map_t, typename _ = std::enable_if_t<std::is_constructible_v<
value_type, typename std::iterator_traits<typename map_t::iterator>::value_type>>>
basic_object(map_t map) : _object_data(std::make_move_iterator(map.begin()), std::make_move_iterator(map.end()))
{}

~basic_object() = default;

Expand Down Expand Up @@ -1332,18 +1332,6 @@ MEOJSON_INLINE typename basic_value<string_t>::var_t basic_value<string_t>::deep
// * basic_array impl *
// ******************************

template <typename string_t>
MEOJSON_INLINE basic_array<string_t>::basic_array(const raw_array& arr) : _array_data(arr)
{
;
}

template <typename string_t>
MEOJSON_INLINE basic_array<string_t>::basic_array(raw_array&& arr) noexcept : _array_data(std::move(arr))
{
;
}

template <typename string_t>
MEOJSON_INLINE basic_array<string_t>::basic_array(std::initializer_list<value_type> init_list) : _array_data(init_list)
{
Expand Down Expand Up @@ -1653,24 +1641,11 @@ MEOJSON_INLINE bool basic_array<string_t>::operator==(const basic_array<string_t
// * basic_object impl *
// *******************************

template <typename string_t>
MEOJSON_INLINE basic_object<string_t>::basic_object(const raw_object& raw_obj) : _object_data(raw_obj)
{
;
}

template <typename string_t>
MEOJSON_INLINE basic_object<string_t>::basic_object(raw_object&& raw_obj) : _object_data(std::move(raw_obj))
{
;
}

template <typename string_t>
MEOJSON_INLINE basic_object<string_t>::basic_object(std::initializer_list<value_type> init_list)
: _object_data(std::make_move_iterator(init_list.begin()), std::make_move_iterator(init_list.end()))
{
for (const auto& [key, val] : init_list) {
emplace(key, val);
}
;
}

template <typename string_t>
Expand Down Expand Up @@ -2565,21 +2540,19 @@ namespace _serialization_helper
template <typename T, typename = void>
constexpr bool is_container = false;
template <typename T>
constexpr bool is_container<
T, void_t<typename T::value_type, decltype(std::declval<T>().begin()), decltype(std::declval<T>().end())>> =
true;
constexpr bool is_container<T, void_t<typename T::value_type, typename T::iterator,
typename std::iterator_traits<typename T::iterator>::value_type>> =
std::is_same_v<typename T::value_type, typename std::iterator_traits<typename T::iterator>::value_type>;

// something like a map
template <typename T, typename = void>
constexpr bool is_associative_container = false;
constexpr bool is_map = false;
template <typename T>
constexpr bool is_associative_container<T, void_t<typename T::key_type, typename T::mapped_type>> = is_container<T>;
constexpr bool is_map<T, void_t<typename T::key_type, typename T::mapped_type>> = is_container<T>;

// something like a vector
template <typename T, typename = void>
constexpr bool is_sequence_container = false;
constexpr bool is_collection = false;
template <typename T>
constexpr bool is_sequence_container<T> = is_container<T> && !is_associative_container<T>;
constexpr bool is_collection<T> = is_container<T> && !is_map<T>;

template <bool loose, typename string_t>
struct string_converter
Expand Down Expand Up @@ -2615,12 +2588,14 @@ namespace _serialization_helper
void unable_to_serialize()
{
static_assert(!sizeof(T), "Unable to serialize T. "
"You can define the conversion of T to json, or overload operator<< for it.");
"You can define the conversion of T to json, or overload operator<< for it. "
#ifdef _MSC_VER
static_assert(!sizeof(T), "See T below: " __FUNCSIG__);
"See T below: " __FUNCSIG__
#else
// static_assert(!sizeof(T), "See T below: " __PRETTY_FUNCTION__);
//"See T below: " __PRETTY_FUNCTION__

#endif
);
}
} // namespace _serialization_helper

Expand All @@ -2641,7 +2616,7 @@ MEOJSON_INLINE basic_value<string_t> serialize(any_t&& arg, string_converter_t&&
else if constexpr (std::decay_t<string_converter_t>::template is_convertible<any_t>) {
return string_converter(std::forward<any_t>(arg));
}
else if constexpr (is_sequence_container<std::decay_t<any_t>>) {
else if constexpr (is_collection<std::decay_t<any_t>>) {
basic_value<string_t> result;
for (auto&& val : arg) {
using value_t = decltype(val);
Expand All @@ -2651,7 +2626,7 @@ MEOJSON_INLINE basic_value<string_t> serialize(any_t&& arg, string_converter_t&&
}
return result;
}
else if constexpr (is_associative_container<std::decay_t<any_t>>) {
else if constexpr (is_map<std::decay_t<any_t>>) {
basic_value<string_t> result;
for (auto&& [key, val] : arg) {
using key_t = decltype(key);
Expand Down

0 comments on commit 2f4d842

Please sign in to comment.