C++20 Function for converting arbitrary structs to json without serialisers #4394
ptesavol
started this conversation in
Show and tell
Replies: 1 comment
|
Very cool proof of concept - using boost::pfr with C++20 to auto-generate to_json for arbitrary structs is a neat way to cut down on boilerplate. Given the library stays header-only with no Boost dependency and supports pre-C++20 compilers, this is best kept as an external helper, but thanks for sharing the Compiler Explorer link! This reply was drafted by Claude Code on behalf of @nlohmann, as part of a review of open discussions. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I wrote a templated function
nlohmann::json structToJson(anyStruct)that can convert arbitrary structs to nlohmann::json without the need of writing serialisers. Implementing the function was made possible by C++20 and boost::pfr. The function is still at proof-of-concept stage, but it can already convert quite complex nested structs that have maps and vectors of user-defined data types as members.You can try it out with the Compiler Explorer at https://godbolt.org/z/TKEo65aje
It would be nice to have similar functionality in the library itself, but I understand that it might not be realistic because of the requirement for C++20 and boost::pfr
All reactions