简体   繁体   English

使用@JsonAnyGetter时忽略了PropertyNamingStrategy

[英]PropertyNamingStrategy ignored when using @JsonAnyGetter

Scenario: 场景:

Using Jackson 2.4.5 I have a dynamic bean to be serialised into JSON which can store some of its state in 'optional' properties in an internal map and uses @JsonAnyGetter on an accessor method that returns this map, eg: 使用Jackson 2.4.5,我有一个要序列化为JSON的动态bean,它可以将其某些状态存储在内部映射的“可选”属性中,并在返回此映射的访问器方法上使用@JsonAnyGetter ,例如:

public class DynamicJsonView {

private final Map<String, Object> optionalProperties = new HashMap<>();

private final String rqdProperty = "blah";

public String getRqdProperty() {
    return rqdProperty;
}

public DynamicJsonView() {
    optionalProperties.put("PROP_1", "value 1");
    optionalProperties.put("PROP_2", "value 2");
    optionalProperties.put("PROP_3", "value 3");
    // etc - in reality populated from another map
}

@JsonAnyGetter
public Map<String, Object> any() {
    return Collections.unmodifiableMap(optionalProperties);
} 
}

Note the map keys are UPPER_CASE. 请注意,映射键为UPPER_CASE。 When we setup our ObjectMapper we set the following naming strategy to convert properties to lower case (and replace camelCase with snake_case), eg: 设置ObjectMapper我们设置了以下命名策略以将属性转换为小写(并将camelCase替换为snake_case),例如:

objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);

Problem: 问题:

This works exactly as expected with normal java properties, ie rqdProperty in the example above converts to rqd_property in the JSON serialized form, but the naming strategy is seemingly ignored for the map 'properties', with the upper case keys appearing unmodified. 该工程完全按照正常的Java性能预期,即rqdProperty在上面的例子中转换为rqd_property在JSON序列化形式,但命名策略似乎忽略了地图上“属性”,与上按键的情况下出现的修改。 Having debugged the jackson LowerCaseWithUnderscoresStrategy#translate method and watched the input parameter values passed in as the object is serialised, it seems the keys are never passed through the naming strategy. 调试了jackson LowerCaseWithUnderscoresStrategy#translate方法并观察了对象被序列化时传递的input参数值,看来这些键从未通过命名策略传递。

The obvious workaround is to pre-process the map keys and convert them all to lower case, but I wondered if there's something I'm missing with regards to the property naming strategy, or if this is simply a limitation of the library? 显而易见的解决方法是对映射键进行预处理并将其全部转换为小写,但是我想知道关于属性命名策略是否缺少某些东西,或者这仅仅是库的限制吗?

This is as designed since NamingStrategy is only applied to actual concrete properties, and not for Map keys, or "any" properties. 之所以这样设计是因为NamingStrategy仅应用于实际的混凝土属性,而不应用于Map键或“任何”属性。

But if ability to include name mangling for any properties sounds like a good idea, you could request a new feature to do that: it could be enabled (for example) by a flag of @JsonAnySetter : 但是,如果包括对任何属性进行名称修饰的功能听起来像是一个好主意,则可以请求一个新功能来实现:例如,可以通过@JsonAnySetter标志启用该@JsonAnySetter

https://github.com/FasterXML/jackson-databind/issues/ https://github.com/FasterXML/jackson-databind/issues/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM