Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string_view support for object access #2685

Closed
wants to merge 55 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
e3c2d55
:alembic: at() for std::string_view #1529
nlohmann Mar 24, 2021
edd2462
:bug: add missing negation
nlohmann Mar 24, 2021
d770517
:bug: add missing negation
nlohmann Mar 24, 2021
7742859
:alembic: add more std::string_view support
nlohmann Mar 24, 2021
6019b5d
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 25, 2021
5ea15c4
:recycle: unify types for object keys
nlohmann Mar 25, 2021
ba97e9f
:memo: update documentation
nlohmann Mar 25, 2021
360f21e
:white_check_mark: add tests for count() and contains()
nlohmann Mar 25, 2021
2f2c759
:green_heart: fix build
nlohmann Mar 25, 2021
16fb0cb
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 26, 2021
d01a6a4
:construction: add overload for erase(std::string_view)
nlohmann Mar 26, 2021
4a78689
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 28, 2021
d42f37a
:construction: implement operator[] for string_view
nlohmann Mar 28, 2021
47c7e2c
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 29, 2021
268600e
:ok_hand: apply review comments
nlohmann Mar 29, 2021
203b53d
:bulb: update documentation
nlohmann Mar 29, 2021
5c1c79b
:green_heart: fix build
nlohmann Mar 29, 2021
0ff33d0
:ok_hand: apply review comments
nlohmann Mar 29, 2021
f359d74
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 30, 2021
69d74d4
:recycle: clean up
nlohmann Mar 30, 2021
418fdba
:alembic: exclude iterators from KeyType
nlohmann Mar 30, 2021
02b36e9
:alembic: add forgotten forward
nlohmann Mar 30, 2021
ea4891f
:ok_hand: implement some review comments
nlohmann Mar 31, 2021
4a16e55
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann May 3, 2021
0818554
:twisted_rightwards_arrows: merge develop
nlohmann May 3, 2021
12c0bc4
:memo: document less-than comparability
nlohmann May 3, 2021
eb0f8d7
:ok_hand: apply review comments
nlohmann May 3, 2021
aaef7cd
:ok_hand: apply review comments
nlohmann May 4, 2021
b27938a
:ok_hand: apply review comments
nlohmann May 4, 2021
80cb607
:construction: fix return type
nlohmann Jul 16, 2021
8ba8c43
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Jul 16, 2021
575c28c
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Jul 22, 2021
1bd8a10
:ok_hand: fix code according to review comments
nlohmann Jul 22, 2021
3212700
:ok_hand: fix code according to review comments
nlohmann Jul 22, 2021
86dd665
:alembic: add fix for Clang 3.5
nlohmann Jul 22, 2021
5f39b09
:rewind: undo experimental fix for Clang 3.5
nlohmann Jul 23, 2021
d15ad85
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Aug 30, 2021
8b9e297
:twisted_rightwards_arrows: merge develop
nlohmann Aug 30, 2021
11eab8a
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Oct 17, 2021
9b838a0
:twisted_rightwards_arrows: update from develop
nlohmann Oct 17, 2021
76109db
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Nov 20, 2021
e86d8b9
:green_heart: add fix from https://github.com/Minipeps/json/commit/dd…
nlohmann Nov 20, 2021
cf06ba8
:green_heart: add fix from https://github.com/Minipeps/json/commit/dd…
nlohmann Nov 20, 2021
25ca594
:memo: overwork documentation
nlohmann Nov 20, 2021
5379b5d
:white_check_mark: improve coverage
nlohmann Nov 20, 2021
15e4598
:ok_hand: address review comments
nlohmann Nov 23, 2021
0eac6b3
:ok_hand: address review comments
nlohmann Nov 23, 2021
47b9b7e
:ok_hand: address review comments
nlohmann Nov 23, 2021
253f39c
:alembic: experiment from https://github.com/nlohmann/json/pull/2685#…
nlohmann Dec 23, 2021
6115454
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Dec 31, 2021
72d8615
:twisted_rightwards_arrows: merge develop branch
nlohmann Dec 31, 2021
d8db435
:alembic: try different type alias
nlohmann Jan 1, 2022
2cdf3dd
:rewind: undo commit 15e45981ead5ca64a963d79c5dd5b6d18b2c7a3f
nlohmann Jan 2, 2022
b01293f
Merge remote-tracking branch 'origin/string_view' into string_view
nlohmann Jan 2, 2022
b18d97d
:rewind: undo commit d8db435c1e1e60ebd93f9e5036437f9b8cc478e4
nlohmann Jan 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/ci.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ add_custom_target(ci_test_coverage
COMMAND CXX=g++ ${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
Original file line number Diff line number Diff line change
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 object 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` that is less-than comparable with `string_t`. This
can also be a string literal or a string view (C++17).

## Parameters

`idx` (in)
Expand Down Expand Up @@ -172,5 +180,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.5.
3. Added in version 2.0.0.
7 changes: 4 additions & 3 deletions doc/mkdocs/docs/api/basic_json/contains.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
```cpp
// (1)
template<typename KeyT>
bool contains(KeyT && key) const;
bool contains(const KeyT& key) const;

// (2)
bool contains(const json_pointer& ptr) const;
Expand All @@ -16,7 +16,8 @@ bool contains(const json_pointer& ptr) const;
## 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` that is less-than comparable with `string_t`. This
can also be a string literal or a string view (C++17).

## Parameters

Expand Down Expand Up @@ -90,5 +91,5 @@ Logarithmic in the size of the JSON object.

## Version history

1. Added in version 3.6.0.
1. Added in version 3.6.0. Extended template `KeyT` in version 3.10.5 to also support `std::string_view`.
2. Added in version 3.7.0.
6 changes: 4 additions & 2 deletions doc/mkdocs/docs/api/basic_json/count.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```cpp
template<typename KeyT>
size_type count(KeyT&& key) const;
size_type count(const 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,8 @@ 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 that is less-than comparable with `string_t`. This can also be a string literal or a string
view (C++17).

## Parameters

Expand Down Expand Up @@ -53,3 +54,4 @@ This method always returns `0` when executed on a JSON type that is not an objec
## Version history

- Added in version 1.0.0.
- Extended template `KeyT` in version 3.10.5 to also support `std::string_view`.
10 changes: 9 additions & 1 deletion doc/mkdocs/docs/api/basic_json/erase.md
Original file line number Diff line number Diff line change
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(const KeyT& key);

// (4)
void erase(const size_type idx);
Expand All @@ -31,6 +32,12 @@ void erase(const size_type idx);

4. Removes an element from a JSON array by index.

## Template parameters

`KeyT`
: A type for an object key that is less-than comparable with `string_t`. This can also be a string literal or a string
view (C++17).

## Parameters

`pos` (in)
Expand Down Expand Up @@ -174,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.5.
8 changes: 5 additions & 3 deletions doc/mkdocs/docs/api/basic_json/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

template<typename KeyT>
const_iterator find(KeyT&& key) const;
const_iterator find(const 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,8 @@ object, `end()` is returned.
## Template parameters

`KeyT`
: A type for an object key.
: A type for an object key that is less-than comparable with `string_t`. This can also be a string literal or a string
view (C++17).

## Parameters

Expand Down Expand Up @@ -61,3 +62,4 @@ This method always returns `end()` when executed on a JSON type that is not an o
## Version history

- Added in version 1.0.0.
- Extended template `KeyT` in version 3.10.5 to also support `std::string_view`.
18 changes: 9 additions & 9 deletions doc/mkdocs/docs/api/basic_json/operator[].md
Original file line number Diff line number Diff line change
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[](const KeyT& key);
template<typename KeyT>
const_reference operator[](const 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` that is less-than comparable with `string_t`. This
can also be a string literal or a string view (C++17).

## Parameters

Expand Down Expand Up @@ -192,5 +191,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.5 which now also supports `std::string_view`.
3. Added in version 2.0.0.
13 changes: 8 additions & 5 deletions doc/mkdocs/docs/api/basic_json/value.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

```cpp
// (1)
template<class ValueType>
ValueType value(const typename object_t::key_type& key,
const ValueType& default_value) const;
template<class KeyType, class ValueType>
ValueType value(const KeyType& key,
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
ValueType && default_value) const;

// (2)
template<class ValueType>
ValueType value(const json_pointer& ptr,
const ValueType& default_value) const;
ValueType && default_value) const;
```

1. Returns either a copy of an object's element at the specified key `key` or a given default value if no element with
Expand Down Expand Up @@ -44,6 +44,9 @@ ValueType value(const json_pointer& ptr,

## Template parameters

`KeyType`
: A type for an object key that is less-than comparable with `string_t`. 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 @@ -126,4 +129,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.5.
27 changes: 27 additions & 0 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,33 @@ struct is_constructible_tuple : std::false_type {};
template<typename T1, typename... Args>
struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};

/// type to check if KeyType can be used as object key

template<typename ComparatorType, typename KeyType, typename ObjectKeyType, typename = void>
struct is_key_type_comparable
{
static constexpr bool value = false;
};

template<typename ComparatorType, typename KeyType, typename ObjectKeyType>
struct is_key_type_comparable<ComparatorType, KeyType, ObjectKeyType, void_t<
decltype(ComparatorType()(std::declval<KeyType const&>(), std::declval<ObjectKeyType const&>())),
decltype(ComparatorType()(std::declval<ObjectKeyType const&>(), std::declval<KeyType const&>()))
>>
{
static constexpr bool value = true;
};

template<typename BasicJsonType, typename KeyType>
struct is_usable_as_key_type
{
static constexpr bool value =
is_key_type_comparable<typename BasicJsonType::object_comparator_t, typename BasicJsonType::object_t::key_type, KeyType>::value &&
!std::is_same<KeyType, typename BasicJsonType::iterator>::value &&
!std::is_same<KeyType, typename BasicJsonType::const_iterator>::value;
};


// a naive helper to check if a type is an ordered_map (exploits the fact that
// ordered_map inherits capacity() from std::vector)
template <typename T>
Expand Down
Loading