1
我有一个用于java.time.Instant(和其他类型)的自定义反序列化器的Jackson ObjectMapper。如何读取单个值而不是完整的JSON blob,例如Jackson - 用Jackson的反序列化器反序列化一个值
objectMapper.readValue("2017-02-01T00:00:00Z", Instant.class)
澄清回应评论请求用例:
我已经分析的JSON到
class MyConfiguration {
public Instant someField;
public Map<String, String> otherFields;
@JsonAnyGetter
public Map<String, String> any() {
return otherFields;
}
@JsonAnySetter
public void set(String name, String value) {
otherFields.put(name, value);
}
}
现在我有:
interface Something {
void init(MyConfiguration config)
}
class Something2 implements Something {
public void init(MyConfiguration config) {
//Here I would be like the implementation to be able to
//get a value from otherFields deserialised to a specified type.
}
}
我意识到我可以使用@JsonSubTypes
直接映射它们,但我试图使系统具有可扩展性,不知道实现。
有什么用例呢?如果您没有JSON,使用JSON库没有多大意义。只需使用https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#parse-java.lang.CharSequence- – pedromss
@pedromss我解析一个YAML文件的配置一些动态属性(一个Map),我试图让一个接口的不同实现将属性拉出特定类型,例如' T getProperty(String prop,Map map,Class clazz)' –
bcoughlan
我还是感到困惑。你有一些JSON,你想提取一些字段,只反序列化? –