gpt4 book ai didi

json - Xml 序列化 JAXB vs Jackson-dataformat-xml?

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:27 25 4
gpt4 key购买 nike

我有一个 XSD,我希望从中支持 JSON 和 XML 数据格式的序列化/反序列化。

我使用 xjc 工具生成了我的模型类。

到目前为止,我已经使用 Jackson JSON 库处理了 JSON 数据。

我不能修改我的 Java 类,所以我配置了 ObjectMapper Mix-In annotations 和其他功能,如 PropertyNamingStrategy(更改字段名称) , SerializationFeature.WRAP_ROOT_VALUE 通过代码为我的序列化提供配置。

现在我想对 XML 序列化过程做同样的事情。

我已经在线阅读了各种选项:

  1. JAXB
  2. Jackson 库 + Jackson-dataformat-xml.jar
  3. 流媒体

哪个最适合我的情况(不能用注释编辑我的 POJO,只允许代码配置)??

最佳答案

我投票给 #2 Jackson 库 + Jackson-dataformat-xml.jar看看 JSON 和 XML 的代码,这里和那里的位更改都是一样的。

主类

  import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class MainClass {

public static void main(String[] args) throws JsonProcessingException {

// Serialization: java obj to json--> writeValueAsString
// DeSerialization: json to java obj--> ReadValue

XmlMapper mapper1 = new XmlMapper();
ObjectMapper mapper2 = new ObjectMapper();

mapper1.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper2.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

mapper1.enable(SerializationFeature.INDENT_OUTPUT);
mapper2.enable(SerializationFeature.INDENT_OUTPUT);

MyPojo mypojo = new MyPojo();
mypojo.setName("Dhani");
mypojo.setId("18082013");
mypojo.setAge(5);

String jsonStringXML = mapper1.writeValueAsString(mypojo);
String jsonStringJSON = mapper2.writeValueAsString(mypojo);
// takes java class with def or customized constructors and creates JSON

System.out.println("XML is " + "\n" + jsonStringXML + "\n");
System.out.println("Json is " + "\n" + jsonStringJSON);
} }

MyPojo.java

   import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JsonPropertyOrder({ "name", "age", "id", "note" })
@JacksonXmlRootElement(namespace = "urn:stackify:jacksonxml", localName = "myPOJO")
public class MyPojo {

@JsonProperty("_id")
private String id;

private String name;

private int age;

@JsonIgnore
private String note;

public String getNote() {
return note;
}

public void setNote(String note) {
this.note = note;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
} }

结果

XML

        <myPOJO xmlns="urn:stackify:jacksonxml">
<name xmlns="">Dhani</name>
<age xmlns="">5</age>
<_id xmlns="">18082013</_id>
</myPOJO>

JSON

     {
"name" : "Dhani",
"age" : 5,
"_id" : "18082013"
}

关于json - Xml 序列化 JAXB vs Jackson-dataformat-xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39304246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com