Browse Source

Added object mapping SPI

pull/7/head
Kalle Stenflo 13 years ago
parent
commit
8195cb6832
  1. 34
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  2. 4
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProviderFactory.java
  3. 24
      json-path/src/main/java/com/jayway/jsonpath/spi/MappingProvider.java
  4. 31
      json-path/src/main/java/com/jayway/jsonpath/spi/MappingProviderFactory.java
  5. 36
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java
  6. 9
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartJsonProvider.java

34
json-path/src/main/java/com/jayway/jsonpath/JsonModel.java

@ -17,9 +17,8 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory;
import com.jayway.jsonpath.spi.MappingProviderFactory;
import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.CollectionType;
import java.io.IOException;
import java.io.InputStream;
@ -38,8 +37,6 @@ import static org.apache.commons.lang.Validate.notNull;
*/
public class JsonModel {
private static ObjectMapper objectMapper;
private Object jsonObject;
// --------------------------------------------------------
@ -248,19 +245,6 @@ public class JsonModel {
// Private helpers
//
// --------------------------------------------------------
private static ObjectMapper getObjectMapper() {
if (JsonModel.objectMapper == null) {
synchronized (JsonModel.class) {
try {
Class.forName("org.codehaus.jackson.map.ObjectMapper");
} catch (ClassNotFoundException e) {
throw new RuntimeException("org.codehaus.jackson.map.ObjectMapper not found on classpath. This is an optional dependency needed for POJO conversions.");
}
JsonModel.objectMapper = new ObjectMapper();
}
}
return JsonModel.objectMapper;
}
private <T> T getTargetObject(JsonPath jsonPath, Class<T> clazz) {
notNull(jsonPath, "jsonPath can not be null");
@ -292,9 +276,7 @@ public class JsonModel {
// Interfaces
//
// --------------------------------------------------------
public interface MappingModelReader extends ListMappingModelReader, ObjectMappingModelReader {
}
public interface ObjectMappingModelReader {
<T> T to(Class<T> targetClass);
@ -310,6 +292,10 @@ public class JsonModel {
<T> Set<T> toSetOf(Class<T> targetClass);
}
public interface MappingModelReader extends ListMappingModelReader, ObjectMappingModelReader {
}
public interface ObjectOps {
Map<String, Object> getTarget();
@ -432,12 +418,10 @@ public class JsonModel {
}
private static class DefaultMappingModelReader implements MappingModelReader {
private ObjectMapper objectMapper;
private Object model;
private DefaultMappingModelReader(Object model) {
this.model = model;
this.objectMapper = JsonModel.getObjectMapper();
}
@Override
@ -455,8 +439,7 @@ public class JsonModel {
if (!(model instanceof List)) {
model = asList(model);
}
CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(List.class, targetClass);
return objectMapper.convertValue(model, colType);
return MappingProviderFactory.getInstance().convertValue(model, List.class, targetClass);
}
@Override
@ -466,13 +449,12 @@ public class JsonModel {
setModel.add(model);
model = setModel;
}
CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(Set.class, targetClass);
return objectMapper.convertValue(model, colType);
return MappingProviderFactory.getInstance().convertValue(model, Set.class, targetClass);
}
@Override
public <T> T to(Class<T> targetClass) {
return objectMapper.convertValue(model, targetClass);
return MappingProviderFactory.getInstance().convertValue(model, targetClass);
}

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

@ -1,6 +1,6 @@
package com.jayway.jsonpath.spi;
import com.jayway.jsonpath.spi.impl.JsonSmartProvider;
import com.jayway.jsonpath.spi.impl.JsonSmartJsonProvider;
/**
* Created by IntelliJ IDEA.
@ -11,7 +11,7 @@ import com.jayway.jsonpath.spi.impl.JsonSmartProvider;
public abstract class JsonProviderFactory {
public static JsonProvider getInstance() {
return new JsonSmartProvider();
return new JsonSmartJsonProvider();
}
}

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

@ -0,0 +1,24 @@
package com.jayway.jsonpath.spi;
import org.codehaus.jackson.map.type.CollectionType;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.map.type.TypeModifier;
import org.codehaus.jackson.map.type.TypeParser;
import org.codehaus.jackson.type.JavaType;
import java.util.Collection;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/5/12
* Time: 11:03 AM
*/
public interface MappingProvider {
public <T> T convertValue(Object fromValue, Class<T> toValueType) throws IllegalArgumentException;
public <T extends Collection<E>, E> T convertValue(Object fromValue, Class<T> collectionType, Class<E> elementType) throws IllegalArgumentException;
}

31
json-path/src/main/java/com/jayway/jsonpath/spi/MappingProviderFactory.java

@ -0,0 +1,31 @@
package com.jayway.jsonpath.spi;
import com.jayway.jsonpath.spi.impl.JacksonProvider;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/5/12
* Time: 11:03 AM
*/
public class MappingProviderFactory {
private static MappingProvider mappingProvider;
static {
try {
Class.forName("org.codehaus.jackson.map.ObjectMapper");
mappingProvider = new JacksonProvider();
} catch (ClassNotFoundException e) {
throw new RuntimeException("org.codehaus.jackson.map.ObjectMapper not found on classpath. This is an optional dependency needed for POJO conversions.", e);
}
}
public static MappingProvider getInstance() {
return mappingProvider;
}
}

36
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java

@ -16,21 +16,22 @@ package com.jayway.jsonpath.spi.impl;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.MappingProvider;
import com.jayway.jsonpath.spi.Mode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.CollectionType;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* @author Kalle Stenflo
*/
public class JacksonProvider extends AbstractJsonProvider {
public class JacksonProvider extends AbstractJsonProvider implements MappingProvider{
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public Mode getMode() {
return Mode.STRICT;
@ -39,7 +40,7 @@ public class JacksonProvider extends AbstractJsonProvider {
@Override
public Object parse(String json) throws InvalidJsonException {
try {
return new ObjectMapper().readValue(json, Object.class);
return objectMapper.readValue(json, Object.class);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
@ -48,7 +49,7 @@ public class JacksonProvider extends AbstractJsonProvider {
@Override
public Object parse(Reader jsonReader) throws InvalidJsonException {
try {
return new ObjectMapper().readValue(jsonReader, Object.class);
return objectMapper.readValue(jsonReader, Object.class);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
@ -57,7 +58,7 @@ public class JacksonProvider extends AbstractJsonProvider {
@Override
public Object parse(InputStream jsonStream) throws InvalidJsonException {
try {
return new ObjectMapper().readValue(jsonStream, Object.class);
return objectMapper.readValue(jsonStream, Object.class);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
@ -77,4 +78,23 @@ public class JacksonProvider extends AbstractJsonProvider {
public List<Object> createList() {
return new LinkedList<Object>();
}
//-------------------------------------------------------------------
//
// Mapping provider
//
//-------------------------------------------------------------------
@Override
public <T> T convertValue(Object fromValue, Class<T> toValueType) throws IllegalArgumentException {
return objectMapper.convertValue(fromValue, toValueType);
}
@Override
@SuppressWarnings({"unchecked"})
public <T extends Collection<E>, E> T convertValue(Object fromValue, Class<T> collectionType, Class<E> elementType) throws IllegalArgumentException {
CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(collectionType, elementType);
return (T)objectMapper.convertValue(fromValue, colType);
}
}

9
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartProvider.java → json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartJsonProvider.java

@ -15,15 +15,12 @@
package com.jayway.jsonpath.spi.impl;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.Mode;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONAware;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
@ -33,17 +30,17 @@ import java.util.Map;
/**
* @author Kalle Stenflo
*/
public class JsonSmartProvider extends AbstractJsonProvider {
public class JsonSmartJsonProvider extends AbstractJsonProvider {
private Mode mode;
private JSONParser parser;
public JsonSmartProvider() {
public JsonSmartJsonProvider() {
this(Mode.SLACK);
}
public JsonSmartProvider(Mode mode) {
public JsonSmartJsonProvider(Mode mode) {
this.mode = mode;
this.parser = new JSONParser(mode.intValue());
}
Loading…
Cancel
Save