Skip to content

Commit a47daca

Browse files
committed
🥁 Reinstate STJ source generation
This reverts commit 37785dd.
1 parent 0e11068 commit a47daca

File tree

3 files changed

+13
-62
lines changed

3 files changed

+13
-62
lines changed

YoutubeDl.Wpf/Models/Settings.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.ComponentModel;
5-
using System.Text.Encodings.Web;
6-
using System.Text.Json;
75
using System.Threading;
86
using System.Threading.Tasks;
97
using YoutubeDl.Wpf.Utils;
@@ -145,23 +143,14 @@ public class Settings
145143
/// </remarks>
146144
public List<string> CookiesBrowserArgHistory { get; set; } = [];
147145

148-
private static readonly JsonSerializerOptions s_jsonSerializerOptions = new()
149-
{
150-
AllowTrailingCommas = true,
151-
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
152-
IgnoreReadOnlyProperties = true,
153-
ReadCommentHandling = JsonCommentHandling.Skip,
154-
WriteIndented = true,
155-
};
156-
157146
/// <summary>
158147
/// Loads settings from Settings.json.
159148
/// </summary>
160149
/// <param name="cancellationToken">A token that may be used to cancel the read operation.</param>
161150
/// <returns>The <see cref="Settings"/> object.</returns>
162151
public static async Task<Settings> LoadAsync(CancellationToken cancellationToken = default)
163152
{
164-
Settings settings = await FileHelper.LoadFromJsonFileAsync<Settings>("Settings.json", s_jsonSerializerOptions, cancellationToken).ConfigureAwait(false);
153+
Settings settings = await FileHelper.LoadFromJsonFileAsync("Settings.json", SettingsJsonSerializerContext.Default.Settings, cancellationToken).ConfigureAwait(false);
165154
settings.Update();
166155
settings.Validate();
167156
return settings;
@@ -173,7 +162,7 @@ public static async Task<Settings> LoadAsync(CancellationToken cancellationToken
173162
/// <param name="cancellationToken">A token that may be used to cancel the write operation.</param>
174163
/// <returns>A task that represents the asynchronous write operation.</returns>
175164
public Task SaveAsync(CancellationToken cancellationToken = default)
176-
=> FileHelper.SaveToJsonFileAsync("Settings.json", this, s_jsonSerializerOptions, cancellationToken);
165+
=> FileHelper.SaveToJsonFileAsync("Settings.json", this, SettingsJsonSerializerContext.Default.Settings, cancellationToken);
177166

178167
/// <summary>
179168
/// Updates settings to the latest version.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace YoutubeDl.Wpf.Models;
4+
5+
[JsonSerializable(typeof(Settings))]
6+
[JsonSourceGenerationOptions(
7+
IgnoreReadOnlyProperties = true,
8+
WriteIndented = true)]
9+
public partial class SettingsJsonSerializerContext : JsonSerializerContext
10+
{
11+
}

YoutubeDl.Wpf/Utils/FileHelper.cs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ public static class FileHelper
2828
}
2929
}
3030

31-
/// <summary>
32-
/// Loads the specified JSON file and deserializes its content as a <typeparamref name="TValue"/>.
33-
/// </summary>
34-
/// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
35-
/// <param name="path">JSON file path.</param>
36-
/// <param name="jsonSerializerOptions">Deserialization options.</param>
37-
/// <param name="cancellationToken">A token that may be used to cancel the read operation.</param>
38-
/// <returns>A <typeparamref name="TValue"/>.</returns>
39-
public static async Task<TValue> LoadFromJsonFileAsync<TValue>(string path, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default) where TValue : class, new()
40-
{
41-
if (!File.Exists(path))
42-
return new();
43-
44-
var fileStream = new FileStream(path, FileMode.Open);
45-
await using (fileStream.ConfigureAwait(false))
46-
{
47-
return await JsonSerializer.DeserializeAsync<TValue>(fileStream, jsonSerializerOptions, cancellationToken).ConfigureAwait(false) ?? new();
48-
}
49-
}
50-
5131
/// <summary>
5232
/// Serializes the provided value as JSON and saves to the specified file.
5333
/// </summary>
@@ -76,33 +56,4 @@ public static async Task SaveToJsonFileAsync<TValue>(
7656
if (canReplace)
7757
File.Replace(newPath, path, $"{path}.old");
7858
}
79-
80-
/// <summary>
81-
/// Serializes the provided value as JSON and saves to the specified file.
82-
/// </summary>
83-
/// <typeparam name="TValue">The type of the value to serialize.</typeparam>
84-
/// <param name="path">JSON file path.</param>
85-
/// <param name="value">The value to save.</param>
86-
/// <param name="jsonSerializerOptions">Serialization options.</param>
87-
/// <param name="cancellationToken">A token that may be used to cancel the write operation.</param>
88-
/// <returns>A task that represents the asynchronous write operation.</returns>
89-
public static async Task SaveToJsonFileAsync<TValue>(
90-
string path,
91-
TValue value,
92-
JsonSerializerOptions? jsonSerializerOptions = null,
93-
CancellationToken cancellationToken = default)
94-
{
95-
// File.Replace throws an exception when the destination file does not exist.
96-
var canReplace = File.Exists(path);
97-
var newPath = canReplace ? $"{path}.new" : path;
98-
var fileStream = new FileStream(newPath, FileMode.Create);
99-
100-
await using (fileStream.ConfigureAwait(false))
101-
{
102-
await JsonSerializer.SerializeAsync(fileStream, value, jsonSerializerOptions, cancellationToken);
103-
}
104-
105-
if (canReplace)
106-
File.Replace(newPath, path, $"{path}.old");
107-
}
10859
}

0 commit comments

Comments
 (0)