Class Parser.Builder<SELF extends Parser.Builder<SELF>>

Direct Known Subclasses:
InputStreamParser.Builder, Parser.DefaultBuilder, ReaderParser.Builder
Enclosing class:
Parser

public abstract static class Parser.Builder<SELF extends Parser.Builder<SELF>> extends MarshallingContextable.Builder<SELF>
Builder class.
  • Constructor Details

    • Builder

      protected Builder()
      Constructor, default settings.
    • Builder

      protected Builder(Parser.Builder<?> copyFrom)
      Copy constructor.
      Parameters:
      copyFrom - The builder to copy from.
      Cannot be null.
    • Builder

      protected Builder(Parser copyFrom)
      Copy constructor.
      Parameters:
      copyFrom - The bean to copy from.
      Cannot be null.
  • Method Details

    • autoCloseStreams

      Auto-close streams.

      When enabled, InputStreams and Readers passed into parsers will be closed after parsing is complete.

      Example:

      // Create a parser using strict mode. ReaderParser parser = JsonParser .create() .autoCloseStreams() .build(); Reader myReader = new FileReader("/tmp/myfile.json"); MyBean myBean = parser.read(myReader, MyBean.class); assertTrue(myReader.isClosed());

      Returns:
      This object.
    • autoCloseStreams

      public SELF autoCloseStreams(boolean value)
      Same as autoCloseStreams() but allows you to explicitly specify the value.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.
    • build

      public Parser build()
      Description copied from class: Context.Builder
      Build the object.
      Overrides:
      build in class Context.Builder<SELF extends Parser.Builder<SELF>>
      Returns:
      The built object.
    • consumes

      public SELF consumes(String value)
      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 be null (treated as empty string, no media types will be specified).
      Returns:
      This object.
    • copy

      public abstract SELF copy()
      Description copied from class: Context.Builder
      Copy creator.
      Specified by:
      copy in class MarshallingContextable.Builder<SELF extends Parser.Builder<SELF>>
      Returns:
      A new mutable copy of this builder.
    • debugOutputLines

      public SELF debugOutputLines(int value)
      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. ReaderParser parser = JsonParser .create() .debug() // Enable debug mode to capture Reader contents as strings. .debugOuputLines(100) .build(); Reader myReader = new FileReader("/tmp/mybadfile.json"); try { parser.read(myReader, Object.class); } catch (ParseException e) { 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 is 5.
      Returns:
      This object.
    • getConsumes

      public String getConsumes()
      Returns the current value for the 'consumes' property.
      Returns:
      The current value for the 'consumes' property, or null if not specified.
    • hashKey

      public HashKey hashKey()
      Description copied from class: Context.Builder
      Returns the hashkey of this builder.

      Used to return previously instantiated context beans that have matching hashkeys. The HashKey object is suitable for use as a hashmap key of a map of context beans. A context bean is considered equivalent if the HashKey.equals(Object) method is the same.

      Overrides:
      hashKey in class MarshallingContextable.Builder<SELF extends Parser.Builder<SELF>>
      Returns:
      The hashkey of this builder.
    • listener

      public SELF listener(Class<? extends ParserListener> value)
      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 MyParserListener extends ParserListener { // A simple property to store our events. public List<String> events = new LinkedList<>(); @Override public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean) { Position position = parser.getPosition(); events.add(propertyName + "," + position.getLine() + "," + position.getColumn()); } } // Create a parser using our listener. ReaderParser parser = JsonParser .create() .listener(MyParserListener.class) .build(); // Create a session object. // Needed because listeners are created per-session. try (ReaderParserSession session = parser.createSession()) { // Parse some JSON object. MyBean myBean = session.read("{...}", MyBean.class); // Get the listener. MyParserListener listener = 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 be null (no listener will be used, listener methods will not be called).
      Returns:
      This object.
    • nulls

      public SELF nulls(Nulls value)
      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. ReaderParser parser = JsonParser .create() .nulls(Nulls.EMPTY) .build();

      Parameters:
      value - The new value for this property.
      null or Nulls.NOT_SET disables the default (each property falls back to Nulls.LEAVE).
      Returns:
      This object.
      Since:
      10.0.0
    • getNulls

      public Nulls getNulls()
      Returns the current context-level Nulls default configured on this builder.
      Returns:
      The current value (never null; Nulls.NOT_SET when unconfigured).
      Since:
      10.0.0
    • trimStrings

      public SELF 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. ReaderParser parser = JsonParser .create() .trimStrings() .build(); // Use it. String json = "{' 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

      public SELF trimStrings(boolean value)
      Same as trimStrings() but allows you to explicitly specify the value.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.
    • unbuffered

      public SELF 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. ReaderParser parser = 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. ReaderParserSession session = parser.createSession(); // Read input with multiple POJOs Reader json = new StringReader("{foo:'bar'}{foo:'baz'}"); MyBean myBean1 = session.read(json, MyBean.class); MyBean myBean2 = 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.
    • unbuffered

      public SELF unbuffered(boolean value)
      Same as unbuffered() but allows you to explicitly specify the value.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.