Skip to content

Commit 80c58ea

Browse files
Improved the developer experience by adding a tolerant enum converter (#398)
1 parent 1ff27fe commit 80c58ea

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

app/MindWork AI Studio/Settings/SettingsManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public sealed class SettingsManager(ILogger<SettingsManager> logger)
2020
private static readonly JsonSerializerOptions JSON_OPTIONS = new()
2121
{
2222
WriteIndented = true,
23-
Converters = { new JsonStringEnumConverter() },
23+
Converters = { new TolerantEnumConverter() },
2424
};
2525

2626
private readonly ILogger<SettingsManager> logger = logger;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace AIStudio.Settings;
5+
6+
/// <summary>
7+
/// Tries to convert a JSON string to an enum value.
8+
/// </summary>
9+
/// <remarks>
10+
/// When the target enum value does not exist, the value will be the default value.
11+
/// This converter handles enum values as property names and values.
12+
/// </remarks>
13+
public sealed class TolerantEnumConverter : JsonConverter<object>
14+
{
15+
private static readonly ILogger<TolerantEnumConverter> LOG = Program.LOGGER_FACTORY.CreateLogger<TolerantEnumConverter>();
16+
17+
public override bool CanConvert(Type typeToConvert) => typeToConvert.IsEnum;
18+
19+
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
20+
{
21+
// Is this token a string?
22+
if (reader.TokenType == JsonTokenType.String)
23+
// Try to use that string as the name of the enum value:
24+
if (Enum.TryParse(typeToConvert, reader.GetString(), out var result))
25+
return result;
26+
27+
// In any other case, we will return the default enum value:
28+
LOG.LogWarning($"Cannot read '{reader.GetString()}' as '{typeToConvert.Name}' enum; token type: {reader.TokenType}");
29+
return Activator.CreateInstance(typeToConvert);
30+
}
31+
32+
public override object ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
33+
{
34+
// Is this token a property name?
35+
if (reader.TokenType == JsonTokenType.PropertyName)
36+
// Try to use that property name as the name of the enum value:
37+
if (Enum.TryParse(typeToConvert, reader.GetString(), out var result))
38+
return result;
39+
40+
// In any other case, we will return the default enum value:
41+
LOG.LogWarning($"Cannot read '{reader.GetString()}' as '{typeToConvert.Name}' enum; token type: {reader.TokenType}");
42+
return Activator.CreateInstance(typeToConvert)!;
43+
}
44+
45+
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
46+
{
47+
writer.WriteStringValue(value.ToString());
48+
}
49+
50+
public override void WriteAsPropertyName(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
51+
{
52+
writer.WritePropertyName(value.ToString()!);
53+
}
54+
}

app/MindWork AI Studio/wwwroot/changelog/v0.9.40.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
- Added support for the announced OpenAI `o4` models. We hope that these `o4` models will be usable by the well-known chat completion API instead of the new responses API, though. AI Studio cannot use the new responses API right now.
33
- Added Alibaba Cloud as a new provider.
44
- Improved the provider selection by showing the name of the provider in the provider selection instead of its identifier.
5+
- Improved the developer experience by adding a tolerant enum converter for better configuration handling.
56
- Fixed an issue where OpenAI `o3` models were not shown in the model selection.

0 commit comments

Comments
 (0)