{8.1.3-updated} Configurable Properties

Serializers and parsers have a wide variety of configurable properties. They all extend from the {@link oaj.BeanContextBuilder} class that allows you to easily construct new instances from scratch or build upon existing instances. For example, the following code shows how to configure a JSON serializer:

WriterSerializer s = JsonSerializer .create() // Create a JsonSerializerBuilder .simple() // Simple mode .ws() // Use whitespace .sq() // Use single quotes .build(); // Create a JsonSerializer

Configurable settings can also be set declaratively. The following produces the same serializer.

WriterSerializer s = JsonSerializer .create() .set(JSON_simpleMode, true) .set(WSERIALIZER_useWhitespace, true) .set(WSERIALIZER_quoteChar, "'") .build();

However, each of the serializers and parsers already contain reusable instances with common configurations. For example, JSON has the following predefined reusable serializers and parsers:

These can be used directly, as follows:

// Serialize a POJO to LAX JSON. String json = SimpleJsonSerializer.DEFAULT.serialize(myPojo);

For performance reasons, serializers and parsers are immutable. However, they can be 'copied' and modified using the builder() method.

// Clone and customize an existing serializer. WriterSerializer s = SimpleJsonSerializer.DEFAULT .builder() // Create a new builder with copied settings. .quoteChar('"') // Use a different quote character. .build();

Configurable properties can be set globally using either system properties or environment variables.
For example, the WSERIALIZER_useWhitespace variable resolves to the string "WriterSerializer.useWhitespace.b". This setting can be enabled by setting the system property "WriterSerializer.useWhitespace" or environment variables "WriterSerializer_useWhitespace" or "WRITERSERIALIZER_USEWHITESPACE" to "true".