How do I serialize & deserialize CSV properly?

浪尽此生 提交于 2019-12-02 04:32:54

From the error, I would like to believe that it has something to do with your schema for a Car, which has the columns of {"color"} taken from @JsonPropertyOrder on Car and not a "name" value.

You probably want to add "parts" in there, but you would get the same error that "name" is not part of that schema.

After a few changes to your code, I was able to serialize and deserialize a Car object.

Part

Here, after some other changes it needed a constructor with a single String value, so add that

@JsonPropertyOrder({"name"})
public static class Part {
    @JsonProperty("name")
    private String name;

    public Part() {
        this("");
    }

    public Part(String partJSON) {
        // TODO: Unserialize the parameter... it is a serialized Part string... 
        this.name = partJSON;
    }

Car

Here, you will need to implement a method that will convert the List<Part> into a CSV-readable format manually.

Such a method would look like this

@JsonGetter("parts")
public String getPartString() {
    String separator = ";";
    StringBuilder sb = new StringBuilder();

    Iterator<Part> iter = this.parts.iterator();
    while (iter.hasNext()) {
        Part p = iter.next();
        sb.append(p.getName());

        if (iter.hasNext())
            sb.append(separator);
    }

    return sb.toString();
} 

Also, don't forget to fix the schema at the top of the class

@JsonPropertyOrder({"color", "parts"})
public static class Car {

    @JsonProperty("color")
    private String color;
    @JsonProperty("parts")
    private List<Part> parts;

    public Car() {
        this.parts = new ArrayList<>();
    }

serialize

You can change your serialize method to take the type of the class as a generic type parameter instead of an explicit Class like so.

public static final synchronized <T> String serialize(final T object, final Boolean withHeaders) throws IOException {
    CsvMapper csvMapper = new CsvMapper();
    CsvSchema csvSchema = csvMapper.schemaFor(object.getClass());

    if (withHeaders) {
        csvSchema = csvSchema.withHeader();
    } else {
        csvSchema = csvSchema.withoutHeader();
    }

    return csvMapper.writer(csvSchema).writeValueAsString(object);
}

main - writer

Now, if you serialize a Car, you should see

color,parts
red,gearbox;door;bumper

main - reader

And reading that CSV string and looping over the Car.getParts()

Car car = mapper.readerFor(Car.class).with(csvSchema).readValue(csv);

for (Part p : car.getParts()) {
    System.out.println(p.getName());
}
gearbox
door
bumper

Full working CSV Serialize & Deserialize solution:

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import java.io.IOException;
import java.util.ArrayList;
import static java.util.Arrays.asList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NestedWrapping {

    @JsonPropertyOrder({"color", "parts"})
    public static class Car {

        @JsonProperty("color")
        private String color;

        @JsonProperty("parts")
        private List<Part> parts;

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public List<Part> getParts() {
            return parts;
        }

        public void setParts(List<Part> parts) {
            this.parts = parts;
        }

        public Car() {
            this.parts = new ArrayList<>();
        }

        @JsonGetter("parts")
        public String getPartString() {
            String separator = ";";
            StringBuilder sb = new StringBuilder();

            Iterator<Part> iter = this.parts.iterator();
            while (iter.hasNext()) {
                Part p = iter.next();
                sb.append(p.getName());

                if (iter.hasNext()) {
                    sb.append(separator);
                }
            }

            return sb.toString();
        }

        @Override
        public String toString() {
            return "Car{" + "color=" + color + ", parts=" + parts + '}';
        }

    }

    @JsonPropertyOrder({
        "name"
    })
    public static class Part {

        @JsonProperty("name")
        private String name;

        public Part() {
        }

        public Part(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Part{" + "name=" + name + '}';
        }

    }

    public static void main(String args[]) {
        try {
            Car car = new Car();
            car.setColor("red");
            Part part1 = new Part();
            part1.setName("geabox");
            Part part2 = new Part();
            part2.setName("door");
            Part part3 = new Part();
            part3.setName("bumper");
            car.setParts(asList(part1, part2, part3));
            String serialized = serialize(car, Car.class, true);
            System.out.println("serialized: " + serialized);
            List<Car> deserializedCars = (List) deserialize(serialized, Car.class, true);
            for (Car deserializedCar : deserializedCars) {
                System.out.println("deserialized: " + deserializedCar.toString());
            }
        } catch (IOException ex) {
            Logger.getLogger(NestedWrapping.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static final synchronized String serialize(final Object object, final Class type, final Boolean withHeaders) throws IOException {
        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema;
        if (withHeaders) {
            csvSchema = csvMapper.schemaFor(type).withHeader();
        } else {
            csvSchema = csvMapper.schemaFor(type).withoutHeader();
        }
        return csvMapper.writer(csvSchema).writeValueAsString(object);
    }

    public static final synchronized List<Object> deserialize(final String csv, final Class type, final Boolean hasHeaders) throws IOException {
        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema;
        if (hasHeaders) {
            csvSchema = csvMapper.schemaFor(type).withHeader();
        } else {
            csvSchema = csvMapper.schemaFor(type).withoutHeader();
        }
        MappingIterator<Object> mappingIterator = csvMapper.readerFor(type).with(csvSchema).readValues(csv);
        List<Object> objects = new ArrayList<>();
        while (mappingIterator.hasNext()) {
            objects.add(mappingIterator.next());
        }
        return objects;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!