I'm attempting to refactor my deserialization method to use generics to enable me to deserialize any type. I can do this just fine for objects that are not in a collection, like this:
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;
}
Here is the method that I want to refactor:
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;
}
Here is my failed attempt to refactor the method to use generics:
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;
}
However, this code doesn't compile because it's not using Java generics correctly. How could I deserialize my JSON objects into a generic list/collection/something-similar type?
Here is an example of the data that I'm deserializing into the ComponentPOCO
class:
[
{ "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"
}
]
Here is the code of the ComponentPOCO type:
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;
}