【发布时间】:2020-01-08 10:22:05
【问题描述】:
我正在尝试重构我的反序列化方法以使用泛型来反序列化任何类型。我可以对不在集合中的对象执行此操作,如下所示:
public static <T> T parseProductData(String jsonData, Class<T> typeClass) throws IOException, IllegalAccessException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
T inputMessage = objectMapper.readValue(jsonData, typeClass);
return inputMessage;
}
这是我要重构的方法:
public static List<ComponentPOCO> parseJsonComponentFromString(String fileContents){
try {
ObjectMapper mapper = new ObjectMapper()
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<ComponentPOCO> component = mapper.readValue(fileContents, new TypeReference<List<ComponentPOCO>>() {});
return component;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这是我尝试重构方法以使用泛型的失败尝试:
public static List<T> T parseJsonComponentFromString(String fileContents, Class<T> typeClass){
try {
ObjectMapper mapper = new ObjectMapper()
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<T> component = mapper.readValue(fileContents, new TypeReference<List<T>>() {});
return component;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
但是,此代码无法编译,因为它没有正确使用 Java 泛型。如何将我的 JSON 对象反序列化为通用列表/集合/类似类型?
这是我反序列化到 ComponentPOCO 类中的数据示例:
[
{ "artifactPathOrUrl": "http://www.java2s.com/Code/JarDownload/sample/sample.jar.zip",
"namespace": "exampleNamespace1",
"name": "exampleName1",
"tenant": "exampleTenant1"
},
{
"artifactPathOrUrl": "http://www.java2s.com/Code/JarDownload/sample-calculator/sample-calculator-bundle-2.0.jar.zip",
"namespace": "exampleNamespace1",
"name": "exampleName2",
"tenant": "exampleTenant1"
},
{
"artifactPathOrUrl": "http://www.java2s.com/Code/JarDownload/helloworld/helloworld.jar.zip",
"namespace": "exampleNamespace1",
"name": "exampleName3",
"tenant": "exampleTenant1"
},
{
"artifactPathOrUrl": "http://www.java2s.com/Code/JarDownload/fabric-activemq/fabric-activemq-demo-7.0.2.fuse-097.jar.zip",
"namespace": "exampleNamespace1",
"name": "exampleName4",
"tenant": "exampleTenant1"
}
]
这里是ComponentPOCO类型的代码:
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.pulsar.common.io.SinkConfig;
import org.apache.pulsar.common.io.SourceConfig;
import java.util.List;
import java.util.Map;
@Setter
@Getter
@EqualsAndHashCode
@ToString
@Accessors(chain = true)
@Data
public class ComponentPOCO {
@JsonProperty
private String namespace;
@JsonProperty
private String tenant;
@JsonProperty
private String name;
@JsonProperty
private String type;
@JsonProperty
private String destinationTopicName;
@JsonProperty
private String artifactPathOrUrl;
@JsonProperty
private String className;
@JsonProperty
private List<String> inputs;
@JsonProperty
private String output;
@JsonProperty
private Map<String, Object> userConfig;
@JsonProperty
private String logTopic;
@JsonProperty
private Map<String, Object> configs;
@JsonProperty
private Integer parallelism;
@JsonProperty
public String sinkType;
@JsonProperty
private String sourceType;
@JsonProperty
public String runtimeFlags;
}
【问题讨论】:
-
这是我个人但经过深思熟虑的观点,即在 Java 问题中包含对 Java 的咆哮是一个坏主意。与在表明您对该主题不太了解的问题中调用 Java 泛型实现“不那么智能”相同。
-
@fdreger 这个问题实际上是有据可查的。例如,请参见:softwareengineering.stackexchange.com/questions/22642/…
-
您应该以动态方式构建类型:
mapper.getTypeFactory().constructCollectionType(List.class, typeClass)。请参阅:Using Jackson to deserialize into a Map 和 How to parse nested arrays with Jackson?
标签: java list generics arraylist jackson