Exemple #1
0
 protected BeanPropertyFactory createBeanPropertyFactory() {
   if (withBeanViewConverter)
     beanPropertyFactories.add(
         new BeanViewDescriptorProvider.BeanViewPropertyFactory(registeredViews));
   beanPropertyFactories.add(new BeanPropertyFactory.StandardFactory());
   return new BeanPropertyFactory.CompositeFactory(beanPropertyFactories);
 }
Exemple #2
0
  /**
   * Creates the standard BeanDescriptorProvider that will be used to provide {@link
   * com.owlike.genson.reflect.BeanDescriptor BeanDescriptor} instances for
   * serialization/deserialization of all types that couldn't be handled by standard and custom
   * converters and converter factories.
   *
   * @return the BeanDescriptorProvider instance.
   */
  protected BeanDescriptorProvider createBeanDescriptorProvider() {

    ContextualConverterFactory contextualConverterFactory =
        new ContextualConverterFactory(contextualFactories);
    BeanPropertyFactory beanPropertyFactory = createBeanPropertyFactory();

    List<BeanDescriptorProvider> providers = new ArrayList<BeanDescriptorProvider>();
    for (GensonBundle bundle : _bundles) {
      BeanDescriptorProvider provider =
          bundle.createBeanDescriptorProvider(
              contextualConverterFactory,
              beanPropertyFactory,
              getMutatorAccessorResolver(),
              getPropertyNameResolver(),
              this);

      if (provider != null) providers.add(provider);
    }

    providers.add(
        new BaseBeanDescriptorProvider(
            new AbstractBeanDescriptorProvider.ContextualConverterFactory(contextualFactories),
            createBeanPropertyFactory(),
            getMutatorAccessorResolver(),
            getPropertyNameResolver(),
            useGettersAndSetters,
            useFields,
            true));

    return new CompositeBeanDescriptorProvider(providers);
  }
Exemple #3
0
  /**
   * You can override this method if you want to change the {@link
   * com.owlike.genson.reflect.PropertyNameResolver PropertyNameResolver} that are registered by
   * default. You can also simply replace the default PropertyNameResolver by setting another one
   * with {@link #set(PropertyNameResolver)}.
   *
   * @return the property name resolver to be used. It should be an instance of {@link
   *     com.owlike.genson.reflect.PropertyNameResolver.CompositePropertyNameResolver
   *     PropertyNameResolver.CompositePropertyNameResolver}, otherwise you will not be able to add
   *     others PropertyNameResolvers using {@link #with(PropertyNameResolver...)} method.
   */
  protected PropertyNameResolver createPropertyNameResolver() {
    List<PropertyNameResolver> resolvers = new ArrayList<PropertyNameResolver>();
    resolvers.add(new PropertyNameResolver.AnnotationPropertyNameResolver());
    resolvers.add(new PropertyNameResolver.ConventionalBeanPropertyNameResolver());
    if (withDebugInfoPropertyNameResolver)
      resolvers.add(new ASMCreatorParameterNameResolver(isThrowExceptionOnNoDebugInfo()));

    return new PropertyNameResolver.CompositePropertyNameResolver(resolvers);
  }
Exemple #4
0
  protected BeanMutatorAccessorResolver createBeanMutatorAccessorResolver() {
    List<BeanMutatorAccessorResolver> resolvers = new ArrayList<BeanMutatorAccessorResolver>();
    resolvers.add(new BeanMutatorAccessorResolver.GensonAnnotationsResolver());

    resolvers.add(
        new BeanMutatorAccessorResolver.StandardMutaAccessorResolver(
            propertyFilter, methodFilter, constructorFilter));

    return new BeanMutatorAccessorResolver.CompositeResolver(resolvers);
  }
Exemple #5
0
 /**
  * Register some genson bundles. For example to enable JAXB support:
  *
  * <p>
  *
  * <pre>
  * builder.withBundle(new JAXBExtension());
  * </pre>
  *
  * <b>All bundles should be registered before any other customization.</b>
  *
  * @see GensonBundle
  */
 public GensonBuilder withBundle(GensonBundle... bundles) {
   for (GensonBundle bundle : bundles) {
     bundle.configure(this);
     _bundles.add(bundle);
   }
   return this;
 }
Exemple #6
0
  /**
   * Creates an instance of Genson. You may use this method as many times you want. It wont change
   * the state of the builder, in sense that the returned instance will have always the same
   * configuration.
   *
   * @return a new instance of Genson built for the current configuration.
   */
  public Genson create() {
    if (propertyNameResolver == null) propertyNameResolver = createPropertyNameResolver();
    if (mutatorAccessorResolver == null)
      mutatorAccessorResolver = createBeanMutatorAccessorResolver();

    List<Converter<?>> converters = getDefaultConverters();
    addDefaultSerializers(converters);
    addDefaultDeserializers(converters);
    addDefaultSerializers(getDefaultSerializers());
    addDefaultDeserializers(getDefaultDeserializers());

    List<Factory<? extends Converter<?>>> convFactories =
        new ArrayList<Factory<? extends Converter<?>>>();
    addDefaultConverterFactories(convFactories);
    converterFactories.addAll(convFactories);

    List<Factory<? extends Serializer<?>>> serializerFactories =
        new ArrayList<Factory<? extends Serializer<?>>>();
    addDefaultSerializerFactories(serializerFactories);
    converterFactories.addAll(serializerFactories);

    List<Factory<? extends Deserializer<?>>> deserializerFactories =
        new ArrayList<Factory<? extends Deserializer<?>>>();
    addDefaultDeserializerFactories(deserializerFactories);
    converterFactories.addAll(deserializerFactories);

    List<ContextualFactory<?>> defaultContextualFactories = new ArrayList<ContextualFactory<?>>();
    addDefaultContextualFactories(defaultContextualFactories);
    contextualFactories.addAll(defaultContextualFactories);

    beanDescriptorProvider = createBeanDescriptorProvider();

    if (withBeanViewConverter) {
      List<BeanMutatorAccessorResolver> resolvers = new ArrayList<BeanMutatorAccessorResolver>();
      resolvers.add(new BeanViewDescriptorProvider.BeanViewMutatorAccessorResolver());
      resolvers.add(mutatorAccessorResolver);
      beanViewDescriptorProvider =
          new BeanViewDescriptorProvider(
              new AbstractBeanDescriptorProvider.ContextualConverterFactory(contextualFactories),
              registeredViews,
              createBeanPropertyFactory(),
              new BeanMutatorAccessorResolver.CompositeResolver(resolvers),
              getPropertyNameResolver());
    }

    return create(createConverterFactory(), withClassAliases);
  }
Exemple #7
0
 /**
  * Allows you to register new BeanPropertyFactory responsible of creating BeanProperty accessors,
  * mutators and BeanCreators. This is a very low level feature, you probably don't need it.
  */
 public GensonBuilder withBeanPropertyFactory(BeanPropertyFactory... factories) {
   beanPropertyFactories.addAll(Arrays.asList(factories));
   return this;
 }