Skip to content

Using json_pointer causes the warning of deprecated declarations #5068

@zheng-yu-yang

Description

@zheng-yu-yang

Description

The JSON pointer example produces compiler warning.

Reproduction steps

  1. Pick the code from https://json.nlohmann.me/features/json_pointer/#value-access.
  2. Modify the code a litle so that it can be compiled and run directly (attached below).
  3. Compile the code with g++-11 and g++-13.
  4. See the compiler warning of [-Wdeprecated-declarations].

Note:
clang++-14 and clang++-18 compiles the same code without the deprecated-declarations warning.

Expected vs. actual results

I want to access the json node with json_pointer without compiler warning. If I follow the warning by using to_string method, the meaning is totally different and causes runtime exception.

To be specific, the parameter p1 should be binded to

reference at(const json_pointer& ptr) 

but its binded to

 template<class KeyType, detail::enable_if_t< detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0> reference at(KeyType && key) 

Minimal code example

#include "nlohmann/json.hpp" using json = nlohmann::json; int main() { // the JSON value from above auto j = json::parse(R"({  "array": ["A", "B", "C"],  "nested": {  "one": 1,  "two": 2,  "three": [true, false]  }  })"); auto p1 = json::json_pointer("/nested/three/1"); auto p2 = json::json_pointer("/nested/four"); // access values auto val2 = j.at(p1); // false auto val3 = j.value(p2, 0); // 0 }

Error messages

given by g++-11 (similar is from g++-13): In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp: In instantiation of ‘struct nlohmann::json_abi_v3_12_0::detail::is_comparable<std::less<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char>, nlohmann::json_abi_v3_12_0::json_pointer<std::__cxx11::basic_string<char> >&, void>’: include/nlohmann/detail/meta/type_traits.hpp:581:31: required by substitution of ‘template<class KeyType, typename std::enable_if<typename std::conditional<(typename std::conditional<(((nlohmann::json_abi_v3_12_0::detail::is_comparable<std::less<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char>, KeyType, void>::value && (!(true && std::is_same<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type, std::__cxx11::basic_string<char> >::value))) && ((! true) || std::integral_constant<bool, false>::value)) && (! nlohmann::json_abi_v3_12_0::detail::is_specialization_of<nlohmann::json_abi_v3_12_0::json_pointer, typename std::remove_cv<typename std::remove_reference<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type>::type>::type>::value)), std::integral_constant<bool, true>, std::integral_constant<bool, false> >::type::value && (! nlohmann::json_abi_v3_12_0::detail::is_json_iterator_of<nlohmann::json_abi_v3_12_0::basic_json<>, typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type>::value)), std::integral_constant<bool, true>, std::integral_constant<bool, false> >::type::value, int>::type <anonymous> > const value_type& nlohmann::json_abi_v3_12_0::basic_json<>::at<KeyType, <anonymous> >(KeyType&&) const [with KeyType = nlohmann::json_abi_v3_12_0::json_pointer<std::__cxx11::basic_string<char> >&; typename std::enable_if<typename std::conditional<(typename std::conditional<(((nlohmann::json_abi_v3_12_0::detail::is_comparable<std::less<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char>, KeyType, void>::value && (!(true && std::is_same<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type, std::__cxx11::basic_string<char> >::value))) && ((! true) || std::integral_constant<bool, false>::value)) && (! nlohmann::json_abi_v3_12_0::detail::is_specialization_of<nlohmann::json_abi_v3_12_0::json_pointer, typename std::remove_cv<typename std::remove_reference<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type>::type>::type>::value)), std::integral_constant<bool, true>, std::integral_constant<bool, false> >::type::value && (! nlohmann::json_abi_v3_12_0::detail::is_json_iterator_of<nlohmann::json_abi_v3_12_0::basic_json<>, typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type>::value)), std::integral_constant<bool, true>, std::integral_constant<bool, false> >::type::value, int>::type <anonymous> = <missing>]’ main.cpp:20:21: required from here include/nlohmann/detail/meta/type_traits.hpp:568:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 568 | decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())), | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp:569:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 569 | decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>())) | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp:568:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 568 | decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())), | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp:569:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 569 | decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>())) | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp: In instantiation of ‘struct nlohmann::json_abi_v3_12_0::detail::is_comparable<std::less<std::__cxx11::basic_string<char> >, const std::__cxx11::basic_string<char>&, nlohmann::json_abi_v3_12_0::json_pointer<std::__cxx11::basic_string<char> >&, void>’: include/nlohmann/json.hpp:2328:20: required by substitution of ‘template<class ValueType, class KeyType, class ReturnType, typename std::enable_if<((((nlohmann::json_abi_v3_12_0::detail::is_transparent<std::less<std::__cxx11::basic_string<char> > >::value && (! nlohmann::json_abi_v3_12_0::detail::is_specialization_of<nlohmann::json_abi_v3_12_0::json_pointer, typename std::remove_cv<typename std::remove_reference<_Arg>::type>::type>::value)) && nlohmann::json_abi_v3_12_0::detail::is_comparable<std::less<std::__cxx11::basic_string<char> >, const std::__cxx11::basic_string<char>&, KeyType, void>::value) && nlohmann::json_abi_v3_12_0::detail::is_getable<nlohmann::json_abi_v3_12_0::basic_json<>, ReturnType>::value) && (! std::is_same<nlohmann::json_abi_v3_12_0::detail::value_t, typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type>::value)), int>::type <anonymous> > ReturnType nlohmann::json_abi_v3_12_0::basic_json<>::value<ValueType, KeyType, ReturnType, <anonymous> >(KeyType&&, ValueType&&) const [with ValueType = int; KeyType = nlohmann::json_abi_v3_12_0::json_pointer<std::__cxx11::basic_string<char> >&; ReturnType = int; typename std::enable_if<((((nlohmann::json_abi_v3_12_0::detail::is_transparent<std::less<std::__cxx11::basic_string<char> > >::value && (! nlohmann::json_abi_v3_12_0::detail::is_specialization_of<nlohmann::json_abi_v3_12_0::json_pointer, typename std::remove_cv<typename std::remove_reference<_Arg>::type>::type>::value)) && nlohmann::json_abi_v3_12_0::detail::is_comparable<std::less<std::__cxx11::basic_string<char> >, const std::__cxx11::basic_string<char>&, KeyType, void>::value) && nlohmann::json_abi_v3_12_0::detail::is_getable<nlohmann::json_abi_v3_12_0::basic_json<>, ReturnType>::value) && (! std::is_same<nlohmann::json_abi_v3_12_0::detail::value_t, typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type>::value)), int>::type <anonymous> = <missing>]’ main.cpp:21:24: required from here include/nlohmann/detail/meta/type_traits.hpp:568:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 568 | decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())), | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp:569:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 569 | decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>())) | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp:568:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 568 | decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())), | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ In file included from include/nlohmann/detail/exceptions.hpp:25, from include/nlohmann/detail/conversions/from_json.hpp:26, from include/nlohmann/adl_serializer.hpp:14, from include/nlohmann/json.hpp:34, from main.cpp:1: include/nlohmann/detail/meta/type_traits.hpp:569:33: warning: ‘nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::operator nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t() const [with RefStringType = std::__cxx11::basic_string<char>; nlohmann::json_abi_v3_12_0::json_pointer<RefStringType>::string_t = std::__cxx11::basic_string<char>]’ is deprecated: Since 3.11.0; use to_string() [-Wdeprecated-declarations] 569 | decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>())) | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from include/nlohmann/json.hpp:50, from main.cpp:1: include/nlohmann/detail/json_pointer.hpp:81:5: note: declared here 81 | operator string_t() const | ^~~~~~~~ main.cpp: In function ‘int main()’: main.cpp:21:10: warning: unused variable ‘val3’ [-Wunused-variable] 21 | auto val3 = j.value(p2, 0); // 0 | ^~~~

Compiler and operating system

gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu122.04.2) / gcc version 13.1.0 (Ubuntu 13.1.0-8ubuntu122.04)

Library version

nlohmann json 3.12.0

Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions