Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.dataformat.csv.CsvMapper;
- import com.fasterxml.jackson.dataformat.csv.CsvSchema;
- import java.io.IOException;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- import java.util.ArrayList;
- class Person {
- Person() {}
- Person(String name, String surname, int age) {
- this.name = name;
- this.surname = surname;
- this.age = age;
- }
- public String name;
- public String surname;
- public int age;
- }
- public class Marshalling {
- // ↓ Know that using this is a very bad practise,
- public static void main(String[] args) throws IOException { // but it's only for this example :)
- // CSV sample input:
- String input = "name,surname,age\nAndrew,McDonalds,25\nLuke,Smith,78";
- // Deserializing input:
- ArrayList<Person> list = deserialize(input, new TypeReference<ArrayList<Person>>(){});
- // Printing out some fields:
- System.out.println(list.get(0).name);
- System.out.println(list.get(1).surname);
- System.out.println(list.get(0).age + 3);
- // Serializing with a person more:
- Person person = new Person("Rowena", "Chandler", 27);
- list.add(person);
- String output = serialize(list);
- // Printing out the resulting CSV:
- System.out.println(output);
- }
- // If you test the following method, it works. But I had to change its signature
- // (I replaced Class<> with Jackson TypeReference<>) and I'd have preferred to not do it!
- // At the end of the file, there's why.
- // ↓ Removed 'format' because I'm testing only CSV
- public static <R> R deserialize(String content, TypeReference<R> type) throws IOException {
- ObjectMapper mapper = new CsvMapper();
- // Getting the type contained in the TypeReference object:
- Type parentType = type.getType(); // example of parentType: ArrayList<Person>
- // Checking if parentType raw type is ArrayList:
- if (!(parentType instanceof ParameterizedType) || !(ArrayList.class.equals(((ParameterizedType) parentType).getRawType()))) {
- throw new IllegalArgumentException("R raw type must be java.util.ArrayList");
- }
- Type baseType = ((ParameterizedType) parentType).getActualTypeArguments()[0]; // example of baseType: Person
- // Reading and parsing CSV with Jackson
- CsvSchema headerSchema = CsvSchema.emptySchema().withHeader();
- return (R) mapper.readerFor(((Class<?>) baseType)) // You can see, here Jackson needs the base type!
- .with(headerSchema)
- .readValues(content).readAll(); // I should use a try-with-resources but for simplicity I omitted it
- }
- public static <T> String serialize(Object object) throws IOException {
- ObjectMapper mapper = new CsvMapper();
- // Checking if object is an instance of ArrayList:
- if (!(object instanceof ArrayList))
- throw new IllegalArgumentException("R raw type must be java.util.ArrayList");
- Class parentType = object.getClass(); // example of ParentType: ArrayList
- // I think it's ABOVE that I LOOSE INFORMATION ON THE ARGUMENT TYPE
- // Here I tried in so many ways but it always prints out a cast exception:
- // "TypeVariableImpl cannot be cast to class java.lang.Class" // FIXME
- // (I've put the "fix me" only to highlight where my code is broken)
- Type baseType = ((ParameterizedType) parentType.getGenericSuperclass()).getActualTypeArguments()[0];
- System.out.println(baseType);
- // Writing CSV:
- return mapper.writerFor((Class<?>) baseType).writeValueAsString(object); // You can see, here Jackson needs the base type!
- }
- public static class TypeReference<T> extends com.fasterxml.jackson.core.type.TypeReference<T> {}
- }
- /*
- * WHY DID I CHANGE SIGNATURE IN THE "deserialize" METHOD?
- *
- * It's because if it had the following signature:
- * public static <R> R deserialize(String content, Class<R> type);
- * I'd write in the main something like:
- * deserialize(content, ArrayList<Person>.class) // ← but here there's a compile error
- * // because of type erasure
- * I really don't know how use Class<> but not loosing information...
- *
- * Thanks again in advance, for any kind of help!
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement