1
1
package ginyu .persist ;
2
2
3
3
import com .alibaba .fastjson .JSONObject ;
4
+ import com .alibaba .fastjson .parser .ParserConfig ;
5
+ import com .alibaba .fastjson .parser .deserializer .ObjectDeserializer ;
6
+ import com .alibaba .fastjson .util .FieldInfo ;
4
7
import ginyu .common .Consoles ;
5
8
import ginyu .core .Server ;
6
9
import ginyu .db .Db ;
7
10
import ginyu .event .Events ;
11
+ import ginyu .object .RedisObject ;
12
+ import ginyu .object .StringObject ;
8
13
import org .apache .commons .io .FileUtils ;
9
14
10
15
import java .io .File ;
11
16
import java .io .IOException ;
17
+ import java .lang .reflect .Type ;
12
18
import java .nio .charset .Charset ;
13
19
import java .util .function .Supplier ;
14
20
21
27
*/
22
28
public class SnapshotSaver implements Saver {
23
29
30
+ private final ParserConfig parserConfig = new ParserConfig () {
31
+ @ Override
32
+ public ObjectDeserializer getDeserializer (FieldInfo fieldInfo ) {
33
+ Consoles .info ("{}" , fieldInfo );
34
+ return super .getDeserializer (fieldInfo );
35
+ }
36
+
37
+ @ Override
38
+ public ObjectDeserializer getDeserializer (Type type ) {
39
+ if (type == RedisObject .class ) {
40
+ return super .getDeserializer (StringObject .class );
41
+ }
42
+ return super .getDeserializer (type );
43
+ }
44
+ };
45
+
24
46
@ Override
25
- public void save () {
47
+ public synchronized void save () {
26
48
Events .post ((Supplier <Void >) () -> {
27
49
final Server server = Server .INSTANCE ;
28
50
ServerForSaver serverForSaver = new ServerForSaver ();
@@ -42,17 +64,17 @@ public void save() {
42
64
}
43
65
44
66
@ Override
45
- public void load (String filePath ) throws IOException {
67
+ public synchronized void load (String filePath ) throws IOException {
46
68
if (filePath == null ) {
47
69
filePath = DEFAULT_SNAPSHOT_PATH ;
48
70
}
49
71
File file = new File (filePath );
50
72
if (!file .exists () || !file .canRead ()) {
51
- Consoles .debug ("The snapshot %s can't be read" , file );
73
+ Consoles .debug ("The snapshot {} can't be read" , file );
52
74
return ;
53
75
}
54
76
String snapshotJson = FileUtils .readFileToString (file , Charset .defaultCharset ());
55
- ServerForSaver serverForSaver = JSONObject .parseObject (snapshotJson , ServerForSaver .class );
77
+ ServerForSaver serverForSaver = JSONObject .parseObject (snapshotJson , ServerForSaver .class , parserConfig );
56
78
Server .INSTANCE .loadFromSaver (serverForSaver );
57
79
}
58
80
}
0 commit comments