json_pointer - a code example from doc's does not compile
Description
Gnu C++ does not compile the code example below. GCC 11.4.0, with -std=c++11 option
Example taken from https://json.nlohmann.me/features/json_pointer/#introduction
json::json_pointer p = "/nested/one";
main.cpp code found in "Minimal code example" field.
The _json_pointer literal is the only line that compiles correctly.
note: Clang compiler (clang++ v14.0.0) basically reports the same errors.
Reproduction steps
g++ main.cpp --std=c++11
Expected vs. actual results
JSON Pointer creation¶
JSON Pointers can be created from a string:
json::json_pointer p = "/nested/one";
Minimal code example
#include <string>
#include "json.hpp"
using json = nlohmann::json;
int main()
{
std::string ptrStr = "/nested";
ptrStr += "/one";
json::json_pointer ptr1 = "/nested/one";
auto ptr2 = "/nested/one"_json_pointer;
json::json_pointer ptr3 = ptrStr;
}
Error messages
GNU:
main.cpp: In function ‘int main()’:
main.cpp:11:31: error: conversion from ‘const char [12]’ to non-scalar type ‘nlohmann::json_abi_v3_11_3::basic_json<>::json_pointer’ {aka ‘nlohmann::json_abi_v3_11_3::json_pointer<std::__cxx11::basic_string<char> >’} requested
11 | json::json_pointer ptr1 = "/nested/one";
| ^~~~~~~~~~~~~
main.cpp:13:31: error: conversion from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to non-scalar type ‘nlohmann::json_abi_v3_11_3::basic_json<>::json_pointer’ {aka ‘nlohmann::json_abi_v3_11_3::json_pointer<std::__cxx11::basic_string<char> >’} requested
13 | json::json_pointer ptr3 = ptrStr;
| ^~~~~~
Clang:
main.cpp:11:24: error: no viable conversion from 'const char[12]' to 'json::json_pointer' (aka 'json_pointer<std::basic_string<char>>')
json::json_pointer ptr1 = "/nested/one";
^ ~~~~~~~~~~~~~
./json.hpp:13841:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char[12]' to 'const nlohmann::json_pointer<std::basic_string<char>> &' for 1st argument
class json_pointer
^
./json.hpp:13841:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const char[12]' to 'nlohmann::json_pointer<std::basic_string<char>> &&' for 1st argument
./json.hpp:13868:14: note: explicit constructor is not a candidate
explicit json_pointer(const string_t& s = "")
^
main.cpp:13:24: error: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'json::json_pointer' (aka 'json_pointer<std::basic_string<char>>')
json::json_pointer ptr3 = ptrStr;
^ ~~~~~~
./json.hpp:13841:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::string' (aka 'basic_string<char>') to 'const nlohmann::json_pointer<std::basic_string<char>> &' for 1st argument
class json_pointer
^
./json.hpp:13841:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::string' (aka 'basic_string<char>') to 'nlohmann::json_pointer<std::basic_string<char>> &&' for 1st argument
./json.hpp:13868:14: note: explicit constructor is not a candidate
explicit json_pointer(const string_t& s = "")
^
2 errors generated.
Compiler and operating system
Ubuntu 22.04
Library version
3.11.1 and 3.11.3
Validation
- [ ] The bug also occurs if the latest version from the
developbranch is used. - [ ] I can successfully compile and run the unit tests.
I'm having a similar issue using json_pointers which we use extensively on openFrameworks projectGenerator
[json.exception.parse_error.109] parse error: array index 'objects' is not a number
errors like this in code that used to work well before 3.11.3
The compilation errors occur because the constructor of json::json_pointer is marked as explicit.
Removing the explicit keyword allows the code to compile and pass the tests, but i think it might violate the design intentions.
So, I think the better approach is to adjust the example code to use direct initialization.
Minimal code example (Modified)
#include <string>
#include "json.hpp"
using json = nlohmann::json;
int main()
{
std::string ptrStr = "/nested";
ptrStr += "/one";
json::json_pointer ptr1("/nested/one");
auto ptr2 = "/nested/one"_json_pointer;
json::json_pointer ptr3(ptrStr);
}
This issue has been marked as stale because it has been open for 90 days without activity. If this issue is still relevant, please add a comment or remove the "stale" label. Otherwise, it will be closed in 10 days. Thank you for helping us prioritize our work!
I can confirm the issue.
Hello @nlohmann , I'm interested to work on this issue. Could you give me some hints on how to fix this?