Skip to content

Commit

Permalink
Merge 418fdba into 7126d88
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Mar 30, 2021
2 parents 7126d88 + 418fdba commit f357c66
Show file tree
Hide file tree
Showing 12 changed files with 856 additions and 310 deletions.
1 change: 1 addition & 0 deletions cmake/ci.cmake
Expand Up @@ -470,6 +470,7 @@ add_custom_target(ci_test_coverage
COMMAND CXX=${GCC_TOOL} ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=Debug -GNinja -DCMAKE_CXX_FLAGS="--coverage;-fprofile-arcs;-ftest-coverage"
-DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON
-DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
-S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_coverage
COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_coverage
COMMAND cd ${PROJECT_BINARY_DIR}/build_coverage && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure
Expand Down
14 changes: 11 additions & 3 deletions doc/mkdocs/docs/api/basic_json/at.md
Expand Up @@ -6,8 +6,10 @@ reference at(size_type idx);
const_reference at(size_type idx) const;

// (2)
reference at(const typename object_t::key_type& key);
const_reference at(const typename object_t::key_type& key) const;
template<typename KeyT>
reference at(const KeyT& key);
template<typename KeyT>
const_reference at(const KeyT& key) const;

// (3)
reference at(const json_pointer& ptr);
Expand All @@ -18,6 +20,12 @@ const_reference at(const json_pointer& ptr) const;
2. Returns a reference to the element at with specified key `key`, with bounds checking.
3. Returns a reference to the element at with specified JSON pointer `ptr`, with bounds checking.
## Template parameters
`KeyT`
: A type for an object key other than `basic_json::json_pointer`. This can also be a string literal or a string view
(C++17).
## Parameters
`idx` (in)
Expand Down Expand Up @@ -167,5 +175,5 @@ Strong exception safety: if an exception occurs, the original value stays intact
## Version history

1. Added in version 1.0.0.
2. Added in version 1.0.0.
2. Added in version 1.0.0; `KeyT` template added in version 3.10.0.
3. Added in version 2.0.0.
3 changes: 2 additions & 1 deletion doc/mkdocs/docs/api/basic_json/contains.md
Expand Up @@ -11,7 +11,8 @@ value is not an object, `#!cpp false` is returned.
## Template parameters
`KeyT`
: A type for an object key other than `basic_json::json_pointer`.
: A type for an object key other than `basic_json::json_pointer`. This can also be a string literal or a string view
(C++17).
## Parameters
Expand Down
4 changes: 2 additions & 2 deletions doc/mkdocs/docs/api/basic_json/count.md
Expand Up @@ -2,7 +2,7 @@

```cpp
template<typename KeyT>
size_type count(KeyT&& key) const;
size_type count(KeyT && key) const;
```
Returns the number of elements with key `key`. If `ObjectType` is the default `std::map` type, the return value will
Expand All @@ -11,7 +11,7 @@ always be `0` (`key` was not found) or `1` (`key` was found).
## Template parameters
`KeyT`
: A type for an object key.
: A type for an object key. This can also be a string literal or a string view (C++17).
## Parameters
Expand Down
9 changes: 8 additions & 1 deletion doc/mkdocs/docs/api/basic_json/erase.md
Expand Up @@ -10,7 +10,8 @@ iterator erase(iterator first, iterator last);
const_iterator erase(const_iterator first, const_iterator last);

// (3)
size_type erase(const typename object_t::key_type& key);
template<typename KeyT>
size_type erase(KeyT && key);

// (4)
void erase(const size_type idx);
Expand All @@ -31,6 +32,11 @@ void erase(const size_type idx);
4. Removes an element from a JSON array by index.
## Template parameters
`KeyT`
: A type convertible to an object key. This can also be a string literal or a string view (C++17).
## Parameters
`pos` (in)
Expand Down Expand Up @@ -175,3 +181,4 @@ Strong exception safety: if an exception occurs, the original value stays intact

- Added in version 1.0.0.
- Added support for binary types in version 3.8.0.
- Added `KeyT` template in version 3.10.0.
6 changes: 3 additions & 3 deletions doc/mkdocs/docs/api/basic_json/find.md
Expand Up @@ -2,10 +2,10 @@

```cpp
template<typename KeyT>
iterator find(KeyT&& key);
iterator find(KeyT && key);

template<typename KeyT>
const_iterator find(KeyT&& key) const
const_iterator find(KeyT && key) const
```
Finds an element in a JSON object with key equivalent to `key`. If the element is not found or the JSON value is not an
Expand All @@ -14,7 +14,7 @@ object, `end()` is returned.
## Template parameters
`KeyT`
: A type for an object key.
: A type for an object key. This can also be a string literal or a string view (C++17).
## Parameters
Expand Down
18 changes: 9 additions & 9 deletions doc/mkdocs/docs/api/basic_json/operator[].md
Expand Up @@ -6,12 +6,10 @@ reference operator[](size_type idx);
const_reference operator[](size_type idx) const;

// (2)
reference operator[](const typename object_t::key_type& key);
const_reference operator[](const typename object_t::key_type& key) const;
template<typename T>
reference operator[](T* key);
template<typename T>
const_reference operator[](T* key) const;
template<typename KeyT>
reference operator[](KeyT && key);
template<typename KeyT>
const_reference operator[](KeyT && key) const;

// (3)
reference operator[](const json_pointer& ptr);
Expand All @@ -24,8 +22,9 @@ const_reference operator[](const json_pointer& ptr) const;

## Template parameters

`T`
: string literal convertible to `object_t::key_type`
`KeyT`
: A type for an object key other than `basic_json::json_pointer`. This can also be a string literal or a string view
(C++17).

## Parameters

Expand Down Expand Up @@ -187,5 +186,6 @@ Strong exception safety: if an exception occurs, the original value stays intact
## Version history

1. Added in version 1.0.0.
2. Added in version 1.0.0. Overloads for `T* key` added in version 1.1.0.
2. Added in version 1.0.0. Overloads for `T* key` added in version 1.1.0. Template `T* key` replaced by template `KeyT`
in version 3.10.0 which now also supports `std::string_view`.
3. Added in version 2.0.0.
8 changes: 5 additions & 3 deletions doc/mkdocs/docs/api/basic_json/value.md
Expand Up @@ -2,8 +2,8 @@

```cpp
// (1)
template<class ValueType>
ValueType value(const typename object_t::key_type& key,
template<class KeyType, class ValueType>
ValueType value(const KeyType& key,
const ValueType& default_value) const;

// (2)
Expand Down Expand Up @@ -41,6 +41,8 @@ Unlike [`operator[]`](operator[].md), this function does not implicitly add an e

## Template parameters

`KeyType`
: A type for an object key. This can also be a string literal or a string view (C++17).
`ValueType`
: type compatible to JSON values, for instance `#!cpp int` for JSON integer numbers, `#!cpp bool` for JSON booleans,
or `#!cpp std::vector` types for JSON arrays. Note the type of the expected value at `key`/`ptr` and the default
Expand Down Expand Up @@ -118,4 +120,4 @@ changes to any JSON value.
## Version history

1. Added in version 1.0.0.
2. Added in version 2.0.2.
2. Added in version 2.0.2. `KeyType` template parameter added in 3.10.0.
14 changes: 14 additions & 0 deletions include/nlohmann/detail/meta/type_traits.hpp
Expand Up @@ -394,5 +394,19 @@ 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>...> {};

/// type to check if KeyType can be used as object key
template<typename BasicJsonType, typename KeyType>
struct is_key_type
{
static constexpr bool value = (
#if defined(JSON_HAS_CPP_17)
std::is_same<typename std::decay<KeyType>::type, std::string_view>::value ||
#endif
std::is_convertible<KeyType, typename BasicJsonType::object_t::key_type>::value)
&& !std::is_same<KeyType, typename BasicJsonType::iterator>::value
&& !std::is_same<KeyType, typename BasicJsonType::const_iterator>::value;
};

} // namespace detail
} // namespace nlohmann

0 comments on commit f357c66

Please sign in to comment.