Class Parser.Builder<SELF extends Parser.Builder<SELF>>
- Direct Known Subclasses:
InputStreamParser.Builder,Parser.DefaultBuilder,ReaderParser.Builder
- Enclosing class:
- Parser
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBuilder()Constructor, default settings.protectedCopy constructor.protectedBuilder(Parser.Builder<?> copyFrom) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionAuto-close streams.autoCloseStreams(boolean value) Same asautoCloseStreams()but allows you to explicitly specify the value.build()Build the object.Specifies the media type that this parser consumes.abstract SELFcopy()Copy creator.debugOutputLines(int value) Debug output lines.Returns the current value for the 'consumes' property.getNulls()Returns the current context-levelNullsdefault configured on this builder.hashKey()Returns the hashkey of this builder.listener(Class<? extends ParserListener> value) Parser listener.Sets the default per-property null-coercion policy applied during parsing.Trim parsed strings.trimStrings(boolean value) Same astrimStrings()but allows you to explicitly specify the value.Unbuffered.unbuffered(boolean value) Same asunbuffered()but allows you to explicitly specify the value.Methods inherited from class org.apache.juneau.marshall.MarshallingContextable.Builder
activeView, annotations, beanClassVisibility, beanConstructorVisibility, beanDictionary, beanFieldVisibility, beanInterceptor, beanMapPutReturnsOldValue, beanMethodVisibility, beanProperties, beanProperties, beanProperties, beanPropertiesExcludes, beanPropertiesExcludes, beanPropertiesExcludes, beanPropertiesReadOnly, beanPropertiesReadOnly, beanPropertiesReadOnly, beanPropertiesWriteOnly, beanPropertiesWriteOnly, beanPropertiesWriteOnly, beansRequireDefaultConstructor, beansRequireSerializable, beansRequireSettersForGetters, bigNumberFormat, binaryFormat, bitSetFormat, booleanFormat, calendarFormat, classFormat, classLoader, currencyFormat, dateFormat, debug, dictionaryOn, disableBeansRequireSomeProperties, disableDefaultViewInclusion, disableIgnoreMissingSetters, disableIgnoreTransientFields, disableIgnoreUnknownNullBeanProperties, disableInterfaceProxies, durationFormat, enumFormat, example, example, findFluentSetters, findFluentSetters, floatFormat, ignoreInvocationExceptionsOnGetters, ignoreInvocationExceptionsOnSetters, ignoreUnknownBeanProperties, ignoreUnknownEnumValues, implClass, implClasses, interfaceClass, interfaces, locale, localeFormat, marshallingContext, marshallingContext, marshallingContext, marshallingContext, mediaType, notBeanClasses, notBeanPackages, periodFormat, propertyNamer, propertyNamer, recordComponentOrder, stopClass, swap, swap, swaps, swaps, temporalFormat, timeZone, timeZoneFormat, typeName, typePropertyName, typePropertyName, unsortedProperties, unsortedProperties, useJavaBeanIntrospector, uuidFormat, validateSchema, validateSchemaMethods inherited from class org.apache.juneau.marshall.Context.Builder
annotations, apply, applyAnnotations, applyAnnotations, asSubtype, build, cache, canApply, copyOrNull, debug, getApplied, getType, impl, isDebug, registerBuilders, self, type
-
Constructor Details
-
Builder
protected Builder()Constructor, default settings. -
Builder
Copy constructor.- Parameters:
copyFrom- The builder to copy from.
Cannot benull .
-
Builder
Copy constructor.- Parameters:
copyFrom- The bean to copy from.
Cannot benull .
-
-
Method Details
-
autoCloseStreams
Auto-close streams.When enabled,
InputStreams andReaders passed into parsers will be closed after parsing is complete.Example:
// Create a parser using strict mode. ReaderParserparser = JsonParser .create () .autoCloseStreams() .build(); ReadermyReader =new FileReader("/tmp/myfile.json" ); MyBeanmyBean =parser .read(myReader , MyBean.class );assertTrue (myReader .isClosed());- Returns:
- This object.
-
autoCloseStreams
Same asautoCloseStreams()but allows you to explicitly specify the value.- Parameters:
value- The value for this setting.- Returns:
- This object.
-
build
Description copied from class:Context.BuilderBuild the object.- Overrides:
buildin classContext.Builder<SELF extends Parser.Builder<SELF>>- Returns:
- The built object.
-
consumes
Specifies the media type that this parser consumes.Token streams: this setting is not honored by
readTokens— media-type metadata is informational, not behavioral.- Parameters:
value- The value for this setting.
Can benull (treated as empty string, no media types will be specified).- Returns:
- This object.
-
copy
Description copied from class:Context.BuilderCopy creator.- Specified by:
copyin classMarshallingContextable.Builder<SELF extends Parser.Builder<SELF>>- Returns:
- A new mutable copy of this builder.
-
debugOutputLines
Debug output lines.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.
Example:
// Create a parser whose exceptions print out 100 lines before and after the parse error location. ReaderParserparser = JsonParser .create () .debug()// Enable debug mode to capture Reader contents as strings. .debugOuputLines(100) .build(); ReadermyReader =new FileReader("/tmp/mybadfile.json" );try {parser .read(myReader , Object.class ); }catch (ParseExceptione ) { System.err .println(e .getMessage());// Will display 200 lines of the output. }Token streams: this setting is not honored by
readTokens— it controls error-message formatting on the POJO databind path; the cursor uses its own exception text.- Parameters:
value- The new value for this property.
The default value is5 .- Returns:
- This object.
-
getConsumes
Returns the current value for the 'consumes' property.- Returns:
- The current value for the 'consumes' property, or
null if not specified.
-
hashKey
Description copied from class:Context.BuilderReturns the hashkey of this builder.Used to return previously instantiated context beans that have matching hashkeys. The
HashKeyobject is suitable for use as a hashmap key of a map of context beans. A context bean is considered equivalent if theHashKey.equals(Object)method is the same.- Overrides:
hashKeyin classMarshallingContextable.Builder<SELF extends Parser.Builder<SELF>>- Returns:
- The hashkey of this builder.
-
listener
Parser listener.Class used to listen for errors and warnings that occur during parsing.
Example:
// Define our parser listener. // Simply captures all unknown bean property events. public class MyParserListenerextends ParserListener {// A simple property to store our events. public List<String>events =new LinkedList<>();@Override public <T>void onUnknownBeanProperty(ParserSessionsession , StringpropertyName , Class<T>beanClass , Tbean ) { Positionposition =parser .getPosition();events .add(propertyName +"," +position .getLine() +"," +position .getColumn()); } }// Create a parser using our listener. ReaderParserparser = JsonParser .create () .listener(MyParserListener.class ) .build();// Create a session object. // Needed because listeners are created per-session. try (ReaderParserSessionsession =parser .createSession()) {// Parse some JSON object. MyBeanmyBean =session .read("{...}" , MyBean.class );// Get the listener. MyParserListenerlistener =session .getListener(MyParserListener.class );// Dump the results to the console. Json5.DEFAULT .println(listener .events ); }Token streams: this setting is not honored by
readTokens— listeners are POJO-level observers with no token-layer analog.- Parameters:
value- The new value for this property.
Can benull (no listener will be used, listener methods will not be called).- Returns:
- This object.
-
nulls
Sets the default per-property null-coercion policy applied during parsing.Juneau's analog of Jackson's
ObjectMapper.setDefaultSetterInfo(JsonSetter.Value.forValueNulls(...)) . Applied as the fallback when no per-property@MarshalledProp(nulls=…)is configured.Example:
// Substitute the empty value for any property whose wire value is null. ReaderParserparser = JsonParser .create () .nulls(Nulls.EMPTY ) .build();- Parameters:
value- The new value for this property.nullorNulls.NOT_SETdisables the default (each property falls back toNulls.LEAVE).- Returns:
- This object.
- Since:
- 10.0.0
-
getNulls
Returns the current context-levelNullsdefault configured on this builder.- Returns:
- The current value (never
null ;Nulls.NOT_SETwhen unconfigured). - Since:
- 10.0.0
-
trimStrings
Trim parsed strings.When enabled, string values will be trimmed of whitespace using
String.trim()before being added to the POJO.Example:
// Create a parser with trim-strings enabled. ReaderParserparser = JsonParser .create () .trimStrings() .build();// Use it. Stringjson ="{' foo ':' bar '}" ; Map<String,String>myMap =parser .read(json , HashMap.class , String.class , String.class );// Make sure strings are parsed. assertEquals ("bar" ,myMap .get("foo" ));- Returns:
- This object.
-
trimStrings
Same astrimStrings()but allows you to explicitly specify the value.- Parameters:
value- The value for this setting.- Returns:
- This object.
-
unbuffered
Unbuffered.When enabled, don't use internal buffering during parsing.
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.Example:
// Create a parser using strict mode. ReaderParserparser = JsonParser. .create () .unbuffered(true ) .build();// If you're calling parse on the same input multiple times, use a session instead of the parser directly. // It's more efficient because we don't need to recalc the session settings again. ReaderParserSessionsession =parser .createSession();// Read input with multiple POJOs Readerjson =new StringReader("{foo:'bar'}{foo:'baz'}" ); MyBeanmyBean1 =session .read(json , MyBean.class ); MyBeanmyBean2 =session .read(json , MyBean.class );Notes:
-
This only allows for multi-input streams for the following parsers:
It has no effect on the following parsers:
MsgPackParser- It already doesn't use buffering.XmlParser,HtmlParser- These use StAX which doesn't allow for more than one root element anyway.- RDF parsers - These read everything into an internal model before any parsing begins.
- Returns:
- This object.
-
This only allows for multi-input streams for the following parsers:
It has no effect on the following parsers:
-
unbuffered
Same asunbuffered()but allows you to explicitly specify the value.- Parameters:
value- The value for this setting.- Returns:
- This object.
-