亲爱的人们我需要你的建议。将json嵌入式节点反序列化为值使用Jackson的地图
我是,试图实现自定义Jackson
解串器的地图,但有困难。作为输入数据,我有以下的JSON:
{
"someMap": {
"one_value": "1",
"another_value: "2"
},
"anotherMap": "{\"100000000\": 360000,\"100000048\": 172800,\"100000036\": 129600,\"100000024\": 86400,\"100000012\": 43200}"
}
正如你可以在它节点值内JSON地图(我做那样的意向第二种情况中看到,因为我想要更换。来自env变量的值:"anotherMap": "${SOME_MAP:-{\"100000000\": 360000,\"100000048\": 172800,\"100000036\": 129600,\"100000024\": 86400,\"100000012\": 43200}}"
)。据我了解,我必须区分这两个地图反序列化流程。所以对于第一个映射,我需要使用默认的一个映射反序列化器作为第二个自定义映射器,以便从值中正确解析映射。目前,我写的代码来做到这一点:
// invokation code
new ObjectMapper().registerModule(new ConfigModule()).readValue(is, ConfigModuleTestConfigWrapper.class);
// module code
public class ConfigModule extends SimpleModule {
@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.addDeserializers(new Deserializers.Base() {
@Override
public JsonDeserializer<?> findMapDeserializer(MapType type, DeserializationConfig config, BeanDescription beanDesc,
KeyDeserializer keyDeserializer, TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer) throws JsonMappingException {
return new MapPropertyDeserializer(type);
}
});
}
private static class MapPropertyDeserializer extends StdScalarDeserializer<Map<String, Integer>> {
MapPropertyDeserializer(MapType type) {
super(type);
}
@Override
public Map<String, Integer> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode node = p.readValueAsTree();
if (node == null || node.isContainerNode()) {
// return <default jackson deserializer>
}
System.out.println("isContainerNode ?: " + node.isContainerNode());
System.out.println("isValueNode ?: " + node.isValueNode());
// some parsing flow goes below
JsonNode valueNode = node.get(1);
valueNode.asText();
return new HashMap<>();
}
}
// bean description
@JsonIgnoreProperties
public class ConfigSubstitutorModuleTestConfigWrapper {
private final Map<String, String> someMap;
private final Map<String, Integer> anotherMap;
@JsonCreator
public ConfigSubstitutorModuleTestConfigWrapper(
@JsonProperty("someMap") Map<String, String> someMap,
@JsonProperty("anotherMap") Map<String, Integer> anotherMap
) {
this.someMap = someMap;
this.anotherMap = anotherMap;
}
public Map<String, String> getSomeMap() {
return someMap;
}
public Map<String, Integer> getAnotherMap() {
return anotherMap;
}
}
的问题是(我的理解:)),我不知道如何从反序列化方法返回默认地图解串器。
有没有人有任何线索我可以在那里做到达到预期的目标?
你不能修复而产生的内部JSON这个丑陋的JSON字符串的一部分?这是看起来破碎的部分。 –
我不能那样做。这里的想法是用环境变量中的值替换那个丑陋的json,比如''anotherMap“:”$ {SOME_MAP: - {\“100000000 \”:360000,\“100000048 \”:172800,\“100000036 \”: 129600,\“100000024 \”:86400,\“100000012 \”:43200}}“'。 所以我按意愿去做。 –
可能的重复http://stackoverflow.com/questions/18313323/how-do-i-call-the-default-deserializer-from-a-custom-deserializer-in-jackson –