Skip to content

Commit 74f350a

Browse files
committed
update: remove datetime converters built into .net 7
1 parent a10e2c4 commit 74f350a

File tree

3 files changed

+7
-60
lines changed

3 files changed

+7
-60
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111

1212
* None yet!
1313

14+
## [0.19.1] - 02/04/2023
15+
16+
### Updates
17+
18+
* Removed custom dateonly and timeonly json converters in facor of built in .net 7 options
19+
1420
## [0.19.0] - 02/04/2023
1521

1622
### Updates

Craftsman/Builders/ExtensionBuilders/ServiceConfigurationBuilder.cs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public static void ConfigureServices(this WebApplicationBuilder builder)
6868
builder.Services.AddBoundaryServices(Assembly.GetExecutingAssembly());
6969
7070
builder.Services
71-
.AddMvc(options => options.Filters.Add<ErrorHandlerFilterAttribute>())
72-
.AddJsonOptions(opt => opt.JsonSerializerOptions.AddDateOnlyConverters());
71+
.AddMvc(options => options.Filters.Add<ErrorHandlerFilterAttribute>());
7372
7473
if(builder.Environment.EnvironmentName != Consts.Testing.FunctionalTestingEnvName)
7574
{{
@@ -105,62 +104,6 @@ private static void AddBoundaryServices(this IServiceCollection services, params
105104
}}
106105
}}
107106
}}
108-
}}
109-
110-
// TODO these will be baked into System.Text.Json in .NET 7
111-
public static class DateOnlyConverterExtensions
112-
{{
113-
public static void AddDateOnlyConverters(this JsonSerializerOptions options)
114-
{{
115-
options.Converters.Add(new DateOnlyConverter());
116-
options.Converters.Add(new DateOnlyNullableConverter());
117-
}}
118-
}}
119-
120-
public class DateOnlyConverter : JsonConverter<DateOnly>
121-
{{
122-
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
123-
{{
124-
if (reader.TryGetDateTime(out var dt))
125-
{{
126-
return DateOnly.FromDateTime(dt);
127-
}};
128-
var value = reader.GetString();
129-
if (value == null)
130-
{{
131-
return default;
132-
}}
133-
var match = new Regex(""^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)(T|\\s|\\z)"").Match(value);
134-
return match.Success
135-
? new DateOnly(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value))
136-
: default;
137-
}}
138-
139-
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
140-
=> writer.WriteStringValue(value.ToString(""yyyy-MM-dd""));
141-
}}
142-
143-
public class DateOnlyNullableConverter : JsonConverter<DateOnly?>
144-
{{
145-
public override DateOnly? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
146-
{{
147-
if (reader.TryGetDateTime(out var dt))
148-
{{
149-
return DateOnly.FromDateTime(dt);
150-
}};
151-
var value = reader.GetString();
152-
if (value == null)
153-
{{
154-
return default;
155-
}}
156-
var match = new Regex(""^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)(T|\\s|\\z)"").Match(value);
157-
return match.Success
158-
? new DateOnly(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value))
159-
: default;
160-
}}
161-
162-
public override void Write(Utf8JsonWriter writer, DateOnly? value, JsonSerializerOptions options)
163-
=> writer.WriteStringValue(value?.ToString(""yyyy-MM-dd""));
164107
}}";
165108
}
166109
}

Craftsman/Builders/Tests/Utilities/HttpClientExtensionsBuilder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,12 @@ public static async Task<HttpResponseMessage> DeleteRequestAsync(this HttpClient
5858
public static async Task<HttpResponseMessage> PostJsonRequestAsync(this HttpClient client, string url, object value)
5959
{{
6060
var options = new JsonSerializerOptions();
61-
options.AddDateOnlyConverters();
6261
return await client.PostAsJsonAsync(url, value, options).ConfigureAwait(false);
6362
}}
6463
6564
public static async Task<HttpResponseMessage> PutJsonRequestAsync(this HttpClient client, string url, object value)
6665
{{
6766
var options = new JsonSerializerOptions();
68-
options.AddDateOnlyConverters();
6967
return await client.PutAsJsonAsync(url, value, options).ConfigureAwait(false);
7068
}}
7169
}}";

0 commit comments

Comments
 (0)