public final class JsonParserBuilder extends Object
JsonParser. To obtain a new builder, call JsonParser.builder() static method.
Example usage:
public final class PrettyPrinter
implements ResetHook,
ObjectStartHandler, ObjectEndHandler,
ArrayStartHandler, ArrayEndHandler,
ObjectKeyHandler, StringValueHandler,
NumberHandler, BooleanHandler, NullHandler {
public static void main(String[] args) {
PrettyPrinter prettyPrinter = new PrettyPrinter();
JsonParser parser = JsonParser.builder()
.applyAdapter(prettyPrinter)
.options(JsonParserOption.ALLOW_COMMENTS)
.build();
parser.parse(... /* not formatted JSON as Bytes */);
parser.finish();
System.out.println(prettyPrinter);
}
private int indent = 0;
private int indentStep = 4;
private StringBuilder sb = new StringBuilder();JsonParser.builder()| Modifier and Type | Method and Description |
|---|---|
JsonParserBuilder |
applyAdapter(JsonHandlerBase a)
Convenient method to apply the adapter which implements several handler interfaces in one call.
|
ArrayEndHandler |
arrayEndHandler()
Returns the parser's array end handler, or
null if the handler is not set. |
JsonParserBuilder |
arrayEndHandler(ArrayEndHandler arrayEndHandler)
Sets the parser's array end handler, or removes it if
null is passed. |
ArrayStartHandler |
arrayStartHandler()
Returns the parser's array start handler, or
null if the handler is not set. |
JsonParserBuilder |
arrayStartHandler(ArrayStartHandler arrayStartHandler)
Sets the parser's array start handler, or removes it if
null is passed. |
BooleanHandler |
booleanHandler()
Returns the parser's boolean value handler, or
null if the handler is not set. |
JsonParserBuilder |
booleanHandler(BooleanHandler booleanHandler)
Sets the parser's boolean value handler, or removes it if
null is passed. |
JsonParser |
build()
Builds and returns a new
JsonParser with the configured options and handlers. |
JsonParserBuilder |
clearOptions()
Clears an option set.
|
boolean |
eachTokenMustBeHandled()
Returns if each input JSON token must be handled,
true by default. |
JsonParserBuilder |
eachTokenMustBeHandled(boolean eachTokenMustBeHandled)
Sets if each input JSON token must be handled.
|
FloatingHandler |
floatingHandler()
Returns the parser's floating value handler, or
null if the handler is not set. |
JsonParserBuilder |
floatingHandler(FloatingHandler floatingHandler)
Sets the parser's floating value handler, or removes it if
null is passed. |
IntegerHandler |
integerHandler()
Returns the parser's integer value handler, or
null if the handler is not set. |
JsonParserBuilder |
integerHandler(IntegerHandler integerHandler)
Sets the parser's integer value handler, or removes it if
null is passed. |
NullHandler |
nullHandler()
Returns the parser's null value handler, or
null if the handler is not set. |
JsonParserBuilder |
nullHandler(NullHandler nullHandler)
Sets the parser's null value handler, or removes it if
null is passed. |
NumberHandler |
numberHandler()
Returns the parser's number value handler, or
null if the handler is not set. |
JsonParserBuilder |
numberHandler(NumberHandler numberHandler)
Sets the parser's number value handler, or removes it if
null is passed. |
ObjectEndHandler |
objectEndHandler()
Returns the parser's object end handler, or
null if the handler is not set. |
JsonParserBuilder |
objectEndHandler(ObjectEndHandler objectEndHandler)
Sets the parser's object end handler, or removes it if
null is passed. |
ObjectKeyHandler |
objectKeyHandler()
Returns the parser's object key handler, or
null if the handler is not set. |
JsonParserBuilder |
objectKeyHandler(ObjectKeyHandler objectKeyHandler)
Sets the parser's object key handler, or removes it if
null is passed. |
ObjectStartHandler |
objectStartHandler()
Returns the parser's object start handler, or
null if the handler is not set. |
JsonParserBuilder |
objectStartHandler(ObjectStartHandler objectStartHandler)
Sets the parser's object start handler, or removes it if
null is passed. |
Set<JsonParserOption> |
options()
Returns the parser options as read-only set.
|
JsonParserBuilder |
options(Collection<JsonParserOption> options)
Sets the parser options.
|
JsonParserBuilder |
options(JsonParserOption first,
JsonParserOption... rest)
Sets the parser options.
|
ResetHook |
resetHook()
Returns the parser's
reset hook, or null if the hook is not set. |
JsonParserBuilder |
resetHook(ResetHook resetHook)
Sets the parser's
reset hook, or removes it if null is passed. |
StringValueHandler |
stringValueHandler()
Returns the parser's string value handler, or
null if the handler is not set. |
JsonParserBuilder |
stringValueHandler(StringValueHandler stringValueHandler)
Sets the parser's string value handler, or removes it if
null is passed. |
JsonParserTopLevelStrategy |
topLevelStrategy()
Returns the parser's top-level strategy.
|
JsonParserBuilder |
topLevelStrategy(JsonParserTopLevelStrategy topLevelStrategy)
Sets the parser's top-level strategy
|
public JsonParser build()
JsonParser with the configured options and handlers.
After construction a JsonParser don't depend on it's builder, so you can change the builder
state and construct a different parser.
JsonParserpublic Set<JsonParserOption> options()
public JsonParserBuilder options(Collection<JsonParserOption> options)
options - parser optionspublic JsonParserBuilder options(JsonParserOption first, JsonParserOption... rest)
first - parser optionrest - the rest parser optionspublic JsonParserBuilder clearOptions()
options() returns an empty set.@NotNull public JsonParserTopLevelStrategy topLevelStrategy()
public JsonParserBuilder topLevelStrategy(@NotNull JsonParserTopLevelStrategy topLevelStrategy)
topLevelStrategy - a new top-level strategypublic boolean eachTokenMustBeHandled()
true by default.public JsonParserBuilder eachTokenMustBeHandled(boolean eachTokenMustBeHandled)
IllegalStateException is thrown. Otherwise, this situation of
silently ignored. The profit of the latter behaviour - the parser can skip unneeded tokens to speed up.
But it is error-prone.eachTokenMustBeHandled - if each input JSON token must be handledpublic JsonParserBuilder applyAdapter(JsonHandlerBase a)
Is equivalent to
if (a instanceof ObjectStartHandler)
objectStartHandler((ObjectStartHandler) a);
if (a instanceof ObjectEndHandler)
objectEndHandler((ObjectEndHandler) a);
// ... the same job for the rest handler interfaces
a - the adapter - an object, implementing some of concrete handler interfacesIllegalArgumentException - if the adapter doesn't implement any of concrete handler
interfaces@Nullable public ObjectStartHandler objectStartHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder objectStartHandler(@Nullable ObjectStartHandler objectStartHandler)
null is passed.objectStartHandler - a new object start handler. null means there shouldn't be an object
start handler in the built parser.@Nullable public ObjectEndHandler objectEndHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder objectEndHandler(@Nullable ObjectEndHandler objectEndHandler)
null is passed.objectEndHandler - a new object end handler. null means there shouldn't be an object end
handler in the built parser.@Nullable public ArrayStartHandler arrayStartHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder arrayStartHandler(@Nullable ArrayStartHandler arrayStartHandler)
null is passed.arrayStartHandler - a new array start handler. null means there shouldn't be an array
start handler in the built parser.@Nullable public ArrayEndHandler arrayEndHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder arrayEndHandler(@Nullable ArrayEndHandler arrayEndHandler)
null is passed.arrayEndHandler - a new array end handler. null means there shouldn't be an array end
handler in the built parser.@Nullable public BooleanHandler booleanHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder booleanHandler(@Nullable BooleanHandler booleanHandler)
null is passed.booleanHandler - a new boolean value handler. null means there shouldn't be a boolean
value handler in the built parser.@Nullable public NullHandler nullHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder nullHandler(@Nullable NullHandler nullHandler)
null is passed.nullHandler - a new null value handler. null means there shouldn't be a null value handler
in the built parser.@Nullable public StringValueHandler stringValueHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder stringValueHandler(@Nullable StringValueHandler stringValueHandler)
null is passed.stringValueHandler - a new string value handler. null means there shouldn't be a string
value handler in the built parser.@Nullable public ObjectKeyHandler objectKeyHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder objectKeyHandler(@Nullable ObjectKeyHandler objectKeyHandler)
null is passed.objectKeyHandler - a new object key handler. null means there shouldn't be an object key
handler in the built parser.@Nullable public NumberHandler numberHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder numberHandler(@Nullable NumberHandler numberHandler)
null is passed.numberHandler - a new number value handler. null means there shouldn't be a number value
handler in the built parser.IllegalStateException - if integerHandler() or floatingHandler() is
already set@Nullable public IntegerHandler integerHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder integerHandler(@Nullable IntegerHandler integerHandler)
null is passed.integerHandler - a new integer value handler. null means there shouldn't be a integer
value handler in the built parser.IllegalStateException - if numberHandler() is already set@Nullable public FloatingHandler floatingHandler()
null if the handler is not set.null if the handler is not setpublic JsonParserBuilder floatingHandler(@Nullable FloatingHandler floatingHandler)
null is passed.floatingHandler - a new floating value handler. null means there shouldn't be a floating
value handler in the built parser.IllegalStateException - if numberHandler() is already set@Nullable public ResetHook resetHook()
reset hook, or null if the hook is not set.reset hook, or null if the hook is not setpublic JsonParserBuilder resetHook(@Nullable ResetHook resetHook)
reset hook, or removes it if null is passed.Copyright © 2014. All rights reserved.