netheril96 / StaticJSON

Fast, direct and static typed parsing of JSON with C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ArrayHandler only supports PATCH, not PUT

tribal-tec opened this issue · comments

Imagine you have an object with a std::vector of values. You receive a JSON array with new values. Now using the standard ArrayHandler for std::vector, it will emplace_back all new values to the existing one. This is correct if the JSON payload was part of an HTTP PATCH.

Now I would like support of PUT, where it clears the vector first before adding all new values. Is there already support in the library? I haven't seen any so far, so I'm dropping this question here. Maybe it's just me, but I find it the current behavior counter-intuitive.

I do not reset objects before parsing JSON value into them. It gives the user more control as they can set default values in that way. The user can always reset objects themselves without the help of this library.

It could maybe be a flag for add_property? std::array and PODs are naturally replaced, but not other arrays. So at least this should be mentioned somewhere as I was stumbling upon it while debugging :)

Edit: clearing before might not be a good thing if from_json_string() fails if the JSON was malformed. I still think it's more correct to do it in the library.

OK, I will clarify it in the documentation.

Sorry for being annoying, but it needs library support. If you have an object with multiple vectors which are optional, you cannot clear before from_json(), unless the user parses the JSON him/herself to see which array is about to be updated.

Also, std::string is technically a vector as well, and this get replaced. Append does not make sense most of the time, but controlling this at least with a flag should be possible imo.

You are right. Not clearing the vector is inconsistent behavior. I will change it in the next commit.

OK, on a second thought, it seems a harder problem that I had in mind. I need to think through.

Thanks for your fast response and looking into this. Really like your library!

Commit 945d2c6 should solve your problem. Please git pull and test if it indeed works as you intend.

Works like a charm. Thx a lot for your efforts and the quick fix!