0

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?

15
  • 1
    Currently it is not possible to chain IIncrementalGenerator source 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. Commented Nov 20, 2024 at 14:10
  • 1
    How many root DTOs are there in the project? Why is it such a problem to use the JsonSerializable attribute 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 Commented Nov 20, 2024 at 14:14
  • The suggested workaround is to chain projects instead, and run the dependent source generator in a dependent project. But honestly I can't see how that workaround could be used in this case. Commented Nov 20, 2024 at 14:14
  • @PanagiotisKanavos The core idea is that we don't have to maintain the JSON context manually whenever a class is added to one of our entity projects. The generator should -- ideally -- read a configuration file where I can define each context to be created (and its name) along with the project names from which the types should be added. Commented Nov 20, 2024 at 14:18
  • @PanagiotisKanavos We have a lot of root types. The main point to take away here is convinience: If the generator automatically creates contexts and adds the desired types as specified per configuration, this no longer needs to be maintained manually and does not have be versioned. I'm not arguing that a .json file is better than a .cs file in this use case. My task is to make this dynamic and that's what I'm simply trying to achieve. Commented Nov 20, 2024 at 14:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.