Skip to content

Commit

Permalink
feat: generate JSON converter (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarllot authored Feb 26, 2024
1 parent 7cab1ff commit 2afb0ed
Show file tree
Hide file tree
Showing 64 changed files with 970 additions and 916 deletions.
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
9 changes: 5 additions & 4 deletions src/EnumUtilities.Abstractions/EnumGeneratorAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Raiqub.Generators.EnumUtilities;
using System.Diagnostics.CodeAnalysis;

namespace Raiqub.Generators.EnumUtilities;

[AttributeUsage(AttributeTargets.Enum)]
public sealed class EnumGeneratorAttribute : Attribute
{
}
[ExcludeFromCodeCoverage]
public sealed class EnumGeneratorAttribute : Attribute;
10 changes: 5 additions & 5 deletions src/EnumUtilities.Abstractions/EnumUtilities.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<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>

</Project>
11 changes: 11 additions & 0 deletions src/EnumUtilities.Abstractions/JsonConverterGeneratorAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Diagnostics.CodeAnalysis;

namespace Raiqub.Generators.EnumUtilities;

[AttributeUsage(AttributeTargets.Enum)]
[ExcludeFromCodeCoverage]
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

0 comments on commit 2afb0ed

Please sign in to comment.