DefaultDeserializerpublic interface SerDeserializer
This allows beans stored under an old version to be read in by a newer version.
Methods are called in order as follows:
lookupMetaBean
createBuilder
lookupMetaProperty, then setValue - once per property
build
A renamed property can be handled by overriding the lookupMetaProperty:
public MetaProperty> findMetaProperty(Class> beanType, MetaBean metaBean, String propertyName) {
if ("firstName".equals(propertyName)) {
return metaBean.metaProperty("forename");
}
return super.findMetaProperty(beanType, metaBean, propertyName);
}
A property type change can be handled by overriding the lookupMetaProperty
and setValue:
private MetaPropertyNUMBER_OF_CARS_STRING = StandaloneMetaProperty.of("numberOfCars", SimplePerson.meta(), String.class); public MetaProperty> findMetaProperty(Class> beanType, MetaBean metaBean, String propertyName) { if ("numberOfCars".equals(propertyName)) { return NUMBER_OF_CARS_STRING; // replica of the old property } return super.findMetaProperty(beanType, metaBean, propertyName); } public void setValue(BeanBuilder> builder, MetaProperty> metaProp, Object value) { if (metaProp == NUMBER_OF_CARS_STRING && value != null) { String oldValue = value.toString(); switch (oldValue) { case "One": value = 1; break; case "Two": value = 2; break; case "Lots": value = 3; break; default: value = 0; break; } } super.setValue(builder, metaProp, value); }
A semantic change can be handled by overriding the createBuilder
and build, buffering the input to process at the end of the bean:
public BeanBuilder> createBuilder(Class> beanType, MetaBean metaBean) {
return BufferingBeanBuilder.of(metaBean);
}
public Object build(Class> beanType, BeanBuilder> builder) {
BufferingBeanBuilder> bld = (BufferingBeanBuilder>) builder;
if ("Stephen".equals(bld.getBuffer().get(SimplePerson.meta().forename())) &&
"Colebourne".equals(bld.getBuffer().get(SimplePerson.meta().surname()))) {
bld.set(SimplePerson.meta().forename(), "Steve");
}
return bld.build();
}
| Modifier and Type | Method | Description |
|---|---|---|
java.lang.Object |
build(java.lang.Class<?> beanType,
BeanBuilder<?> builder) |
Builds the resulting object.
|
BeanBuilder<?> |
createBuilder(java.lang.Class<?> beanType,
MetaBean metaBean) |
Creates the stateful builder that captures state as the parse progresses.
|
MetaBean |
findMetaBean(java.lang.Class<?> beanType) |
Lookup the meta-bean for the speecified type.
|
MetaProperty<?> |
findMetaProperty(java.lang.Class<?> beanType,
MetaBean metaBean,
java.lang.String propertyName) |
Lookup the meta-property for the specified property name.
|
void |
setValue(BeanBuilder<?> builder,
MetaProperty<?> metaProp,
java.lang.Object value) |
Sets the parsed value into the builder.
|
MetaBean findMetaBean(java.lang.Class<?> beanType)
If the type is not a bean, then null may be returned.
beanType - the type being processed, not nullBeanBuilder<?> createBuilder(java.lang.Class<?> beanType, MetaBean metaBean)
This is normally a BeanBuilder however any type may be returned.
beanType - the type being processed, not nullmetaBean - the meta-bean, null if not a bean typeMetaProperty<?> findMetaProperty(java.lang.Class<?> beanType, MetaBean metaBean, java.lang.String propertyName)
Return null if a property has been deleted, which will cause the parser to discard the property.
Return a non-null meta-property to parse the property. If the property was renamed, or had a type change, then the meta-property should match the property as originally stored.
beanType - the type being processed, not nullmetaBean - the meta-bean, null if not a bean typepropertyName - the property name being parsed, not nullvoid setValue(BeanBuilder<?> builder, MetaProperty<?> metaProp, java.lang.Object value)
builder - the builder, null if not interested in the parse progressmetaProp - the meta-property, not nullvalue - the parsed value, may be nulljava.lang.Object build(java.lang.Class<?> beanType,
BeanBuilder<?> builder)
This method finishes the builder and returns the final object. The migrator could validate or manipulate data here once all data is parsed, for example to default a missing field.
beanType - the type being processed, not nullbuilder - the builder, null if not interested in the parse progressCopyright © 2007–2018 Joda.org. All rights reserved.