-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Why are flattened empty objects/arrays not representable? #1874
Comments
The reason is that JSON pointers only allow for primitive values. Therefore, you describe exactly the edge case that is not properly representable. |
Ah, I suppose that makes sense. Thanks so much for the quick response! It's unfortunate it seems that there a few features missing from JSON pointer/patch that would be so useful. This is certainly one - seems like it would be pretty trivial to allow a pointer to refer to a structured object and have the value for the "flattened" output above be On a possibly related side note, flattening an empty A second related feature would be to allow The third missing piece as I see it is to have a
You could definitely break this patch up into two Obviously these aren't necessarily spec, but I'm curious what you think? |
I do not understand your proposal. Could you elaborate? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Apologies for the delayed reply! Happy to elaborate. I was thinking the following: Patch
|
I need to check whether the described behavior for [{"op": "add", "path": "/foo/-", "value": 123}] is standard compliant. I am afraid adding a |
http://jsonpatch.com states:
But in your example, you start with an empty object #include <iostream>
#include "json.hpp"
using json = nlohmann::json;
int main() {
json ja = json::array();
json jo = json::object();
json p = R"([{"op": "add", "path": "/-", "value": 123}])"_json;
std::cout << ja.patch(p) << std::endl;
std::cout << jo.patch(p) << std::endl;
} Output:
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
When you call
flatten()
on an object with an empty array, the flattened value isnull
instead of[]
. As a result, when youunflatten()
the data back, you do not get the original object. Unfortunately, this makes it difficult to take advantage of flattening to operate on the object tree directly without having to traverse it (e.g., remove all entries where the path contains "x").The
flatten()
documentation (https://nlohmann.github.io/json/classnlohmann_1_1basic__json_ab838f000d76662917ffd6ec529569e03.html#ab838f000d76662917ffd6ec529569e03) notes that this will be the case, but it's not clear why the values must be primitives since the flattened object is just a JSON object itself. Is there a reason the values need to be primitives?For example:
produces the following:
Compiler: GCC 7.4
Library version: 3.7.3 single include
The text was updated successfully, but these errors were encountered: