我如何杰克逊将包装器类型序列化到字符串和从字符串中序列化?
我从他们网站的两个不同示例中合并了以下内容。但是 HostName 类型被序列化/反序列化为
{ "name" : "my.host.name.com" }
当我希望它只是字符串时
"my.host.name.com"
请注意,我有很多XName 类型,因此使用了 Immutables 包装器。所以我更喜欢一个能减少样板数量的解决方案。
@Value.Immutable @AbstractName.Wrapper
public abstract class _HostName extends AbstractName { }
...
public abstract class AbstractName {
@JsonSerialize
@JsonDeserialize
@Value.Style(
// Detect names starting with underscore
typeAbstract = "_*",
// Generate without any suffix, just raw detected name
typeImmutable = "*",
// Make generated public, leave underscored as package private
visibility = Value.Style.ImplementationVisibility.PUBLIC,
// Seems unnecessary to have builder or superfluous copy method
defaults = @Value.Immutable(builder = false, copy = false))
public @interface Wrapper {}
@Value.Parameter
public abstract String name();
@Override
public String toString() { return name(); }
}