Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json Serialization doubled when using Setting Storage Template / FileService Bug? #4703

Open
1 of 3 tasks
Oali3n opened this issue Jul 27, 2023 · 1 comment
Open
1 of 3 tasks
Labels
bug The issue relates to the wizard not working or a problem with the generated code.

Comments

@Oali3n
Copy link

Oali3n commented Jul 27, 2023

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).

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

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Additional context

No response

Applies to the following platforms:

  • WinUI
  • WPF
  • UWP

About your setup

  • Visual Studio Version: Microsoft Visual Studio Community 2022 (64-Bit) - Current Version 17.6.5
  • Template Studio Wizard Version: 5.4
  • Windows Build:
@Oali3n Oali3n added the bug The issue relates to the wizard not working or a problem with the generated code. label Jul 27, 2023
@phills3
Copy link

phills3 commented Oct 21, 2024

I found that changing

return await Json.ToObjectAsync((string)obj);

to

return (typeof(T) == typeof(string)) ? (T)obj : await Json.ToObjectAsync((string)obj);

plus a similar fix for saving settings, fixed the problem for me. It was really a problem for strings only as all other data types and objects worked as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue relates to the wizard not working or a problem with the generated code.
Projects
None yet
Development

No branches or pull requests

2 participants