Serializers

One of the goals of Juneau was to make serialization as simple as possible. In a single line of code, you should be able to serialize and parse most POJOs. Despite this simplicity, Juneau provides lots of extensibility and configuration properties for tailoring how POJOs are serialized and parsed.

The built-in serializers in Juneau are fast, efficient, and highly configurable. They work by serializing POJOs directly to streams instead of using intermediate Document Object Model objects.

In most cases, you can serialize objects in one line of code by using one of the default serializers:

// A simple bean public class Person { public String name = "John Smith"; public int age = 21; } // Serialize to JSON, XML, or HTML Person p = new Person(); // Produces: // "{\"name\":\"John Smith\",\"age\":21}" String json = JsonSerializer. DEFAULT.serialize(p); // Produces: // "{name:'John Smith',age:21}" String json = SimpleJsonSerializer. DEFAULT.serialize(p); // Produces: // <object> // <name>John Smith</name> // <age>21</age> // </object> String xml = XmlSerializer. DEFAULT.serialize(p); // Produces: // <table> // <tr><th>key</th><th>value</th></tr> // <tr><td>name</td><td>John Smith</td></tr> // <tr><td>age</td><td>21</td></tr> // </table> String html = HtmlSerializer. DEFAULT.serialize(p); // Produces: // "(name='John Smith',age=21)" String uon = UonSerializer. DEFAULT.serialize(p); // Produces: // "name='John+Smith'&age=21" String urlencoding = UrlEncodingSerializer. DEFAULT.serialize(p); // Produces: // 82 A4 6E 61 6D 65 AA 4A 6F 68 6E 20 53 6D 69 74 68 A3 61 67 65 15 byte[] b = MsgPackSerializer. DEFAULT.serialize(p);

In addition to the default serializers, customized serializers can be created using various built-in options:

// 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 JsonSerializer serializer = JsonSerializer. DEFAULT.builder().sq().build(); // Serialize a POJO to JSON String json = serializer.serialize(someObject);

Default serialization support is provided for Java primitives, `Maps`, `Collections`, beans, and arrays.
Extensible support for other data types such as `Calendars`, `Dates`, `Iterators` is available through the use of POJO swaps (described later).

The class hierarchy for the serializers (excluding specialized subclasses) are:

- {@link oaj.serializer.Serializer}
  - {@link oaj.serializer.WriterSerializer}
    - {@link oaj.json.JsonSerializer}
      - {@link oaj.json.JsonSchemaSerializer}
      - {@link oaj.json.SimpleJsonSerializer}
    - {@link oaj.plaintext.PlainTextSerializer}
    - {@link oaj.jena.RdfSerializer}
      - {@link oaj.jena.N3Serializer}
      - {@link oaj.jena.NTripleSerializer}
      - {@link oaj.jena.RdfXmlSerializer}
      - {@link oaj.jena.RdfXmlAbbrevSerializer}
      - {@link oaj.jena.TurtleSerializer}
    - {@link oaj.uon.UonSerializer}
      - {@link oaj.oapi.OpenApiSerializer}
      - {@link oaj.urlencoding.UrlEncodingSerializer}
    - {@link oaj.xml.XmlSerializer}
      - {@link oaj.html.HtmlSerializer}
        - {@link oaj.html.HtmlSchemaSerializer}
        - {@link oaj.html.HtmlStrippedDocSerializer}
          - {@link oaj.html.HtmlDocSerializer}
            - {@link oaj.html.HtmlSchemaDocSerializer}
      - {@link oaj.soap.SoapXmlSerializer}
      - {@link oaj.xml.XmlDocSerializer}
      - {@link oaj.xmlschema.XmlSchemaSerializer}
        - {@link oaj.xmlschema.XmlSchemaDocSerializer}
  - {@link oaj.serializer.OutputStreamSerializer}
    - {@link oaj.jso.JsoSerializer}
    - {@link oaj.msgpack.MsgPackSerializer}