JsonSerializerexcludes the collections, and that this behavior is controlled by the
deepflag. 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.
exclude()methods we can include and exclude a property referenced by it's path. Let's take the following class as an example:
JsonSerializerwe will get JSON object with 3 keys:
phonesis not serialized by default as it is a collection.
phoneswith the following code:
Phone) is serialized.
*). Wildcard replaces more properties at once. Wildcard can only substitute whole property names, not partials. Here is how it can be used:
name, as all others properties are excluded.
@JSONannotation marks a property (getter or a field) as included by default.
Phonesare integral part of a
Personand that we should always have them serialized. So we can do the following:
JSONannotations 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.
JSONannotation 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 the
workis not serialized as it is not annotated and the class is serialized in
JSONannotation can change the name of the generated keys, e.g.:
InputStreamtype. We could also add the following rule: