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

Inserting in unordered json using a pointer retains the leading slash #2958

Closed
1 of 5 tasks
emanuel-raad opened this issue Aug 20, 2021 · 2 comments
Closed
1 of 5 tasks
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@emanuel-raad
Copy link

emanuel-raad commented Aug 20, 2021

What is the issue you have?

Inserting a node in an unordered json object using a pointer retains the leading slash

Please describe the steps to reproduce the issue.

I am using the single json.hpp file

Can you provide a small but working code example?

using json = nlohmann::json;
using ojson = nlohmann::ordered_json;
using json_pointer = nlohmann::json::json_pointer;

std::string p = "/root";

json test1;
test1[json_pointer(p)] = json::object();
std::cout << test1 << '\n';

ojson test2;
test2[json_pointer(p)] = json::object();
std::cout << test2 << '\n';

I also tested with "/root"_json_pointer instead of json_pointer(p) and I get the same behaviour.

What is the expected behavior?

Output from the code above:

{"root":{}}
{"/root":{}}

And what is the actual behavior instead?

I expect both outputs to be the same, without the leading forward slash:

{"root":{}}
{"root":{}}

Which compiler and operating system are you using?

  • Compiler: MSVC\14.28.29910
  • Operating system: Windows 10\19043.1165

Which version of the library did you use?

  • latest release version 3.10.0
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

Not a compilation error.

  • yes
  • no - please copy/paste the error message below
@nlohmann
Copy link
Owner

The reason for this behavior is that you use a nlohmann::json::json_pointer with a nlohmann::unordered_json:

ojson test2;
test2[json_pointer(p)] = json::object();

There are two overloads for ordered_json::operator[]: one for strings and one for nlohmann:: unordered_json::json_pointer. As json_pointer(p) is not of the latter type, but has an implicit conversion to std::string, the former overload is used with /root.

You can fix this by changing the type of the JSON Pointer in the second example:

ojson test2;
test2[ojson::json_pointer(p)] = json::object();

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels Aug 20, 2021
@emanuel-raad
Copy link
Author

Thanks for the explanation about the overloads! I switched to using nlohmann:: unordered_json::json_pointer and it works now

falbrechtskirchinger added a commit to falbrechtskirchinger/json that referenced this issue Apr 5, 2022
falbrechtskirchinger added a commit to falbrechtskirchinger/json that referenced this issue Apr 6, 2022
falbrechtskirchinger added a commit to falbrechtskirchinger/json that referenced this issue Apr 6, 2022
falbrechtskirchinger added a commit to falbrechtskirchinger/json that referenced this issue Apr 7, 2022
falbrechtskirchinger added a commit to falbrechtskirchinger/json that referenced this issue Apr 7, 2022
nlohmann pushed a commit that referenced this issue Apr 12, 2022
* Make exception context optional

Change exception context parameter to pointer and replace context with
nullptr where appropriate.

* Support escaping other string types

* Add string concatenation function

Add variadic concat() function for concatenating char *, char, and
string types.

* Replace string concatenations using + with concat()

* Template json_pointer on string type

Change json_pointer from being templated on basic_json to being
templated on string type.

* Add unit test for #3388

Closes #3388.

* Fix regression test for #2958

* Add backwards compatibility with json_pointer<basic_json>

* Update json_pointer docs

* Allow comparing different json_pointers

* Update version numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants