3

我一直在尝试将对象序列化为 CSV String,但该对象包含 aList并且@JsonUnwrapped不适用于List对象。

预期样本输出

color,part.name\n
red,gearbox\n
red,door\n
red,bumper

实际输出

com.fasterxml.jackson.core.JsonGenerationException: Unrecognized column 'name':

这是我的代码:(大部分是 2 个 POJO)

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import java.io.IOException;
import static java.util.Arrays.asList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NestedWrapping {

@JsonRootName("Car")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonPropertyOrder({"color"})
public static class Car {

    @JsonProperty("color")
    private String color;

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @JacksonXmlElementWrapper(useWrapping = false)
    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;
    }

}

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonPropertyOrder({
    "name"
})
public static class Part {

    @JsonProperty("name")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.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));
        System.out.println("serialized: " + serialize(car, Car.class, true));
    } 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);
}

}

我尝试的任何方法似乎都不起作用,我已经阅读了有关该主题的 stackoverflow 和 github 上的每篇文章,但我找不到有效的解决方案。

抱歉,我无缘无故留下了任何毫无意义的注释,如果您用代码回答,请随时删除它们。

4

2 回答 2

6

从错误中,我想相信它与您的架构有关 a Car,它的列{"color"}取自@JsonPropertyOrderonCar而不是"name"值。

您可能想在"parts"其中添加,但您会得到"name"不属于该架构的相同错误。

在对您的代码进行一些更改后,我能够序列化和反序列化一个Car对象。

部分

在这里,经过一些其他更改后,它需要一个具有单个 String 值的构造函数,因此添加

@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;
    }

在这里,您将需要实现一种方法,该方法将List<Part>手动转换为 CSV 可读格式。

这种方法看起来像这样

@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();
} 

另外,不要忘记在类的顶部修复模式

@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方法以将类的类型作为泛型类型参数,而不是Class像这样的显式参数。

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);
}

主要作家

现在,如果你序列化 a Car,你应该看到

color,parts
red,gearbox;door;bumper

主要读者

并读取该 CSV 字符串并循环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
于 2016-04-29T19:50:35.243 回答
1

完整的 CSV序列化序列化解决方案:

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;
    }

}
于 2016-05-05T09:57:15.387 回答