POJO Categories
In general, Juneau allows for marshalling for a wide variety of POJO types including:
The following chart shows POJOs categorized into groups and whether they can be serialized or parsed:
General POJO serialization/parsing support
| Type | Description | Can serialize? | Can parse? |
|---|---|---|---|
| Java primitives and primitive objects | |||
| Standard Java primitive types ( | yes | yes |
| Java Collections Framework objects, Java arrays, Java Optionals | |||
| With standard keys/values | yes | yes |
| With non-standard keys/values | yes | no |
| Streamable sequence types | yes | no |
| Date/time and duration types | yes | yes |
| Java Beans | |||
| Standard beans | Beans with one or more properties defined by public getters or public fields. | yes | yes |
| Non-standard or partial beans | True beans that have one or more properties defined by getter and setter methods but whose property
types include non-standard, raw-streamed, or toString-only values. | yes | no |
| Virtual beans | Unimplemented bean interfaces with properties of standard, collection, or swapped types. | yes | yes |
| Read-only beans | Beans with readable properties but no setters and no constructor-arg mapping. Equivalent to standard beans but parse-only via a swapped form is not recoverable. | yes | no |
| Java Records | Java record classes ( | yes | yes |
| Builder-based beans | Classes that are constructed via a separate builder class. The framework auto-detects a builder when
the object class has a public static | yes | yes |
| Swapped objects Objects that are not directly serializable but have ObjectSwaps associated with them.
The purpose of a POJO swap is to convert an object to another object that is easier to serialize and parse.
| |||
| 2-way swapped | Swapped to a primitive/string, standard collection, or standard bean form — reconstruction from the wire form is possible.
| yes | yes |
| 1-way swapped | Swapped to a simpler form where reconstruction from the wire form is not possible. | yes | no |
Readers and InputStreamsContents are serialized directly to the output stream or writer. Typically used for low-level language-specific replacement of POJOs using per-Media-Type POJO swaps. | |||
| Raw Reader and InputStream objects. Contents are written directly; no structural wrapping. | yes | no |
| Non-serializable objects with standard methods for converting to a serializable form | |||
| 2-way convertible | Classes with a method that converts it to a serializable form:
And a method that converts it back:
Examples:
| yes | yes |
| 1-way convertible | Classes that only have a method to convert to a serializable form:
| yes | no |
| All other objects | Anything that doesn't fall into one of the groups above is simply converted to | yes | no |
Serializers are designed to work on tree-shaped POJO models.
These are models where there are no referential loops (e.g. leaves with references to nodes, or nodes in one branch
referencing nodes in another branch).
There is a serializer setting detectRecursions to look for and handle these kinds of loops (by setting these
references to null) but it is not enabled by default since it introduces a moderate performance penalty.
POJOs convertible to/from Strings
A separate category exists for POJOs that can be converted to and from Strings. These are used in places such as:
- Serializing of POJOs to Strings in the REST client API when no serializers are registered.
- Parsing of POJOs from Strings in the REST server API for
text/plainrequests wheretext/plainis not already mapped to an existing serializer.
As a general rule, all POJOs are converted to Strings using the toString() method.
However, there is one exception:
POJOs are convertible from Strings using any of the following (matched in the specified order):
- Any of the following public static non-deprecated methods:
create(String)fromString(String)fromValue(String)valueOf(String)parse(String)parseString(String)forName(String)forString(String)
- Has a public constructor that takes in a
String.
Exceptions exist for the following classes:
_ with -."null" are interpreted as null values.void.class) - Uses the primitive wrapper classes for instantiating from Strings.POJOs convertible to/from other types
POJOs are also converted to various other types in places such as the Open-API serializers and parsers.
In this section, the type being converted to will be referred to as X.
POJOs are considered convertible from X if it has any of the following (matched in the specified order):
- Any any of the following public static non-deprecated methods:
create(X)from*(X)
- Has a public constructor that takes in an
X. - The X class has a public non-static no-arg non-deprecated method called
to*().
POJOs are considered convertible from X if any of the reverse of above are true.