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);
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -29,11 +29,13 @@ import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.cloud.function.json.GsonMapper;
|
||||
import org.springframework.cloud.function.json.JacksonMapper;
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
@@ -54,8 +56,8 @@ public class JsonMapperTests {
|
||||
@Test
|
||||
public void vanillaArray() {
|
||||
String json = "[{\"value\":\"foo\"},{\"value\":\"foo\"}]";
|
||||
List<Foo> list = mapper.toList(json,
|
||||
Foo.class);
|
||||
List<Foo> list = mapper.toObject(json,
|
||||
ResolvableType.forClassWithGenerics(List.class, Foo.class).getType());
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(list.get(0).getValue()).isEqualTo("foo");
|
||||
assertThat(mapper.toString(list)).isEqualTo(json);
|
||||
@@ -63,34 +65,36 @@ public class JsonMapperTests {
|
||||
|
||||
@Test
|
||||
public void intArray() {
|
||||
List<Integer> list = mapper.toList("[123,456]", Integer.class);
|
||||
List<Integer> list = mapper.toObject("[123,456]",
|
||||
ResolvableType.forClassWithGenerics(List.class, Integer.class).getType());
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(list.get(0)).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyArray() {
|
||||
List<Foo> list = mapper.toList("[]", Foo.class);
|
||||
List<Foo> list = mapper.toObject("[]",
|
||||
ResolvableType.forClassWithGenerics(List.class, Foo.class).getType());
|
||||
assertThat(list).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vanillaObject() {
|
||||
String json = "{\"value\":\"foo\"}";
|
||||
Foo foo = mapper.toSingle(json, Foo.class);
|
||||
Foo foo = mapper.toObject(json, Foo.class);
|
||||
assertThat(foo.getValue()).isEqualTo("foo");
|
||||
assertThat(mapper.toString(foo)).isEqualTo(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intValue() {
|
||||
int foo = mapper.toSingle("123", Integer.class);
|
||||
int foo = mapper.toObject("123", Integer.class);
|
||||
assertThat(foo).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void empty() {
|
||||
Foo foo = mapper.toSingle("{}", Foo.class);
|
||||
Foo foo = mapper.toObject("{}", Foo.class);
|
||||
assertThat(foo.getValue()).isNull();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user