Is it possible to add JsonSerializable attributes for types from project based on some filter? #103349
Replies: 1 comment
-
Just an idea for a less-than-optimal approach: How about a pre-build event script that collects the respective type names and then overwrites/modifies the source code file containing your partial JsonSerializerContext class declaration, adding the necessary JsonSerializableAttributes to it? Sort of like a source generator for your partial JsonSerializerContext class declaration without it being a Roslyn-supported source generator, if you will. (It's a bit how ya goin', but sometimes you gotta do what you gotta do; and in my opinion it should be less complex and less of a maintenance burden than having a copy of the STJ code gen hanging off your leg while trying to muzzle the original SDK-based STJ code gen the compiler would want to execute implicitly...) |
Beta Was this translation helpful? Give feedback.
-
I'm wondering, it is possible somehow to avoid adding JsonSerializable attributes for each type that I need to serialize? E.g. to add this attribute for any type with name ending with Request or Response in my project.
I realize that there is no such thing available built-in, but are there any ideas how could I implement that on my own?
E.g. the first thing that comes to mind is to write a source generator, which would add those attributes. Unfortunately, Roslyn does not allow to chain source generators.
I have an idea to just copy STJ generator's source code, and invoke it directly, but that doesn't sound great...
Beta Was this translation helpful? Give feedback.
All reactions