1
1
package org .baderlab .csplugins .enrichmentmap .model .io ;
2
2
3
+ import java .awt .Color ;
3
4
import java .lang .reflect .Type ;
4
5
import java .nio .file .Path ;
5
6
import java .nio .file .Paths ;
@@ -50,6 +51,7 @@ public static String serialize(EnrichmentMap map, boolean pretty) {
50
51
GsonBuilder builder = new GsonBuilder ()
51
52
.registerTypeHierarchyAdapter (Path .class , new PathAdapter ())
52
53
.registerTypeAdapter (EnrichmentResult .class , new EnrichmentResultAdapter ())
54
+ .registerTypeHierarchyAdapter (Color .class , new ColorAdapter ())
53
55
.serializeSpecialFloatingPointValues (); // really important, we allow NaN in expression files
54
56
55
57
if (pretty ) {
@@ -68,6 +70,7 @@ public static EnrichmentMap deserialize(String json) {
68
70
.registerTypeAdapter (BiMap .class , new BiMapAdapter ())
69
71
.registerTypeHierarchyAdapter (Path .class , new PathAdapter ())
70
72
.registerTypeAdapter (EnrichmentResult .class , new EnrichmentResultAdapter ())
73
+ .registerTypeHierarchyAdapter (Color .class , new ColorAdapter ())
71
74
.registerTypeAdapter (immutableIntSetType , new ImmutableIntSetAdapter ()).create ();
72
75
73
76
try {
@@ -123,6 +126,31 @@ public JsonElement serialize(Path path, Type type, JsonSerializationContext cont
123
126
return new JsonPrimitive (path .toString ());
124
127
}
125
128
}
129
+
130
+ public static class ColorAdapter implements JsonDeserializer <Color >, JsonSerializer <Color > {
131
+ @ Override
132
+ public Color deserialize (JsonElement jsonElement , Type type , JsonDeserializationContext context ) {
133
+ int rgba = Color .GRAY .getRGB ();
134
+ try {
135
+ if (jsonElement .isJsonObject ()) {
136
+ JsonObject jsonObject = jsonElement .getAsJsonObject ();
137
+ JsonElement element = jsonObject .get ("value" );
138
+ rgba = element .getAsInt ();
139
+ } else if (jsonElement .isJsonPrimitive ()) {
140
+ rgba = jsonElement .getAsInt ();
141
+ }
142
+ } catch (Exception e ) { }
143
+
144
+ return new Color (rgba , true );
145
+ }
146
+
147
+ @ Override
148
+ public JsonElement serialize (Color color , Type type , JsonSerializationContext context ) {
149
+ JsonObject obj = new JsonObject ();
150
+ obj .add ("value" , new JsonPrimitive (color .getRGB ()));
151
+ return obj ;
152
+ }
153
+ }
126
154
127
155
// Note: This can be solved with RuntimeTypeAdapterFactory, but its not part of
128
156
// the default GSON distribution
0 commit comments