@@ -13,12 +13,17 @@ namespace Flow.Launcher.Infrastructure.Storage
1313    public  class  JsonStorage < T >  where  T  :  new ( ) 
1414    { 
1515        protected  T ?  Data ; 
16+ 
1617        // need a new directory name 
1718        public  const  string  DirectoryName  =  "Settings" ; 
1819        public  const  string  FileSuffix  =  ".json" ; 
20+ 
1921        protected  string  FilePath  {  get ;  init ;  }  =  null ! ; 
22+ 
2023        private  string  TempFilePath  =>  $ "{ FilePath } .tmp"; 
24+ 
2125        private  string  BackupFilePath  =>  $ "{ FilePath } .bak"; 
26+ 
2227        protected  string  DirectoryPath  {  get ;  init ;  }  =  null ! ; 
2328
2429
@@ -35,7 +40,7 @@ public T Load()
3540            { 
3641                try 
3742                { 
38-                     Data  =  JsonSerializer . Deserialize < T > ( serialized ) ??  TryLoadBackup ( )  ??  LoadDefault ( ) ; 
43+                     Data  =  JsonSerializer . Deserialize < T > ( serialized )   ??  TryLoadBackup ( )  ??  LoadDefault ( ) ; 
3944                } 
4045                catch  ( JsonException ) 
4146                { 
@@ -46,6 +51,7 @@ public T Load()
4651            { 
4752                Data  =  TryLoadBackup ( )  ??  LoadDefault ( ) ; 
4853            } 
54+ 
4955            return  Data . NonNull ( ) ; 
5056        } 
5157
@@ -67,12 +73,15 @@ private T LoadDefault()
6773            try 
6874            { 
6975                var  data  =  JsonSerializer . Deserialize < T > ( File . ReadAllText ( BackupFilePath ) ) ; 
76+ 
7077                if  ( data  !=  null ) 
7178                { 
7279                    Log . Info ( $ "|JsonStorage.Load|Failed to load settings.json, { BackupFilePath }  restored successfully") ; 
7380                    File . Replace ( BackupFilePath ,  FilePath ,  null ) ; 
81+ 
7482                    return  data ; 
7583                } 
84+ 
7685                return  default ; 
7786            } 
7887            catch  ( JsonException ) 
@@ -94,14 +103,22 @@ private void BackupOriginFile()
94103
95104        public  void  Save ( ) 
96105        { 
97-             string  serialized  =  JsonSerializer . Serialize ( Data ,  new  JsonSerializerOptions 
98-             { 
99-                 WriteIndented  =  true 
100-             } ) ; 
106+             string  serialized  =  JsonSerializer . Serialize ( Data , 
107+                 new  JsonSerializerOptions 
108+                 { 
109+                     WriteIndented  =  true 
110+                 } ) ; 
101111
102112            File . WriteAllText ( TempFilePath ,  serialized ) ; 
103-             File . Replace ( TempFilePath ,  FilePath ,  BackupFilePath ) ; 
104-             File . Delete ( TempFilePath ) ; 
113+ 
114+             if  ( ! File . Exists ( FilePath ) ) 
115+             { 
116+                 File . Move ( TempFilePath ,  FilePath ) ; 
117+             } 
118+             else 
119+             { 
120+                 File . Replace ( TempFilePath ,  FilePath ,  BackupFilePath ) ; 
121+             } 
105122        } 
106123    } 
107124} 
0 commit comments