1
1
package org .baderlab .csplugins .enrichmentmap .model .io ;
2
2
3
3
import java .awt .Color ;
4
+ import java .io .File ;
5
+ import java .io .FileReader ;
6
+ import java .io .FileWriter ;
7
+ import java .io .IOException ;
8
+ import java .io .Reader ;
9
+ import java .io .StringReader ;
10
+ import java .io .StringWriter ;
4
11
import java .lang .reflect .Type ;
5
12
import java .nio .file .Path ;
6
13
import java .nio .file .Paths ;
@@ -46,6 +53,18 @@ public static String serialize(EnrichmentMap map) {
46
53
}
47
54
48
55
public static String serialize (EnrichmentMap map , boolean pretty ) {
56
+ var writer = new StringWriter ();
57
+ serialize (map , pretty , writer );
58
+ return writer .toString ();
59
+ }
60
+
61
+ public static void serialize (EnrichmentMap map , File file ) throws IOException {
62
+ try (var writer = new FileWriter (file )) {
63
+ serialize (map , true , writer );
64
+ }
65
+ }
66
+
67
+ private static void serialize (EnrichmentMap map , boolean pretty , Appendable writer ) {
49
68
// When saving to the session file DO NOT enable pretty printing, the Cytoscape
50
69
// CSV parser is very slow for multi-line text
51
70
GsonBuilder builder = new GsonBuilder ()
@@ -54,16 +73,28 @@ public static String serialize(EnrichmentMap map, boolean pretty) {
54
73
.registerTypeHierarchyAdapter (Color .class , new ColorAdapter ())
55
74
.serializeSpecialFloatingPointValues (); // really important, we allow NaN in expression files
56
75
57
- if (pretty ) {
76
+ if (pretty )
58
77
builder .setPrettyPrinting ();
59
- }
60
78
61
79
Gson gson = builder .create ();
62
- String json = gson .toJson (map );
63
- return json ;
80
+ gson .toJson (map , writer );
81
+
82
+ }
83
+
84
+ public static EnrichmentMap deserialize (File file ) throws IOException {
85
+ try (var reader = new FileReader (file )) {
86
+ return deserialize (reader );
87
+ }
64
88
}
65
89
66
90
public static EnrichmentMap deserialize (String json ) {
91
+ try (var reader = new StringReader (json )) {
92
+ return deserialize (reader );
93
+ }
94
+ }
95
+
96
+
97
+ private static EnrichmentMap deserialize (Reader reader ) {
67
98
Type immutableIntSetType = new TypeToken <ImmutableSet <Integer >>() {}.getType ();
68
99
69
100
Gson gson = new GsonBuilder ()
@@ -74,7 +105,7 @@ public static EnrichmentMap deserialize(String json) {
74
105
.registerTypeAdapter (immutableIntSetType , new ImmutableIntSetAdapter ()).create ();
75
106
76
107
try {
77
- EnrichmentMap map = gson .fromJson (json , EnrichmentMap .class );
108
+ EnrichmentMap map = gson .fromJson (reader , EnrichmentMap .class );
78
109
for (EMDataSet dataset : map .getDataSetList ()) {
79
110
dataset .setParent (map );
80
111
}
0 commit comments