Class BeanMapper
Simplified mapping of objects to/from XML, JSON, and Yaml.
Polymorphism
Polymorphism is supported in a few different ways.
Via Annotations
First, classes annotated with JsonTypeInfo and JsonSubTypes
are properly handled by this mapper.
Via registration
For cases where no annotations are used, you can register one or more
classes (typically interfaces) that can have subclasses with
BeanMapper.BeanMapperBuilder.polymorphicType(Class, Predicate).
When doing so, the classpath will be scanned for matching implementations
and automatically register them as subtypes using their simple class name.
To speed up the discovery process and avoid possible classloader issues,
it is best that you provide a predicate (over fully qualified class names)
to help quickly filter discovered subtypes.
It relies on the "class" property to find the class reference and
(de)serialize sub-types.
Class mapping
When auto-registering subtypes, class short names are used (class name without package name). You can optionally provide in the source the full canonical name of a class to have that class recognized at deserialization time, regardless whether it was registered or not. This can be useful when dynamically adding classes that could not be previously registered for some reason.
While this mapper supports a wide variety of use cases, it is recommended to use a more elaborate serialization tool for more complex needs.
Configurable objects
Objects implementing Configurable indicates that a
separate class dedicated to configuration is used for bean-style mapping.
By default, this mapper will ignore all properties of a configurable class
except for its getConfiguration() method which will be
treated as if annotated with @valid. That
configuration class will be populated without the need for "configuration"
wrapper elements (automatically adds @JsonUnwrapped
This behavior can be turned off with
BeanMapper.BeanMapperBuilder.configurableDetectionDisabled(boolean)
- Since:
- 3.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enum -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BeanMapperA build mapper initialized with default settings. -
Method Summary
Modifier and TypeMethodDescriptionvoidassertWriteRead(@NonNull Object obj, BeanMapper.Format... formats) Throws aBeanExceptionif the given object is not equal to itself after writing it to specified formats and back.static BeanMapper.BeanMapperBuilderbuilder()MultiValuedMap<Class<?>, Class<?>> <T> Tread(@NonNull Class<T> type, @NonNull Reader reader, @NonNull BeanMapper.Format format) Reads an XML, JSON, or Yaml source and map it into a new object of the given type.<T> Tread(T object, @NonNull Reader reader, @NonNull BeanMapper.Format format) Reads an XML, JSON, or Yaml source and map it into an existing object.com.fasterxml.jackson.databind.ObjectMappertoObjectMapper(BeanMapper.Format format) Gets a JacksonObjectMapperfor the given format.voidwrite(@NonNull Object object, @NonNull Writer writer, @NonNull BeanMapper.Format format) Write the given object as XML, JSON, or Yaml.
-
Field Details
-
DEFAULT
A build mapper initialized with default settings.
-
-
Method Details
-
getPolymorphicTypes
-
write
public void write(@NonNull @NonNull Object object, @NonNull @NonNull Writer writer, @NonNull @NonNull BeanMapper.Format format) Write the given object as XML, JSON, or Yaml.- Parameters:
object- the object to writewriter- the target to write toformat- the target format to write- Throws:
BeanException- if writing failed
-
read
public <T> T read(@NonNull T object, @NonNull @NonNull Reader reader, @NonNull @NonNull BeanMapper.Format format) Reads an XML, JSON, or Yaml source and map it into an existing object.- Type Parameters:
T- the type of the object to populate- Parameters:
object- the object to populatereader- the source content to readformat- the source format- Returns:
- populated object (same instance)
- Throws:
BeanException- if reading failedjakarta.validation.ConstraintViolationException- on bean validation error
-
read
public <T> T read(@NonNull @NonNull Class<T> type, @NonNull @NonNull Reader reader, @NonNull @NonNull BeanMapper.Format format) Reads an XML, JSON, or Yaml source and map it into a new object of the given type.- Type Parameters:
T- the type of the object returned- Parameters:
type- a class of the expected returned typereader- the source content to readformat- the source format- Returns:
- populated object
- Throws:
BeanException- if reading failedjakarta.validation.ConstraintViolationException- on bean validation error
-
assertWriteRead
Throws aBeanExceptionif the given object is not equal to itself after writing it to specified formats and back. Not specifying any format is equivalent to testing them all (XML, JSON, and Yaml).- Parameters:
obj- the objectformats- zero or more formats
-
toObjectMapper
Gets a JacksonObjectMapperfor the given format.- Parameters:
format- format for which to get the mapper- Returns:
- Jackson ObjectMapper
-
builder
-