UON Serializers
The UonSerializer class is used to serialize POJOs into UON.
When to use it
Use UonSerializer for values that travel in URLs or HTTP headers, where JSON's braces, quotes and colons would need escaping.
UON writes a bean as (name=value,...) and a collection as @(a,b), so the result drops into a query-string or header value with little extra encoding.
For a whole application/x-www-form-urlencoded body, use URL-Encoding, which builds on UON.
Example
import org.apache.juneau.marshall.uon.*;
public class UonSerializerExample {
public static class Person {
public String name = "John Smith";
public int age = 45;
}
public static void main(String[] args) throws Exception {
Person person = new Person();
// Compact, single-line output.
System.out.println(UonSerializer.DEFAULT.write(person));
// Same data, indented for reading.
System.out.println(UonSerializer.DEFAULT_READABLE.write(person));
}
}
The compact form is (age=45,name='John Smith'). Properties are written in alphabetical order.
Options
The following pre-configured serializers are provided for convenience:
| Instance | Output |
|---|---|
UonSerializer.DEFAULT | Compact output; strings are quoted only when they need to be. |
UonSerializer.DEFAULT_READABLE | Indented, multi-line output for people. |
UonSerializer.DEFAULT_ENCODING | URL-encodes characters that are unsafe in a URL. |
For anything else, start from the builder's create() method. The settings readers reach for first:
| Builder method | Effect |
|---|---|
encoding() | URL-encode unsafe characters in the output. |
paramFormat(...) / paramFormatPlain() | Control how top-level values are written when used as URL parameters. |
addBeanTypesUon() | Add _type properties to beans whose runtime type differs from the declared one. |
ws() | Use indentation and newlines. |
keepNullProperties() | Write bean properties whose value is null. |
trimEmptyCollections() / trimEmptyMaps() | Skip empty collections or maps. |
sortCollections() / sortMaps() | Sort collections or map entries before writing. |
The class hierarchy for the builder of this serializer is below. Refer to the builder javadocs for the full set of configurable settings.
See also
- UON Parsers: the reverse direction.
- UON Support
- URL-Encoding Serializers
- Serializers and Parsers