Creating values at non-existing paths with json_pointer: is it an undocumented behaviour? #4446
Replies: 1 comment
-
Yes, this is the behavior. The RFC for JSON Pointer does not specify how values are created, so we had to improvise here.
https://json.nlohmann.me/api/basic_json/operator%5B%5D/ is the documentation, and it does not specifically mentions the strategy for JSON Pointers. We mimic the sam behavior for |
Beta Was this translation helpful? Give feedback.
-
I am using v3.11.3
For the above ptr, using operator[] to insert elements into a non-exisitng nested path, what is to be expected? It creates the below json:
So it seems to interpret the non-numeric token as a key to create and insert an object where nothing existed previously and then numeric tokens are interpreted as array indices which creates arrays for the previously non-existing positions.
If I change the pointer to
/foo/one/one/one
, it creates the following:If I understand it correctly then for non-existing values, the type (object or array) is inserted depending on if the token in the json pointer is convertible to a number or not? So if it is convertible to a number then, it is treated like an array index which creates an array otherwise it is treated like an object key and an object is inserted?
The documentation mentions how numeric and non-numeric token inserts elements in a pre-existing type (either array or object) but doesn't talk (or maybe I can't find it?) about how it behaves when nothing exists. Is this undocumented behaviour or do I not understand the documentation? Would really appreciate if someone could clarify this.
Beta Was this translation helpful? Give feedback.
All reactions