Is there a way to make the process of registering types to serialise dynamic, i.e. not having to tediously write [JsonSerializable(typeof(<type goes here>))] for each type.
So far, I've tried to get all types from the desired assemblies (and their property types by recursive descent) and add the JsonSerializable attribute for each type at runtime using TypeDescriptors. Since source generation happens at compile time this obviously did not work (duh).
After googling to no real end I opted for my last resort which was implementing a custom IIncrementalGenerator that basically generates a partial class that derives the JsonSerializerContext and adds all required JsonSerializable attributes on top of it.
Now that I have a minimally working generator I get an error that my context class doesn't implement some members -- the same error that occurs when the sources have not yet been generated by the JsonSourceGenerator.
Is there a way to run my generator before the one from System.Text.Json? Or has anyone come up with another way to achieve what I'm trying to implement (the hard way) here?
IIncrementalGeneratorsource generators in a single project, see Is it possible to chain source generators in .net? and (specifically for System.Text.Json) Why does JsonSerializerContext generate empty JSON for a model containing[ObservableProperty]properties? and Allow for a way to ensure some source generators run before / after others #57239.JsonSerializableattribute for the root DTOs? If there are multiple libraries or big namespaces in a solution, you can create a separate context for each and combine them.jsonfile is better than a.csfile in this use case. My task is to make this dynamic and that's what I'm simply trying to achieve.