1- using System ;
1+ #nullable enable
2+ using System ;
23using System . Globalization ;
34using System . IO ;
45using System . Text . Json ;
@@ -11,62 +12,73 @@ namespace Flow.Launcher.Infrastructure.Storage
1112 /// </summary>
1213 public class JsonStorage < T > where T : new ( )
1314 {
14- protected T _data ;
15+ protected T ? Data ;
1516 // need a new directory name
1617 public const string DirectoryName = "Settings" ;
1718 public const string FileSuffix = ".json" ;
18- public string FilePath { get ; set ; }
19- public string DirectoryPath { get ; set ; }
19+ protected string FilePath { get ; init ; } = null ! ;
20+ private string TempFilePath => $ "{ FilePath } .tmp";
21+ private string BackupFilePath => $ "{ FilePath } .bak";
22+ protected string DirectoryPath { get ; init ; } = null ! ;
2023
2124
2225 public T Load ( )
2326 {
27+ string ? serialized = null ;
28+
2429 if ( File . Exists ( FilePath ) )
2530 {
26- var serialized = File . ReadAllText ( FilePath ) ;
27- if ( ! string . IsNullOrWhiteSpace ( serialized ) )
31+ serialized = File . ReadAllText ( FilePath ) ;
32+ }
33+
34+ if ( ! string . IsNullOrEmpty ( serialized ) )
35+ {
36+ try
2837 {
29- Deserialize ( serialized ) ;
38+ Data = JsonSerializer . Deserialize < T > ( serialized ) ?? TryLoadBackup ( ) ?? LoadDefault ( ) ;
3039 }
31- else
40+ catch ( JsonException )
3241 {
33- LoadDefault ( ) ;
42+ Data = TryLoadBackup ( ) ?? LoadDefault ( ) ;
3443 }
3544 }
3645 else
3746 {
38- LoadDefault ( ) ;
47+ Data = TryLoadBackup ( ) ?? LoadDefault ( ) ;
3948 }
40- return _data . NonNull ( ) ;
49+ return Data . NonNull ( ) ;
4150 }
4251
43- private void Deserialize ( string serialized )
52+ private T LoadDefault ( )
4453 {
45- try
46- {
47- _data = JsonSerializer . Deserialize < T > ( serialized ) ;
48- }
49- catch ( JsonException e )
54+ if ( File . Exists ( FilePath ) )
5055 {
51- LoadDefault ( ) ;
52- Log . Exception ( $ "|JsonStorage.Deserialize|Deserialize error for json <{ FilePath } >", e ) ;
56+ BackupOriginFile ( ) ;
5357 }
5458
55- if ( _data == null )
56- {
57- LoadDefault ( ) ;
58- }
59+ return new T ( ) ;
5960 }
6061
61- private void LoadDefault ( )
62+ private T ? TryLoadBackup ( )
6263 {
63- if ( File . Exists ( FilePath ) )
64+ if ( ! File . Exists ( BackupFilePath ) )
65+ return default ;
66+
67+ try
6468 {
65- BackupOriginFile ( ) ;
69+ var data = JsonSerializer . Deserialize < T > ( File . ReadAllText ( BackupFilePath ) ) ;
70+ if ( data != null )
71+ {
72+ Log . Info ( $ "|JsonStorage.Load|Failed to load settings.json, { BackupFilePath } restored successfully") ;
73+ File . Replace ( BackupFilePath , FilePath , null ) ;
74+ return data ;
75+ }
76+ return default ;
77+ }
78+ catch ( JsonException )
79+ {
80+ return default ;
6681 }
67-
68- _data = new T ( ) ;
69- Save ( ) ;
7082 }
7183
7284 private void BackupOriginFile ( )
@@ -82,13 +94,14 @@ private void BackupOriginFile()
8294
8395 public void Save ( )
8496 {
85- string serialized = JsonSerializer . Serialize ( _data , new JsonSerializerOptions ( ) { WriteIndented = true } ) ;
97+ string serialized = JsonSerializer . Serialize ( Data , new JsonSerializerOptions
98+ {
99+ WriteIndented = true
100+ } ) ;
86101
87- File . WriteAllText ( FilePath , serialized ) ;
102+ File . WriteAllText ( TempFilePath , serialized ) ;
103+ File . Replace ( TempFilePath , FilePath , BackupFilePath ) ;
104+ File . Delete ( TempFilePath ) ;
88105 }
89106 }
90-
91- [ Obsolete ( "Deprecated as of Flow Launcher v1.8.0, on 2021.06.21. " +
92- "This is used only for Everything plugin v1.4.9 or below backwards compatibility" ) ]
93- public class JsonStrorage < T > : JsonStorage < T > where T : new ( ) { }
94107}
0 commit comments