stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.23k stars 123 forks source link

Fix json parsing from arrays to set like types with custom structs #1394

Closed sjanel closed 1 month ago

sjanel commented 1 month ago

Hello,

Several things in this PR:

stephenberry commented 1 month ago

@sjanel, thanks for submitting this pull request. I'll look into it ASAP, though it may be a few days until I have the time.

stephenberry commented 1 month ago

@sjanel, I was able to look at the issue and thankfully its simple. For writing we have to iterate through the container, however std::set iterators (even from non-const containers) are const qualified (see https://en.cppreference.com/w/cpp/container/set/begin). This means that our custom serializer must take in a const reference: const custom_struct& value.

This means that you cannot mutate the set elements on writing. If you think about it, it makes sense, because mutating the values would need to change their location in the set. So, a std::set is probably the wrong type if you're needing to mutate on write.

I've updated the unit test to build and pass. I'll merge in this fix, thanks so much!