Skip navigation links

Package org.apache.juneau.parser

Parser API

See: Description

Package org.apache.juneau.parser Description

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 ReaderParser or InputStreamParser.

1.1 - The ParserGroup class

The 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 ParserGroup for additional information.

2 - Defining a new Parser

Defining a new parser is quite simple if you subclass directly from ReaderParser or 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 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 JsonParser class.

Skip navigation links

Copyright © 2017 Apache. All rights reserved.