Skip to content

Commit

Permalink
feat: try add namespace ext
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 26, 2024
1 parent ad54795 commit 1f8263b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 50 deletions.
8 changes: 4 additions & 4 deletions include/common/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class basic_array
// explicit basic_array(basic_value<string_t>&& val);

template <typename collection_t,
typename = std::enable_if_t<std::is_constructible_v<value_type, utils::range_value_t<collection_t>>>>
typename = std::enable_if_t<std::is_constructible_v<value_type, _utils::range_value_t<collection_t>>>>
basic_array(collection_t arr)
: _array_data(std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()))
{}
template <typename jsonization_t, std::enable_if_t<utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
template <typename jsonization_t, std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
basic_array(const jsonization_t& jsonization) : basic_array(jsonization.to_json())
{}
template <typename jsonization_t, std::enable_if_t<utils::has_to_json_in_global<jsonization_t>::value, bool> = true>
basic_array(const jsonization_t& jsonization) : basic_array(to_json(jsonization))
template <typename jsonization_t, std::enable_if_t<_utils::has_to_json_in_global<jsonization_t>::value, bool> = true>
basic_array(const jsonization_t& jsonization) : basic_array(ext::to_json(jsonization))
{}

~basic_array() noexcept = default;
Expand Down
12 changes: 6 additions & 6 deletions include/common/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class basic_object
// explicit basic_object(basic_value<string_t>&& val);

template <typename map_t,
typename = std::enable_if_t<std::is_constructible_v<value_type, utils::range_value_t<map_t>>>>
typename = std::enable_if_t<std::is_constructible_v<value_type, _utils::range_value_t<map_t>>>>
basic_object(map_t map) : _object_data(std::make_move_iterator(map.begin()), std::make_move_iterator(map.end()))
{}
template <typename jsonization_t, std::enable_if_t<utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
template <typename jsonization_t, std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
basic_object(const jsonization_t& jsonization) : basic_object(jsonization.to_json())
{}
template <typename jsonization_t, std::enable_if_t<utils::has_to_json_in_global<jsonization_t>::value, bool> = true>
basic_object(const jsonization_t& jsonization) : basic_object(to_json(jsonization))
template <typename jsonization_t, std::enable_if_t<_utils::has_to_json_in_global<jsonization_t>::value, bool> = true>
basic_object(const jsonization_t& jsonization) : basic_object(ext::to_json(jsonization))
{}

~basic_object() = default;
Expand Down Expand Up @@ -185,7 +185,7 @@ inline string_t basic_object<string_t>::to_string() const
string_t str { '{' };
for (auto iter = _object_data.cbegin(); iter != _object_data.cend();) {
const auto& [key, val] = *iter;
str += char_t('"') + utils::unescape_string(key) + string_t { '\"', ':' } + val.to_string();
str += char_t('"') + _utils::unescape_string(key) + string_t { '\"', ':' } + val.to_string();
if (++iter != _object_data.cend()) {
str += ',';
}
Expand All @@ -203,7 +203,7 @@ inline string_t basic_object<string_t>::format(size_t indent, size_t indent_time
string_t str { '{', '\n' };
for (auto iter = _object_data.cbegin(); iter != _object_data.cend();) {
const auto& [key, val] = *iter;
str += body_indent + char_t('"') + utils::unescape_string(key) + string_t { '\"', ':', ' ' } +
str += body_indent + char_t('"') + _utils::unescape_string(key) + string_t { '\"', ':', ' ' } +
val.format(indent, indent_times + 1);
if (++iter != _object_data.cend()) {
str += ',';
Expand Down
14 changes: 7 additions & 7 deletions include/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using warray = basic_array<std::wstring>;
using wobject = basic_object<std::wstring>;
}

namespace json::utils
namespace json::_utils
{
template <typename T>
using iterator_t = decltype(std::declval<T&>().begin());
Expand All @@ -37,8 +37,8 @@ using range_value_t = iter_value_t<iterator_t<R>>;
template <typename T, typename = void>
constexpr bool is_container = false;
template <typename T>
constexpr bool is_container<T, std::void_t<typename T::value_type, utils::range_value_t<T>>> =
std::is_same_v<typename T::value_type, utils::range_value_t<T>>;
constexpr bool is_container<T, std::void_t<typename T::value_type, range_value_t<T>>> =
std::is_same_v<typename T::value_type, range_value_t<T>>;

template <typename T, typename = void>
constexpr bool is_map = false;
Expand Down Expand Up @@ -67,7 +67,7 @@ template <typename T>
class has_to_json_in_global
{
template <typename U>
static auto test(int) -> decltype(to_json(std::declval<U>()), std::true_type());
static auto test(int) -> decltype(ext::to_json(std::declval<U>()), std::true_type());

template <typename U>
static std::false_type test(...);
Expand Down Expand Up @@ -95,7 +95,7 @@ class has_check_json_in_global
{
template <typename U>
static auto test(int)
-> decltype(check_json(std::declval<json::basic_value<string_t>>(), std::declval<U>()), std::true_type());
-> decltype(ext::check_json(std::declval<json::basic_value<string_t>>(), std::declval<U>()), std::true_type());

template <typename U>
static std::false_type test(...);
Expand Down Expand Up @@ -123,7 +123,7 @@ class has_from_json_in_global
{
template <typename U>
static auto test(int)
-> decltype(from_json(std::declval<json::basic_value<string_t>>(), std::declval<U&>()), std::true_type());
-> decltype(ext::from_json(std::declval<json::basic_value<string_t>>(), std::declval<U&>()), std::true_type());

template <typename U>
static std::false_type test(...);
Expand Down Expand Up @@ -211,4 +211,4 @@ string_t to_basic_string(any_t&& arg)
static_assert(!sizeof(any_t), "Unsupported type");
}
}
} // namespace json::utils
} // namespace json::_utils
58 changes: 29 additions & 29 deletions include/common/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ class basic_value

template <typename collection_t,
std::enable_if_t<std::is_constructible_v<typename basic_array<string_t>::value_type,
utils::range_value_t<collection_t>>,
_utils::range_value_t<collection_t>>,
bool> = true>
basic_value(collection_t&& collection) : basic_value(basic_array<string_t>(std::forward<collection_t>(collection)))
{}
template <typename map_t, std::enable_if_t<std::is_constructible_v<typename basic_object<string_t>::value_type,
utils::range_value_t<map_t>>,
_utils::range_value_t<map_t>>,
bool> = true>
basic_value(map_t&& map) : basic_value(basic_object<string_t>(std::forward<map_t>(map)))
{}

template <typename jsonization_t, std::enable_if_t<utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
template <typename jsonization_t, std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
basic_value(const jsonization_t& jsonization) : basic_value(jsonization.to_json())
{}
template <typename jsonization_t, std::enable_if_t<utils::has_to_json_in_global<jsonization_t>::value, bool> = true>
basic_value(const jsonization_t& jsonization) : basic_value(to_json(jsonization))
template <typename jsonization_t, std::enable_if_t<_utils::has_to_json_in_global<jsonization_t>::value, bool> = true>
basic_value(const jsonization_t& jsonization) : basic_value(ext::to_json(jsonization))
{}

template <typename value_t, std::enable_if_t<!std::is_convertible_v<value_t, basic_value<string_t>>, bool> = true>
Expand Down Expand Up @@ -208,19 +208,19 @@ class basic_value
explicit operator basic_object<string_t>() const { return as_object(); }

template <typename value_t, template <typename...> typename collection_t = std::vector,
typename _ = std::enable_if_t<utils::is_collection<collection_t<value_t>>>>
typename _ = std::enable_if_t<_utils::is_collection<collection_t<value_t>>>>
explicit operator collection_t<value_t>() const
{
return as_collection<value_t, collection_t>();
}
template <typename value_t, template <typename...> typename map_t = std::map,
typename _ = std::enable_if_t<utils::is_map<map_t<string_t, value_t>>>>
typename _ = std::enable_if_t<_utils::is_map<map_t<string_t, value_t>>>>
explicit operator map_t<string_t, value_t>() const
{
return as_map<value_t, map_t>();
}
template <typename jsonization_t,
std::enable_if_t<utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> = true>
std::enable_if_t<_utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> = true>
explicit operator jsonization_t() const
{
jsonization_t dst;
Expand All @@ -230,11 +230,11 @@ class basic_value
return dst;
}
template <typename jsonization_t,
std::enable_if_t<utils::has_from_json_in_global<jsonization_t, string_t>::value, bool> = true>
std::enable_if_t<_utils::has_from_json_in_global<jsonization_t, string_t>::value, bool> = true>
explicit operator jsonization_t() const
{
jsonization_t dst;
if (!from_json(*this, dst)) {
if (!ext::from_json(*this, dst)) {
throw exception("Wrong JSON");
}
return dst;
Expand Down Expand Up @@ -278,52 +278,52 @@ inline basic_value<string_t>::basic_value(basic_value<string_t>&& rhs) noexcept
template <typename string_t>
inline basic_value<string_t>::basic_value(bool b)
: _type(value_type::boolean),
_raw_data(string_t(b ? utils::true_string<string_t>() : utils::false_string<string_t>()))
_raw_data(string_t(b ? _utils::true_string<string_t>() : _utils::false_string<string_t>()))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(int num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(unsigned num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(long num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(unsigned long num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(long long num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(unsigned long long num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(float num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(double num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
inline basic_value<string_t>::basic_value(long double num)
: _type(value_type::number), _raw_data(utils::to_basic_string<string_t>(num))
: _type(value_type::number), _raw_data(_utils::to_basic_string<string_t>(num))
{}

template <typename string_t>
Expand Down Expand Up @@ -373,24 +373,24 @@ inline bool basic_value<string_t>::is() const noexcept
else if constexpr (std::is_same_v<basic_array<string_t>, value_t>) {
return is_array();
}
else if constexpr (utils::is_collection<value_t>) {
else if constexpr (_utils::is_collection<value_t>) {
return is_array() && all<typename value_t::value_type>();
}
else if constexpr (std::is_same_v<basic_object<string_t>, value_t>) {
return is_object();
}
else if constexpr (utils::is_map<value_t>) {
else if constexpr (_utils::is_map<value_t>) {
return is_object() && std::is_constructible_v<string_t, typename value_t::key_type> &&
all<typename value_t::mapped_type>();
}
else if constexpr (std::is_constructible_v<string_t, value_t>) {
return is_string();
}
else if constexpr (utils::has_check_json_in_member<value_t, string_t>::value) {
else if constexpr (_utils::has_check_json_in_member<value_t, string_t>::value) {
return value_t().check_json(*this);
}
else if constexpr (utils::has_check_json_in_global<value_t, string_t>::value) {
return check_json(*this, value_t());
else if constexpr (_utils::has_check_json_in_global<value_t, string_t>::value) {
return ext::check_json(*this, value_t());
}
else {
static_assert(!sizeof(value_t), "Unsupported type");
Expand Down Expand Up @@ -504,10 +504,10 @@ template <typename string_t>
inline bool basic_value<string_t>::as_boolean() const
{
if (is_boolean()) {
if (const string_t& b_str = as_basic_type_str(); b_str == utils::true_string<string_t>()) {
if (const string_t& b_str = as_basic_type_str(); b_str == _utils::true_string<string_t>()) {
return true;
}
else if (b_str == utils::false_string<string_t>()) {
else if (b_str == _utils::false_string<string_t>()) {
return false;
}
else {
Expand Down Expand Up @@ -725,12 +725,12 @@ inline string_t basic_value<string_t>::to_string() const
{
switch (_type) {
case value_type::null:
return utils::null_string<string_t>();
return _utils::null_string<string_t>();
case value_type::boolean:
case value_type::number:
return as_basic_type_str();
case value_type::string:
return char_t('"') + utils::unescape_string(as_basic_type_str()) + char_t('"');
return char_t('"') + _utils::unescape_string(as_basic_type_str()) + char_t('"');
case value_type::array:
return as_array().to_string();
case value_type::object:
Expand Down
6 changes: 3 additions & 3 deletions include/parser/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ inline basic_value<string_t> parser<string_t, parsing_t, accel_traits>::parse_va
template <typename string_t, typename parsing_t, typename accel_traits>
inline basic_value<string_t> parser<string_t, parsing_t, accel_traits>::parse_null()
{
for (const auto& ch : utils::null_string<string_t>()) {
for (const auto& ch : _utils::null_string<string_t>()) {
if (_cur != _end && *_cur == ch) {
++_cur;
}
Expand All @@ -182,7 +182,7 @@ inline basic_value<string_t> parser<string_t, parsing_t, accel_traits>::parse_bo
{
switch (*_cur) {
case 't':
for (const auto& ch : utils::true_string<string_t>()) {
for (const auto& ch : _utils::true_string<string_t>()) {
if (_cur != _end && *_cur == ch) {
++_cur;
}
Expand All @@ -192,7 +192,7 @@ inline basic_value<string_t> parser<string_t, parsing_t, accel_traits>::parse_bo
}
return true;
case 'f':
for (const auto& ch : utils::false_string<string_t>()) {
for (const auto& ch : _utils::false_string<string_t>()) {
if (_cur != _end && *_cur == ch) {
++_cur;
}
Expand Down
5 changes: 4 additions & 1 deletion sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void serializing()
outter.my_vec.emplace_back(mine);

json::value j_outter = outter;

// output:
// {"my_vec":[{"map":{"key_1":[{"inner_key_1":[7,8,9]},{"inner_key_2":[10]}]},"vec":[0.500000],"x":0}],"outter_a":10}
std::cout << j_outter.to_string() << std::endl;
Expand Down Expand Up @@ -166,6 +166,8 @@ struct ThirdPartyStruct
int a = 100;
};

namespace json::ext
{
json::value to_json(const ThirdPartyStruct& t)
{
return t.a;
Expand All @@ -179,6 +181,7 @@ bool from_json(const json::value& j, ThirdPartyStruct& out)
out.a = j.as_integer();
return true;
}
}

void third_party_jsonization()
{
Expand Down

0 comments on commit 1f8263b

Please sign in to comment.