Class JsonFactory

All Implemented Interfaces:
Serializable, Snapshottable<TokenStreamFactory>, Versioned

public class JsonFactory extends TextualTSFactory implements Serializable
JSON-backed TokenStreamFactory implementation that will create token readers ("parsers") and writers ("generators") for handling JSON-encoded content.

Note that this class used to reside at main tools.jackson.core in 2.x, but moved here to denote its changed role as implementation, not base class for factories.

See Also:
  • Field Details

    • FORMAT_NAME_JSON

      public static final String FORMAT_NAME_JSON
      Name used to identify JSON format (and returned by getFormatName()
      See Also:
    • DEFAULT_ROOT_VALUE_SEPARATOR

      public static final SerializableString DEFAULT_ROOT_VALUE_SEPARATOR
    • DEFAULT_QUOTE_CHAR

      public static final char DEFAULT_QUOTE_CHAR
      See Also:
    • _characterEscapes

      protected final CharacterEscapes _characterEscapes
      Definition of custom character escapes to use for generators created by this factory, if any. If null, standard data format specific escapes are used.
    • _rootValueSeparator

      protected final SerializableString _rootValueSeparator
      Separator used between root-level values, if any; null indicates "do not add separator". Default separator is a single space character.
    • _maximumNonEscapedChar

      protected final int _maximumNonEscapedChar
      Optional threshold used for automatically escaping character above certain character code value: either 0 to indicate that no threshold is specified, or value at or above 127 to indicate last character code that is NOT automatically escaped (but depends on other configuration rules for checking).
    • _quoteChar

      protected final char _quoteChar
      Character used for quoting property names (if property name quoting has not been disabled with JsonWriteFeature.QUOTE_PROPERTY_NAMES) and JSON String values.
    • _rootCharSymbols

      protected final transient CharsToNameCanonicalizer _rootCharSymbols
      Each factory comes equipped with a shared root symbol table. It should not be linked back to the original blueprint, to avoid contents from leaking between factories.
    • _byteSymbolCanonicalizer

      protected final transient ByteQuadsCanonicalizer _byteSymbolCanonicalizer
      Alternative to the basic symbol table, some stream-based parsers use different name canonicalization method.
  • Constructor Details

    • JsonFactory

      public JsonFactory()
      Default constructor used to create factory instances. Creation of a factory instance is a light-weight operation, but it is still a good idea to reuse limited number of factory instances (and quite often just a single instance): factories are used as context for storing some reused processing objects (such as symbol tables parsers use) and this reuse only works within context of a single factory instance.
    • JsonFactory

      protected JsonFactory(JsonFactory src)
      Copy constructor.
      Parameters:
      src - Original factory to copy configuration from
    • JsonFactory

      protected JsonFactory(JsonFactoryBuilder b)
      Constructors used by JsonFactoryBuilder for instantiation.
      Parameters:
      b - Builder that has configuration to use
      Since:
      3.0
  • Method Details

    • rebuild

      public JsonFactoryBuilder rebuild()
      Description copied from class: TokenStreamFactory
      Method that can be used to create differently configured stream factories: it will create and return a Builder instance with exact settings of this stream factory.
      Specified by:
      rebuild in class TokenStreamFactory
      Returns:
      Builder instance initialized with configuration this stream factory has
    • builder

      public static JsonFactoryBuilder builder()
      Main factory method to use for constructing JsonFactory instances with different configuration.
      Returns:
      Builder instance to use
    • builderWithJackson2Defaults

      public static JsonFactoryBuilder builderWithJackson2Defaults()
      Factory method to use for constructing JsonFactory instances with different configuration. The builder returned uses default settings more closely matching the default configs used in Jackson 2.x versions.

      This method is still a work in progress and may not yet fully replicate the default settings of Jackson 2.x.

      Returns:
      Builder instance to use
    • copy

      public JsonFactory copy()
      Method for constructing a new JsonFactory that has the same settings as this instance, but is otherwise independent (i.e. nothing is actually shared, symbol tables are separate).
      Specified by:
      copy in class TokenStreamFactory
      Returns:
      Copy of this factory instance
    • snapshot

      public TokenStreamFactory snapshot()
      Description copied from class: TokenStreamFactory
      Method for constructing a new TokenStreamFactory that has the same settings as this instance, but is otherwise independent (i.e. nothing is actually shared, symbol tables are separate).

      Although assumption is that all factories are immutable -- and that we could usually simply return `this` -- method is left unimplemented at this level to make implementors aware of requirement to consider immutability.

      Specified by:
      snapshot in interface Snapshottable<TokenStreamFactory>
      Specified by:
      snapshot in class TokenStreamFactory
      Returns:
      This factory instance to allow call chaining
    • readResolve

      protected Object readResolve()
      Method that we need to override to actually make restoration go through constructors etc; called by JDK serialization system.
      Returns:
      A properly initialized copy of this factory instance
    • version

      public Version version()
      Accessor for getting version of the core package, given a parser instance. Left for sub-classes to implement.
      Specified by:
      version in interface Versioned
      Specified by:
      version in class TokenStreamFactory
      Returns:
      Version of this generator (derived from version declared for jackson-core jar that contains the class
    • canParseAsync

      public boolean canParseAsync()
      Description copied from class: TokenStreamFactory
      Introspection method that can be used to check whether this factory can create non-blocking parsers: parsers that do not use blocking I/O abstractions but instead use a NonBlockingInputFeeder.
      Specified by:
      canParseAsync in class TokenStreamFactory
      Returns:
      Whether this factory supports non-blocking ("async") parsing or not (and consequently whether createNonBlockingXxx() method(s) work)
    • isEnabled

      public final boolean isEnabled(JsonReadFeature f)
      Checked whether specified parser feature is enabled.
      Parameters:
      f - Feature to check
      Returns:
      True if feature is enabled; false otherwise
    • isEnabled

      public final boolean isEnabled(JsonWriteFeature f)
      Check whether specified generator feature is enabled.
      Parameters:
      f - Feature to check
      Returns:
      True if feature is enabled; false otherwise
    • canUseSchema

      public boolean canUseSchema(FormatSchema schema)
      Method that can be used to quickly check whether given schema is something that parsers and/or generators constructed by this factory could use. Note that this means possible use, at the level of data format (i.e. schema is for same data format as parsers and generators this factory constructs); individual schema instances may have further usage restrictions.
      Specified by:
      canUseSchema in class TokenStreamFactory
      Parameters:
      schema - Schema instance to check
      Returns:
      Whether parsers and generators constructed by this factory can use specified format schema instance
    • getFormatName

      public String getFormatName()
      Method that returns short textual id identifying format this factory supports.

      Note: sub-classes should override this method; default implementation will return null for all sub-classes

      Specified by:
      getFormatName in class TokenStreamFactory
      Returns:
      Name of the format handled by parsers, generators this factory creates
    • getFormatReadFeatureType

      public Class<? extends FormatFeature> getFormatReadFeatureType()
      Description copied from class: TokenStreamFactory
      Method for accessing kind of FormatFeature that a parser JsonParser produced by this factory would accept, if any; null returned if none.
      Overrides:
      getFormatReadFeatureType in class TokenStreamFactory
      Returns:
      Type of format-specific stream read features, if any; null if none
    • getFormatWriteFeatureType

      public Class<? extends FormatFeature> getFormatWriteFeatureType()
      Description copied from class: TokenStreamFactory
      Method for accessing kind of FormatFeature that a parser JsonGenerator produced by this factory would accept, if any; null returned if none.
      Overrides:
      getFormatWriteFeatureType in class TokenStreamFactory
      Returns:
      Type of format-specific stream read features, if any; null if none
    • getCharacterEscapes

      public CharacterEscapes getCharacterEscapes()
      Method for accessing custom escapes factory uses for JsonGenerators it creates.
      Returns:
      CharacterEscapes configured to be used by parser instances
    • getRootValueSeparator

      public String getRootValueSeparator()
    • createNonBlockingByteArrayParser

      public JsonParser createNonBlockingByteArrayParser(ObjectReadContext readCtxt)
      Description copied from class: TokenStreamFactory
      Optional method for constructing parser for non-blocking parsing via ByteArrayFeeder interface (accessed using JsonParser.nonBlockingInputFeeder() from constructed instance).

      If this factory does not support non-blocking parsing (either at all, or from byte array), will throw UnsupportedOperationException.

      Note that JSON-backed factory only supports parsing of UTF-8 encoded JSON content (and US-ASCII since it is proper subset); other encodings are not supported at this point.

      Overrides:
      createNonBlockingByteArrayParser in class TokenStreamFactory
      Parameters:
      readCtxt - Object read context to use
      Returns:
      Non-blocking parser constructed
    • createNonBlockingByteBufferParser

      public JsonParser createNonBlockingByteBufferParser(ObjectReadContext readCtxt)
      Description copied from class: TokenStreamFactory
      Optional method for constructing parser for non-blocking parsing via ByteBufferFeeder interface (accessed using JsonParser.nonBlockingInputFeeder() from constructed instance).

      If this factory does not support non-blocking parsing (either at all, or from byte array), will throw UnsupportedOperationException.

      Note that JSON-backed factory only supports parsing of UTF-8 encoded JSON content (and US-ASCII since it is proper subset); other encodings are not supported at this point.

      Overrides:
      createNonBlockingByteBufferParser in class TokenStreamFactory
      Parameters:
      readCtxt - Object read context to use
      Returns:
      Non-blocking parser constructed
    • _createNonBlockingContext

      protected IOContext _createNonBlockingContext(Object srcRef)
    • _createParser

      protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ioCtxt, InputStream in) throws JacksonException
      Specified by:
      _createParser in class TextualTSFactory
      Throws:
      JacksonException
    • _createParser

      protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ioCtxt, Reader r) throws JacksonException
      Specified by:
      _createParser in class TextualTSFactory
      Throws:
      JacksonException
    • _createParser

      protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ioCtxt, char[] data, int offset, int len, boolean recyclable) throws JacksonException
      Specified by:
      _createParser in class TextualTSFactory
      Throws:
      JacksonException
    • _createParser

      protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ioCtxt, byte[] data, int offset, int len) throws JacksonException
      Specified by:
      _createParser in class TextualTSFactory
      Throws:
      JacksonException
    • _createParser

      protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ioCtxt, DataInput input) throws JacksonException
      Specified by:
      _createParser in class TextualTSFactory
      Throws:
      JacksonException
    • _createGenerator

      protected JsonGenerator _createGenerator(ObjectWriteContext writeCtxt, IOContext ioCtxt, Writer out) throws JacksonException
      Description copied from class: TextualTSFactory
      Overridable factory method that actually instantiates generator for given Writer and context object.

      This method is specifically designed to remain compatible between minor versions so that sub-classes can count on it being called as expected. That is, it is part of official interface from sub-class perspective, although not a public method available to users of factory implementations.

      Specified by:
      _createGenerator in class TextualTSFactory
      Parameters:
      writeCtxt - Object write context for generator to use
      ioCtxt - IOContext for generator to use
      out - Writer for generator to use
      Returns:
      Generator constructed
      Throws:
      JacksonException - If there is a problem constructing generator
    • _createUTF8Generator

      protected JsonGenerator _createUTF8Generator(ObjectWriteContext writeCtxt, IOContext ioCtxt, OutputStream out) throws JacksonException
      Description copied from class: TextualTSFactory
      Overridable factory method that actually instantiates generator for given OutputStream and context object, using UTF-8 encoding.

      This method is specifically designed to remain compatible between minor versions so that sub-classes can count on it being called as expected. That is, it is part of official interface from sub-class perspective, although not a public method available to users of factory implementations.

      Specified by:
      _createUTF8Generator in class TextualTSFactory
      Parameters:
      writeCtxt - Object write context for generator to use
      ioCtxt - IOContext for generator to use
      out - OutputStream for generator to use
      Returns:
      Generator constructed
      Throws:
      JacksonException - If there is a problem constructing generator
    • constructNameMatcher

      public PropertyNameMatcher constructNameMatcher(List<Named> matches, boolean alreadyInterned)
      Description copied from class: TokenStreamFactory
      Factory method for constructing case-sensitive PropertyNameMatcher for given names. It will call String.intern() on names unless specified that this has already been done by caller.
      Overrides:
      constructNameMatcher in class TokenStreamFactory
      Parameters:
      matches - Names to match, including both primary names and possible aliases
      alreadyInterned - Whether name Strings are already String.intern()ed or not
      Returns:
      Case-sensitive PropertyNameMatcher instance to use
    • constructCINameMatcher

      public PropertyNameMatcher constructCINameMatcher(List<Named> matches, boolean alreadyInterned, Locale locale)
      Description copied from class: TokenStreamFactory
      Factory method for constructing case-insensitive PropertyNameMatcher for given names. It will call String.intern() on names unless specified that this has already been done by caller.
      Overrides:
      constructCINameMatcher in class TokenStreamFactory
      Parameters:
      matches - Names to match, including both primary names and possible aliases
      alreadyInterned - Whether name Strings are already String.intern()ed or not
      locale - Locale to use for case-handling
      Returns:
      Case-insensitive PropertyNameMatcher instance to use