Jackson XML序列化 – 移除字段标签

huangapple go评论83阅读模式
英文:

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:

&lt;CRequest&gt;
  &lt;abc:Name&gt;Smith&lt;/abc:Name&gt;
  &lt;abc:FirstName&gt;John&lt;/abc:Surname&gt;
  &lt;abc:Age&gt;12&lt;/abc:Age&gt;
  &lt;abc:Name&gt;Jones&lt;/abc:Name&gt;
  &lt;abc:FirstName&gt;Jake&lt;/abc:Surname&gt;
  &lt;abc:Age&gt;10&lt;/abc:Age&gt;
  &lt;abc:Name&gt;Johnson&lt;/abc:Name&gt;
  &lt;abc:FirstName&gt;Paul&lt;/abc:Surname&gt;
  &lt;abc:Age&gt;12&lt;/abc:Age&gt;
&lt;/CRequest&gt;

However, the best I could do was:

&lt;CRequest&gt;
  &lt;children&gt;
    &lt;abc:Name&gt;Smith&lt;/abc:Name&gt;
    &lt;abc:FirstName&gt;John&lt;/abc:Surname&gt;
    &lt;abc:Age&gt;12&lt;/abc:Age&gt;
  &lt;/children&gt;
  &lt;children&gt;
    &lt;abc:Name&gt;Jones&lt;/abc:Name&gt;
    &lt;abc:FirstName&gt;Jake&lt;/abc:Surname&gt;
    &lt;abc:Age&gt;12&lt;/abc:Age&gt;
  &lt;/children&gt;
  &lt;children&gt;
    &lt;abc:Name&gt;Johnson&lt;/abc:Name&gt;
    &lt;abc:FirstName&gt;Paul&lt;/abc:Surname&gt;
    &lt;abc:Age&gt;12&lt;/abc:Age&gt;
  &lt;/children&gt;
&lt;/CRequest&gt;

I have the following Java classes:

@JsonRootName(&quot;CRequest&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ChildrenRequest {

    @JacksonXmlElementWrapper(useWrapping = false)
    private List&lt;Child&gt; children= new ArrayList&lt;&gt;();
    ...

and

@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyOrder({&quot;Name&quot;, &quot;FirstName&quot;, &quot;Age&quot;})
public class Child{

    @JsonProperty(&quot;Name&quot;)
    @JacksonXmlProperty(localName = &quot;abc:Name&quot;)
    private String name;

    @JsonProperty(&quot;Surname&quot;)
    @JacksonXmlProperty(localName = &quot;FirstName&quot;)
    private String firstName;

    @JsonProperty(&quot;Age&quot;)
    @JacksonXmlProperty(localName = &quot;abc:Age&quot;)
    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&lt;ChildrenRequest&gt; {
	@Override
	public void serialize(ChildrenRequest value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		ToXmlGenerator xmlGen = (ToXmlGenerator) gen;
		writeStartObject(xmlGen);
		JsonSerializer&lt;Object&gt; 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(&quot;&quot;, rootName.getSimpleName()));
		xmlGen.writeStartObject();
	}
}

And you can register serialiser as below:

@JsonRootName(&quot;CRequest&quot;)
@JsonSerialize(using = ChildrenRequestJsonSerializer.class)
class ChildrenRequest

See also:

huangapple
  • 本文由 发表于 2020年8月4日 01:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63234236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定