问题描述
我一直在尝试将对象序列化为CSV String
,但对象包含 List
和 @JsonUnwrapped
在列表
对象上不起作用。
预期示例输出:
: color,part.name\\\
red,gearbox \\\
red,door \\\
red,bumper
strong>实际输出:
com.fasterxml.jackson.core.JsonGenerationException:无法识别的列'name':
这里是我的代码 :(大部分是2 POJO的) / p>
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>部分;
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的每个,但我可以找到一个有效的解决方案。
对于任何无意义的注释,我遗漏了没有理由,如果你回答代码,请随时删除它们。
从错误中,我想相信它与你的模式有关的 > > > > on
Car
而不是name
您可能想在其中添加parts
,但您会得到与<$ c相同的错误$ c>name不是该模式的一部分。
对代码进行了一些更改后,我能够序列化和反序列化 Car
对象。
部分
这里,经过一些其他修改后,单个String值,因此添加
@JsonPropertyOrder({name})
public static class Part {
@JsonProperty(name)
私有字符串名称;
public Part(){
this();
}
public Part(String partJSON){
// TODO:取消序列化参数...它是一个序列化的部分字符串...
this.name = partJSON;
}
车
在这里,您需要实现一个方法,它将 List< Part>
手动转换为CSV可读格式。
这样的方法看起来像这样
@JsonGetter $ b public String getPartString(){
String separator =;;
StringBuilder sb = new StringBuilder();
迭代器< 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>部分;
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);
}
主要作家
$ b
现在,如果你序列化一个 Car
,你应该看到
color,parts
red,gearbox; door; bumper
主要读者
读取CSV字符串并循环遍历 Car.getParts c>
Car car = mapper.readerFor(Car.class).with(csvSchema).readValue(csv);
for(Part p:car.getParts()){
System.out.println(p.getName());
}
gearbox
door
bumper
I've been trying to serialize an object to a CSV String
but the object contains a List
and @JsonUnwrapped
doesn't work on List
objects.
Expected sample output:
color,part.name\n
red,gearbox\n
red,door\n
red,bumper
Actual output:
com.fasterxml.jackson.core.JsonGenerationException: Unrecognized column 'name':
Here is my code: (Most of it is the 2 POJO's)
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);
}
}
Nothing I try seems to work, I've read every post on stackoverflow and github about the topic but I can't find a working solution.
Sorry about any pointless annotations that I've left behind for no reason and if you answer with code, please feel free to remove them.
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
这篇关于我如何序列化&反序列化CSV正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!