Interface SerDeserializer

  • All Known Implementing Classes:
    DefaultDeserializer

    public interface SerDeserializer
    Assists with deserialization allowing migration of data from an old data format to a new one.

    This allows beans stored under an old version to be read in by a newer version.

    Methods are called in order as follows:

    1. lookupMetaBean
    2. createBuilder
    3. lookupMetaProperty, then setValue - once per property
    4. 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 MetaProperty NUMBER_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();
      }
     
    • Method Detail

      • findMetaBean

        MetaBean findMetaBean​(Class<?> beanType)
        Lookup the meta-bean for the specified type.

        If the type is not a bean, then null may be returned.

        Parameters:
        beanType - the type being processed, not null
        Returns:
        the meta-bean, null if not a bean type
      • createBuilder

        BeanBuilder<?> createBuilder​(Class<?> beanType,
                                     MetaBean metaBean)
        Creates the stateful builder that captures state as the parse progresses.

        This is normally a BeanBuilder however any type may be returned.

        Parameters:
        beanType - the type being processed, not null
        metaBean - the meta-bean, null if not a bean type
        Returns:
        the builder, null if not interested in the parse progress
      • findMetaProperty

        MetaProperty<?> findMetaProperty​(Class<?> beanType,
                                         MetaBean metaBean,
                                         String propertyName)
        Lookup the meta-property for the specified property name.

        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.

        Parameters:
        beanType - the type being processed, not null
        metaBean - the meta-bean, null if not a bean type
        propertyName - the property name being parsed, not null
        Returns:
        the meta-property, null to ignore the property
      • setValue

        void setValue​(BeanBuilder<?> builder,
                      MetaProperty<?> metaProp,
                      Object value)
        Sets the parsed value into the builder.
        Parameters:
        builder - the builder, null if not interested in the parse progress
        metaProp - the meta-property, not null
        value - the parsed value, may be null
      • build

        Object build​(Class<?> beanType,
                     BeanBuilder<?> builder)
        Builds the resulting object.

        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.

        Parameters:
        beanType - the type being processed, not null
        builder - the builder, null if not interested in the parse progress
        Returns:
        the final built object, may be null