You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(I should definitely have another look at all the overloads of insert of the STL containers. For instance, std::map::iterator insert( const_iterator hint, const value_type& value ); exists, but an insert function with the same signature is currently only allowed for arrays.)
I have also needed this type of functionality. For the time being until this is closed, I have been using this code which modifies the json object in place.
One thing to consider is the desired behavior when j1 and j2 have matching keys. In this case j1 is over-written by values in j2.
Well in python it interprets json data as an dict and then it could append (or insert) . In that case append is an alias of insert that would append to the end of the json, but insert would allow you to specify where to insert entries at. Maybe this library could copy some of that logic even though it would not be an dict.
I would prefer an shortcut that would overload operator>> where if it is another json object it would move the data over to the other one and return the object with it merged so when someone does this.
json2 >> json1;
That json2's data would move to json1 (or so it would seem like it).
@AraHaan What exactly do you propose? We have several overloads for push_back and insert. And Python only interprets JSON objects as dict. Arrays are mapped to lists.
Activity
nlohmann commentedon Jan 15, 2017
This is an interesting question. Currently, there is no
insert(InputIt first, InputIt last)function such asstd::map::insert.nlohmann commentedon Jan 15, 2017
(I should definitely have another look at all the overloads of
insertof the STL containers. For instance,std::map::iterator insert( const_iterator hint, const value_type& value );exists, but aninsertfunction with the same signature is currently only allowed for arrays.)andnp commentedon Mar 23, 2017
I have also needed this type of functionality. For the time being until this is closed, I have been using this code which modifies the json object in place.
One thing to consider is the desired behavior when
j1andj2have matching keys. In this casej1is over-written by values inj2.Hopefully this helps for now.
nlohmann commentedon Mar 24, 2017
Thanks for the hint. I shall have a look at the
insertfunctions of the STL containers and propose an extension here.✨ proposal for #428
nlohmann commentedon Apr 7, 2017
Here is a proposal for an insert function that takes an iterator range and can merge an object into another: 97a25de. Feedback very appreciated!
AraHaan commentedon Apr 8, 2017
Well in python it interprets json data as an dict and then it couldappend(orinsert) . In that case append is an alias of insert that would append to the end of the json, but insert would allow you to specify where to insert entries at. Maybe this library could copy some of that logic even though it would not be an dict.I would prefer an shortcut that would overload
operator>>where if it is another json object it would move the data over to the other one and return the object with it merged so when someone does this.That
json2's data would move tojson1(or so it would seem like it).nlohmann commentedon Apr 8, 2017
@AraHaan What exactly do you propose? We have several overloads for push_back and insert. And Python only interprets JSON objects as dict. Arrays are mapped to lists.
nlohmann commentedon Apr 8, 2017
@AraHaan I fear that overload of
operator>>would be very unintuitive for C++ programmers.itviewer commentedon Apr 11, 2017
Thanks,It's good for me!
nlohmann commentedon Apr 22, 2017
Merged with 4b316ec.
mlund commentedon Jul 3, 2017
Great this has been added; #252 addresses a similar issue and I have used
I found the scenario below slightly counter-intuitive using the new approach,
json j1 = { {"a",1} }; json j2 = { {"a",2} }; j1.insert(j2.begin(), j2.end()); // --> a=1But perhaps this is also the behaviour of maps?
mlund commentedon Jul 3, 2017
std::map::insertdoes not overwrite either.nlohmann commentedon Jul 3, 2017
We use std::map::insert internally to provide the least surprising behavior. I also would have needed to look up the behavior in case of a duplicate.
mlund commentedon Jul 3, 2017
That makes sense. I think the solution is simply to pay attention to order when merging,
Thanks!