Skip to main content

HTML Serializers

The HtmlSerializer class is used to serialize POJOs into HTML. The HtmlDocSerializer class is the same but wraps the serialized POJO inside a document template consisting of header, nav, aside, and footer sections.

The class hierarchy for the builder of this serializer is:

Context.BuilderMarshallingContextable.BuilderMarshallingTraverseContext.BuilderSerializer.BuilderWriterSerializer.BuilderXmlSerializer.BuilderHtmlSerializer.Builder

Refer to the builder javadocs for configurable settings.

The following pre-configured serializers are provided for convenience:

HtmlSerializerDEFAULTDEFAULT_SQDEFAULT_SQ_READABLEHtmlDocSerializerDEFAULT

Data type representations​

The following tables are the complete reference for how each POJO datatype is rendered by HtmlSerializer. They mirror how the same data structures are represented in JSON. See HTML Methodology for a conceptual overview of the mapping rules.

Maps​

Maps and beans are represented as tables. The _type attribute is added to differentiate between objects (Maps/beans) and arrays (arrays/Collections).

Data typeJSON exampleHTML
Map
{
k1: 'v1',
k2: null
}
<table _type='object'>
<tr>
<td>k1</td>
<td>v1</td>
</tr>
<tr>
<td>k2</td>
<td><null></null></td>
</tr>
</table>
Map
{
k1: 123,
k2: 1.23,
k3: null
}
<table _type='object'>
<tr>
<td>k1</td>
<td><number>123</number></td>
</tr>
<tr>
<td>k2</td>
<td><number>1.23</number></td>
</tr>
<tr>
<td>k3</td>
<td><null></null></td>
</tr>
</table>
Map
{
k1: 'v1',
k2: 123,
k3: 1.23,
k4: true,
k5: null
}
<table _type='object'>
<tr>
<td>k1</td>
<td>v1</td>
</tr>
<tr>
<td>k2</td>
<td><number>123</number></td>
</tr>
<tr>
<td>k3</td>
<td><number>1.23</number></td>
</tr>
<tr>
<td>k4</td>
<td><boolean>true</boolean></td>
</tr>
<tr>
<td>k5</td>
<td><null></null></td>
</tr>
</table>

Arrays​

Collections and arrays are represented as ordered lists.

Data typeJSON exampleHTML
String[]
['foo', null]
<ul>
<li>foo</li>
<li><null></null></li>
</ul>
Number[]
[123, 1.23, null]
<ul>
<li><number>123</number></li>
<li><number>1.23</number></li>
<li><null></null></li>
</ul>
Object[]
['foo', 123, 1.23, true, null]
<ul>
<li>foo</li>
<li><number>123</number></li>
<li><number>1.23</number></li>
<li><boolean>true</boolean></li>
<li><null></null></li>
</ul>
String[][]
[['foo', null], null]
<ul>
<li>
<ul>
<li>foo</li>
<li><null></null></li>
</ul>
</li>
<li><null></null></li>
</ul>
int[]
[123]
<ul>
<li><number>123</number></li>
</ul>
boolean[]
[true]
<ul>
<li><boolean>true</boolean></li>
</ul>

Collections​

Collections serialize identically to their array equivalents.

Data typeJSON exampleHTML
List<String>
['foo', null]
<ul>
<li>foo</li>
<li><null></null></li>
</ul>
List<Number>
[123, 1.23, null]
<ul>
<li><number>123</number></li>
<li><number>1.23</number></li>
<li><null></null></li>
</ul>
List<Object>
['foo', 123, 1.23, true, null]
<ul>
<li>foo</li>
<li><number>123</number></li>
<li><number>1.23</number></li>
<li><boolean>true</boolean></li>
<li><null></null></li>
</ul>

Beans​

Data typeJSON exampleHTML
class MyBean {
public String a;
public int b;
public Object c; // String value
public Object d; // Integer value
public MyBean2 e;
public String[] f;
public int[] g;
}

class MyBean2 {
String h;
}
{
a: 'foo',
b: 123,
c: 'bar',
d: 456,
e: {
h: 'baz'
},
f: ['qux'],
g: [789]
}
<table _type='object'>
<tr>
<td>a</td>
<td>foo</td>
</tr>
<tr>
<td>b</td>
<td><number>123</number></td>
</tr>
<tr>
<td>c</td>
<td>bar</td>
</tr>
<tr>
<td>d</td>
<td><number>456</number></td>
</tr>
<tr>
<td>e</td>
<td>
<table _type='object'>
<tr>
<td>h</td>
<td>baz</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>f</td>
<td>
<ul>
<li>qux</li>
</ul>
</td>
</tr>
<tr>
<td>g</td>
<td>
<ul>
<li><number>789</number></li>
</ul>
</td>
</tr>
</table>

Beans with Map properties​

Data typeJSON exampleHTML
class MyBean {
public Map a;
public Map b;
public Map c;
}
{
a: { k1: 'foo' },
b: { k2: 123 },
c: {
k3: 'bar',
k4: 456,
k5: true,
k6: null
}
}
<table _type='object'>
<tr>
<td>a</td>
<td>
<table _type='object'>
<tr>
<td>k1</td>
<td>foo</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>b</td>
<td>
<table _type='object'>
<tr>
<td>k2</td>
<td><number>123</number></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>c</td>
<td>
<table _type='object'>
<tr>
<td>k3</td>
<td>bar</td>
</tr>
<tr>
<td>k4</td>
<td><number>456</number></td>
</tr>
<tr>
<td>k5</td>
<td><boolean>true</boolean></td>
</tr>
<tr>
<td>k6</td>
<td><null></null></td>
</tr>
</table>
</td>
</tr>
</table>