Class JsonSerializer

All Implemented Interfaces:
AnnotationProvider, JsonMetaProvider
Direct Known Subclasses:
Json5Serializer, JsonSchemaSerializer, JsonSerializer.Readable, JsonSerializer.ReadableSafe

public class JsonSerializer extends WriterSerializer implements JsonMetaProvider
Serializes POJO models to JSON.
Media types

Handles Accept types: application/json, text/json

Produces Content-Type types: application/json

Description

The conversion is as follows...

  • Maps (e.g. HashMaps, TreeMaps) are converted to JSON objects.
  • Collections (e.g. HashSets, LinkedLists) and Java arrays are converted to JSON arrays.
  • Strings are converted to JSON strings.
  • Numbers (e.g. Integer, Long, Double) are converted to JSON numbers.
  • Booleans are converted to JSON booleans.
  • nulls are converted to JSON nulls.
  • arrays are converted to JSON arrays.
  • beans are converted to JSON objects.

The types above are considered "JSON-primitive" object types. Any non-JSON-primitive object types are transformed into JSON-primitive object types through ObjectSwaps associated through the BeanContext.Builder.swaps(Class...) method. Several default transforms are provided for transforming Dates, Enums, Iterators, etc...

This serializer provides several serialization options. Typically, one of the predefined DEFAULT serializers will be sufficient. However, custom serializers can be constructed to fine-tune behavior.

Behavior-specific subclasses

The following direct subclasses are provided for convenience:

Example:

// Use one of the default serializers to serialize a POJO String json = JsonSerializer.DEFAULT.serialize(someObject); // Create a custom serializer for lax syntax using single quote characters JsonSerializer serializer = JsonSerializer.create().simple().sq().build(); // Clone an existing serializer and modify it to use single-quotes serializer = JsonSerializer.DEFAULT.copy().sq().build(); // Serialize a POJO to JSON String json = serializer.serialize(someObject);

Notes:
  • This class is thread safe and reusable.
See Also: