java 没有注释的 Jackson 是否绝对需要 Setter?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4822856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Hymanson Without Annotations Absolutely Require Setters?
提问by duffymo
I'm using Hymanson 1.6.4 and Java JDK 6.
我正在使用 Hymanson 1.6.4 和 Java JDK 6。
I don't want to use Hymanson annotations; I want to have immutable Java objects without setters.
我不想使用 Hymanson 注释;我想要不带 setter 的不可变 Java 对象。
The two requirements appear to conflict.
这两个要求似乎有冲突。
If I add private setters deserialization works fine.
如果我添加私有设置器反序列化工作正常。
I'm trying to not resort to private setters for my immutable objects - I'm stubborn that way.
我试图不为我的不可变对象诉诸私人设置器 - 我很固执。
I'm in the process of trying a custom implementation of VisibilityChecker to allow ANY field access.
我正在尝试 VisibilityChecker 的自定义实现以允许任何字段访问。
But if anyone has some advice or lessons learned they can share I'd appreciate hearing them.
但如果有人有一些建议或经验教训,他们可以分享,我很高兴听到他们。
UPDATE: It's working.
更新:它正在工作。
Builder pattern, private constructor - a la Bloch "Effective Java".
构建器模式,私有构造器 - la Bloch “Effective Java”。
It took setting deserialization configuration and visibility, but now it's good to go.
需要设置反序列化配置和可见性,但现在一切顺利。
public class JsonMapper
{
private static final int INITIAL_SIZE = 2048;
/** See http://wiki.fasterxml.com/HymansonBestPracticeThreadSafety?highlight=(\bCategoryHymanson\b) */
private static ObjectMapper mapper;
static
{
mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
SerializationConfig serializationConfig = mapper.getSerializationConfig();
serializationConfig.setDateFormat(Person.DEFAULT_FORMATTER);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
deserializationConfig.setDateFormat(Person.DEFAULT_FORMATTER);
deserializationConfig.enable(DeserializationConfig.Feature.AUTO_DETECT_FIELDS);
mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
}
public static <T> String serialize(T o) throws IOException
{
StringWriter sw = new StringWriter(INITIAL_SIZE);
mapper.writeValue(sw, o);
return sw.toString();
}
public static <T> T deserialize(String source, Class<T> targetClass) throws IOException
{
ByteArrayInputStream stream = new ByteArrayInputStream(source.getBytes());
TreeTraversingParser treeTraversingParser = new TreeTraversingParser(mapper.readTree(stream));
treeTraversingParser.setCodec(mapper);
return treeTraversingParser.readValueAs(targetClass);
}
}
回答by StaxMan
Glad to hear you made it work -- ability to change auto-detection visibility levels is a very powerful feature, but there are so many features that it is not trivial to find all there is.
很高兴听到您成功了——更改自动检测可见性级别的能力是一项非常强大的功能,但功能太多,要找到所有功能并非易事。
Couple of additional pointers: if you do not want to add Hymanson annotations in POJOs, you can still use mix-in annotations. With this, you can use @JsonCreator to specify non-default constructor to use which allows true immutable value types (more on Hymanson and immutable types on this article).
几个额外的提示:如果您不想在 POJO 中添加 Hymanson 注释,您仍然可以使用mix-in annotations。有了这个,您可以使用 @JsonCreator 指定要使用的非默认构造函数,它允许真正的不可变值类型(更多关于Hyman逊和本文的不可变类型)。
And finally: while builder pattern is not yet directly supported, it has been planned as per this Jira entry.
最后:虽然还没有直接支持构建器模式,但已按照此 Jira 条目进行了计划。