Parser API

##### Table of Contents

## 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:

- Finds parsers based on HTTP `Content-Type` header values.
- Sets common properties on all parsers in a single method call.
- Locks all parsers in a single method call.
- Clones existing groups and all parsers within the group in a single method call.

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 .

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

/** Parser for converting byte streams to images */ public class ImageParser extends InputStreamParser { /** * Constructor. * @param propertyStore The property store containing all the settings for this object. */ public ImageParser(PropertyStore propertyStore) { super(propertyStore, "image/png", "image/jpeg"); } @Override /* Parser */ protected <T> T doParse(ParserSession session, ClassMeta<T> type) throws Exception { return (T)ImageIO.read(session.getInputStream()); } }

Parsers that take advantage of the entire {@link org.apache.juneau.CoreObject} 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.