Jackson XML::试图在没有打开的开始元素时写入属性



我有一个Java类,我想将其序列化/反序列化到XML。该类的每个属性都应该与XML元素的属性进行序列化/反序列化。

XML看起来像:

<element fullName="Asim" age="30" score="0.78" readonly="true" bounds="[0,0][10,20]" tags="tag1,tag2,tag3">
...
...
</element>

如果属性是简单的(String,int,boolean),这是有效的。我可以简单地使用@JacksonXmlProperty注释,它可以完成工作:

@JacksonXmlProperty(localName = "fullName", isAttribute = true)
private String fullName;

然而,一些属性是类对象(bounds,list),我需要在序列化/反序列化期间转换它们。我已经能够使用@JsonDeserialize注释来读取XML:

@JacksonXmlProperty(localName = "bounds", isAttribute = true)
@JsonDeserialize(using = BoundsDeserializer.class)
private Rectangle bounds;

另一方面,序列化这些字段被证明是一个相当大的挑战。我试过使用JsonSerialize(using = BoundsSerializer.class)JsonSerialize(converter = BoundsConverter.class),没有任何作用。要么我得到以下异常:

com.fasterxml.jackson.core.JsonGenerationException: Trying to write an attribute when there is no open start element.
或者生成的XML看起来像:
<element fullName="Asim" age="30" score="0.78" readonly="true">
<bounds>
<x>0</x>
<y>0</y>
<width>10</width>
<height>20</width>
</bounds>
<tags tags="tag1" tags="tag2" tags="tag3" />
...
...
</element>

如何序列化类的非原语属性作为XML中的字符串属性?


编辑

主调用代码:

try {
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String xml = mapper.writeValueAsString(this);
return xml;
}
catch (Exception ex) { ... }

要序列化/反序列化的类的相关部分:注释行是我尝试过(但失败了)的替代方法。

@JacksonXmlRootElement(localName = "root")
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Slf4j
public class XML {
@JacksonXmlProperty(localName = "text", isAttribute = true)
private String text;
@JacksonXmlProperty(localName = "tags", isAttribute = true)
@JsonDeserialize(using = ListDeserializer.class)
@JsonSerialize(converter = ListConverter.class)
//@JsonSerialize(using = ListSerializer.class)
//@JsonUnwrapped
private List<String> tags;
@JacksonXmlProperty(localName = "bounds", isAttribute = true)
@JsonDeserialize(using = BoundsDeserializer.class)
//@JsonSerialize(using = BoundsSerializer.class, contentAs = String.class)
private Rectangle bounds;
}

边界反序列化器:

public class BoundsDeserializer extends JsonDeserializer<Rectangle> {
private static final Pattern BOUNDS_PATTERN = Pattern.compile("\[(-?\d+),(-?\d+)]\[(-?\d+),(-?\d+)]");
@Override
@Nullable
public Rectangle deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getValueAsString();
if (value.isBlank()) {
return null;
}
Matcher m = BOUNDS_PATTERN.matcher(value);
if (!m.matches()) {
return ctxt.reportInputMismatch(Rectangle.class, "Not a valid bounds string: '%s'", value);
}
final int x1 = Integer.parseInt(m.group(1));
final int y1 = Integer.parseInt(m.group(2));
final int x2 = Integer.parseInt(m.group(3));
final int y2 = Integer.parseInt(m.group(4));
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
}

反序列化器列表:

public class ListDeserializer extends JsonDeserializer<List<String>> {
@Override
@Nullable
public List<String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getValueAsString();
if (value.isBlank()) {
return null;
}
String deBracketed = value.trim().replaceAll("^\[(.*)]$", "$1");
List<String> listValues = Arrays.stream(deBracketed.split(","))
.map(String::trim)
.filter(Predicate.not(String::isEmpty))
.collect(Collectors.toUnmodifiableList());
return listValues;
}
}

列表转换器:

public class ListConverter extends StdConverter<List<String>, String> {
@Override
public String convert(List<String> list) {
return String.join(",", list);
}
}

谢谢!Asim

所以我解决这个问题的方法如下:

@JacksonXmlProperty(localName = "tags", isAttribute = true)
@JsonDeserialize(using = ListDeserializer.class)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private List<String> tags;
@JacksonXmlProperty(localName = "tags", isAttribute = true)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String tagsAsString() {
return String.join(",", this.tags);
}

第一个字段是只写的,所以它将被反序列化。第二个字段是只读的,因此它只会被序列化。然而,这感觉真的很粗糙,我认为必须有一个比这更好的解决方案。:/

我想到了一个更好的方法来做同样的事情:

// Used during serialization
@JacksonXmlProperty(localName = "bounds", isAttribute = true)
@JsonSerialize(converter = RectangleToStringConverter.class)
// Used during deserialization
@JsonProperty("bounds")
@JsonDeserialize(converter = StringToRectangleConverter.class)
private Rectangle bounds;

相关内容

最新更新