@ConfigurableContext public abstract class Parser extends BeanContext
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 PojoSwaps
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 c = parser.parse(
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 | ObjectMap |
array | ObjectList |
number | Number (depending on length and format, could be Integer ,
Double , Float , etc...) |
boolean | Boolean |
string | String |
Modifier and Type | Field and Description |
---|---|
static String |
PARSER_autoCloseStreams
Configuration property: Auto-close streams.
|
static String |
PARSER_debugOutputLines
Configuration property: Debug output lines.
|
static String |
PARSER_listener
Configuration property: Parser listener.
|
static String |
PARSER_strict
Configuration property: Strict mode.
|
static String |
PARSER_trimStrings
Configuration property: Trim parsed strings.
|
static String |
PARSER_unbuffered
Configuration property: Unbuffered.
|
BEAN_beanClassVisibility, BEAN_beanConstructorVisibility, BEAN_beanDictionary, BEAN_beanDictionary_add, BEAN_beanDictionary_remove, BEAN_beanFieldVisibility, BEAN_beanFilters, BEAN_beanFilters_add, BEAN_beanFilters_remove, BEAN_beanMapPutReturnsOldValue, BEAN_beanMethodVisibility, BEAN_beansRequireDefaultConstructor, BEAN_beansRequireSerializable, BEAN_beansRequireSettersForGetters, BEAN_beansRequireSomeProperties, BEAN_beanTypePropertyName, BEAN_debug, BEAN_examples, BEAN_excludeProperties, BEAN_fluentSetters, BEAN_ignoreInvocationExceptionsOnGetters, BEAN_ignoreInvocationExceptionsOnSetters, BEAN_ignorePropertiesWithoutSetters, BEAN_ignoreUnknownBeanProperties, BEAN_ignoreUnknownNullBeanProperties, BEAN_implClasses, BEAN_includeProperties, BEAN_locale, BEAN_mediaType, BEAN_notBeanClasses, BEAN_notBeanClasses_add, BEAN_notBeanClasses_remove, BEAN_notBeanPackages, BEAN_notBeanPackages_add, BEAN_notBeanPackages_remove, BEAN_pojoSwaps, BEAN_pojoSwaps_add, BEAN_pojoSwaps_remove, BEAN_propertyNamer, BEAN_sortProperties, BEAN_timeZone, BEAN_useEnumNames, BEAN_useInterfaceProxies, BEAN_useJavaBeanIntrospector, DEFAULT_SORTED
Modifier | Constructor and Description |
---|---|
protected |
Parser(PropertyStore ps,
String... consumes)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
ParserBuilder |
builder()
Creates a builder from this context object.
|
boolean |
canHandle(String contentType)
Returns
|
static ParserBuilder |
create()
Instantiates a new clean-slate
ParserBuilder object. |
ParserSessionArgs |
createDefaultSessionArgs()
Defines default session arguments used when calling the
Context.createSession() method. |
ParserSession |
createSession()
Create a new bean session based on the properties defined on this context.
|
abstract ParserSession |
createSession(ParserSessionArgs args)
Create the session object that will be passed in to the parse method.
|
protected int |
getDebugOutputLines()
Configuration property: Debug output lines.
|
protected Class<? extends ParserListener> |
getListener()
Configuration property: Parser listener.
|
MediaType[] |
getMediaTypes()
Returns the media types handled based on the values passed to the
|
MediaType |
getPrimaryMediaType()
Returns the first media type handled based on the values passed to the
|
protected boolean |
isAutoCloseStreams()
Configuration property: Auto-close streams.
|
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.
|
<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)
|
<T> T |
parse(String input,
ClassMeta<T> type)
|
<T> T |
parse(String input,
Type type,
Type... args)
|
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.
|
ObjectMap |
toMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
_class, createBeanSession, createBeanSession, createDefaultBeanSessionArgs, createSession, createSession, dumpCacheStats, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionaryClasses, getBeanFieldVisibility, getBeanFilters, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getExamples, getExcludeProperties, getExcludeProperties, getImplClassConstructor, getImplClasses, getIncludeProperties, getIncludeProperties, getLocale, getMediaType, getNotBeanClasses, getNotBeanPackagesNames, getNotBeanPackagesPrefixes, getPojoSwaps, getPropertyNamer, getTimeZone, hasSameCache, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isDebug, isFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnorePropertiesWithoutSetters, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isNotABean, isSortProperties, isUseEnumNames, isUseInterfaceProxies, isUseJavaBeanIntrospector, object, resolveClassMeta, string
equals, getArrayProperty, getArrayProperty, getBooleanProperty, getCdlProperty, getClassArrayProperty, getClassArrayProperty, getClassArrayProperty, getClassListProperty, getClassListProperty, getClassMapProperty, getClassMapProperty, getClassProperty, getClassSetProperty, getClassSetProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, getStringPropertyWithNone, hashCode, identityCode, toString
public static final String PARSER_autoCloseStreams
If
public static final String PARSER_debugOutputLines
When parse errors occur, this specifies the number of lines of input before and after the error location to be printed as part of the exception message.
public static final String PARSER_listener
Class used to listen for errors and warnings that occur during parsing.
public static final String PARSER_strict
If
Strict mode can mean different things for different parsers.
Parser class | Strict behavior |
---|---|
All reader-based parsers |
When enabled, throws ParseExceptions on malformed charset input.
Otherwise, malformed input is ignored.
|
JsonParser |
When enabled, throws exceptions on the following invalid JSON syntax:
|
public static final String PARSER_trimStrings
If String.trim()
before being added to
the POJO.
public static final String PARSER_unbuffered
If
This is useful in cases when you want to parse the same input stream or reader multiple times
because it may contain multiple independent POJOs to parse.
Buffering would cause the parser to read past the current POJO in the stream.
MsgPackParser
- It already doesn't use buffering.
XmlParser
, HtmlParser
- These use StAX which doesn't allow for more than one root element anyway.
protected Parser(PropertyStore ps, String... consumes)
ps
- The property store containing all the settings for this object.consumes
- The list of media types that this parser consumes (e.g. public ParserBuilder builder()
Context
Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
builder
in class BeanContext
public static ParserBuilder create()
ParserBuilder
object.
This is equivalent to simply calling
.
Note that this method creates a builder initialized to all default settings, whereas builder()
copies
the settings of the object called on.
ParserBuilder
object.public boolean isReaderParser()
ReaderParser
.ReaderParser
.public abstract ParserSession createSession(ParserSessionArgs args)
It's up to implementers to decide what the session object looks like, although typically it's going to be a
subclass of ParserSession
.
args
- Runtime arguments.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
CharSequence
containing encoded bytes according to the InputStreamParser.ISPARSER_binaryFormat
setting.
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 underlying stream.for argument syntax for maps and collections.
public final <T> T parse(String input, Type type, Type... args) throws ParseException
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.
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.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
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
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 ParserSession createSession()
BeanContext
Use this method for creating sessions if you don't need to override any properties or locale/timezone currently set on this context.
createSession
in class BeanContext
public final ParserSessionArgs createDefaultSessionArgs()
Context
Context.createSession()
method.createDefaultSessionArgs
in class BeanContext
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.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.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.public final MediaType[] getMediaTypes()
public final MediaType getPrimaryMediaType()
public boolean canHandle(String contentType)
contentType
- The content type to test.protected final boolean isAutoCloseStreams()
PARSER_autoCloseStreams
protected final int getDebugOutputLines()
PARSER_debugOutputLines
protected final Class<? extends ParserListener> getListener()
PARSER_listener
protected final boolean isStrict()
PARSER_strict
protected final boolean isTrimStrings()
String.trim()
before being added to
the POJO.PARSER_trimStrings
protected final boolean isUnbuffered()
PARSER_unbuffered
public ObjectMap toMap()
Context
toMap
in class BeanContext
Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.