Class Parser
- All Implemented Interfaces:
- AnnotationProvider
- Direct Known Subclasses:
- InputStreamParser,- Parser.Null,- ParserSet.Inherit,- ParserSet.NoInherit,- ReaderParser
Valid data conversions
Parsers can parse any parsable POJO types, as specified in the POJO Categories.
Some examples of conversions are shown below...
| Data type | Class type | JSON example | XML example | Class examples | 
|---|---|---|---|---|
| object | Maps, Java beans | {name: | HashMap, TreeMap<String,Integer> | |
| array | Collections, Java arrays | [1,2,3] | List<Integer>, | |
| number | Numbers | 123 | Integer, Long, Float, | |
| boolean | Booleans | Boolean | ||
| string | CharSequences | String, StringBuilder | 
 In addition, any class types with ObjectSwaps associated with them on the registered
 bean context can also be passed in.
 
 For example, if the TemporalCalendarSwap transform is used to generalize Calendar objects to String
 objects.
 When registered with this parser, you can construct Calendar objects from Strings using the
 following syntax...
 
   Calendar 
 If Object. is specified as the target type, then the parser automatically determines the
 data types and generates the following object types...
 
| JSON type | Class type | 
|---|---|
| object | JsonMap | 
| array | JsonList | 
| number | Number(depending on length and format, could be Integer,Double,Float, etc...) | 
| boolean | Boolean | 
| string | String | 
Notes:
- This class is thread safe and reusable.
See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static classRepresents no Parser.
- 
Field SummaryFields inherited from class org.apache.juneau.ContextCONTEXT_APPLY_FILTERFields inherited from interface org.apache.juneau.AnnotationProviderDEFAULT, DISABLE_ANNOTATION_CACHING
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionbooleanReturnstrue if this parser can handle the specified content type.copy()Creates a builder from this context object.static Parser.Buildercreate()Creates a new builder for this object.static Parser.BuildercreateParserBuilder(Class<? extends Parser> c) Instantiates a builder of the specified parser class.Create a session builder based on the properties defined on this context.<T> TdoParse(ParserSession session, ParserPipe pipe, ClassMeta<T> type) Workhorse method.protected final intDebug output lines.protected final Class<? extends ParserListener>Parser listener.Returns the media types handled based on the values passed to theconsumes constructor parameter.final MediaTypeReturns the first media type handled based on the values passed to theconsumes constructor parameter.Returns a session to use for this context.protected final booleanAuto-close streams.booleanReturnstrue if this parser subclasses fromReaderParser.protected final booleanisStrict()Strict mode.protected final booleanTrim parsed strings.protected final booleanUnbuffered.final <T> TSame asparse(Object, Type, Type...)except optimized for a non-parameterized class.final <T> TParses input into the specified object type.final <T> TSame asparse(Object, Type, Type...)except the type has already been converted into aClassMetaobject.final <T> Tfinal <T> Tfinal <T> Tfinal Object[]Parses the specified array input with each entry in the object defined by theargTypesargument.final <E> Collection<E>parseIntoCollection(Object input, Collection<E> c, Type elementType) Parses the contents of the specified reader and loads the results into the specified collection.final <K,V> Map<K, V> parseIntoMap(Object input, Map<K, V> m, Type keyType, Type valueType) Parses the contents of the specified reader and loads the results into the specified map.protected JsonMapReturns the properties on this bean as a map for debugging.Methods inherited from class org.apache.juneau.BeanContextablegetBeanContextMethods inherited from class org.apache.juneau.ContextcreateBuilder, firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, init, isDebug, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation, toString
- 
Constructor Details- 
ParserConstructor.- Parameters:
- builder- The builder this object.
 
 
- 
- 
Method Details- 
createCreates a new builder for this object.- Returns:
- A new builder.
 
- 
createParserBuilderInstantiates a builder of the specified parser class.Looks for a public static method called create that returns an object that can be passed into a public or protected constructor of the class.- Parameters:
- c- The builder to create.
- Returns:
- A new builder.
 
- 
copyDescription copied from class:ContextCreates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations. 
- 
isReaderParserReturnstrue if this parser subclasses fromReaderParser.- Returns:
- true if this parser subclasses from- ReaderParser.
 
- 
parseParses input into the specified object type.The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps). Examples:ReaderParser parser = JsonParser.DEFAULT ;// Parse into a linked-list of strings. Listlist1 =parser .parse(json , LinkedList.class , String.class );// Parse into a linked-list of beans. Listlist2 =parser .parse(json , LinkedList.class , MyBean.class );// Parse into a linked-list of linked-lists of strings. Listlist3 =parser .parse(json , LinkedList.class , LinkedList.class , String.class );// Parse into a map of string keys/values. Mapmap1 =parser .parse(json , TreeMap.class , String.class , String.class );// Parse into a map containing string keys and values of lists containing beans. Mapmap2 =parser .parse(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:- 
      Use the parse(Object, Class)method instead if you don't need a parameterized map/collection.
 - 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:- null 
- Reader
- CharSequence
- InputStreamcontaining UTF-8 encoded text (or charset defined by- ReaderParser.Builder.streamCharset(Charset)property value).
- byte []- ReaderParser.Builder.streamCharset(Charset)property value).
- Filecontaining system encoded text (or charset defined by- ReaderParser.Builder.fileCharset(Charset)property value).
 
 Stream-based parsers can handle the following input class types:- null 
- InputStream
- byte []
- File
- CharSequencecontaining encoded bytes according to the- InputStreamParser.Builder.binaryFormat(BinaryFormat)setting.
 
- 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:
 
- 
      Use the 
- 
parse- Type Parameters:
- T- The class type of the object being created.
- Parameters:
- input- The input. See- parse(Object, Type, Type...)for details.
- 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.
 
- 
parseSame asparse(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:ReaderParser parser = JsonParser.DEFAULT ;// Parse into a string. Stringstring =parser .parse(json , String.class );// Parse into a bean. MyBeanbean =parser .parse(json , MyBean.class );// Parse into a bean array. MyBean[]beanArray =parser .parse(json , MyBean[].class );// Parse into a linked-list of objects. Listlist =parser .parse(json , LinkedList.class );// Parse into a map of object keys/values. Mapmap =parser .parse(json , TreeMap.class );- Type Parameters:
- T- The class type of the object being created.
- Parameters:
- input- The input. See- parse(Object, Type, Type...)for details.
- type- The object type to create.
- Returns:
- The parsed object.
- Throws:
- ParseException- Malformed input encountered.
- IOException- Thrown by the underlying stream.
 
- 
parse- Type Parameters:
- T- The class type of the object being created.
- Parameters:
- input- The input. See- parse(Object, Type, Type...)for details.
- type- The object type to create.
- Returns:
- The parsed object.
- Throws:
- ParseException- Malformed input encountered.
 
- 
parseSame asparse(Object, Type, Type...)except the type has already been converted into aClassMetaobject.This is mostly an internal method used by the framework. - Type Parameters:
- T- The class type of the object being created.
- Parameters:
- input- The input. See- parse(Object, Type, Type...)for details.
- type- The object type to create.
- Returns:
- The parsed object.
- Throws:
- ParseException- Malformed input encountered.
- IOException- Thrown by the underlying stream.
 
- 
parse- Type Parameters:
- T- The class type of the object being created.
- Parameters:
- input- The input. See- parse(Object, Type, Type...)for details.
- type- The object type to create.
- Returns:
- The parsed object.
- Throws:
- ParseException- Malformed input encountered.
 
- 
createSessionDescription copied from class:ContextCreate a session builder based on the properties defined on this context.Use this method for creating sessions where you want to override basic settings. Otherwise, use Context.getSession()directly.- Overrides:
- createSessionin class- Context
- Returns:
- A new session builder.
 
- 
getSessionDescription copied from class:ContextReturns a session to use for this context.Note that subclasses may opt to return a reusable non-modifiable session. - Overrides:
- getSessionin class- Context
- Returns:
- A new session object.
 
- 
doParsepublic <T> T doParse(ParserSession session, ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException Workhorse method.Subclasses are expected to either implement this method or ParserSession.doParse(ParserPipe, ClassMeta).- Type Parameters:
- T- The class type of the object to create.
- Parameters:
- session- The current session.
- pipe- Where to get the input from.
- type- The class type of the object to create. If- null or- Object., object type is based on what's being parsed. For example, when parsing JSON text, it may return a- class - String ,- Number ,- JsonMap , etc...
- Returns:
- The parsed object.
- Throws:
- IOException- Thrown by underlying stream.
- ParseException- Malformed input encountered.
- ExecutableException- Exception occurred on invoked constructor/method/field.
 
- 
parseIntoMappublic final <K,V> Map<K,V> parseIntoMap(Object input, Map<K, V> m, Type keyType, Type valueType) throws ParseExceptionParses the contents of the specified reader and loads the results into the specified map.Reader must contain something that serializes to a map (such as text containing a JSON object). Used in the following locations: - 
      The various character-based constructors in JsonMap(e.g.JsonMap(CharSequence,Parser)).
 - Type Parameters:
- K- The key class type.
- V- The value class type.
- Parameters:
- input- The input. See- parse(Object, ClassMeta)for supported input types.
- m- The map being loaded.
- keyType- The class type of the keys, or- null to default to- String..- class 
- valueType- The class type of the values, or- null to default to whatever is being parsed.
- Returns:
- The same map that was passed in to allow this method to be chained.
- Throws:
- ParseException- Malformed input encountered.
- UnsupportedOperationException- If not implemented.
 
- 
      The various character-based constructors in 
- 
parseIntoCollectionpublic final <E> Collection<E> parseIntoCollection(Object input, Collection<E> c, Type elementType) throws ParseException Parses the contents of the specified reader and loads the results into the specified collection.Used in the following locations: - 
      The various character-based constructors in JsonList(e.g.JsonList(CharSequence,Parser).
 - Type Parameters:
- E- The element class type.
- Parameters:
- input- The input. See- parse(Object, ClassMeta)for supported input types.
- c- The collection being loaded.
- elementType- The class type of the elements, or- null to default to whatever is being parsed.
- Returns:
- The same collection that was passed in to allow this method to be chained.
- Throws:
- ParseException- Malformed input encountered.
- UnsupportedOperationException- If not implemented.
 
- 
      The various character-based constructors in 
- 
parseArgsParses the specified array input with each entry in the object defined by theargTypesargument.Used for converting arrays (e.g. "[arg1,arg2,...]" ) into anObject[]that can be passed to theMethod.invoke(target, args)method.Used in the following locations: - 
      Used to parse argument strings in the ObjectIntrospector.invokeMethod(Method, Reader)method.
 - Parameters:
- input- The input. Subclasses can support different input types.
- argTypes- Specifies the type of objects to create for each entry in the array.
- Returns:
- An array of parsed objects.
- Throws:
- ParseException- Malformed input encountered.
 
- 
      Used to parse argument strings in the 
- 
getMediaTypesReturns the media types handled based on the values passed to theconsumes constructor parameter.- Returns:
- The list of media types.  Never null .
 
- 
getPrimaryMediaTypeReturns the first media type handled based on the values passed to theconsumes constructor parameter.- Returns:
- The media type.
 
- 
canHandleReturnstrue if this parser can handle the specified content type.- Parameters:
- contentType- The content type to test.
- Returns:
- true if this parser can handle the specified content type.
 
- 
isAutoCloseStreamsAuto-close streams.- Returns:
- true if- InputStreams and- Readers passed into parsers will be closed after parsing is complete.
- See Also:
 
- 
getDebugOutputLinesDebug output lines.- Returns:
- The number of lines of input before and after the error location to be printed as part of the exception message.
- See Also:
 
- 
getListenerParser listener.- Returns:
- Class used to listen for errors and warnings that occur during parsing.
- See Also:
 
- 
isStrictStrict mode.- Returns:
- true if strict mode for the parser is enabled.
- See Also:
 
- 
isTrimStringsTrim parsed strings.- Returns:
- true if string values will be trimmed of whitespace using- String.trim()before being added to the POJO.
- See Also:
 
- 
isUnbufferedUnbuffered.- Returns:
- true if parsers don't use internal buffering during parsing.
- See Also:
 
- 
propertiesDescription copied from class:ContextReturns the properties on this bean as a map for debugging.- Overrides:
- propertiesin class- BeanContextable
- Returns:
- The properties on this bean as a map for debugging.
 
 
-