Skip to content

Commit

Permalink
Merge pull request nlohmann#32 from nlohmann/develop
Browse files Browse the repository at this point in the history
Sync Fork from Upstream Repo
  • Loading branch information
sthagen authored Dec 29, 2020
2 parents 7a98ca1 + ff3863d commit 1009afc
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 16 deletions.
2 changes: 2 additions & 0 deletions include/nlohmann/detail/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <cstddef> // size_t, uint8_t
#include <functional> // hash

#include <nlohmann/detail/macro_scope.hpp>

namespace nlohmann
{
namespace detail
Expand Down
3 changes: 2 additions & 1 deletion include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <limits> // numeric_limits
#include <string> // char_traits, string
#include <utility> // make_pair, move
#include <vector> // vector

#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/input/input_adapters.hpp>
Expand Down Expand Up @@ -2340,7 +2341,7 @@ class binary_reader
break;
}
result.push_back(static_cast<typename string_t::value_type>(current));
};
}
return success;
}

Expand Down
31 changes: 26 additions & 5 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,35 @@ typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapte
}

// Convenience shorthand from container to iterator
// Enables ADL on begin(container) and end(container)
// Encloses the using declarations in namespace for not to leak them to outside scope

namespace container_input_adapter_factory_impl {

using std::begin;
using std::end;

template<typename ContainerType, typename Enable = void>
struct container_input_adapter_factory {};

template<typename ContainerType>
auto input_adapter(const ContainerType& container) -> decltype(input_adapter(begin(container), end(container)))
struct container_input_adapter_factory< ContainerType,
void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))> >
{
// Enable ADL
using std::begin;
using std::end;
using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));

static adapter_type create(const ContainerType& container)
{
return input_adapter(begin(container), end(container));
}
};

}

return input_adapter(begin(container), end(container));
template<typename ContainerType>
typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
{
return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
}

// Special cases with fast paths
Expand Down
2 changes: 2 additions & 0 deletions include/nlohmann/detail/iterators/primitive_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <cstddef> // ptrdiff_t
#include <limits> // numeric_limits

#include <nlohmann/detail/macro_scope.hpp>

namespace nlohmann
{
namespace detail
Expand Down
1 change: 1 addition & 0 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <limits> // numeric_limits
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
#include <utility> // declval
#include <tuple> // tuple

#include <nlohmann/detail/iterators/iterator_traits.hpp>
#include <nlohmann/detail/macro_scope.hpp>
Expand Down
2 changes: 2 additions & 0 deletions include/nlohmann/thirdparty/hedley/hedley_undef.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#undef JSON_HEDLEY_ALWAYS_INLINE
#undef JSON_HEDLEY_ARM_VERSION
#undef JSON_HEDLEY_ARM_VERSION_CHECK
Expand Down
47 changes: 39 additions & 8 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,7 @@ constexpr T static_const<T>::value;
#include <limits> // numeric_limits
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
#include <utility> // declval
#include <tuple> // tuple

// #include <nlohmann/detail/iterators/iterator_traits.hpp>

Expand Down Expand Up @@ -4671,6 +4672,9 @@ class byte_container_with_subtype : public BinaryType
#include <cstddef> // size_t, uint8_t
#include <functional> // hash

// #include <nlohmann/detail/macro_scope.hpp>


namespace nlohmann
{
namespace detail
Expand Down Expand Up @@ -4799,6 +4803,7 @@ std::size_t hash(const BasicJsonType& j)
#include <limits> // numeric_limits
#include <string> // char_traits, string
#include <utility> // make_pair, move
#include <vector> // vector

// #include <nlohmann/detail/exceptions.hpp>

Expand Down Expand Up @@ -5178,14 +5183,35 @@ typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapte
}

// Convenience shorthand from container to iterator
// Enables ADL on begin(container) and end(container)
// Encloses the using declarations in namespace for not to leak them to outside scope

namespace container_input_adapter_factory_impl {

using std::begin;
using std::end;

template<typename ContainerType, typename Enable = void>
struct container_input_adapter_factory {};

template<typename ContainerType>
auto input_adapter(const ContainerType& container) -> decltype(input_adapter(begin(container), end(container)))
struct container_input_adapter_factory< ContainerType,
void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))> >
{
// Enable ADL
using std::begin;
using std::end;
using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));

static adapter_type create(const ContainerType& container)
{
return input_adapter(begin(container), end(container));
}
};

return input_adapter(begin(container), end(container));
}

template<typename ContainerType>
typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
{
return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
}

// Special cases with fast paths
Expand Down Expand Up @@ -10065,7 +10091,7 @@ class binary_reader
break;
}
result.push_back(static_cast<typename string_t::value_type>(current));
};
}
return success;
}

Expand Down Expand Up @@ -10707,6 +10733,9 @@ class parser
#include <cstddef> // ptrdiff_t
#include <limits> // numeric_limits

// #include <nlohmann/detail/macro_scope.hpp>


namespace nlohmann
{
namespace detail
Expand Down Expand Up @@ -16793,7 +16822,7 @@ class basic_json
detail::parser_callback_t<basic_json>cb = nullptr,
const bool allow_exceptions = true,
const bool ignore_comments = false
)
)
{
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
std::move(cb), allow_exceptions, ignore_comments);
Expand Down Expand Up @@ -25338,7 +25367,7 @@ template<>
inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept(
is_nothrow_move_constructible<nlohmann::json>::value&&
is_nothrow_move_assignable<nlohmann::json>::value
)
)
{
j1.swap(j2);
}
Expand Down Expand Up @@ -25410,6 +25439,8 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std
#undef JSON_EXPLICIT

// #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>


#undef JSON_HEDLEY_ALWAYS_INLINE
#undef JSON_HEDLEY_ARM_VERSION
#undef JSON_HEDLEY_ARM_VERSION_CHECK
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-class_lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ TEST_CASE("lexer class")
// store scan() result
const auto res = scan_string(s.c_str());

CAPTURE(s);
CAPTURE(s)

switch (c)
{
Expand Down
27 changes: 26 additions & 1 deletion test/src/unit-user_defined_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const char* end(const MyContainer& c)
return c.data + strlen(c.data);
}

TEST_CASE("Custom container")
TEST_CASE("Custom container non-member begin/end")
{

MyContainer data{"[1,2,3,4]"};
Expand All @@ -75,6 +75,31 @@ TEST_CASE("Custom container")

}

TEST_CASE("Custom container member begin/end")
{
struct MyContainer2
{
const char* data;

const char* begin() const
{
return data;
}

const char* end() const
{
return data + strlen(data);
}
};

MyContainer2 data{"[1,2,3,4]"};
json as_json = json::parse(data);
CHECK(as_json.at(0) == 1);
CHECK(as_json.at(1) == 2);
CHECK(as_json.at(2) == 3);
CHECK(as_json.at(3) == 4);
}

TEST_CASE("Custom iterator")
{
const char* raw_data = "[1,2,3,4]";
Expand Down

0 comments on commit 1009afc

Please sign in to comment.