public abstract class ParserSession extends BeanSession
Parser
.
This class is NOT thread safe. It is typically discarded after one-time use although it can be reused against multiple inputs.
Modifier | Constructor and Description |
---|---|
protected |
ParserSession(Parser ctx,
ParserSessionArgs args)
Create a new session using properties specified in the context.
|
protected |
ParserSession(ParserSessionArgs args)
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
ObjectMap |
asMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
protected Object |
cast(ObjectMap m,
BeanPropertyMeta pMeta,
ClassMeta<?> eType)
Converts the specified
ObjectMap into a bean identified by the |
protected <T> T |
convertAttrToType(Object outer,
String s,
ClassMeta<T> type)
Converts the specified string to the specified type.
|
ParserPipe |
createPipe(Object input)
Wraps the specified input object into a
ParserPipe object so that it can be easily converted into
a stream or reader. |
protected abstract <T> T |
doParse(ParserPipe pipe,
ClassMeta<T> type)
Workhorse method.
|
protected <E> Collection<E> |
doParseIntoCollection(ParserPipe pipe,
Collection<E> c,
Type elementType)
Implementation method.
|
protected <K,V> Map<K,V> |
doParseIntoMap(ParserPipe pipe,
Map<K,V> m,
Type keyType,
Type valueType)
Implementation method.
|
protected ClassMeta<?> |
getClassMeta(String typeName,
BeanPropertyMeta pMeta,
ClassMeta<?> eType)
Give the specified dictionary name, resolve it to a class.
|
protected Method |
getJavaMethod()
Returns the Java method that invoked this parser.
|
ObjectMap |
getLastLocation()
Returns information used to determine at what location in the parse a failure occurred.
|
ParserListener |
getListener()
Returns the listener associated with this session.
|
<T extends ParserListener> |
getListener(Class<T> c)
Returns the listener associated with this session.
|
protected Object |
getOuter()
Returns the outer object used for instantiating top-level non-static member classes.
|
abstract boolean |
isReaderParser()
Returns
ReaderParser . |
protected boolean |
isStrict()
Returns the
Parser.PARSER_strict setting value for this session. |
protected boolean |
isTrimStrings()
Returns the
Parser.PARSER_trimStrings setting value for this session. |
protected <T> void |
onUnknownProperty(ParserPipe pipe,
String propertyName,
BeanMap<T> beanMap,
int line,
int col)
Method that gets called when an unknown bean property name is encountered.
|
<T> T |
parse(Object input,
Class<T> type)
Same as
parse(Object, Type, Type...) except optimized for a non-parameterized class. |
<T> T |
parse(Object input,
ClassMeta<T> type)
Same as
parse(Object, Type, Type...) except the type has already been converted into a ClassMeta
object. |
<T> T |
parse(Object input,
Type type,
Type... args)
Parses input into the specified object type.
|
Object[] |
parseArgs(Object input,
Type[] argTypes)
Parses the specified array input with each entry in the object defined by the
argTypes
argument. |
<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.
|
<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 void |
setCurrentClass(ClassMeta<?> currentClass)
Sets the current class being parsed for proper error messages.
|
protected void |
setCurrentProperty(BeanPropertyMeta currentProperty)
Sets the current bean property being parsed for proper error messages.
|
protected static void |
setName(ClassMeta<?> cm,
Object o,
Object name)
Convenience method for calling the
@NameProperty method on the specified object if it exists. |
protected static void |
setParent(ClassMeta<?> cm,
Object o,
Object parent)
Convenience method for calling the
@ParentProperty method on the specified object if it
exists. |
protected <K> K |
trim(K o)
|
protected String |
trim(String s)
Trims the specified string if
isTrimStrings() returns |
_class, checkForWarnings, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, getArgsClassMeta, getBeanMeta, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getLocale, getMediaType, getStringBuilder, getTimeZone, isBean, isBean, isDebug, isIgnoreUnknownBeanProperties, newBean, newBean, newBeanMap, newBeanMap, newInstance, newInstance, newInstanceFromOuter, object, returnStringBuilder, string, toArray, toBeanMap, toBeanMap
addToCache, addToCache, addWarning, getClassProperty, getFromCache, getInstanceArrayProperty, getInstanceProperty, getLogger, getProperties, getProperty, getProperty, getProperty, getPropertyKeys, getWarnings, hasWarnings, toString
protected ParserSession(Parser ctx, ParserSessionArgs args)
ctx
- The context creating this session object.
The context contains all the configuration settings for this object.args
- Runtime session arguments.protected ParserSession(ParserSessionArgs args)
args
- Runtime session arguments.public ObjectMap asMap()
Session
asMap
in class BeanSession
protected abstract <T> T doParse(ParserPipe pipe, ClassMeta<T> type) throws Exception
T
- The class type of the object to create.pipe
- Where to get the input from.type
- The class type of the object to create.
If Object.class
, object type is based on what's being parsed.
For example, when parsing JSON text, it may return a String
, Number
,
ObjectMap
, etc...Exception
- If thrown from underlying stream, or if the input contains a syntax error or is malformed.public abstract boolean isReaderParser()
ReaderParser
.ReaderParser
.public final ParserPipe createPipe(Object input)
ParserPipe
object so that it can be easily converted into
a stream or reader.input
- The input.
Reader
CharSequence
InputStream
containing UTF-8 encoded text (or whatever the encoding specified by
Parser.PARSER_inputStreamCharset
).
byte []
containing UTF-8 encoded text (or whatever the encoding specified by
Parser.PARSER_inputStreamCharset
).
File
containing system encoded text (or whatever the encoding specified by
Parser.PARSER_fileCharset
).
InputStream
byte []
File
ParserPipe
wrapper around the specified input object.public final ObjectMap getLastLocation()
{line:123,column:456,currentProperty:"foobar"}
protected final Method getJavaMethod()
When using the REST API, this is the Java method invoked by the REST call. Can be used to access annotations defined on the method or class.
protected final Object getOuter()
When using the REST API, this is the servlet object.
protected final void setCurrentProperty(BeanPropertyMeta currentProperty)
currentProperty
- The current property being parsed.protected final void setCurrentClass(ClassMeta<?> currentClass)
currentClass
- The current class being parsed.protected final boolean isTrimStrings()
Parser.PARSER_trimStrings
setting value for this session.Parser.PARSER_trimStrings
setting value for this session.protected final boolean isStrict()
Parser.PARSER_strict
setting value for this session.Parser.PARSER_strict
setting value for this session.protected final <K> K trim(K o)
o
- The object to trim.protected final String trim(String s)
isTrimStrings()
returns s
- The input string to trim.protected final Object cast(ObjectMap m, BeanPropertyMeta pMeta, ClassMeta<?> eType)
ObjectMap
into a bean identified by the m
- The map to convert to a bean.pMeta
- The current bean property being parsed.eType
- The current expected type being parsed.protected final ClassMeta<?> getClassMeta(String typeName, BeanPropertyMeta pMeta, ClassMeta<?> eType)
typeName
- The dictionary name to resolve.pMeta
- The bean property we're currently parsing.eType
- The expected type we're currently parsing.protected final <T> void onUnknownProperty(ParserPipe pipe, String propertyName, BeanMap<T> beanMap, int line, int col) throws ParseException
T
- The class type of the bean map that doesn't have the expected property.pipe
- The parser input.propertyName
- The unknown bean property name.beanMap
- The bean that doesn't have the expected property.line
- The line number where the property was found. -1
if line numbers are not available.col
- The column number where the property was found. -1
if column numbers are not available.ParseException
- Automatically thrown if BeanContext.BEAN_ignoreUnknownBeanProperties
setting on this parser is
public final <T> T parse(Object input, Type type, Type... args) throws ParseException
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
ReaderParser p = JsonParser.
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.
parse(Object, Class)
method instead if you don't need a parameterized map/collection.
T
- The class type of the object to create.input
- The input.
Reader
CharSequence
InputStream
containing UTF-8 encoded text (or charset defined by
Parser.PARSER_inputStreamCharset
property value).
byte []
containing UTF-8 encoded text (or charset defined by
Parser.PARSER_inputStreamCharset
property value).
File
containing system encoded text (or charset defined by
Parser.PARSER_fileCharset
property value).
InputStream
byte []
File
type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.for argument syntax for maps and collections.
public final <T> T parse(Object input, Class<T> type) throws ParseException
parse(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.
ReaderParser p = JsonParser.
T
- The class type of the object being created.input
- The input.
See parse(Object, Type, Type...)
for details.type
- The object type to create.ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.public final <T> T parse(Object input, ClassMeta<T> type) throws ParseException
parse(Object, Type, Type...)
except the type has already been converted into a ClassMeta
object.
This is mostly an internal method used by the framework.
T
- The class type of the object being created.input
- The input.
See parse(Object, Type, Type...)
for details.type
- The object type to create.ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.public final <K,V> Map<K,V> parseIntoMap(Object input, Map<K,V> m, Type keyType, Type valueType) throws ParseException
Reader must contain something that serializes to a map (such as text containing a JSON object).
Used in the following locations:
ObjectMap
(e.g.
ObjectMap.ObjectMap(CharSequence,Parser)
).
K
- The key class type.V
- The value class type.input
- The input. See parse(Object, ClassMeta)
for supported input types.m
- The map being loaded.keyType
- The class type of the keys, or String.class
.valueType
- The class type of the values, or ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.UnsupportedOperationException
- If not implemented.protected <K,V> Map<K,V> doParseIntoMap(ParserPipe pipe, Map<K,V> m, Type keyType, Type valueType) throws Exception
Default implementation throws an UnsupportedOperationException
.
pipe
- The parser input.m
- The map being loaded.keyType
- The class type of the keys, or String.class
.valueType
- The class type of the values, or Exception
- If thrown from underlying stream, or if the input contains a syntax error or is malformed.public final <E> Collection<E> parseIntoCollection(Object input, Collection<E> c, Type elementType) throws ParseException
Used in the following locations:
ObjectList
(e.g.
ObjectList.ObjectList(CharSequence,Parser)
.
E
- The element class type.input
- The input. See parse(Object, ClassMeta)
for supported input types.c
- The collection being loaded.elementType
- The class type of the elements, or ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.UnsupportedOperationException
- If not implemented.protected <E> Collection<E> doParseIntoCollection(ParserPipe pipe, Collection<E> c, Type elementType) throws Exception
Default implementation throws an UnsupportedOperationException
.
pipe
- The parser input.c
- The collection being loaded.elementType
- The class type of the elements, or Exception
- If thrown from underlying stream, or if the input contains a syntax error or is malformed.public final Object[] parseArgs(Object input, Type[] argTypes) throws ParseException
argTypes
argument.
Used for converting arrays (e.g. Object[]
that can be passed
to the Method.invoke(target, args)
method.
Used in the following locations:
PojoIntrospector.invokeMethod(Method, Reader)
method.
input
- The input. Subclasses can support different input types.argTypes
- Specifies the type of objects to create for each entry in the array.ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.protected final <T> T convertAttrToType(Object outer, String s, ClassMeta<T> type) throws Exception
T
- The class type to convert the string to.outer
- The outer object if we're converting to an inner object that needs to be created within the context
of an outer object.s
- The string to convert.type
- The class type to convert the string to.Exception
- If the input contains a syntax error or is malformed, or is not valid for the specified type.protected static final void setParent(ClassMeta<?> cm, Object o, Object parent) throws Exception
@ParentProperty
method on the specified object if it
exists.cm
- The class type of the object.o
- The object.parent
- The parent to set.Exception
protected static final void setName(ClassMeta<?> cm, Object o, Object name) throws Exception
@NameProperty
method on the specified object if it exists.cm
- The class type of the object.o
- The object.name
- The name to set.Exception
public ParserListener getListener()
public <T extends ParserListener> T getListener(Class<T> c)
c
- The listener class to cast to.Copyright © 2018 Apache. All rights reserved.