Skip to content

Commit

Permalink
fix: Improved SystemTextJsonSerializer.GetContextType.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 31, 2024
1 parent 38ed407 commit a9d7d66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<PropertyGroup Label="Versioning">
<Version>0.9.3</Version>
<Version>0.9.4</Version>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>
<MinVerTagPrefix>v</MinVerTagPrefix>
<MinVerDefaultPreReleaseIdentifiers>dev</MinVerDefaultPreReleaseIdentifiers>
Expand Down
42 changes: 32 additions & 10 deletions src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace OpenApiGenerator.Core.Json;
public class SystemTextJsonSerializer : IJsonSerializer
{
public static IJsonSerializer Instance { get; } = new SystemTextJsonSerializer();

public string GeneratePropertyAttribute(string id, bool isRequired)
{
return $"[global::System.Text.Json.Serialization.JsonPropertyName(\"{id}\")]";
Expand All @@ -28,29 +28,51 @@ public string GenerateConverterAttribute(string type)

return $"[global::System.Text.Json.Serialization.JsonConverter(typeof(global::OpenApiGenerator.JsonConverters.{type}))]";
}

private static readonly char[] ContextTypeSeparators = [',', '<', '>'];

private static string GetContextType(string type)
{
type = type.Replace("global::", string.Empty).TrimEnd('?');

return type switch
{
"System.Collections.Generic.IList<string>" => "IListString",
"string" => "String",
"object" => "Object",
_ => type,
};
return string.Concat(type
.Replace("global::", string.Empty)
.TrimEnd('?')
.Replace("System.Collections.Generic.", string.Empty)
.Replace("System.", string.Empty)
.Split(ContextTypeSeparators, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim() switch
{
"bool" => "Boolean",
"short" => "Int16",
"int" => "Int32",
"long" => "Int64",
"sbyte" => "SByte",
"ushort" => "UInt16",
"uint" => "UInt32",
"ulong" => "UInt64",
"float" => "Single",
"double" => "Double",
"decimal" => "Decimal",
"string" => "String",
"char" => "Char",
"byte" => "Byte",
"object" => "Object",
_ => x.Trim(),
}));
}

public string GenerateSerializeCall(string type, string jsonSerializerContext)
{
type = type ?? throw new ArgumentNullException(nameof(type));

return string.IsNullOrWhiteSpace(jsonSerializerContext)
? "global::System.Text.Json.JsonSerializer.Serialize(request)"
: $"global::System.Text.Json.JsonSerializer.Serialize(request, global::{jsonSerializerContext}.Default.{GetContextType(type)})";
}

public string GenerateDeserializeCall(string type, string jsonSerializerContext)
{
type = type ?? throw new ArgumentNullException(nameof(type));

return string.IsNullOrWhiteSpace(jsonSerializerContext)
? $"global::System.Text.Json.JsonSerializer.Deserialize<{type}>(content)"
: $"global::System.Text.Json.JsonSerializer.Deserialize(content, global::{jsonSerializerContext}.Default.{GetContextType(type)})";
Expand Down

0 comments on commit a9d7d66

Please sign in to comment.