Added support for input Collection param
Added support for function parameter type Collection Updated both Jackson and Gson mappers Defined a new toObject() operation on JsonMapper and deprecated the old Resolves #210
This commit is contained in:
committed by
Dave Syer
parent
435e4d0438
commit
2e5c6d22b5
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 the original author or authors.
|
||||
* Copyright 2016-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,16 +15,13 @@
|
||||
*/
|
||||
package org.springframework.cloud.function.json;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class GsonMapper implements JsonMapper {
|
||||
@@ -36,13 +33,7 @@ public class GsonMapper implements JsonMapper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> toList(String json, Class<T> type) {
|
||||
return gson.fromJson(json,
|
||||
ResolvableType.forClassWithGenerics(ArrayList.class, type).getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T toSingle(String json, Class<T> type) {
|
||||
public <T> T toObject(String json, Type type) {
|
||||
return gson.fromJson(json, type);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 the original author or authors.
|
||||
* Copyright 2016-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,17 +15,15 @@
|
||||
*/
|
||||
package org.springframework.cloud.function.json;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class JacksonMapper implements JsonMapper {
|
||||
|
||||
@@ -36,23 +34,12 @@ public class JacksonMapper implements JsonMapper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> toList(String json, Class<T> type) {
|
||||
public <T> T toObject(String json, Type type) {
|
||||
try {
|
||||
return mapper.readValue(json, mapper.getTypeFactory()
|
||||
.constructCollectionLikeType(ArrayList.class, type));
|
||||
return mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot convert JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T toSingle(String json, Class<T> type) {
|
||||
try {
|
||||
return mapper.readValue(json, type);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot convert JSON", e);
|
||||
throw new IllegalArgumentException("Cannot convert JSON " + json, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 the original author or authors.
|
||||
* Copyright 2016-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,17 +15,41 @@
|
||||
*/
|
||||
package org.springframework.cloud.function.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public interface JsonMapper {
|
||||
|
||||
<T> List<T> toList(String json, Class<T> type);
|
||||
/**
|
||||
* @deprecated in favor of {@link #toObject(String, Type)}
|
||||
*/
|
||||
@Deprecated
|
||||
default <T> List<T> toList(String json, Class<T> type) {
|
||||
Type actualType = (json.startsWith("[") && !List.class.isAssignableFrom(type))
|
||||
? ResolvableType.forClassWithGenerics(ArrayList.class, (Class<?>) type)
|
||||
.getType()
|
||||
: type;
|
||||
return toObject(json, actualType);
|
||||
}
|
||||
|
||||
<T> T toSingle(String json, Class<T> type);
|
||||
/**
|
||||
* @since 2.0
|
||||
*/
|
||||
<T> T toObject(String json, Type type);
|
||||
|
||||
/**
|
||||
* @deprecated in favor of {@link #toObject(String, Type)}
|
||||
*/
|
||||
default <T> T toSingle(String json, Class<T> type) {
|
||||
return toObject(json, type);
|
||||
}
|
||||
|
||||
String toString(Object value);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user