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

json_pointer - a code example from doc's does not compile #4453

Open
2 tasks
jrockinl opened this issue Sep 9, 2024 · 2 comments
Open
2 tasks

json_pointer - a code example from doc's does not compile #4453

jrockinl opened this issue Sep 9, 2024 · 2 comments

Comments

@jrockinl
Copy link

jrockinl commented Sep 9, 2024

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

@dimitre
Copy link

dimitre commented Sep 11, 2024

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

@kfwu1999
Copy link

kfwu1999 commented Sep 25, 2024

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants