Skip to content

Commit

Permalink
Support jsonSerializationContext in global namespace (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam authored Nov 8, 2023
1 parent 32028fe commit 7bd4719
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//HintName: ProjectJsonSerializerContext.g.cs
#nullable enable

namespace PolymorphicGlobalNamespace;

public partial class ProjectJsonSerializerContext
{
public static System.Collections.Generic.IReadOnlyCollection<System.Text.Json.Serialization.JsonConverter> GetPolymorphicConverters() => new System.Text.Json.Serialization.JsonConverter[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ IReadOnlyCollection<JsonConverter> converters
}

var targetNamespace = jsonSerializerContextClassType.ContainingNamespace.IsGlobalNamespace
? JsonPolymorphicConverterIncrementalGenerator.EmptyPolymorphicNamespace
? null
: jsonSerializerContextClassType.ContainingNamespace.ToDisplayString(JsonPolymorphicConverterIncrementalGenerator.NamespaceFormat);

return Generate(
Expand All @@ -55,14 +55,24 @@ IReadOnlyCollection<JsonConverter> converters
{
var converterTypeString = converterType.ToString();

var namespaceLine = string.Empty;
if (jsonSerializerContext.Namespace is { } jsonConverterNamespace)
{
namespaceLine =
// language=cs
$"""

namespace {jsonConverterNamespace};

""";
}

return new FileWithName(
$"{jsonSerializerContext.ClassName}.g.cs",
// language=cs
$$"""
#nullable enable

namespace {{jsonSerializerContext.Namespace}};

{{namespaceLine}}
{{jsonSerializerContext.ClassAccessibility}} partial class {{jsonSerializerContext.ClassName}}
{
public static System.Collections.Generic.IReadOnlyCollection<System.Text.Json.Serialization.JsonConverter> Get{{converterTypeString}}Converters() => new System.Text.Json.Serialization.JsonConverter[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class JsonPolymorphicConverterIncrementalGenerator : IIncrementalGenerato
{
public const string Id = "AVI_JPC";

public const string EmptyPolymorphicNamespace = "PolymorphicGlobalNamespace";
private const string EmptyPolymorphicNamespace = "PolymorphicGlobalNamespace";

internal static readonly SymbolDisplayFormat NamespaceFormat = new(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Aviationexam.GeneratedJsonConverters.SourceGenerator;

internal sealed record JsonSerializerContext(
string ClassAccessibility,
string Namespace,
string? Namespace,
string ClassName
);

0 comments on commit 7bd4719

Please sign in to comment.