{updated-8.2.0} Parsing into Generic Models

The Juneau parsers are not limited to parsing back into the original bean classes. If the bean classes are not available on the parsing side, the parser can also be used to parse into a generic model consisting of Maps, Collections, and primitive objects.

You can parse into any Map type (e.g. HashMap, TreeMap), but using {@link oaj.collections.OMap} is recommended since it has many convenience methods for converting values to various types. The same is true when parsing collections. You can use any Collection (e.g. HashSet, LinkedList) or array (e.g. Object[], String[], String[][]), but using {@link oaj.collections.OList} is recommended.

When the map or list type is not specified, or is the abstract Map, Collection, or List types, the parser will use OMap and OList by default.

For example, given the following JSON:

{ id: 1, name: 'John Smith', uri: 'http://sample/addressBook/person/1', addressBookUri: 'http://sample/addressBook', birthDate: '1946-08-12T00:00:00Z', addresses: [ { uri: 'http://sample/addressBook/address/1', personUri: 'http://sample/addressBook/person/1', id: 1, street: '100 Main Street', city: 'Anywhereville', state: 'NY', zip: 12345, isCurrent: true } ] }

We can parse this into a generic OMap:

// Parse JSON into a generic POJO model. OMap omap = JsonParser.DEFAULT.parse(json, OMap.class); // Convert it back to JSON. String json = SimpleJsonSerializer.DEFAULT_READABLE.serialize(omap);

What we end up with is the exact same output. Even the numbers and booleans are preserved because they are parsed into Number and Boolean objects when parsing into generic models.

{ id: 1, name: 'John Smith', uri: 'http://sample/addressBook/person/1', addressBookUri: 'http://sample/addressBook', birthDate: '1946-08-12T00:00:00Z', addresses: [ { uri: 'http://sample/addressBook/address/1', personUri: 'http://sample/addressBook/person/1', id: 1, street: '100 Main Street', city: 'Anywhereville', state: 'NY', zip: 12345, isCurrent: true } ] }

Once parsed into a generic model, various convenience methods are provided on the OMap and OList classes to retrieve values:

// Parse JSON into a generic POJO model. OMap omap = JsonParser.DEFAULT.parse(json, OMap.class); // Get some simple values. String name = omap.getString("name"); int id = omap.getInt("id"); // Get a value convertable from a String. URI uri = omap.get(URI.class, "uri"); // Get a value using a swap. TemporalCalendarSwap swap = new TemporalCalendarSwap.IsoInstant(); Calendar birthDate = omap.get(swap, "birthDate"); // Get the addresses. OList addresses = omap.getList("addresses"); // Get the first address and convert it to a bean. Address address = addresses.get(Address.class, 0);

As a general rule, parsing into beans is often more efficient than parsing into generic models. And working with beans is often less error prone than working with generic models.