Fine-Tuning
So far we have seen that JsonSerializer
excludes the collections, and that this behavior is controlled by the deep
flag. There is more sophisticated way on how to configure what to serialize. Serialization process can be fine-tuned: properties can be included and excluded. There are several ways how to do this.
Include/Exclude methods
By using include()
and exclude()
methods we can include and exclude a property referenced by it's path. Let's take the following class as an example:
If we serialize this class using default JsonSerializer
we will get JSON object with 3 keys: name
, home
and work
. Property phones
is not serialized by default as it is a collection.
We can include phones
with the following code:
In the same way we can exclude a property. For example:
Resulting JSON in this case would also be a map with 3 keys: name
, home
and phones
.
Of course, you can specify nested include/exclude paths, like:
This time we changed how the inner object (Phone
) is serialized.
Include/exclude paths can contain a wildcard (*
). Wildcard replaces more properties at once. Wildcard can only substitute whole property names, not partials. Here is how it can be used:
Resulting JSON map this time contains just one key: name
, as all others properties are excluded.
JSON Annotation
Using include/exclude methods can be cumbersome if done frequently. Json provides a way to express these rules using annotation. The @JSON
annotation marks a property (getter or a field) as included by default.
In above example we may say that Phones
are integral part of a Person
and that we should always have them serialized. So we can do the following:
That's it! But wait, that's not all :) Json supports two ways how a class can be annotated:
In the default mode,
JSON
annotations simply defines additional properties that have to be included. All other properties, that are not marked with an annotation, are also included according to the rules. This mode is usually used to include collection properties, that are excluded by default.In the strict mode,
JSON
annotation defines only properties that have to be included. All other properties, that are not marked with an annotation, are not included, even though they should be according to the rules. Strict mode is enabled by annotating the class with the annotation and setting thestrict
element totrue
:
Here property work
is not serialized as it is not annotated and the class is serialized in strict
mode.
Custom key names
Furthermore, JSON
annotation can change the name of the generated keys, e.g.:
We have changed the name of the home
property to home_address
.
Custom JSON Annotation
An awesome feature is that it is possible to set custom JSON annotation. If you do not want to use default annotation from Jodd JSON, then just create your own annotation and register it. You don't have to copy all annotation fields, just those that you really need.
Custom annotations is a great way how you can integrate Json with the existing codebase.
Excluding types
We can also exclude properties of certain type or that matches certain type name wildcard pattern. Sometimes we need to serialize complex beans that contain properties that are meaningless for the serialization, like streams. We can exclude such properties from getting serialized:
This will exclude properties that are of InputStream
type. We could also add the following rule:
where the whole package gets excluded.
Last updated