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 CalendarSwap
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_fileCharset
Configuration property: File charset.
|
static String |
PARSER_inputStreamCharset
Configuration property: Input stream charset.
|
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_excludeProperties, 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_useInterfaceProxies, BEAN_useJavaBeanIntrospector, DEFAULT_SORTED
Modifier and Type | Method and Description |
---|---|
ObjectMap |
asMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
ParserBuilder |
builder()
Creates a builder from this context object.
|
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.
|
MediaType[] |
getMediaTypes()
Returns the media types handled based on the values passed to the
consumes constructor parameter. |
MediaType |
getPrimaryMediaType()
Returns the first media type handled based on the values passed to the
consumes constructor parameter. |
boolean |
isReaderParser()
Returns
ReaderParser . |
<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.
|
_class, createBeanSession, createBeanSession, createDefaultBeanSessionArgs, createSession, createSession, dumpCacheStats, getBeanMeta, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getExcludeProperties, getImplClassConstructor, getIncludeProperties, hasSameCache, isBean, isNotABean, newInstance, newInstance, newInstanceFromOuter, object, resolveClassMeta, string
equals, getArrayProperty, getArrayProperty, getBooleanProperty, 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, hashCode, toString
public static final String PARSER_autoCloseStreams
Boolean
If
public static final String PARSER_fileCharset
String
The character set to use for reading Files
from the file system.
Used when passing in files to parse(Object, Class)
.
public static final String PARSER_inputStreamCharset
String
The character set to use for converting InputStreams
and byte arrays to readers.
Used when passing in input streams and byte arrays to parse(Object, Class)
.
public static final String PARSER_listener
Class<? extends ParserListener>
Class used to listen for errors and warnings that occur during parsing.
public static final String PARSER_strict
Boolean
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
Boolean
If String.trim()
before being added to
the POJO.
public static final String PARSER_unbuffered
Boolean
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.
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
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_inputStreamCharset
property value).
byte []
containing UTF-8 encoded text (or charset defined by
PARSER_inputStreamCharset
property value).
File
containing system encoded text (or charset defined by
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 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
- If the input contains a syntax error or is malformed, or is not valid for the specified type.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
- If the input contains a syntax error or is malformed, or is not valid for the specified type.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
- If the input contains a syntax error or is malformed, or is not valid for the specified type.public final MediaType[] getMediaTypes()
consumes
constructor parameter.public final MediaType getPrimaryMediaType()
consumes
constructor parameter.public ObjectMap asMap()
Context
asMap
in class BeanContext
Copyright © 2018 Apache. All rights reserved.