@@ -76,6 +76,7 @@ public final class JsonDataConverter implements DataConverter {
76
76
private static final Object [] EMPTY_OBJECT_ARRAY = new Object [0 ];
77
77
public static final String TYPE_FIELD_NAME = "type" ;
78
78
public static final String JSON_CONVERTER_TYPE = "JSON" ;
79
+ public static final String CLASS_NAME_FIELD_NAME = "className" ;
79
80
private final Gson gson ;
80
81
private final JsonParser parser = new JsonParser ();
81
82
@@ -195,7 +196,7 @@ public void write(JsonWriter out, T value) throws IOException {
195
196
@ SuppressWarnings ("unchecked" )
196
197
public T read (JsonReader in ) throws IOException {
197
198
in .beginObject ();
198
- if (!in .nextName ().equals ("type" )) {
199
+ if (!in .nextName ().equals (TYPE_FIELD_NAME )) {
199
200
throw new IOException ("Cannot deserialize DataConverter. Missing type field" );
200
201
}
201
202
String value = in .nextString ();
@@ -208,6 +209,35 @@ public T read(JsonReader in) throws IOException {
208
209
}
209
210
};
210
211
}
212
+ if (Class .class .isAssignableFrom (typeToken .getRawType ())) {
213
+ return new TypeAdapter <T >() {
214
+ @ Override
215
+ public void write (JsonWriter out , T value ) throws IOException {
216
+ out .beginObject ();
217
+ String className = ((Class ) value ).getName ();
218
+ out .name (CLASS_NAME_FIELD_NAME ).value (className );
219
+ out .endObject ();
220
+ }
221
+
222
+ @ Override
223
+ public T read (JsonReader in ) throws IOException {
224
+ in .beginObject ();
225
+ if (!in .nextName ().equals (CLASS_NAME_FIELD_NAME )) {
226
+ throw new IOException (
227
+ "Cannot deserialize class. Missing " + CLASS_NAME_FIELD_NAME + " field" );
228
+ }
229
+ String className = in .nextString ();
230
+ try {
231
+ @ SuppressWarnings ("unchecked" )
232
+ T result = (T ) Class .forName (className );
233
+ in .endObject ();
234
+ return result ;
235
+ } catch (ClassNotFoundException e ) {
236
+ throw new RuntimeException (e );
237
+ }
238
+ }
239
+ };
240
+ }
211
241
if (!Throwable .class .isAssignableFrom (typeToken .getRawType ())) {
212
242
return null ; // this class only serializes 'Throwable' and its subtypes
213
243
}
0 commit comments