1
|
package eu.dnetlib.dhp.common.utils;
|
2
|
|
3
|
import java.lang.reflect.Type;
|
4
|
|
5
|
import com.google.gson.Gson;
|
6
|
import com.google.gson.GsonBuilder;
|
7
|
import com.google.gson.JsonDeserializationContext;
|
8
|
import com.google.gson.JsonDeserializer;
|
9
|
import com.google.gson.JsonElement;
|
10
|
import com.google.gson.JsonParseException;
|
11
|
|
12
|
/**
|
13
|
* Factory for gson object that supports serializing avro generated classes
|
14
|
*
|
15
|
* @author madryk
|
16
|
*
|
17
|
*/
|
18
|
public final class AvroGsonFactory {
|
19
|
|
20
|
//------------------------ CONSTRUCTORS -------------------
|
21
|
|
22
|
|
23
|
private AvroGsonFactory() {}
|
24
|
|
25
|
|
26
|
//------------------------ LOGIC --------------------------
|
27
|
|
28
|
public static Gson create() {
|
29
|
GsonBuilder builder = new GsonBuilder();
|
30
|
|
31
|
builder.registerTypeAdapter(CharSequence.class, new CharSequenceDeserializer());
|
32
|
|
33
|
return builder.create();
|
34
|
}
|
35
|
|
36
|
public static class CharSequenceDeserializer implements JsonDeserializer<CharSequence> {
|
37
|
|
38
|
@Override
|
39
|
public CharSequence deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
40
|
throws JsonParseException {
|
41
|
return json.getAsString();
|
42
|
}
|
43
|
|
44
|
}
|
45
|
}
|