Class Marshaller

java.lang.Object
org.apache.juneau.marshaller.Marshaller
Direct Known Subclasses:
CharMarshaller, StreamMarshaller

public abstract class Marshaller extends Object
Top-level class for a pairing of a Serializer and Parser into a single class with convenience read/write methods.

The general idea is to combine a single serializer and parser inside a simplified API for reading and writing POJOs.

Examples:

// Using instance. Marshaller json = new Json(); MyPojo myPojo = json.read(string, MyPojo.class); String string = json.write(myPojo);

// Using DEFAULT instance. MyPojo myPojo = Json.DEFAULT.read(string, MyPojo.class); String string = Json.DEFAULT.write(myPojo);

See Also:
  • Constructor Details

    • Marshaller

      protected Marshaller(Serializer s, Parser p)
      Constructor.
      Parameters:
      s - The serializer to use for serializing output.
      Must not be null.
      p - The parser to use for parsing input.
      Must not be null.
  • Method Details

    • getSerializer

      Returns the serializer associated with this marshaller.
      Returns:
      The serializer associated with this marshaller.
    • getParser

      public Parser getParser()
      Returns the parser associated with this marshaller.
      Returns:
      The parser associated with this marshaller.
    • write

      public final void write(Object object, Object output) throws SerializeException, IOException
      Serializes a POJO to the specified output stream or writer.

      Equivalent to calling serializer.createSession().serialize(o, output);

      Parameters:
      object - The object to serialize.
      output - The output object.
      Character-based serializers can handle the following output class types:
      • Writer
      • OutputStream - Output will be written as UTF-8 encoded stream.
      • File - Output will be written as system-default encoded stream.
      • StringBuilder - Output will be written to the specified string builder.

      Stream-based serializers can handle the following output class types:
      Throws:
      SerializeException - If a problem occurred trying to convert the output.
      IOException - Thrown by underlying stream.
    • read

      public final <T> T read(Object input, Type type, Type... args) throws ParseException, IOException
      Parses input into the specified object type.

      The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).

      Examples:

      Marshaller marshaller = Json.DEFAULT; // Parse into a linked-list of strings. List list1 = marshaller .read(json, LinkedList.class, String.class); // Parse into a linked-list of beans. List list2 = marshaller .read(json, LinkedList.class, MyBean.class); // Parse into a linked-list of linked-lists of strings. List list3 = marshaller .read(json, LinkedList.class, LinkedList.class, String.class); // Parse into a map of string keys/values. Map map1 = marshaller .read(json, TreeMap.class, String.class, String.class); // Parse into a map containing string keys and values of lists containing beans. Map map2 = marshaller .read(json, TreeMap.class, String.class, List.class, MyBean.class);

      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 array can be arbitrarily long to indicate arbitrarily complex data structures.

      Notes:
      Type Parameters:
      T - The class type of the object to create.
      Parameters:
      input - The input.
      Character-based parsers can handle the following input class types:
      Stream-based parsers can handle the following input class types:
      type - The object type to create.
      Can be any of the following: ClassMeta, Class, ParameterizedType, GenericArrayType
      args - The type arguments of the class if it's a collection or map.
      Can be any of the following: ClassMeta, Class, ParameterizedType, GenericArrayType
      Ignored if the main type is not a map or collection.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered.
      IOException - Thrown by underlying stream.
      See Also:
    • read

      public final <T> T read(Object input, Class<T> type) throws ParseException, IOException
      Same as read(Object, Type, Type...) except optimized for a non-parameterized class.

      This is the preferred parse method for simple types since you don't need to cast the results.

      Examples:

      Marshaller marshaller = Json.DEFAULT; // Parse into a string. String string = marshaller .read(json, String.class); // Parse into a bean. MyBean bean = marshaller .read(json, MyBean.class); // Parse into a bean array. MyBean[] beanArray = marshaller .read(json, MyBean[].class); // Parse into a linked-list of objects. List list = marshaller .read(json, LinkedList.class); // Parse into a map of object keys/values. Map map = marshaller .read(json, TreeMap.class);

      Type Parameters:
      T - The class type of the object being created.
      Parameters:
      input - The input.
      Character-based parsers can handle the following input class types:
      Stream-based parsers can handle the following input class types:
      type - The object type to create.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered.
      IOException - Thrown by underlying stream.