Complex Data Types
The Juneau parsers have the ability to parse into complex data types that consist of multidimensional arrays and nested
Maps
and Collections
using the methods below:
Arrays are simple enough and can be constructed using the first method:
String json = "[1,2,3]";
int[] array = Json.to(json, int[].class);
For data types consisting of nested Collections
and Maps
such as Map<String,List<MyBean>>
, you need to use the second parse method that
allows you to define the parameter types of the Collections
classes.
String json = "{foo:[{bar:'baz'}]}";
TreeMap map = Json.to(
json, // Input being parsed.
TreeMap.class, // Top-level data type.
String.class, // Key type of map.
LinkedList.class, // Value type of map.
MyBean.class // Value type of list.
);
Collection
classes are assumed to be followed by zero or one objects indicating the element type.
Map
classes are assumed to be followed by zero or two meta objects indicating the key and value types.
The arguments can be arbitrarily long to indicate arbitrarily complex data structures.
Similar methods for converting to complex types can be found on the RequestContent and RequestHttpPart classes, and the BeanSession.convertToType(Object,Type,Type...) method.