Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate JSON converter #73

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageTags>
generator;source;enum;enumeration;reflection;attribute;tostring;isdefined;enummember;description;display;
shortname;resourcetype;resource;parse;tryparse;ignorecase;stringcomparison;factory;extension;validation;
codegen;fast
codegen;fast;json;jsonconverter
</PackageTags>
</PropertyGroup>

Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ This will generate the following methods:
- TryCreateFromDescription(string?, StringComparison)
- TryCreateFromDescription(string?)

### JSON Serialization

Besindes the member name, supports the [EnumMemberAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.enummemberattribute) and [JsonPropertyNameAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonpropertynameattribute) attributes.

Example:

```csharp
[JsonConverterGenerator]
[JsonConverter(typeof(SeasonJsonConverter))]
public enum Season
{
[EnumMember(Value = "\ud83c\udf31")]
Spring = 1,
[EnumMember(Value = "\u2600\ufe0f")]
Summer,
[EnumMember(Value = "\ud83c\udf42")]
Autumn,
[EnumMember(Value = "\u26c4")]
Winter
}
```

This will generate the following JSON converter: [SeasonJsonConverter](./tests/EnumUtilities.IntegrationTests/Generated/Raiqub.Generators.EnumUtilities/Raiqub.Generators.EnumUtilities.EnumUtilitiesGenerator/Raiqub.Generators.EnumUtilities.IntegrationTests.Models.SeasonJsonConverter.g.cs).

## Contributing

If something is not working for you or if you think that the source file
Expand Down
4 changes: 1 addition & 3 deletions src/EnumUtilities.Abstractions/EnumGeneratorAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
namespace Raiqub.Generators.EnumUtilities;

[AttributeUsage(AttributeTargets.Enum)]
public sealed class EnumGeneratorAttribute : Attribute
{
}
public sealed class EnumGeneratorAttribute : Attribute;
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>Raiqub.Generators.EnumUtilities</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>Raiqub.Generators.EnumUtilities</RootNamespace>
</PropertyGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Raiqub.Generators.EnumUtilities;

[AttributeUsage(AttributeTargets.Enum)]
public sealed class JsonConverterGeneratorAttribute : Attribute
{
public bool AllowIntegerValues { get; set; } = true;
public object? DeserializationFailureFallbackValue { get; set; }
}
3 changes: 3 additions & 0 deletions src/EnumUtilities/CodeWriters/EnumExtensionsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ public EnumExtensionsWriter(StringBuilder builder) : base(builder)

public override string GetFileName() => $"{Model.Namespace ?? "_"}.{Model.Name}Extensions.g.cs";

protected override bool CanGenerateFor(EnumToGenerate model) =>
(model.SelectedGenerators & SelectedGenerators.MainGenerator) != 0;


#line default
#line hidden
Expand Down
3 changes: 3 additions & 0 deletions src/EnumUtilities/CodeWriters/EnumExtensionsWriter.tt
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,7 @@ using System;
}

public override string GetFileName() => $"{Model.Namespace ?? "_"}.{Model.Name}Extensions.g.cs";

protected override bool CanGenerateFor(EnumToGenerate model) =>
(model.SelectedGenerators & SelectedGenerators.MainGenerator) != 0;
#>
3 changes: 3 additions & 0 deletions src/EnumUtilities/CodeWriters/EnumFactoryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,9 @@ public EnumFactoryWriter(StringBuilder builder) : base(builder)

public override string GetFileName() => $"{Model.Namespace ?? "_"}.{Model.Name}Factory.g.cs";

protected override bool CanGenerateFor(EnumToGenerate model) =>
(model.SelectedGenerators & SelectedGenerators.MainGenerator) != 0;


#line default
#line hidden
Expand Down
3 changes: 3 additions & 0 deletions src/EnumUtilities/CodeWriters/EnumFactoryWriter.tt
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,7 @@ using System.Globalization;
}

public override string GetFileName() => $"{Model.Namespace ?? "_"}.{Model.Name}Factory.g.cs";

protected override bool CanGenerateFor(EnumToGenerate model) =>
(model.SelectedGenerators & SelectedGenerators.MainGenerator) != 0;
#>
Loading
Loading