Skip to content

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

Closed

Description

@jrockinl

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

Activity

  1. dimitre commented on Sep 11, 2024

    @dimitre

    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

  2. kfwu1999 commented on Sep 25, 2024

    @kfwu1999
    Contributor

    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);
    }
  3. github-actions commented on Jan 19, 2025

    @github-actions

    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!

  4. added
    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated
    on Jan 19, 2025
  5. nlohmann commented on Jan 19, 2025

    @nlohmann
    Owner

    I can confirm the issue.

  6. removed
    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated
    on Jan 20, 2025
  7. codenut commented on Mar 4, 2025

    @codenut
    Contributor

    Hello @nlohmann , I'm interested to work on this issue. Could you give me some hints on how to fix this?

  8. added this to the Release 3.12.1 milestone on May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions