Last active
March 17, 2023 07:19
-
-
Save Zodt/b1bf14121b7ec84a28bd641ceb926c7d to your computer and use it in GitHub Desktop.
TimeSpan converter for Swagger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.Json; | |
using System.Text.Json.Serialization; | |
namespace Core.Json.Converters | |
{ | |
public class JsonTimeSpanConverter : JsonConverter<TimeSpan> | |
{ | |
public bool WithoutSeconds { get; init; } | |
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
{ | |
var input = reader.GetString(); | |
if (string.IsNullOrWhiteSpace(input)) | |
{ | |
return default(TimeSpan); | |
} | |
return TimeSpan.Parse(input); | |
} | |
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options) | |
{ | |
writer.WriteStringValue( | |
WithoutSeconds | |
? $"{value.Hours:D2}:{value.Minutes:D2}" | |
: value.ToString("c")); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.Json.Serialization; | |
using Core.Json.Converters; | |
namespace Core.Json.Attributes | |
{ | |
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | |
public class JsonTimeSpanConverterAttribute : JsonConverterAttribute | |
{ | |
public bool WithoutSeconds { get; init; } | |
public override JsonConverter CreateConverter(Type typeToConvert) | |
{ | |
return new JsonTimeSpanConverter | |
{ | |
WithoutSeconds = WithoutSeconds | |
}; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void AddSwagger(this IServiceCollection services) | |
{ | |
services.AddSwaggerGen(c => | |
{ | |
c.MapType<TimeSpan>(() => new OpenApiSchema | |
{ | |
Type = "string", | |
Example = new OpenApiString("00:00:00") | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Core.Json.Attributes; | |
namespace TimeSpanSwaggerConverter.Models | |
{ | |
public record DummyModel | |
{ | |
public int Id { get; init; } | |
public string Name { get; init; } = null!; | |
[JsonTimeSpanConverter(WithoutSeconds = true)] | |
public TimeSpan Time { get; init; } | |
public bool IsActive { get; init; } | |
public DateTime CreationDate { get; init; } | |
public DateTime LastUpdateDate { get; init; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment