Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/surgicalcoder/JsonPolymorphicGenerator
c# / .net Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes
https://github.com/surgicalcoder/JsonPolymorphicGenerator
Last synced: 3 months ago
JSON representation
c# / .net Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes
- Host: GitHub
- URL: https://github.com/surgicalcoder/JsonPolymorphicGenerator
- Owner: surgicalcoder
- License: mit
- Created: 2023-05-06T10:08:22.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-07-16T17:11:13.000Z (over 1 year ago)
- Last Synced: 2024-05-12T12:41:21.364Z (6 months ago)
- Language: C#
- Size: 19.5 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/surgicalcoder/JsonPolymorphicGenerator
- csharp-source-generators - JsonPolymorphicGenerator - ![stars](https://img.shields.io/github/stars/surgicalcoder/JsonPolymorphicGenerator?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/surgicalcoder/JsonPolymorphicGenerator?style=flat-square&cacheSeconds=86400) - Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes (Source Generators / Serialization)
README
# JsonPolymorphicGenerator
c# / .net Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes## Usage
For this, your base classes need the `partial` and `abstract` key words, and be decorated with `JsonPolymorphic`, and there need to be derived types in that same assembly for this to work.
An example of this is:
```
[JsonPolymorphic]
public abstract partial class BaseClass
{
public string Property1 { get; set; }
}
```This will then generate a partial class, that is decorated with the `JsonDerivedType` attribute, and use the class name as the discriminator.
```
[JsonDerivedType(typeof(GoLive.JsonPolymorphicGenerator.Playground.InheritedClass1), "InheritedClass1")]
[JsonDerivedType(typeof(GoLive.JsonPolymorphicGenerator.Playground.InheritedClass2), "InheritedClass2")]
public partial class BaseClass
{
}
```You can now transform the text of the attributes that gets spat out! You have a number of options, that gets added to an `.editorconfig`, such as:
```
root = true[*.cs]
jsonpolymorphicgenerator.text_preappend = JSON_
jsonpolymorphicgenerator.text_transform = return classname.GetHashCode().ToString()
jsonpolymorphicgenerator.text_postappend = _A
```For the `jsonpolymorphicgenerator.text_transform` option, you have to provide valid c# code, that returns a string - there are 2 input variables - `classname` and `namespacename`