Skip to content

Commit dc0cf6b

Browse files
Added Application Settings
1 parent 9bc201a commit dc0cf6b

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed

src/BlazorPages/AppSettings.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BlazorPages;
4+
5+
public enum ExampleEnum
6+
{
7+
None = 0,
8+
FirstValue = 1,
9+
}
10+
11+
public class AppSettings
12+
{
13+
[JsonPropertyName("stringValue")]
14+
public string? StringValue { get; set; }
15+
16+
[JsonPropertyName("intValue")]
17+
public int IntValue { get; set; }
18+
19+
[JsonPropertyName("enumValue")]
20+
public ExampleEnum EnumValue { get; set; }
21+
22+
/// <summary>
23+
/// This property simply demonstrates that non-existent enum values will not cause an exception during deserialization.
24+
/// See <see cref="Util.UnknownEnumConverter"/>
25+
/// </summary>
26+
[JsonPropertyName("nonexistentEnumValue")]
27+
public ExampleEnum NonexistentEnumValue { get; set; }
28+
}

src/BlazorPages/Program.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
using System.Net.Http.Json;
12
using Microsoft.AspNetCore.Components.Web;
23
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
34
using BlazorPages;
5+
using BlazorPages.Util;
46

57
var builder = WebAssemblyHostBuilder.CreateDefault(args);
68
builder.RootComponents.Add<App>("#app");
79
builder.RootComponents.Add<HeadOutlet>("head::after");
810

9-
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
11+
var http = new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) };
12+
builder.Services.AddScoped(sp => http);
1013

11-
await builder.Build().RunAsync();
14+
var appSettings = await http.GetFromJsonAsync<AppSettings>("appsettings.json", new System.Text.Json.JsonSerializerOptions() { Converters = { new UnknownEnumConverter() } });
15+
builder.Services.AddSingleton(appSettings ?? new());
16+
17+
await builder.Build().RunAsync
18+
();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace BlazorPages.Util.Generic;
5+
6+
/// <summary>
7+
/// Copied from https://gaevoy.com/2023/09/26/dotnet-serialization-unknown-enums-handling-api.html
8+
/// </summary>
9+
/// <typeparam name="T">An <see cref="Enum"/> type</typeparam>
10+
public class UnknownEnumConverter<T> : JsonConverter<T> where T : struct, Enum
11+
{
12+
private readonly JsonConverter<T> _underlying;
13+
14+
public UnknownEnumConverter(JsonConverter<T> underlying)
15+
=> _underlying = underlying;
16+
17+
public override T Read(ref Utf8JsonReader reader, Type enumType, JsonSerializerOptions options)
18+
{
19+
try
20+
{
21+
return _underlying.Read(ref reader, enumType, options);
22+
}
23+
catch (JsonException) when (enumType.IsEnum)
24+
{
25+
return default;
26+
}
27+
}
28+
29+
public override bool CanConvert(Type typeToConvert)
30+
=> _underlying.CanConvert(typeToConvert);
31+
32+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
33+
=> _underlying.Write(writer, value, options);
34+
35+
public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
36+
=> _underlying.ReadAsPropertyName(ref reader, typeToConvert, options);
37+
38+
public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
39+
=> _underlying.WriteAsPropertyName(writer, value, options);
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
using BlazorPages.Util.Generic;
4+
5+
namespace BlazorPages.Util;
6+
7+
/// <summary>
8+
/// Copied from https://gaevoy.com/2023/09/26/dotnet-serialization-unknown-enums-handling-api.html
9+
/// </summary>
10+
public class UnknownEnumConverter : JsonConverterFactory
11+
{
12+
private readonly JsonStringEnumConverter _underlying = new();
13+
14+
public sealed override bool CanConvert(Type enumType)
15+
=> _underlying.CanConvert(enumType);
16+
17+
public sealed override JsonConverter CreateConverter(Type enumType, JsonSerializerOptions options)
18+
{
19+
var underlyingConverter = _underlying.CreateConverter(enumType, options);
20+
var converterType = typeof(UnknownEnumConverter<>).MakeGenericType(enumType);
21+
return (JsonConverter)Activator.CreateInstance(converterType, underlyingConverter);
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"stringValue": "Example String Value",
3+
"intValue": 4,
4+
"enumValue": "FirstValue",
5+
"nonexistentEnumValue": "TheNonexistentEnumValue"
6+
}

0 commit comments

Comments
 (0)