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