-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched from Newtonsoft.Json to System.Text.Json (some very hacky ch…
…anges were required). Updated dependencies.
- Loading branch information
Showing
10 changed files
with
142 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using ReactiveUI; | ||
|
||
using SongProcessor.UI.ViewModels; | ||
|
||
using System.Reactive; | ||
using System.Reactive.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization.Metadata; | ||
|
||
namespace SongProcessor.UI; | ||
|
||
public class JsonSuspensionDriver(string Path) : ISuspensionDriver | ||
{ | ||
private static readonly JsonSerializerOptions _Options = new() | ||
{ | ||
IgnoreReadOnlyFields = true, | ||
IgnoreReadOnlyProperties = true, | ||
WriteIndented = true, | ||
TypeInfoResolver = new DefaultJsonTypeInfoResolver | ||
{ | ||
Modifiers = | ||
{ | ||
UseDataContract, | ||
UseTypeNamesForViewModels, | ||
IgnoreCertainViewModels, | ||
} | ||
} | ||
}; | ||
public bool DeleteOnInvalidState { get; set; } | ||
|
||
public IObservable<Unit> InvalidateState() | ||
{ | ||
if (DeleteOnInvalidState && File.Exists(Path)) | ||
{ | ||
File.Delete(Path); | ||
} | ||
return Observable.Return(Unit.Default); | ||
} | ||
|
||
public IObservable<object> LoadState() | ||
{ | ||
// ReactiveUI relies on this method throwing an exception | ||
// to determine if CreateNewAppState should be called | ||
using var fs = File.OpenRead(Path); | ||
var state = JsonSerializer.Deserialize<MainViewModel>(fs, _Options); | ||
return Observable.Return(state)!; | ||
} | ||
|
||
public IObservable<Unit> SaveState(object state) | ||
{ | ||
using var fs = File.Create(Path); | ||
JsonSerializer.Serialize(fs, state, _Options); | ||
return Observable.Return(Unit.Default); | ||
} | ||
|
||
private static void IgnoreCertainViewModels(JsonTypeInfo typeInfo) | ||
{ | ||
if (typeInfo.Type != typeof(RoutingStateWorkaround)) | ||
{ | ||
return; | ||
} | ||
|
||
// This will be an issue if settings are serialized at any time other than | ||
// application shutdown | ||
typeInfo.OnSerializing = static obj => | ||
{ | ||
var navStack = ((RoutingState)obj).NavigationStack; | ||
for (var i = navStack.Count - 1; i >= 0; --i) | ||
{ | ||
if (navStack[i].GetType() == typeof(EditViewModel)) | ||
{ | ||
navStack.RemoveAt(i); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private static void UseDataContract(JsonTypeInfo typeInfo) | ||
{ | ||
if (typeInfo.Type.GetCustomAttribute<DataContractAttribute>() is null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var propertyInfo in typeInfo.Properties) | ||
{ | ||
if (propertyInfo.AttributeProvider is not ICustomAttributeProvider provider | ||
|| !provider.GetCustomAttributes(true).Any(x => x is DataMemberAttribute)) | ||
{ | ||
propertyInfo.ShouldSerialize = static (_, _) | ||
=> false; | ||
} | ||
} | ||
} | ||
|
||
private static void UseTypeNamesForViewModels(JsonTypeInfo typeInfo) | ||
{ | ||
if (typeInfo.Type != typeof(IRoutableViewModel)) | ||
{ | ||
return; | ||
} | ||
|
||
typeInfo.PolymorphismOptions = new() | ||
{ | ||
DerivedTypes = | ||
{ | ||
new(typeof(SongViewModel), "vm_song"), | ||
new(typeof(AddViewModel), "vm_add"), | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
public class RoutingStateWorkaround : RoutingState; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters