Browse Source

Renamed Converters to Mappers and added JacksonMappingProvider.

pull/56/head
Kalle Stenflo 10 years ago
parent
commit
05a74953ed
  1. 2
      README.md
  2. 46
      json-path/src/main/java/com/jayway/jsonpath/Configuration.java
  3. 4
      json-path/src/main/java/com/jayway/jsonpath/ReadContext.java
  4. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  5. 66
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/converter/DefaultConversionProvider.java
  6. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonProvider.java
  7. 12
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/DateMapper.java
  8. 52
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/DefaultMappingProvider.java
  9. 15
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/GsonMapper.java
  10. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/GsonMappingProvider.java
  11. 46
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/JacksonMappingProvider.java
  12. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/Mapper.java
  13. 15
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/MapperBase.java
  14. 10
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/NumberMapper.java
  15. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/StringMapper.java
  16. 12
      json-path/src/main/java/com/jayway/jsonpath/spi/converter/ConversionException.java
  17. 10
      json-path/src/main/java/com/jayway/jsonpath/spi/converter/ConversionProvider.java
  18. 12
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/MappingException.java
  19. 7
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/MappingProvider.java
  20. 2
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  21. 42
      json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java
  22. 2
      json-path/src/test/java/com/jayway/jsonpath/MapperTest.java
  23. 107
      json-path/src/test/java/com/jayway/jsonpath/ObjectMapperTest.java

2
README.md

@ -167,7 +167,7 @@ Configuration.setDefaults(new Configuration.Defaults() {
}
@Override
public ConversionProvider conversionProvider() {
public ConversionProvider mappingProvider() {
return new DefaultConversionProvider();
}
});

46
json-path/src/main/java/com/jayway/jsonpath/Configuration.java

@ -14,9 +14,9 @@
*/
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.spi.converter.DefaultConversionProvider;
import com.jayway.jsonpath.internal.spi.mapper.DefaultMappingProvider;
import com.jayway.jsonpath.internal.spi.json.JsonSmartJsonProvider;
import com.jayway.jsonpath.spi.converter.ConversionProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.Collections;
@ -30,7 +30,7 @@ public class Configuration {
private static Defaults DEFAULTS = new Defaults() {
private final ConversionProvider conversionProvider = new DefaultConversionProvider();
private final MappingProvider mappingProvider = new DefaultMappingProvider();
public JsonProvider jsonProvider() {
return new JsonSmartJsonProvider();
@ -42,8 +42,8 @@ public class Configuration {
}
@Override
public ConversionProvider conversionProvider() {
return conversionProvider;
public MappingProvider mappingProvider() {
return mappingProvider;
}
};
@ -52,42 +52,42 @@ public class Configuration {
}
private final JsonProvider jsonProvider;
private final ConversionProvider conversionProvider;
private final MappingProvider mappingProvider;
private final Set<Option> options;
private Configuration(JsonProvider jsonProvider, ConversionProvider conversionProvider, EnumSet<Option> options) {
private Configuration(JsonProvider jsonProvider, MappingProvider mappingProvider, EnumSet<Option> options) {
notNull(jsonProvider, "jsonProvider can not be null");
notNull(conversionProvider, "conversionProvider can not be null");
notNull(mappingProvider, "mappingProvider can not be null");
notNull(options, "options can not be null");
this.jsonProvider = jsonProvider;
this.conversionProvider = conversionProvider;
this.mappingProvider = mappingProvider;
this.options = Collections.unmodifiableSet(options);
}
public Configuration jsonProvider(JsonProvider newJsonProvider) {
return Configuration.builder().jsonProvider(newJsonProvider).conversionProvider(conversionProvider).options(options).build();
return Configuration.builder().jsonProvider(newJsonProvider).conversionProvider(mappingProvider).options(options).build();
}
public JsonProvider jsonProvider() {
return jsonProvider;
}
public ConversionProvider conversionProvider() {
return conversionProvider;
public MappingProvider conversionProvider() {
return mappingProvider;
}
public Configuration conversionProvider(ConversionProvider newConversionProvider) {
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(newConversionProvider).options(options).build();
public Configuration conversionProvider(MappingProvider newMappingProvider) {
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(newMappingProvider).options(options).build();
}
public Configuration addOptions(Option... options) {
EnumSet<Option> opts = EnumSet.noneOf(Option.class);
opts.addAll(this.options);
opts.addAll(asList(options));
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(conversionProvider).options(opts).build();
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(mappingProvider).options(opts).build();
}
public Configuration options(Option... options) {
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(conversionProvider).options(options).build();
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(mappingProvider).options(options).build();
}
public Set<Option> getOptions() {
@ -110,7 +110,7 @@ public class Configuration {
public static class ConfigurationBuilder {
private JsonProvider jsonProvider;
private ConversionProvider conversionProvider;
private MappingProvider mappingProvider;
private EnumSet<Option> options = EnumSet.noneOf(Option.class);
public ConfigurationBuilder jsonProvider(JsonProvider provider) {
@ -118,8 +118,8 @@ public class Configuration {
return this;
}
public ConfigurationBuilder conversionProvider(ConversionProvider provider) {
this.conversionProvider = provider;
public ConfigurationBuilder conversionProvider(MappingProvider provider) {
this.mappingProvider = provider;
return this;
}
@ -139,10 +139,10 @@ public class Configuration {
if (jsonProvider == null) {
jsonProvider = DEFAULTS.jsonProvider();
}
if(conversionProvider == null){
conversionProvider = DEFAULTS.conversionProvider();
if(mappingProvider == null){
mappingProvider = DEFAULTS.mappingProvider();
}
return new Configuration(jsonProvider, conversionProvider, options);
return new Configuration(jsonProvider, mappingProvider, options);
}
}
@ -152,7 +152,7 @@ public class Configuration {
Set<Option> options();
ConversionProvider conversionProvider();
MappingProvider mappingProvider();
}
}

4
json-path/src/main/java/com/jayway/jsonpath/ReadContext.java

@ -44,7 +44,7 @@ public interface ReadContext {
* Reads the given path from this context
*
* @param path path to read
* @param type expected return type (will try to convert)
* @param type expected return type (will try to map)
* @param filters filters
* @param <T>
* @return result
@ -64,7 +64,7 @@ public interface ReadContext {
* Reads the given path from this context
*
* @param path path to apply
* @param type expected return type (will try to convert)
* @param type expected return type (will try to map)
* @param <T>
* @return result
*/

2
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -114,7 +114,7 @@ public class JsonReader implements ParseContext, ReadContext {
}
private <T> T convert(Object obj, Class<T> targetType, Configuration configuration){
return configuration.conversionProvider().convert(obj, targetType, configuration);
return configuration.conversionProvider().map(obj, targetType, configuration);
}
}

66
json-path/src/main/java/com/jayway/jsonpath/internal/spi/converter/DefaultConversionProvider.java

@ -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;
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonProvider.java

@ -37,6 +37,10 @@ public class JacksonJsonProvider extends AbstractJsonProvider {
protected ObjectMapper objectMapper;
protected ObjectReader objectReader;
public ObjectMapper getObjectMapper() {
return objectMapper;
}
/**
* Initialize the JacksonProvider with the default ObjectMapper and ObjectReader
*/

12
json-path/src/main/java/com/jayway/jsonpath/internal/spi/converter/DateConverter.java → json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/DateMapper.java

@ -1,15 +1,15 @@
package com.jayway.jsonpath.internal.spi.converter;
package com.jayway.jsonpath.internal.spi.mapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.spi.converter.ConversionException;
import com.jayway.jsonpath.spi.mapper.MappingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
public class DateConverter extends ConverterBase {
public class DateMapper extends MapperBase {
public DateConverter() {
public DateMapper() {
register(Long.class, Date.class);
register(String.class, Date.class);
}
@ -29,10 +29,10 @@ public class DateConverter extends ConverterBase {
try {
return DateFormat.getInstance().parse(src.toString());
} catch (ParseException e) {
throw new ConversionException(e);
throw new MappingException(e);
}
}
throw new ConversionException("Can not convert: " + srcType.getName() + " to: " + targetType.getName());
throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}
}

52
json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/DefaultMappingProvider.java

@ -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;
}
}

15
json-path/src/main/java/com/jayway/jsonpath/internal/spi/converter/GsonConverter.java → json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/GsonMapper.java

@ -1,4 +1,4 @@
package com.jayway.jsonpath.internal.spi.converter;
package com.jayway.jsonpath.internal.spi.mapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
@ -7,7 +7,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.internal.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.converter.ConversionException;
import com.jayway.jsonpath.spi.mapper.MappingException;
import java.math.BigDecimal;
import java.math.BigInteger;
@ -19,9 +19,9 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class GsonConverter extends ConverterBase {
public class GsonMapper extends MapperBase {
public GsonConverter() {
public GsonMapper() {
register(JsonPrimitive.class, Integer.class);
register(JsonPrimitive.class, Long.class);
register(JsonPrimitive.class, Float.class);
@ -29,11 +29,8 @@ public class GsonConverter extends ConverterBase {
register(JsonPrimitive.class, BigDecimal.class);
register(JsonPrimitive.class, BigInteger.class);
register(JsonPrimitive.class, Date.class);
register(JsonPrimitive.class, String.class);
register(JsonPrimitive.class, Boolean.class);
register(JsonArray.class, List.class);
register(JsonObject.class, Map.class);
}
@ -76,7 +73,7 @@ public class GsonConverter extends ConverterBase {
try {
return DateFormat.getInstance().parse(primitive.getAsString());
} catch (ParseException e) {
throw new ConversionException(e);
throw new MappingException(e);
}
}
}
@ -122,6 +119,6 @@ public class GsonConverter extends ConverterBase {
}
}
return null;
throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}
}

21
json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/GsonMappingProvider.java

@ -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);
}
}
}

46
json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/JacksonMappingProvider.java

@ -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);
}
}

4
json-path/src/main/java/com/jayway/jsonpath/spi/converter/Converter.java → json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/Mapper.java

@ -1,4 +1,4 @@
package com.jayway.jsonpath.spi.converter;
package com.jayway.jsonpath.internal.spi.mapper;
import com.jayway.jsonpath.Configuration;
@ -6,7 +6,7 @@ import java.util.Set;
import static com.jayway.jsonpath.internal.Utils.notNull;
public interface Converter {
public interface Mapper {
Set<ConvertiblePair> getConvertibleTypes();

15
json-path/src/main/java/com/jayway/jsonpath/internal/spi/converter/ConverterBase.java → json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/MapperBase.java

@ -1,15 +1,14 @@
package com.jayway.jsonpath.internal.spi.converter;
package com.jayway.jsonpath.internal.spi.mapper;
import com.jayway.jsonpath.spi.converter.ConversionException;
import com.jayway.jsonpath.spi.converter.Converter;
import com.jayway.jsonpath.spi.mapper.MappingException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public abstract class ConverterBase implements Converter{
public abstract class MapperBase implements Mapper {
private final Set<Converter.ConvertiblePair> convertiblePairs = new HashSet<Converter.ConvertiblePair>();
private final Set<Mapper.ConvertiblePair> convertiblePairs = new HashSet<Mapper.ConvertiblePair>();
protected void register(Class<?> srcType, Class<?> targetType){
convertiblePairs.add(new ConvertiblePair(srcType, targetType));
@ -27,14 +26,14 @@ public abstract class ConverterBase implements Converter{
}
if (!srcType.isAssignableFrom(src.getClass())) {
throw new ConversionException("Source: " + src.getClass() + " is not assignable from: " + srcType.getName());
throw new MappingException("Source: " + src.getClass() + " is not assignable from: " + srcType.getName());
}
if(!canConvert(srcType, targetType)){
throw new ConversionException("Can not convert: " + srcType.getName() + " to: " + targetType.getName());
throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}
}
boolean canConvert(Class<?> srcType, Class<?> targetType){
return convertiblePairs.contains(new Converter.ConvertiblePair(srcType, targetType));
return convertiblePairs.contains(new Mapper.ConvertiblePair(srcType, targetType));
}
}

10
json-path/src/main/java/com/jayway/jsonpath/internal/spi/converter/NumberConverter.java → json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/NumberMapper.java

@ -1,13 +1,13 @@
package com.jayway.jsonpath.internal.spi.converter;
package com.jayway.jsonpath.internal.spi.mapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.spi.converter.ConversionException;
import com.jayway.jsonpath.spi.mapper.MappingException;
import java.math.BigDecimal;
public class NumberConverter extends ConverterBase {
public class NumberMapper extends MapperBase {
public NumberConverter() {
public NumberMapper() {
//to long
register(Integer.class, Long.class);
register(Double.class, Long.class);
@ -125,6 +125,6 @@ public class NumberConverter extends ConverterBase {
throw new ConversionException("Can not convert: " + srcType.getName() + " to: " + targetType.getName());
throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}
}

6
json-path/src/main/java/com/jayway/jsonpath/internal/spi/converter/StringConverter.java → json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/StringMapper.java

@ -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);
}

12
json-path/src/main/java/com/jayway/jsonpath/spi/converter/ConversionException.java

@ -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);
}
}

10
json-path/src/main/java/com/jayway/jsonpath/spi/converter/ConversionProvider.java

@ -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);
}

12
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/MappingException.java

@ -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);
}
}

7
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/MappingProvider.java

@ -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);
}

2
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -17,7 +17,7 @@ public class BaseTest {
}
@Override
public ConversionProvider conversionProvider() {
public ConversionProvider mappingProvider() {
return new DefaultConversionProvider();
}
});

42
json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java

@ -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;
}
}

2
json-path/src/test/java/com/jayway/jsonpath/ConverterTest.java → json-path/src/test/java/com/jayway/jsonpath/MapperTest.java

@ -7,7 +7,7 @@ import java.util.Date;
import static com.jayway.jsonpath.JsonPath.parse;
import static org.assertj.core.api.Assertions.assertThat;
public class ConverterTest extends BaseTest {
public class MapperTest extends BaseTest {
@Test

107
json-path/src/test/java/com/jayway/jsonpath/ObjectMapperTest.java

@ -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…
Cancel
Save