Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfc426a8

Browse files
committed
JsonContext improvements and usage within Settings
1 parent065eda1 commitfc426a8

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

‎App/Services/SettingsManager.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public SettingsManager(string? settingsFilePath = null)
7676
thrownewArgumentException("settingsFilePath must be an absolute path if provided",nameof(settingsFilePath));
7777
}
7878

79-
stringfolder=Path.Combine(
79+
varfolder=Path.Combine(
8080
settingsFilePath,
8181
_appName);
8282

@@ -86,9 +86,8 @@ public SettingsManager(string? settingsFilePath = null)
8686
if(!File.Exists(_settingsFilePath))
8787
{
8888
// Create the settings file if it doesn't exist
89-
stringemptyJson=JsonSerializer.Serialize(new{});
90-
File.WriteAllText(_settingsFilePath,emptyJson);
9189
_settings=new();
90+
File.WriteAllText(_settingsFilePath,JsonSerializer.Serialize(_settings,SettingsJsonContext.Default.Settings));
9291
}
9392
else
9493
{
@@ -109,12 +108,12 @@ private void Save(string name, bool value)
109108
FileShare.None);
110109

111110
// Ensure cache is loaded before saving
112-
varfreshCache=JsonSerializer.Deserialize<Settings>(fs)??new();
111+
varfreshCache=JsonSerializer.Deserialize(fs,SettingsJsonContext.Default.Settings)??new();
113112
_settings=freshCache;
114113
_settings.Options[name]=JsonSerializer.SerializeToElement(value);
115114
fs.Position=0;// Reset stream position to the beginning before writing
116115

117-
JsonSerializer.Serialize(fs,_settings,newJsonSerializerOptions{WriteIndented=true});
116+
JsonSerializer.Serialize(fs,_settings,SettingsJsonContext.Default.Settings);
118117

119118
// This ensures the file is truncated to the new length
120119
// if the new content is shorter than the old content
@@ -152,33 +151,39 @@ private Settings Load()
152151
try
153152
{
154153
usingvarfs=File.OpenRead(_settingsFilePath);
155-
returnJsonSerializer.Deserialize<Settings>(fs)??new(null,newDictionary<string,JsonElement>());
154+
returnJsonSerializer.Deserialize(fs,SettingsJsonContext.Default.Settings)??new();
156155
}
157156
catch(Exceptionex)
158157
{
159158
thrownewInvalidOperationException($"Failed to load settings from{_settingsFilePath}. The file may be corrupted or malformed. Exception:{ex.Message}");
160159
}
161160
}
161+
}
162+
163+
publicclassSettings
164+
{
165+
/// <summary>
166+
/// User settings version. Increment this when the settings schema changes.
167+
/// In future iterations we will be able to handle migrations when the user has
168+
/// an older version.
169+
/// </summary>
170+
publicintVersion{get;set;}
171+
publicDictionary<string,JsonElement>Options{get;set;}
162172

163-
[JsonSerializable(typeof(Settings))]
164-
privateclassSettings
173+
privateconstintVERSION=1;// Default version for backward compatibility
174+
publicSettings()
165175
{
166-
/// <summary>
167-
/// User settings version. Increment this when the settings schema changes.
168-
/// In future iterations we will be able to handle migrations when the user has
169-
/// an older version.
170-
/// </summary>
171-
publicintVersion{get;set;}=1;
172-
publicDictionary<string,JsonElement>Options{get;set;}
173-
publicSettings()
174-
{
175-
Options=newDictionary<string,JsonElement>();
176-
}
176+
Version=VERSION;
177+
Options=[];
178+
}
177179

178-
publicSettings(int?version,Dictionary<string,JsonElement>options)
179-
{
180-
Version=version??Version;
181-
Options=options;
182-
}
180+
publicSettings(int?version,Dictionary<string,JsonElement>options)
181+
{
182+
Version=version??VERSION;
183+
Options=options;
183184
}
184185
}
186+
187+
[JsonSerializable(typeof(Settings))]
188+
[JsonSourceGenerationOptions(WriteIndented=true)]
189+
publicpartialclassSettingsJsonContext:JsonSerializerContext;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp