Skip to content

Commit

Permalink
convert ApplicationSettings to System.Text.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Dec 21, 2024
1 parent bf8ae24 commit ccad247
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ private static IEnumerable<Application> EnumerateUserApps()
return Array.Empty<Application>();
}

var additionals = new List<string>(Directory.EnumerateDirectories(additionalProfilesPath));
var userApps = from dir in additionals
var userApps = from dir in Directory.EnumerateDirectories(additionalProfilesPath)
where File.Exists(Path.Combine(dir, "settings.json"))
select Path.GetFileName(dir);

Expand Down
13 changes: 10 additions & 3 deletions Project-Aurora/Project-Aurora/Settings/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
using System.ComponentModel;
using System.Text.Json.Serialization;
using AuroraRgb.Profiles.Generic_Application;

namespace AuroraRgb.Settings;

[JsonSerializable(typeof(ApplicationSettings))]
[JsonSerializable(typeof(FirstTimeApplicationSettings))]
[JsonSerializable(typeof(NewJsonApplicationSettings))]
[JsonSerializable(typeof(GenericApplicationSettings))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class SettingsJsonContext : JsonSerializerContext;

public class ApplicationSettings : INotifyPropertyChanged
{
public bool IsEnabled { get; set; } = true;
Expand Down Expand Up @@ -41,7 +49,6 @@ public override void CompleteInstallation()
}
}


public class NewJsonApplicationSettings : ApplicationSettings
{
public bool IsNewJsonInstalled { get; private set; }
Expand All @@ -53,9 +60,9 @@ public NewJsonApplicationSettings()
}

[method: JsonConstructor]
public NewJsonApplicationSettings(bool isFirstTimeInstalled)
public NewJsonApplicationSettings(bool isNewJsonInstalled)
{
IsNewJsonInstalled = isFirstTimeInstalled;
IsNewJsonInstalled = isNewJsonInstalled;
}

public override void CompleteInstallation()
Expand Down
18 changes: 9 additions & 9 deletions Project-Aurora/Project-Aurora/Settings/ObjectSettings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace AuroraRgb.Settings;

[JsonObject(ItemTypeNameHandling = TypeNameHandling.None)]
public class ObjectSettings<T>
{
protected string? SettingsSavePath { get; set; }
Expand Down Expand Up @@ -37,10 +36,8 @@ protected async Task SaveSettings(Type settingsType)
{
try
{
await File.WriteAllTextAsync(SettingsSavePath, JsonConvert.SerializeObject(Settings, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto, Formatting = Formatting.Indented
}));
var json = JsonSerializer.Serialize(Settings, Settings.GetType(), SettingsJsonContext.Default);
await File.WriteAllTextAsync(SettingsSavePath, json);
return;
}
catch (IOException ioException)
Expand Down Expand Up @@ -71,9 +68,12 @@ protected virtual async Task LoadSettings(Type settingsType)
{
try
{
var json = await File.ReadAllTextAsync(SettingsSavePath);
var jsonSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None };
Settings = (T)JsonConvert.DeserializeObject(json, settingsType, jsonSerializerSettings);
string json;
using (var streamReader = new StreamReader(SettingsSavePath))
{
json = await streamReader.ReadToEndAsync();
}
Settings = (T)JsonSerializer.Deserialize(json, settingsType, SettingsJsonContext.Default);
}
catch (Exception exc)
{
Expand Down

0 comments on commit ccad247

Please sign in to comment.