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 |
---|---|
protected Object |
cast(ObjectMap m,
BeanPropertyMeta pMeta,
ClassMeta<?> eType)
Converts the specified
|
protected <T> T |
convertAttrToType(Object outer,
String s,
ClassMeta<T> type)
Converts the specified string to the specified type.
|
protected 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 int |
getDebugOutputLines()
Configuration property: Debug output lines.
|
String |
getInputAsString()
Returns the input as a string.
|
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 Class<? extends ParserListener> |
getListenerClass()
Configuration property: Parser listener.
|
protected Object |
getOuter()
Returns the outer object used for instantiating top-level non-static member classes.
|
Position |
getPosition()
Returns the current position into the reader or input stream.
|
protected boolean |
isAutoCloseStreams()
Configuration property: Auto-close streams.
|
abstract boolean |
isReaderParser()
Returns
ReaderParser . |
protected boolean |
isStrict()
Configuration property: Strict mode.
|
protected boolean |
isTrimStrings()
Configuration property: Trim parsed strings.
|
protected boolean |
isUnbuffered()
Configuration property: Unbuffered.
|
protected void |
mark()
Marks the current position.
|
protected <T> void |
onUnknownProperty(String propertyName,
BeanMap<T> beanMap)
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.
|
<T> T |
parse(String input,
Class<T> type)
Same as
parse(Object, Class) but parses from a string and doesn't throw an IOException . |
<T> T |
parse(String input,
ClassMeta<T> type)
Same as
parse(Object, ClassMeta) except parses from a string and doesn't throw an IOException . |
<T> T |
parse(String input,
Type type,
Type... args)
Same as
parse(Object,Type,Type...) but parses from a string and doesn't throw an IOException . |
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 ParserPipe |
setPipe(ParserPipe pipe)
The
createPipe(Object) method should call this method to set the pipe for debugging purposes. |
ObjectMap |
toMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
protected <K> K |
trim(K o)
|
protected String |
trim(String s)
Trims the specified string if
isTrimStrings() returns |
protected void |
unmark()
Unmarks the current position.
|
protected Object |
unswap(PojoSwap swap,
Object o,
ClassMeta<?> eType)
Invokes the specified swap on the specified object.
|
_class, checkForWarnings, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, getArgsClassMeta, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionaryClasses, getBeanFieldVisibility, getBeanFilters, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanTypePropertyName, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getClassMetaForObject, getExamples, getExcludeProperties, getImplClasses, getIncludeProperties, getLocale, getMediaType, getNotBeanClasses, getNotBeanPackagesNames, getNotBeanPackagesPrefixes, getPojoSwaps, getPropertyNamer, getSchema, getStringBuilder, getTimeZone, getTimeZoneId, isBean, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isDebug, isFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnorePropertiesWithoutSetters, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isSortProperties, isUseEnumNames, isUseInterfaceProxies, isUseJavaBeanIntrospector, newBean, newBean, newBeanMap, newBeanMap, object, returnStringBuilder, string, toArray, toBeanMap, toBeanMap
addToCache, addToCache, addWarning, getClassProperty, getFromCache, getInstanceArrayProperty, getInstanceProperty, getLogger, getProperties, getProperty, getProperty, getProperty, getPropertyKeys, getWarnings, hasProperty, 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.protected abstract <T> T doParse(ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException, ExecutableException
Subclasses are expected to implement this method.
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 IOException
- Thrown by underlying stream.ParseException
- Malformed input encountered.ExecutableException
- Exception occurred on invoked constructor/method/field.public abstract boolean isReaderParser()
ReaderParser
.ReaderParser
.protected 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
ReaderParser.RPARSER_streamCharset
).
byte []
containing UTF-8 encoded text (or whatever the encoding specified by
ReaderParser.RPARSER_streamCharset
).
File
containing system encoded text (or whatever the encoding specified by
ReaderParser.RPARSER_fileCharset
).
InputStream
byte []
File
CharSequence
containing encoded bytes according to the InputStreamParser.ISPARSER_binaryFormat
setting.
ParserPipe
wrapper around the specified input object.public final ObjectMap getLastLocation()
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 <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)
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(String propertyName, BeanMap<T> beanMap) throws ParseException
T
- The class type of the bean map that doesn't have the expected property.propertyName
- The unknown bean property name.beanMap
- The bean that doesn't have the expected property.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, IOException
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
ReaderParser p = JsonParser.
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
ReaderParser.RPARSER_streamCharset
property value).
byte []
containing UTF-8 encoded text (or charset defined by
ReaderParser.RPARSER_streamCharset
property value).
File
containing system encoded text (or charset defined by
ReaderParser.RPARSER_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
- Malformed input encountered.IOException
- Thrown by the underlying stream.for argument syntax for maps and collections.
public final <T> T parse(String input, Type type, Type... args) throws ParseException
parse(Object,Type,Type...)
but parses from a string and doesn't throw an IOException
.T
- The class type of the object to create.input
- The input.
Reader
CharSequence
InputStream
containing UTF-8 encoded text (or charset defined by
ReaderParser.RPARSER_streamCharset
property value).
byte []
containing UTF-8 encoded text (or charset defined by
ReaderParser.RPARSER_streamCharset
property value).
File
containing system encoded text (or charset defined by
ReaderParser.RPARSER_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
- Malformed input encountered.for argument syntax for maps and collections.
public final <T> T parse(Object input, Class<T> type) throws ParseException, IOException
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
- Malformed input encountered.IOException
- Thrown by the underlying stream.public final <T> T parse(String input, Class<T> type) throws ParseException
parse(Object, Class)
but parses from a string and doesn't throw an IOException
.
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
- Malformed input encountered.public final <T> T parse(Object input, ClassMeta<T> type) throws ParseException, IOException
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
- Malformed input encountered.IOException
- Thrown by the underlying stream.public final <T> T parse(String input, ClassMeta<T> type) throws ParseException
parse(Object, ClassMeta)
except parses from a string and doesn't throw an IOException
.
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
- Malformed input encountered.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
- Malformed input encountered.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
- Malformed input encountered.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
- Malformed input encountered.protected final <T> T convertAttrToType(Object outer, String s, ClassMeta<T> type) throws IOException, ParseException, ExecutableException
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.IOException
- Thrown by underlying stream.ParseException
- Malformed input encountered.ExecutableException
- Exception occurred on invoked constructor/method/field.protected static final void setParent(ClassMeta<?> cm, Object o, Object parent) throws ExecutableException
@ParentProperty
method on the specified object if it
exists.cm
- The class type of the object.o
- The object.parent
- The parent to set.ExecutableException
- Exception occurred on invoked constructor/method/field.protected static final void setName(ClassMeta<?> cm, Object o, Object name) throws ExecutableException
@NameProperty
method on the specified object if it exists.cm
- The class type of the object.o
- The object.name
- The name to set.ExecutableException
- Exception occurred on invoked constructor/method/field.public <T extends ParserListener> T getListener(Class<T> c)
c
- The listener class to cast to.protected ParserPipe setPipe(ParserPipe pipe)
createPipe(Object)
method should call this method to set the pipe for debugging purposes.pipe
- The pipe created for this session.public Position getPosition()
protected void mark()
protected void unmark()
public String getInputAsString()
This always returns a value for input of type CharSequence
.
For other input types, use BeanContext.BEAN_debug
setting to enable caching to a string
before parsing so that this method returns the input.
protected Object unswap(PojoSwap swap, Object o, ClassMeta<?> eType) throws ParseException
swap
- The swap to invoke.o
- The input object.eType
- The expected type.ParseException
- If swap method threw an exception.protected final boolean isAutoCloseStreams()
Parser.PARSER_autoCloseStreams
protected final int getDebugOutputLines()
Parser.PARSER_debugOutputLines
public ParserListener getListener()
protected final boolean isStrict()
Parser.PARSER_strict
protected final boolean isTrimStrings()
String.trim()
before being added to
the POJO.Parser.PARSER_trimStrings
protected final boolean isUnbuffered()
Parser.PARSER_unbuffered
protected final Class<? extends ParserListener> getListenerClass()
Parser.PARSER_listener
public ObjectMap toMap()
Session
toMap
in class BeanSession
Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.