When using the Setting Storage Template the Setting-Object is serialized twice.
At LocalSettingsService.cs the method Json.StringifyAsync(value) is called which serializes the object and after that in the FileService-Method .Save the setting will be serialized again. I think the Json-serialization in FileService is wrong, because it makes the FileService useless for other text file IO cases (see line var fileContent = JsonConvert.SerializeObject(content, Formatting.Indented);).
Of course same with deserialization.
And maybe it would be better to use JsonConvert.SerializeObject(content, Formatting.Indented); instead of JsonConvert.SerializeObject(content).
In LocalSettingsService.cs:
public async Task SaveSettingAsync<T>(string key, T value)
{
if (RuntimeHelper.IsMSIX)
{
ApplicationData.Current.LocalSettings.Values[key] = await Json.StringifyAsync(value);
}
else
{
await InitializeAsync();
_settings[key] = await Json.StringifyAsync(value);
await Task.Run(() => _fileService.Save(_applicationDataFolder, _localsettingsFile, _settings));
}
}
In Json.cs in the Core Project:
public static class Json
{
public static async Task<T> ToObjectAsync<T>(string value)
{
return await Task.Run<T>(() =>
{
return JsonConvert.DeserializeObject<T>(value);
});
}
public static async Task<string> StringifyAsync(object value)
{
return await Task.Run<string>(() =>
{
return JsonConvert.SerializeObject(value);
});
}
}
In FileService.cs in the Core Project:
public void Save<T>(string folderPath, string fileName, T content)
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
var fileContent = JsonConvert.SerializeObject(content, Formatting.Indented);
File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, Encoding.UTF8);
}
Greetings Martin
To Reproduce
Go to '...'
Click on '....'
Scroll down to '....'
See error
Additional context
No response
Applies to the following platforms:
[X] WinUI
[ ] WPF
[ ] UWP
About your setup
Visual Studio Version: Microsoft Visual Studio Community 2022 (64-Bit) - Current Version 17.6.5
Describe the bug
When using the Setting Storage Template the Setting-Object is serialized twice. At LocalSettingsService.cs the method Json.StringifyAsync(value) is called which serializes the object and after that in the FileService-Method .Save the setting will be serialized again. I think the Json-serialization in FileService is wrong, because it makes the FileService useless for other text file IO cases (see line var fileContent = JsonConvert.SerializeObject(content, Formatting.Indented);).
Of course same with deserialization.
And maybe it would be better to use JsonConvert.SerializeObject(content, Formatting.Indented); instead of JsonConvert.SerializeObject(content).
Greetings Martin
To Reproduce
Additional context
No response
Applies to the following platforms:
About your setup