Хорошо, после просмотра исходного кода я нашел решение:
Сначала настройте BeanDeserialzier
class StringBeanDeserializer extends BeanDeserializer {
private ObjectMapper objectMapper;
public StringBeanDeserializer(BeanDeserializerBuilder builder, BeanDescription beanDesc, BeanPropertyMap properties, Map<String, SettableBeanProperty> backRefs, HashSet<String> ignorableProps, boolean ignoreAllUnknown, boolean hasViews, ObjectMapper objectMapper) {
super(builder, beanDesc, properties, backRefs, ignorableProps, ignoreAllUnknown, hasViews);
this.objectMapper = objectMapper == null ? new ObjectMapper() : objectMapper;
}
@Override
public Object deserializeFromString(JsonParser p, DeserializationContext ctxt) throws IOException {
if (this._beanType.isTypeOrSubTypeOf(CharSequence.class)) {
return super.deserializeFromString(p, ctxt);
}
return objectMapper.readValue(p.getText(), this._beanType.getRawClass());
}
}
Затем напишите Builder для создания этого BeanDeserialzier
class StringBeanDeserializerBuilder extends BeanDeserializerBuilder {
private ObjectMapper objectMapper;
public StringBeanDeserializerBuilder(BeanDescription beanDesc, DeserializationContext ctxt) {
super(beanDesc, ctxt);
}
public StringBeanDeserializerBuilder(BeanDescription beanDesc, DeserializationContext ctxt, ObjectMapper objectMapper) {
this(beanDesc, ctxt);
this.objectMapper = objectMapper;
}
protected StringBeanDeserializerBuilder(BeanDeserializerBuilder src) {
super(src);
}
/**
* Method for constructing a {@link BeanDeserializer}, given all
* information collected.
*/
public JsonDeserializer<?> build()
{
Collection<SettableBeanProperty> props = _properties.values();
_fixAccess(props);
BeanPropertyMap propertyMap = BeanPropertyMap.construct(props,
_config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES),
_collectAliases(props));
propertyMap.assignIndexes();
// view processing must be enabled if:
// (a) fields are not included by default (when deserializing with view), OR
// (b) one of properties has view(s) to included in defined
boolean anyViews = !_config.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION);
if (!anyViews) {
for (SettableBeanProperty prop : props) {
if (prop.hasViews()) {
anyViews = true;
break;
}
}
}
// one more thing: may need to create virtual ObjectId property:
if (_objectIdReader != null) {
/* 18-Nov-2012, tatu: May or may not have annotations for id property;
* but no easy access. But hard to see id property being optional,
* so let's consider required at this point.
*/
ObjectIdValueProperty prop = new ObjectIdValueProperty(_objectIdReader, PropertyMetadata.STD_REQUIRED);
propertyMap = propertyMap.withProperty(prop);
}
return new StringBeanDeserializer(this,
_beanDesc, propertyMap, _backRefProperties, _ignorableProps, _ignoreAllUnknown,
anyViews, objectMapper);
}
}
Наконец, напишите фабрику, которая использует этот Builder
public class StringBeanDeserializeFactory extends BeanDeserializerFactory {
private ObjectMapper objectMapper = new ObjectMapper();
/**
* Globally shareable thread-safe instance which has no additional custom deserializers
* registered
*/
public final static BeanDeserializerFactory instance = new StringBeanDeserializeFactory(
new DeserializerFactoryConfig());
public StringBeanDeserializeFactory(DeserializerFactoryConfig config) {
super(config);
}
/**
* Overridable method that constructs a {@link BeanDeserializerBuilder}
* which is used to accumulate information needed to create deserializer
* instance.
*/
protected BeanDeserializerBuilder constructBeanDeserializerBuilder(DeserializationContext ctxt,
BeanDescription beanDesc) {
return new StringBeanDeserializerBuilder(beanDesc, ctxt, objectMapper);
}
}
Пример использования:
ObjectMapper objectMapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(StringBeanDeserializeFactory.instance));
Map<String, String> map = ImmutableMap.of("person", objectMapper.writeValueAsString(new Person("nick", 25)),
"raw", "test");
System.out.println(objectMapper.convertValue(map, Data.class));