public class GensonBuilder extends Object
withConverters(Converter...)
or custom converter Factories
withConverterFactory(Factory)
.
This class combines the GensonBuilder design pattern with template pattern providing handy
configuration and extensibility. All its public methods are intended to be used in the
GensonBuilder "style" and its protected methods are part of the template. When you call
create()
method, it will start assembling all the configuration and build all the
required components by using the protected methods. For example if you wish to use in your
projects a GensonBuilder that will always create some custom
BeanDescriptorProvider
you have to
extend createBeanDescriptorProvider()
, or imagine that you implemented some
Converters that you always want to register then override getDefaultConverters()
.Constructor and Description |
---|
GensonBuilder() |
Modifier and Type | Method and Description |
---|---|
GensonBuilder |
acceptSingleValueAsList(boolean enable)
Wrap a single value into a list when a list is expected.
|
GensonBuilder |
addAlias(String alias,
Class<?> forClass)
Alias used in serialized class metadata instead of the full class name.
|
protected void |
addDefaultContextualFactories(List<ContextualFactory<?>> factories) |
protected void |
addDefaultConverterFactories(List<Factory<? extends Converter<?>>> factories)
Override this method if you want to change the default converter factories.
|
protected void |
addDefaultDeserializerFactories(List<Factory<? extends Deserializer<?>>> deserializerFactories) |
protected void |
addDefaultSerializerFactories(List<Factory<? extends Serializer<?>>> serializerFactories) |
Genson |
create()
Creates an instance of Genson.
|
protected Genson |
create(Factory<Converter<?>> converterFactory,
Map<String,Class<?>> classAliases)
In theory this allows you to extend Genson class and to instantiate it, but actually you
can not do it as Genson class is final.
|
protected BeanDescriptorProvider |
createBeanDescriptorProvider()
Creates the standard BeanDescriptorProvider that will be used to provide
BeanDescriptor instances for
serialization/deserialization of all types that couldn't be handled by standard and
custom converters and converter factories. |
protected BeanMutatorAccessorResolver |
createBeanMutatorAccessorResolver() |
protected BeanPropertyFactory |
createBeanPropertyFactory() |
protected Factory<Converter<?>> |
createConverterFactory()
You should override this method if you want to add custom
ChainedFactory or if you need to chain
them differently. |
protected PropertyNameResolver |
createPropertyNameResolver()
You can override this method if you want to change the
PropertyNameResolver that are
registered by default. |
GensonBuilder |
exclude(Class<?> fieldOfType) |
GensonBuilder |
exclude(String field) |
GensonBuilder |
exclude(String field,
Class<?> fromClass) |
GensonBuilder |
exclude(String field,
Class<?> fromClass,
Class<?> ofType) |
GensonBuilder |
failOnMissingProperty(boolean enable)
If set to true, Genson will throw a JsonBindingException when it encounters a property in the incoming json that does not match
a property in the class.
|
GensonBuilder |
failOnNullPrimitive(boolean enabled)
False by default.
|
protected BeanDescriptorProvider |
getBeanDescriptorProvider() |
protected BeanViewDescriptorProvider |
getBeanViewDescriptorProvider() |
ClassLoader |
getClassLoader() |
protected List<Converter<?>> |
getDefaultConverters()
You can override this methods if you want to change the default converters (remove some,
change the order, etc).
|
protected List<Deserializer<?>> |
getDefaultDeserializers() |
protected List<Serializer<?>> |
getDefaultSerializers() |
Map<Type,Deserializer<?>> |
getDeserializersMap() |
List<Factory<?>> |
getFactories() |
protected BeanMutatorAccessorResolver |
getMutatorAccessorResolver() |
protected PropertyNameResolver |
getPropertyNameResolver() |
Map<Type,Serializer<?>> |
getSerializersMap() |
GensonBuilder |
include(Class<?> fieldOfType) |
GensonBuilder |
include(String field) |
GensonBuilder |
include(String field,
Class<?> fromClass) |
GensonBuilder |
include(String field,
Class<?> fromClass,
Class<?> ofType) |
boolean |
isDateAsTimestamp() |
boolean |
isHtmlSafe() |
boolean |
isSkipNull() |
boolean |
isThrowExceptionOnNoDebugInfo() |
GensonBuilder |
rename(Class<?> fieldOfType,
String toName)
Renames all fields of type fieldOfType to toName.
|
GensonBuilder |
rename(String field,
Class<?> fromClass,
String toName)
Renames all fields named field declared in class fromClass to toName.
|
GensonBuilder |
rename(String field,
Class<?> fromClass,
String toName,
Class<?> ofType)
Renames all fields named field, of type fieldOfType and declared in fromClass to toName.
|
GensonBuilder |
rename(String field,
String toName)
Renames all fields named field to toName.
|
GensonBuilder |
rename(String field,
String toName,
Class<?> fieldOfType)
Renames all fields named field and of type fieldOfType to toName.
|
GensonBuilder |
set(BeanMutatorAccessorResolver resolver)
Replaces default
BeanMutatorAccessorResolver by the specified one. |
GensonBuilder |
set(PropertyNameResolver resolver)
Replaces default
PropertyNameResolver by the specified one. |
GensonBuilder |
setConstructorFilter(VisibilityFilter constructorFilter) |
GensonBuilder |
setFieldFilter(VisibilityFilter propertyFilter) |
GensonBuilder |
setHtmlSafe(boolean htmlSafe)
If true \,<,>,&,= characters will be replaced by ', <, >, &, =
|
GensonBuilder |
setMethodFilter(VisibilityFilter methodFilter) |
GensonBuilder |
setSkipNull(boolean skipNull)
If true will not serialize null values
|
GensonBuilder |
settersMustBeVoid(boolean settersMustBeVoid)
If true, only setters with a return type of void will be considered when deserializing.
|
GensonBuilder |
setThrowExceptionIfNoDebugInfo(boolean throwExcOnNoDebugInfo)
Used in conjunction with
useConstructorWithArguments(boolean) . |
GensonBuilder |
useBeanViews(boolean enabled)
If true
BeanView mechanism will be enabled. |
GensonBuilder |
useByteAsInt(boolean enable) |
GensonBuilder |
useClassMetadata(boolean enabled)
Indicates whether class metadata should be serialized and used during deserialization.
|
GensonBuilder |
useClassMetadataWithStaticType(boolean enable)
If set to false, during serialization class metadata will be serialized only for types where the runtime type differs from the static one.
|
GensonBuilder |
useConstructorWithArguments(boolean enabled)
If true constructor and method arguments name will be resolved from the generated debug
symbols during compilation.
|
GensonBuilder |
useDateAsTimestamp(boolean enabled) |
GensonBuilder |
useDateFormat(DateFormat dateFormat)
Specifies the data format that should be used for java.util.Date serialization and
deserialization.
|
GensonBuilder |
useDefaultValue(Object value,
Class<?> targetType)
Uses the passed value as the default value for this type.
|
GensonBuilder |
useFields(boolean enabled)
If true, fields will be used when no getter/setter is available, except if you specified
that no getter/setter should be used with
useMethods(boolean) , in
that case only fields will be used. |
GensonBuilder |
useFields(boolean enabled,
VisibilityFilter visibility) |
GensonBuilder |
useIndentation(boolean indent)
If true outputed json will be indented using two spaces, otherwise (by default) all is
printed on same line.
|
GensonBuilder |
useMetadata(boolean metadata) |
GensonBuilder |
useMethods(boolean enabled)
If true, getters and setters would be used during serialization/deserialization in favor
of fields.
|
GensonBuilder |
useMethods(boolean enabled,
VisibilityFilter visibility) |
GensonBuilder |
useRuntimePropertyFilter(RuntimePropertyFilter filter) |
GensonBuilder |
useRuntimeType(boolean enabled)
If true the concrete type of the serialized object will always be used.
|
GensonBuilder |
useStrictDoubleParse(boolean strictDoubleParse) |
GensonBuilder |
with(BeanMutatorAccessorResolver... resolvers)
Register additional BeanMutatorAccessorResolver that will be used before the standard
ones.
|
GensonBuilder |
with(PropertyNameResolver... resolvers)
Registers the specified resolvers in the order they were defined and before the standard
ones.
|
GensonBuilder |
withBeanPropertyFactory(BeanPropertyFactory... factories)
Allows you to register new BeanPropertyFactory responsible of creating BeanProperty
accessors, mutators and BeanCreators.
|
GensonBuilder |
withBundle(GensonBundle... bundles)
Register some genson bundles.
|
GensonBuilder |
withClassLoader(ClassLoader loader)
Override the default classloader
|
GensonBuilder |
withContextualFactory(ContextualFactory<?>... factories)
ContextualFactory is actually in a beta status, it will not be removed, but might be
refactored.
|
<T> GensonBuilder |
withConverter(Converter<T> converter,
Class<? extends T> type)
Register converter by mapping it to type argument.
|
<T> GensonBuilder |
withConverter(Converter<T> converter,
GenericType<? extends T> type)
Register converter by mapping it to the parameterized type of type argument.
|
GensonBuilder |
withConverterFactory(ChainedFactory chainedFactory)
A ChainedFactory provides a way to use custom Converters that have access to the default Converters.
|
GensonBuilder |
withConverterFactory(Factory<? extends Converter<?>> factory)
Registers converter factories.
|
GensonBuilder |
withConverters(Converter<?>... converter)
Registers converters mapping them to their corresponding parameterized type.
|
<T> GensonBuilder |
withDeserializer(Deserializer<T> deserializer,
Class<? extends T> type) |
<T> GensonBuilder |
withDeserializer(Deserializer<T> deserializer,
GenericType<? extends T> type) |
GensonBuilder |
withDeserializerFactory(Factory<? extends Deserializer<?>> factory)
Registers deserializer factories.
|
GensonBuilder |
withDeserializers(Deserializer<?>... deserializer) |
<T> GensonBuilder |
withSerializer(Serializer<T> serializer,
Class<? extends T> type) |
<T> GensonBuilder |
withSerializer(Serializer<T> serializer,
GenericType<? extends T> type) |
GensonBuilder |
withSerializerFactory(Factory<? extends Serializer<?>> factory)
Registers serializer factories.
|
GensonBuilder |
withSerializers(Serializer<?>... serializer) |
GensonBuilder |
wrapRootValues(String inputKey,
String outputKey)
Will wrap all the root objects under outputKey during serializaiton and unwrap the content under
inputKey during deserializaiton.
|
public GensonBuilder addAlias(String alias, Class<?> forClass)
ClassMetadataConverter
for more
metadata. If you add an alias, it will automatically enable the class metadata feature,
as if you used useClassMetadata(boolean)
.alias
- forClass
- public GensonBuilder withConverters(Converter<?>... converter)
converter
- public <T> GensonBuilder withConverter(Converter<T> converter, Class<? extends T> type)
converter
- to registertype
- of objects this converter handlespublic <T> GensonBuilder withConverter(Converter<T> converter, GenericType<? extends T> type)
converter
- to registertype
- of objects this converter handlespublic GensonBuilder withSerializers(Serializer<?>... serializer)
public <T> GensonBuilder withSerializer(Serializer<T> serializer, Class<? extends T> type)
public <T> GensonBuilder withSerializer(Serializer<T> serializer, GenericType<? extends T> type)
public GensonBuilder withDeserializers(Deserializer<?>... deserializer)
public <T> GensonBuilder withDeserializer(Deserializer<T> deserializer, Class<? extends T> type)
public <T> GensonBuilder withDeserializer(Deserializer<T> deserializer, GenericType<? extends T> type)
public GensonBuilder withConverterFactory(Factory<? extends Converter<?>> factory)
factory
- to registerpublic GensonBuilder withSerializerFactory(Factory<? extends Serializer<?>> factory)
factory
- to registerpublic GensonBuilder withDeserializerFactory(Factory<? extends Deserializer<?>> factory)
factory
- to registerpublic GensonBuilder withContextualFactory(ContextualFactory<?>... factories)
public GensonBuilder withConverterFactory(ChainedFactory chainedFactory)
public GensonBuilder withBeanPropertyFactory(BeanPropertyFactory... factories)
public GensonBuilder withBundle(GensonBundle... bundles)
builder.withBundle(new JAXBExtension());All bundles should be registered before any other customization.
GensonBundle
public GensonBuilder withClassLoader(ClassLoader loader)
loader
- classloader which will be used to load classes while deserializingpublic GensonBuilder set(BeanMutatorAccessorResolver resolver)
BeanMutatorAccessorResolver
by the specified one.resolver
- public GensonBuilder set(PropertyNameResolver resolver)
PropertyNameResolver
by the specified one.resolver
- public GensonBuilder with(BeanMutatorAccessorResolver... resolvers)
resolvers
- public Map<Type,Serializer<?>> getSerializersMap()
public Map<Type,Deserializer<?>> getDeserializersMap()
public ClassLoader getClassLoader()
public GensonBuilder with(PropertyNameResolver... resolvers)
resolvers
- public GensonBuilder rename(String field, String toName)
public GensonBuilder rename(Class<?> fieldOfType, String toName)
public GensonBuilder rename(String field, Class<?> fromClass, String toName)
public GensonBuilder rename(String field, String toName, Class<?> fieldOfType)
public GensonBuilder rename(String field, Class<?> fromClass, String toName, Class<?> ofType)
public GensonBuilder exclude(String field)
public GensonBuilder exclude(Class<?> fieldOfType)
public GensonBuilder exclude(String field, Class<?> fromClass)
public GensonBuilder exclude(String field, Class<?> fromClass, Class<?> ofType)
public GensonBuilder include(String field)
public GensonBuilder include(Class<?> fieldOfType)
public GensonBuilder include(String field, Class<?> fromClass)
public GensonBuilder include(String field, Class<?> fromClass, Class<?> ofType)
public GensonBuilder setSkipNull(boolean skipNull)
skipNull
- indicates whether null values should be serialized or not.public boolean isSkipNull()
public GensonBuilder setHtmlSafe(boolean htmlSafe)
htmlSafe
- indicates whether serialized data should be html safe.public boolean isHtmlSafe()
public GensonBuilder useClassMetadata(boolean enabled)
ClassMetadataConverter
public GensonBuilder useDateFormat(DateFormat dateFormat)
dateFormat
- public boolean isThrowExceptionOnNoDebugInfo()
public GensonBuilder setThrowExceptionIfNoDebugInfo(boolean throwExcOnNoDebugInfo)
useConstructorWithArguments(boolean)
. If true
an exception will be thrown when a class has been compiled without debug informations.throwExcOnNoDebugInfo
- ASMCreatorParameterNameResolver
public GensonBuilder useMethods(boolean enabled)
useFields(boolean)
. By
default getters, setters and fields will be used.public GensonBuilder useMethods(boolean enabled, VisibilityFilter visibility)
public GensonBuilder settersMustBeVoid(boolean settersMustBeVoid)
public GensonBuilder useFields(boolean enabled)
useMethods(boolean)
, in
that case only fields will be used. By default getters, setters and fields will be used.public GensonBuilder useFields(boolean enabled, VisibilityFilter visibility)
public GensonBuilder useBeanViews(boolean enabled)
BeanView
mechanism will be enabled.public GensonBuilder useRuntimeType(boolean enabled)
enabled
- public GensonBuilder useConstructorWithArguments(boolean enabled)
ASMCreatorParameterNameResolver
.enabled
- setThrowExceptionIfNoDebugInfo(boolean)
public GensonBuilder setFieldFilter(VisibilityFilter propertyFilter)
public GensonBuilder setMethodFilter(VisibilityFilter methodFilter)
public GensonBuilder setConstructorFilter(VisibilityFilter constructorFilter)
public GensonBuilder useStrictDoubleParse(boolean strictDoubleParse)
public GensonBuilder useIndentation(boolean indent)
public GensonBuilder useDateAsTimestamp(boolean enabled)
public GensonBuilder useMetadata(boolean metadata)
public GensonBuilder useByteAsInt(boolean enable)
public GensonBuilder failOnMissingProperty(boolean enable)
enable
- public GensonBuilder useClassMetadataWithStaticType(boolean enable)
class Person { public Address address; }Here if the concrete instance of address is Address then this type will not be serialized as metadata, but if they differ then it is serialized. By default this option is true, all types are serialized.
enable
- public GensonBuilder acceptSingleValueAsList(boolean enable)
public GensonBuilder useDefaultValue(Object value, Class<?> targetType)
public GensonBuilder wrapRootValues(String inputKey, String outputKey)
Genson genson = new GensonBuilder().wrapRootValues("request", "response").create();
// would produce: {"response": {... person properties ...}}
genson.serialize(person);
Person p = genson.deserialize("{\"request\":{...}}", Person.class);
If you need this mechanism only for some types or using different root keys, then you can register JaxbBundle with
wrapRootValues(true) and annotate the specific classes with XmlRootElement.public GensonBuilder failOnNullPrimitive(boolean enabled)
public GensonBuilder useRuntimePropertyFilter(RuntimePropertyFilter filter)
public Genson create()
protected Genson create(Factory<Converter<?>> converterFactory, Map<String,Class<?>> classAliases)
converterFactory
- classAliases
- protected Factory<Converter<?>> createConverterFactory()
ChainedFactory
or if you need to chain
them differently.protected BeanMutatorAccessorResolver createBeanMutatorAccessorResolver()
protected PropertyNameResolver createPropertyNameResolver()
PropertyNameResolver
that are
registered by default. You can also simply replace the default PropertyNameResolver by
setting another one with set(PropertyNameResolver)
.PropertyNameResolver.CompositePropertyNameResolver
, otherwise you will not be
able to add others PropertyNameResolvers using
with(PropertyNameResolver...)
method.protected List<Converter<?>> getDefaultConverters()
protected void addDefaultConverterFactories(List<Factory<? extends Converter<?>>> factories)
factories
- list, is not null.protected void addDefaultContextualFactories(List<ContextualFactory<?>> factories)
protected List<Serializer<?>> getDefaultSerializers()
protected void addDefaultSerializerFactories(List<Factory<? extends Serializer<?>>> serializerFactories)
protected List<Deserializer<?>> getDefaultDeserializers()
protected void addDefaultDeserializerFactories(List<Factory<? extends Deserializer<?>>> deserializerFactories)
protected BeanDescriptorProvider createBeanDescriptorProvider()
BeanDescriptor
instances for
serialization/deserialization of all types that couldn't be handled by standard and
custom converters and converter factories.protected BeanPropertyFactory createBeanPropertyFactory()
protected final PropertyNameResolver getPropertyNameResolver()
protected final BeanMutatorAccessorResolver getMutatorAccessorResolver()
protected final BeanDescriptorProvider getBeanDescriptorProvider()
protected final BeanViewDescriptorProvider getBeanViewDescriptorProvider()
public final boolean isDateAsTimestamp()
Copyright © 2019. All rights reserved.