Parser API

Table of Contents
  1. Parser API

    1. The ParserGroup class

  2. Defining a new Parser

1 - Parser API

The parser API is designed to be easily extensible by developers.
If you are writing your own parser, you will typically subclass directly from either {@link org.apache.juneau.parser.ReaderParser} or {@link org.apache.juneau.parser.InputStreamParser}.

1.1 - The ParserGroup class

The {@link org.apache.juneau.parser.ParserGroup} class represents a group of parser registered with the media types they handle.

Features

The ParserGroup class provides the following features:

Refer to {@link org.apache.juneau.parser.ParserGroup} for additional information.

2 - Defining a new Parser

Defining a new parser is quite simple if you subclass directly from {@link org.apache.juneau.parser.ReaderParser} or {@link org.apache.juneau.parser.InputStreamParser}. In each case, you simply need to implement a single method and specify a {@link org.apache.juneau.annotation.Consumes} annotation.

The following example shows a simple parser that converts input streams to images using standard JRE classes.

@Consumes("image/png,image/jpeg") public static class ImageParser extends InputStreamParser { @Override public <T> T parse(InputStream in, ClassMeta<T> type, ParserSession session) throws ParseException, IOException { BufferedImage image = ImageIO.read(in); return (T)image; } }

Parsers that take advantage of the entire {@link org.apache.juneau.CoreApi} interface to be able to parse arbitrary beans and POJOs is considerably more complex and outside the scope of this document. If developing such a parser, the best course of action would be to replicate what occurs in the {@link org.apache.juneau.json.JsonParser} class.