|
|
|
@ -23,22 +23,24 @@ import com.jayway.jsonpath.TypeRef;
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import java.util.concurrent.Callable; |
|
|
|
|
|
|
|
|
|
public class GsonMappingProvider implements MappingProvider { |
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GsonMappingProvider.class); |
|
|
|
|
|
|
|
|
|
private final Factory<Gson> factory; |
|
|
|
|
private final Callable<Gson> factory; |
|
|
|
|
|
|
|
|
|
public GsonMappingProvider(final Gson gson) { |
|
|
|
|
this(new Factory<Gson>() { |
|
|
|
|
this(new Callable<Gson>() { |
|
|
|
|
@Override |
|
|
|
|
public Gson createInstance() { |
|
|
|
|
public Gson call() { |
|
|
|
|
return gson; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public GsonMappingProvider(Factory<Gson> factory) { |
|
|
|
|
public GsonMappingProvider(Callable<Gson> factory) { |
|
|
|
|
this.factory = factory; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -46,9 +48,9 @@ public class GsonMappingProvider implements MappingProvider {
|
|
|
|
|
super(); |
|
|
|
|
try { |
|
|
|
|
Class.forName("com.google.gson.Gson"); |
|
|
|
|
this.factory = new Factory<Gson>() { |
|
|
|
|
this.factory = new Callable<Gson>() { |
|
|
|
|
@Override |
|
|
|
|
public Gson createInstance() { |
|
|
|
|
public Gson call() { |
|
|
|
|
return new Gson(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
@ -61,7 +63,7 @@ public class GsonMappingProvider implements MappingProvider {
|
|
|
|
|
@Override |
|
|
|
|
public <T> T map(Object source, Class<T> targetType, Configuration configuration) { |
|
|
|
|
try { |
|
|
|
|
return factory.createInstance().getAdapter(targetType).fromJsonTree((JsonElement) source); |
|
|
|
|
return factory.call().getAdapter(targetType).fromJsonTree((JsonElement) source); |
|
|
|
|
} catch (Exception e){ |
|
|
|
|
throw new MappingException(e); |
|
|
|
|
} |
|
|
|
@ -70,7 +72,7 @@ public class GsonMappingProvider implements MappingProvider {
|
|
|
|
|
@Override |
|
|
|
|
public <T> T map(Object source, TypeRef<T> targetType, Configuration configuration) { |
|
|
|
|
try { |
|
|
|
|
return (T) factory.createInstance().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source); |
|
|
|
|
return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source); |
|
|
|
|
} catch (Exception e){ |
|
|
|
|
throw new MappingException(e); |
|
|
|
|
} |
|
|
|
|