Skip to: Site menu | Main content

Jettison

A JSON StAX Implementation

User's Guide Print

Conventions

Jettison includes two JSON/XML mapping conventions. These are detailed below.

Mapped Convention

The best way to demonstrate the Mapped convention is a short example:

Using the mapped convention this can be turned into:

As you can see we've mapped the http://acme.come namespace to the "acme." prefix. This creates a very readable format.

The convention follows the following rules:

1. An element with no characters or child elements is represented by { "element" : "" }

2. No namespaces declarations are ever written

3. An element with multiple child elements of the same name is represented by an array

In JSON this becomse:

BadgerFish

For information on the BadgerFish convention, check out the specification

APIs

Jettison provides some of the most popular XML handling APIs for handling JSON documents.

STaX

StAX (STreaming Api for Xml processing) is a Java based API for pull-parsing XML (and now JSON). Please refer to the specification for more details.

Writing

Here's a simple example of using STaX API to write JSON documents:

You can expect the following result if you are using Mapped convention

and

for BadgerFish convention.

Type conversion

Availability

Icon

Available since 1.1

When writing JSON documents, Jettison tries to convert objects to their primitive representation and not to treat everything as a String. For example the following snippet:

will print

instead of

This works fine for most use cases, but some users may want to have control over this process. Jettison provides customizable type conversion mechanism. Beside default type converter used, you can use SimpleConverter which will treat everything as a String or write your own. For example,

will produce

Parsing

In a similar manner you can parse JSON documents using STaX API: