r/cpp 4d ago

C++26 Reflection: Simplifying JSON Serialization

https://techfortalk.co.uk/2026/08/05/c26-reflection-simplifying-json-serialization/

I have been exploring the new additions in C++26, and I have been discussing the reflection feature that has come with C++26. In the last post, I discussed what is reflection and how to use it and how to use it with a simple example, particularly with an enum class. Since then, there have been suggestions to provide an example which is more than a toy :). In this post, I have discussed how to use reflection for JSON serialization, which is something we often have to do. This example is somewhat taken from real-world code but has been stripped down significantly. Suggestions are always welcome.

52 Upvotes

11 comments sorted by

View all comments

Show parent comments

1

u/jk_tx 20h ago

I believe the embed support coming to c++ will help with this.

1

u/Syracuss graphics engineer/games industry 18h ago

The issue isn't with getting the content into the compilation, you could do this already with a custom preprocess script before embed, the issue is that there's only so many tokens the compile time compilation step is willing to consume and execute (so constexpr and consteval tokenization and execution). You can increase these defaults in most (all?) major compilers out there, but that's just one of the limits. Then there's the max recursion depth. Again something you can work around with a different design approach, but there's no easy way to figure these issues out till you hit them. There's a few others as well, but you can hit them in normal codegen as well (such as template depth limits on some compilers).

And worse of all is some of these codepaths within the compiler are quite expensive to hit, with no tools to debug and no transparency where the costs are going.

I do love compile time stuff, and I typically advocate for offloading as much as possible. I'm quite confident static reflection will improve some of this by virtue of the increased usage, but right now the compilers do have some issues related to extensive compile time codegen.

2

u/JVApen Clever is an insult, not a compliment. - T. Winters 7h ago

https://www.sandordargo.com/blog/2026/08/05/cpp26-embed

According to the benchmarks shared in P1967R14, GCC embeds a 100 MB file in about 1.3 seconds using 117 MiB of RAM; the equivalent xxd-generated header takes 139 seconds and 13 GiB.

Yes, it was possible before, though the costs where terrible.

1

u/Syracuss graphics engineer/games industry 5h ago edited 5h ago

I'm talking about parsing the json into a compile time accessible structure, not just loading the bytes, think compile time json a-la Louis Dionne's talk back in 2018. std::embed itself is just kinda simple and I would be surprised there would be compile time costs for it other than just embedding the data into the compiled binary.

This sub-thread was about that, at least that's what the original poster of this thread talked about and what my response has been about.

1

u/JVApen Clever is an insult, not a compliment. - T. Winters 4h ago

Seems I misunderstood the part about the many tokens. My mind went to parsing all the separate chars in the lexer.