Kalle Stenflo
10 years ago
23 changed files with 349 additions and 150 deletions
@ -1,66 +0,0 @@
|
||||
package com.jayway.jsonpath.internal.spi.converter; |
||||
|
||||
import com.jayway.jsonpath.Configuration; |
||||
import com.jayway.jsonpath.spi.converter.ConversionProvider; |
||||
import com.jayway.jsonpath.spi.converter.Converter; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
public class DefaultConversionProvider implements ConversionProvider { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DefaultConversionProvider.class); |
||||
|
||||
private HashMap<Class<?>, HashMap<Class<?>, Converter>> converters = new HashMap<Class<?>, HashMap<Class<?>, Converter>>(); |
||||
|
||||
public DefaultConversionProvider(){ |
||||
addConverters(new NumberConverter()); |
||||
addConverters(new StringConverter()); |
||||
addConverters(new DateConverter()); |
||||
|
||||
try { |
||||
Class.forName("com.google.gson.Gson"); |
||||
addConverters(new GsonConverter()); |
||||
} catch (ClassNotFoundException e) { |
||||
logger.debug("Gson not found on class path. No converters configured."); |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
public void addConverters(ConverterBase converter) { |
||||
for (Converter.ConvertiblePair convertible : converter.getConvertibleTypes()) { |
||||
if(!converters.containsKey(convertible.getTargetType())){ |
||||
converters.put(convertible.getTargetType(), new HashMap<Class<?>, Converter>()); |
||||
} |
||||
converters.get(convertible.getTargetType()).put(convertible.getSourceType(), converter); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public boolean canConvert(Class<?> sourceType, Class<?> targetType) { |
||||
HashMap<Class<?>, Converter> targetConverters = converters.get(targetType); |
||||
return targetConverters != null && targetConverters.containsKey(sourceType); |
||||
} |
||||
|
||||
@Override |
||||
public <T> T convert(Object source, Class<T> targetType, Configuration configuration) { |
||||
if(source == null){ |
||||
return null; |
||||
} |
||||
HashMap<Class<?>, Converter> targetConverters = converters.get(targetType); |
||||
if(targetConverters != null){ |
||||
Converter converter = targetConverters.get(source.getClass()); |
||||
if(converter != null){ |
||||
return (T)converter.convert(source, source.getClass(), targetType, configuration); |
||||
} |
||||
converter = targetConverters.get(Object.class); |
||||
if(converter != null){ |
||||
return (T)converter.convert(source, source.getClass(), targetType, configuration); |
||||
} |
||||
} |
||||
return (T)source; |
||||
} |
||||
} |
@ -0,0 +1,52 @@
|
||||
package com.jayway.jsonpath.internal.spi.mapper; |
||||
|
||||
import com.jayway.jsonpath.Configuration; |
||||
import com.jayway.jsonpath.spi.mapper.MappingProvider; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
public class DefaultMappingProvider implements MappingProvider { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DefaultMappingProvider.class); |
||||
|
||||
private HashMap<Class<?>, HashMap<Class<?>, Mapper>> converters = new HashMap<Class<?>, HashMap<Class<?>, Mapper>>(); |
||||
|
||||
public DefaultMappingProvider(){ |
||||
addMapper(new NumberMapper()); |
||||
addMapper(new StringMapper()); |
||||
addMapper(new DateMapper()); |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
public void addMapper(Mapper converter) { |
||||
for (Mapper.ConvertiblePair convertible : converter.getConvertibleTypes()) { |
||||
if(!converters.containsKey(convertible.getTargetType())){ |
||||
converters.put(convertible.getTargetType(), new HashMap<Class<?>, Mapper>()); |
||||
} |
||||
converters.get(convertible.getTargetType()).put(convertible.getSourceType(), converter); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public <T> T map(Object source, Class<T> targetType, Configuration configuration) { |
||||
if(source == null){ |
||||
return null; |
||||
} |
||||
HashMap<Class<?>, Mapper> targetConverters = converters.get(targetType); |
||||
if(targetConverters != null){ |
||||
Mapper mapper = targetConverters.get(source.getClass()); |
||||
if(mapper != null){ |
||||
return (T) mapper.convert(source, source.getClass(), targetType, configuration); |
||||
} |
||||
mapper = targetConverters.get(Object.class); |
||||
if(mapper != null){ |
||||
return (T) mapper.convert(source, source.getClass(), targetType, configuration); |
||||
} |
||||
} |
||||
return (T)source; |
||||
} |
||||
} |
@ -0,0 +1,21 @@
|
||||
package com.jayway.jsonpath.internal.spi.mapper; |
||||
|
||||
import com.jayway.jsonpath.spi.mapper.MappingProvider; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
public class GsonMappingProvider extends DefaultMappingProvider implements MappingProvider { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GsonMappingProvider.class); |
||||
|
||||
public GsonMappingProvider() { |
||||
super(); |
||||
try { |
||||
Class.forName("com.google.gson.Gson"); |
||||
addMapper(new GsonMapper()); |
||||
} catch (ClassNotFoundException e) { |
||||
logger.error("Gson not found on class path. No converters configured."); |
||||
throw new RuntimeException("Gson not found on path", e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,46 @@
|
||||
package com.jayway.jsonpath.internal.spi.mapper; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import com.jayway.jsonpath.Configuration; |
||||
import com.jayway.jsonpath.spi.mapper.MappingProvider; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
public class JacksonMappingProvider implements MappingProvider { |
||||
|
||||
private final ObjectMapper objectMapper; |
||||
|
||||
private HashMap<Class<?>, HashMap<Class<?>, Mapper>> converters = new HashMap<Class<?>, HashMap<Class<?>, Mapper>>(); |
||||
|
||||
public JacksonMappingProvider() { |
||||
this(new ObjectMapper()); |
||||
} |
||||
|
||||
public JacksonMappingProvider(ObjectMapper objectMapper) { |
||||
this.objectMapper = objectMapper; |
||||
} |
||||
public void addMapper(Mapper converter) { |
||||
for (Mapper.ConvertiblePair convertible : converter.getConvertibleTypes()) { |
||||
if(!converters.containsKey(convertible.getTargetType())){ |
||||
converters.put(convertible.getTargetType(), new HashMap<Class<?>, Mapper>()); |
||||
} |
||||
converters.get(convertible.getTargetType()).put(convertible.getSourceType(), converter); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public <T> T map(Object source, Class<T> targetType, Configuration configuration) { |
||||
if(source == null){ |
||||
return null; |
||||
} |
||||
HashMap<Class<?>, Mapper> targetConverters = converters.get(targetType); |
||||
if(targetConverters != null){ |
||||
Mapper mapper = targetConverters.get(source.getClass()); |
||||
if(mapper != null){ |
||||
return (T) mapper.convert(source, source.getClass(), targetType, configuration); |
||||
} |
||||
} |
||||
return objectMapper.convertValue(source, targetType); |
||||
} |
||||
} |
@ -1,10 +1,10 @@
|
||||
package com.jayway.jsonpath.internal.spi.converter; |
||||
package com.jayway.jsonpath.internal.spi.mapper; |
||||
|
||||
import com.jayway.jsonpath.Configuration; |
||||
|
||||
public class StringConverter extends ConverterBase { |
||||
public class StringMapper extends MapperBase { |
||||
|
||||
public StringConverter() { |
||||
public StringMapper() { |
||||
register(Object.class, String.class); |
||||
} |
||||
|
@ -1,12 +0,0 @@
|
||||
package com.jayway.jsonpath.spi.converter; |
||||
|
||||
public class ConversionException extends RuntimeException { |
||||
|
||||
public ConversionException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
|
||||
public ConversionException(String message) { |
||||
super(message); |
||||
} |
||||
} |
@ -1,10 +0,0 @@
|
||||
package com.jayway.jsonpath.spi.converter; |
||||
|
||||
import com.jayway.jsonpath.Configuration; |
||||
|
||||
public interface ConversionProvider { |
||||
|
||||
boolean canConvert(Class<?> sourceType, Class<?> targetType); |
||||
|
||||
<T> T convert(Object source, Class<T> targetType, Configuration configuration); |
||||
} |
@ -0,0 +1,12 @@
|
||||
package com.jayway.jsonpath.spi.mapper; |
||||
|
||||
public class MappingException extends RuntimeException { |
||||
|
||||
public MappingException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
|
||||
public MappingException(String message) { |
||||
super(message); |
||||
} |
||||
} |
@ -0,0 +1,7 @@
|
||||
package com.jayway.jsonpath.spi.mapper; |
||||
|
||||
import com.jayway.jsonpath.Configuration; |
||||
|
||||
public interface MappingProvider { |
||||
<T> T map(Object source, Class<T> targetType, Configuration configuration); |
||||
} |
@ -0,0 +1,42 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import com.jayway.jsonpath.internal.spi.json.JacksonJsonProvider; |
||||
import com.jayway.jsonpath.internal.spi.mapper.JacksonMappingProvider; |
||||
import org.junit.Test; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
public class JacksonTest { |
||||
|
||||
private static Configuration config = Configuration |
||||
.builder() |
||||
.conversionProvider(new JacksonMappingProvider()) |
||||
.jsonProvider(new JacksonJsonProvider()) |
||||
.build(); |
||||
|
||||
|
||||
@Test |
||||
public void an_object_can_be_mapped_to_pojo() { |
||||
|
||||
String json = "{\n" + |
||||
" \"foo\" : \"foo\",\n" + |
||||
" \"bar\" : 10,\n" + |
||||
" \"baz\" : true\n" + |
||||
"}"; |
||||
|
||||
|
||||
FooBarBaz fooBarBaz = JsonPath.using(config).parse(json).read("$", FooBarBaz.class); |
||||
|
||||
assertThat(fooBarBaz.foo).isEqualTo("foo"); |
||||
assertThat(fooBarBaz.bar).isEqualTo(10L); |
||||
assertThat(fooBarBaz.baz).isEqualTo(true); |
||||
|
||||
} |
||||
|
||||
public static class FooBarBaz { |
||||
public String foo; |
||||
public Long bar; |
||||
public boolean baz; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,107 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import net.minidev.json.mapper.Mapper; |
||||
import org.junit.Test; |
||||
|
||||
import java.util.Date; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
public class ObjectMapperTest { |
||||
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
||||
@Test |
||||
public void mapping() { |
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>(); |
||||
map.put("foo", "MAP FOO"); |
||||
map.put("bar", Long.MAX_VALUE); |
||||
|
||||
Baz baz = new Baz(); |
||||
baz.setFlurb("FLURB"); |
||||
|
||||
FooBar fooBar = new FooBar(); |
||||
fooBar.setFoo("OBJ FOO"); |
||||
fooBar.setBar(Long.MIN_VALUE); |
||||
fooBar.setBaz(baz); |
||||
|
||||
|
||||
Map mappedMap = objectMapper.convertValue(fooBar, Map.class); |
||||
|
||||
|
||||
System.out.println(mappedMap); |
||||
/* |
||||
FooBar foobar = objectMapper.convertValue(map, FooBar.class); |
||||
|
||||
|
||||
Integer integer = objectMapper.convertValue(1L, Integer.class); |
||||
Integer integer2 = objectMapper.convertValue(1D, Integer.class); |
||||
String date = objectMapper.convertValue(new Date(System.currentTimeMillis()), String.class); |
||||
*/ |
||||
|
||||
|
||||
|
||||
Mapper mapper = new Mapper(); |
||||
|
||||
|
||||
Long convert = mapper.getMapper(Long.class).convert(1D); |
||||
|
||||
System.out.println(convert); |
||||
|
||||
} |
||||
|
||||
|
||||
public static class FooBar { |
||||
private String foo; |
||||
private Long bar; |
||||
|
||||
private Baz baz; |
||||
|
||||
public FooBar() { |
||||
} |
||||
|
||||
public Baz getBaz() { |
||||
return baz; |
||||
} |
||||
|
||||
public void setBaz(Baz baz) { |
||||
this.baz = baz; |
||||
} |
||||
|
||||
public String getFoo() { |
||||
return foo; |
||||
} |
||||
|
||||
public void setFoo(String foo) { |
||||
this.foo = foo; |
||||
} |
||||
|
||||
public Long getBar() { |
||||
return bar; |
||||
} |
||||
|
||||
public void setBar(Long bar) { |
||||
this.bar = bar; |
||||
} |
||||
} |
||||
|
||||
public static class Baz { |
||||
private String flurb; |
||||
|
||||
public Baz() { |
||||
} |
||||
|
||||
public String getFlurb() { |
||||
return flurb; |
||||
} |
||||
|
||||
public void setFlurb(String flurb) { |
||||
this.flurb = flurb; |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue