英文:
Jackson XML Serialization - Remove Field Tags
问题
I need to generate this XML:
<CRequest>
<abc:Name>Smith</abc:Name>
<abc:FirstName>John</abc:FirstName>
<abc:Age>12</abc:Age>
<abc:Name>Jones</abc:Name>
<abc:FirstName>Jake</abc:FirstName>
<abc:Age>10</abc:Age>
<abc:Name>Johnson</abc:Name>
<abc:FirstName>Paul</abc:FirstName>
<abc:Age>12</abc:Age>
</CRequest>
However, the best I could do was:
<CRequest>
<children>
<abc:Name>Smith</abc:Name>
<abc:FirstName>John</abc:FirstName>
<abc:Age>12</abc:Age>
</children>
<children>
<abc:Name>Jones</abc:Name>
<abc:FirstName>Jake</abc:FirstName>
<abc:Age>10</abc:Age>
</children>
<children>
<abc:Name>Johnson</abc:Name>
<abc:FirstName>Paul</abc:FirstName>
<abc:Age>12</abc:Age>
</children>
</CRequest>
I have the following Java classes:
@JsonRootName("CRequest")
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ChildrenRequest {
@JacksonXmlElementWrapper(useWrapping = false)
private List<Child> children = new ArrayList<>();
...
and
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyOrder({"Name", "FirstName", "Age"})
public class Child {
@JsonProperty("Name")
@JacksonXmlProperty(localName = "abc:Name")
private String name;
@JsonProperty("Surname")
@JacksonXmlProperty(localName = "abc:FirstName")
private String firstName;
@JsonProperty("Age")
@JacksonXmlProperty(localName = "abc:Age")
private String age;
...
Is there a way to get rid of the children tags?
PS: Without "useWrapping = false" I get two children tags for every child.
英文:
I need to generate this XML:
<CRequest>
<abc:Name>Smith</abc:Name>
<abc:FirstName>John</abc:Surname>
<abc:Age>12</abc:Age>
<abc:Name>Jones</abc:Name>
<abc:FirstName>Jake</abc:Surname>
<abc:Age>10</abc:Age>
<abc:Name>Johnson</abc:Name>
<abc:FirstName>Paul</abc:Surname>
<abc:Age>12</abc:Age>
</CRequest>
However, the best I could do was:
<CRequest>
<children>
<abc:Name>Smith</abc:Name>
<abc:FirstName>John</abc:Surname>
<abc:Age>12</abc:Age>
</children>
<children>
<abc:Name>Jones</abc:Name>
<abc:FirstName>Jake</abc:Surname>
<abc:Age>12</abc:Age>
</children>
<children>
<abc:Name>Johnson</abc:Name>
<abc:FirstName>Paul</abc:Surname>
<abc:Age>12</abc:Age>
</children>
</CRequest>
I have the following Java classes:
@JsonRootName("CRequest")
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ChildrenRequest {
@JacksonXmlElementWrapper(useWrapping = false)
private List<Child> children= new ArrayList<>();
...
and
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyOrder({"Name", "FirstName", "Age"})
public class Child{
@JsonProperty("Name")
@JacksonXmlProperty(localName = "abc:Name")
private String name;
@JsonProperty("Surname")
@JacksonXmlProperty(localName = "FirstName")
private String firstName;
@JsonProperty("Age")
@JacksonXmlProperty(localName = "abc:Age")
private String age;
...
Is there a way to get rid of the children tags?
PS: Without "useWrapping = false" I get two children tags for every child.
答案1
得分: 0
你需要实现请求类的自定义序列化器:
class ChildrenRequestJsonSerializer extends JsonSerializer<ChildrenRequest> {
@Override
public void serialize(ChildrenRequest value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
ToXmlGenerator xmlGen = (ToXmlGenerator) gen;
writeStartObject(xmlGen);
JsonSerializer<Object> childSerializer = serializers.findValueSerializer(Child.class).unwrappingSerializer(NameTransformer.NOP);
for (Child child : value.getChildren()) {
childSerializer.serialize(child, gen, serializers);
}
xmlGen.writeEndObject();
}
private void writeStartObject(ToXmlGenerator xmlGen) throws IOException {
final XmlMapper mapper = (XmlMapper) xmlGen.getCodec();
final PropertyName rootName = mapper.getSerializationConfig().findRootName(ChildrenRequest.class);
xmlGen.setNextName(new QName("", rootName.getSimpleName()));
xmlGen.writeStartObject();
}
}
然后,你可以像下面这样注册序列化器:
@JsonRootName("CRequest")
@JsonSerialize(using = ChildrenRequestJsonSerializer.class)
class ChildrenRequest
请参考以下链接了解更多信息:
英文:
You need to implement custom serialiser for request class:
class ChildrenRequestJsonSerializer extends JsonSerializer<ChildrenRequest> {
@Override
public void serialize(ChildrenRequest value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
ToXmlGenerator xmlGen = (ToXmlGenerator) gen;
writeStartObject(xmlGen);
JsonSerializer<Object> childSerializer = serializers.findValueSerializer(Child.class).unwrappingSerializer(NameTransformer.NOP);
for (Child child : value.getChildren()) {
childSerializer.serialize(child, gen, serializers);
}
xmlGen.writeEndObject();
}
private void writeStartObject(ToXmlGenerator xmlGen) throws IOException {
final XmlMapper mapper = (XmlMapper) xmlGen.getCodec();
final PropertyName rootName = mapper.getSerializationConfig().findRootName(ChildrenRequest.class);
xmlGen.setNextName(new QName("", rootName.getSimpleName()));
xmlGen.writeStartObject();
}
}
And you can register serialiser as below:
@JsonRootName("CRequest")
@JsonSerialize(using = ChildrenRequestJsonSerializer.class)
class ChildrenRequest
See also:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论