Skip to content

Commit

Permalink
update json library
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanoHao committed Mar 26, 2020
1 parent 227b2f0 commit 737d8aa
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 123 deletions.
2 changes: 1 addition & 1 deletion src/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void from_json(const BasicJsonType& j, std::valarray<T>& l)
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
}
l.resize(j.size());
std::copy(j.m_value.array->begin(), j.m_value.array->end(), std::begin(l));
std::copy(j.begin(), j.end(), std::begin(l));
}

template <typename BasicJsonType, typename T, std::size_t N>
Expand Down
2 changes: 1 addition & 1 deletion src/nlohmann/detail/conversions/to_chars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ inline cached_power get_cached_power_for_binary_exponent(int e)
// ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
// ==> 2^(alpha - e - 1) <= c
//
// If c were an exakt power of ten, i.e. c = 10^k, one may determine k as
// If c were an exact power of ten, i.e. c = 10^k, one may determine k as
//
// k = ceil( log_10( 2^(alpha - e - 1) ) )
// = ceil( (alpha - e - 1) * log_10(2) )
Expand Down
15 changes: 9 additions & 6 deletions src/nlohmann/detail/conversions/to_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ struct external_constructor<value_t::array>
j.m_type = value_t::array;
j.m_value = value_t::array;
j.m_value.array->resize(arr.size());
std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
if (arr.size() > 0)
{
std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
}
j.assert_invariant();
}
};
Expand Down Expand Up @@ -299,8 +302,8 @@ void to_json(BasicJsonType& j, const T(&arr)[N])
external_constructor<value_t::array>::construct(j, arr);
}

template<typename BasicJsonType, typename... Args>
void to_json(BasicJsonType& j, const std::pair<Args...>& p)
template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
{
j = { p.first, p.second };
}
Expand All @@ -319,10 +322,10 @@ void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...>
j = { std::get<Idx>(t)... };
}

template<typename BasicJsonType, typename... Args>
void to_json(BasicJsonType& j, const std::tuple<Args...>& t)
template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
void to_json(BasicJsonType& j, const T& t)
{
to_json_tuple_impl(j, t, index_sequence_for<Args...> {});
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
}

struct to_json_fn
Expand Down
4 changes: 2 additions & 2 deletions src/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ class binary_reader
const int exp = (half >> 10u) & 0x1Fu;
const unsigned int mant = half & 0x3FFu;
assert(0 <= exp and exp <= 32);
assert(0 <= mant and mant <= 1024);
assert(mant <= 1024);
switch (exp)
{
case 0:
Expand Down Expand Up @@ -1929,7 +1929,7 @@ class binary_reader
/*!
@param[in] format the current format
@param[in] detail a detailed error message
@param[in] context further contect information
@param[in] context further context information
@return a message string to use in the parse_error exceptions
*/
std::string exception_message(const input_format_t format,
Expand Down
4 changes: 2 additions & 2 deletions src/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ class input_stream_adapter : public input_adapter_protocol
class input_buffer_adapter : public input_adapter_protocol
{
public:
JSON_HEDLEY_NON_NULL(2)
input_buffer_adapter(const char* b, const std::size_t l) noexcept
: cursor(b), limit(b + l)
: cursor(b), limit(b == nullptr ? nullptr : (b + l))
{}

// delete because of pointer members
Expand All @@ -147,6 +146,7 @@ class input_buffer_adapter : public input_adapter_protocol
{
if (JSON_HEDLEY_LIKELY(cursor < limit))
{
assert(cursor != nullptr and limit != nullptr);
return std::char_traits<char>::to_int_type(*(cursor++));
}

Expand Down
4 changes: 2 additions & 2 deletions src/nlohmann/detail/iterators/iter_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class iter_impl
/*!
@brief const copy constructor
@param[in] other const iterator to copy from
@note This copy constuctor had to be defined explicitely to circumvent a bug
occuring on msvc v19.0 compiler (VS 2015) debug build. For more
@note This copy constructor had to be defined explicitly to circumvent a bug
occurring on msvc v19.0 compiler (VS 2015) debug build. For more
information refer to: https://github.com/nlohmann/json/issues/1608
*/
iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
Expand Down
14 changes: 10 additions & 4 deletions src/nlohmann/detail/iterators/iteration_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace nlohmann
{
namespace detail
{
template<typename string_type>
void int_to_string( string_type& target, std::size_t value )
{
target = std::to_string(value);
}
template <typename IteratorType> class iteration_proxy_value
{
public:
Expand All @@ -20,6 +25,7 @@ template <typename IteratorType> class iteration_proxy_value
using pointer = value_type * ;
using reference = value_type & ;
using iterator_category = std::input_iterator_tag;
using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;

private:
/// the iterator
Expand All @@ -29,9 +35,9 @@ template <typename IteratorType> class iteration_proxy_value
/// last stringified array index
mutable std::size_t array_index_last = 0;
/// a string representation of the array index
mutable std::string array_index_str = "0";
mutable string_type array_index_str = "0";
/// an empty string (to return a reference for primitive values)
const std::string empty_str = "";
const string_type empty_str = "";

public:
explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
Expand Down Expand Up @@ -64,7 +70,7 @@ template <typename IteratorType> class iteration_proxy_value
}

/// return key of the iterator
const std::string& key() const
const string_type& key() const
{
assert(anchor.m_object != nullptr);

Expand All @@ -75,7 +81,7 @@ template <typename IteratorType> class iteration_proxy_value
{
if (array_index != array_index_last)
{
array_index_str = std::to_string(array_index);
int_to_string( array_index_str, array_index );
array_index_last = array_index;
}
return array_index_str;
Expand Down
4 changes: 2 additions & 2 deletions src/nlohmann/detail/json_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class json_pointer
/*!
@brief append an array index at the end of this JSON pointer
@param[in] array_index array index ot append
@param[in] array_index array index to append
@return JSON pointer with @a array_index appended
@liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add}
Expand Down Expand Up @@ -267,7 +267,7 @@ class json_pointer
@since version 3.6.0
*/
const std::string& back()
const std::string& back() const
{
if (JSON_HEDLEY_UNLIKELY(empty()))
{
Expand Down
48 changes: 24 additions & 24 deletions src/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,30 @@
@def NLOHMANN_JSON_SERIALIZE_ENUM
@since version 3.4.0
*/
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
template<typename BasicJsonType> \
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.first == e; \
}); \
j = ((it != std::end(m)) ? it : std::begin(m))->second; \
} \
template<typename BasicJsonType> \
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.second == j; \
}); \
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
template<typename BasicJsonType> \
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.first == e; \
}); \
j = ((it != std::end(m)) ? it : std::begin(m))->second; \
} \
template<typename BasicJsonType> \
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.second == j; \
}); \
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
}

// Ugly macros to avoid uglier copy-paste when specializing basic_json. They
Expand Down
13 changes: 13 additions & 0 deletions src/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,18 @@ struct is_compatible_type_impl <
template <typename BasicJsonType, typename CompatibleType>
struct is_compatible_type
: is_compatible_type_impl<BasicJsonType, CompatibleType> {};

// https://en.cppreference.com/w/cpp/types/conjunction
template<class...> struct conjunction : std::true_type { };
template<class B1> struct conjunction<B1> : B1 { };
template<class B1, class... Bn>
struct conjunction<B1, Bn...>
: std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};

template <typename T1, typename T2>
struct is_constructible_tuple : std::false_type {};

template <typename T1, typename... Args>
struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<std::is_constructible<T1, Args>...> {};
} // namespace detail
} // namespace nlohmann
7 changes: 3 additions & 4 deletions src/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,13 +861,12 @@ class binary_writer
*/
static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
{
std::size_t embedded_document_size = 0ul;
std::size_t array_index = 0ul;

for (const auto& el : value)
const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), 0ul, [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el)
{
embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el);
}
return result + calc_bson_element_size(std::to_string(array_index++), el);
});

return sizeof(std::int32_t) + embedded_document_size + 1ul;
}
Expand Down
35 changes: 32 additions & 3 deletions src/nlohmann/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,12 @@ class serializer

// MODIFIED FOR WFM
case error_handler_t::as_is:
if (bytes > 0)
{
o->write_characters(string_buffer.data(), bytes);
if (bytes > 0)
{
o->write_characters(string_buffer.data(), bytes);
}
break;
}

default: // LCOV_EXCL_LINE
Expand Down Expand Up @@ -646,7 +649,7 @@ class serializer
if (is_negative)
{
*buffer_ptr = '-';
abs_value = static_cast<number_unsigned_t>(std::abs(static_cast<std::intmax_t>(x)));
abs_value = remove_sign(x);

// account one more byte for the minus sign
n_chars = 1 + count_digits(abs_value);
Expand Down Expand Up @@ -827,6 +830,32 @@ class serializer
return state;
}

/*
* Overload to make the compiler happy while it is instantiating
* dump_integer for number_unsigned_t.
* Must never be called.
*/
number_unsigned_t remove_sign(number_unsigned_t x)
{
assert(false); // LCOV_EXCL_LINE
return x; // LCOV_EXCL_LINE
}

/*
* Helper function for dump_integer
*
* This function takes a negative signed integer and returns its absolute
* value as unsigned integer. The plus/minus shuffling is necessary as we can
* not directly remove the sign of an arbitrary signed integer as the
* absolute values of INT_MIN and INT_MAX are usually not the same. See
* #1708 for details.
*/
inline number_unsigned_t remove_sign(number_integer_t x) noexcept
{
assert(x < 0 and x < (std::numeric_limits<number_integer_t>::max)());
return static_cast<number_unsigned_t>(-(x + 1)) + 1;
}

private:
/// the output of the serializer
output_adapter_t<char> o = nullptr;
Expand Down
Loading

0 comments on commit 737d8aa

Please sign in to comment.