Class GeneratorBase

java.lang.Object
tools.jackson.core.JsonGenerator
tools.jackson.core.base.GeneratorBase
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Versioned
Direct Known Subclasses:
JsonGeneratorBase

public abstract class GeneratorBase extends JsonGenerator
This base class implements part of API that a JSON generator exposes to applications, adds shared internal methods that sub-classes can use and adds some abstract methods sub-classes must implement.
  • Field Details

    • SURR1_FIRST

      public static final int SURR1_FIRST
      See Also:
    • SURR1_LAST

      public static final int SURR1_LAST
      See Also:
    • SURR2_FIRST

      public static final int SURR2_FIRST
      See Also:
    • SURR2_LAST

      public static final int SURR2_LAST
      See Also:
    • WRITE_BINARY

      protected static final String WRITE_BINARY
      See Also:
    • WRITE_BOOLEAN

      protected static final String WRITE_BOOLEAN
      See Also:
    • WRITE_NULL

      protected static final String WRITE_NULL
      See Also:
    • WRITE_NUMBER

      protected static final String WRITE_NUMBER
      See Also:
    • WRITE_RAW

      protected static final String WRITE_RAW
      See Also:
    • WRITE_STRING

      protected static final String WRITE_STRING
      See Also:
    • MAX_BIG_DECIMAL_SCALE

      protected static final int MAX_BIG_DECIMAL_SCALE
      This value is the limit of scale allowed for serializing BigDecimal in "plain" (non-engineering) notation; intent is to prevent asymmetric attack whereupon simple eng-notation with big scale is used to generate huge "plain" serialization. See [core#315] for details.
      See Also:
    • DEFAULT_WRITE_CAPABILITIES

      protected static final JacksonFeatureSet<StreamWriteCapability> DEFAULT_WRITE_CAPABILITIES
      Default set of StreamWriteCapabilityies that may be used as basis for format-specific readers (or as bogus instance if non-null set needs to be passed).
    • DEFAULT_TEXTUAL_WRITE_CAPABILITIES

      protected static final JacksonFeatureSet<StreamWriteCapability> DEFAULT_TEXTUAL_WRITE_CAPABILITIES
      Default set of StreamWriteCapabilityies for typical textual formats, to use either as-is, or as a base with possible differences.
    • DEFAULT_BINARY_WRITE_CAPABILITIES

      protected static final JacksonFeatureSet<StreamWriteCapability> DEFAULT_BINARY_WRITE_CAPABILITIES
      Default set of StreamWriteCapabilityies for typical binary formats, to use either as-is, or as a base with possible differences.
    • _objectWriteContext

      protected final ObjectWriteContext _objectWriteContext
      Context object used both to pass some initial settings and to allow triggering of Object serialization through generator.
      Since:
      3.0
    • _ioContext

      protected final IOContext _ioContext
      Low-level I/O context used mostly for buffer recycling.
    • _streamWriteConstraints

      protected final StreamWriteConstraints _streamWriteConstraints
      Constraints to use for this generator.
    • _streamWriteFeatures

      protected int _streamWriteFeatures
      Bit flag composed of bits that indicate which StreamWriteFeatures are enabled.
    • _closed

      protected boolean _closed
      Flag that indicates whether generator is closed or not. Gets set when it is closed by an explicit call (close()).
  • Constructor Details

  • Method Details

    • isEnabled

      public final boolean isEnabled(StreamWriteFeature f)
      Description copied from class: JsonGenerator
      Method for checking whether given feature is enabled. Check StreamWriteFeature for list of available features.
      Specified by:
      isEnabled in class JsonGenerator
      Parameters:
      f - Feature to check
      Returns:
      True if feature is enabled; false if not
    • streamWriteFeatures

      public final int streamWriteFeatures()
      Description copied from class: JsonGenerator
      Bulk access method for getting state of all standard (format-agnostic) StreamWriteFeatures.
      Specified by:
      streamWriteFeatures in class JsonGenerator
      Returns:
      Bit mask that defines current states of all standard StreamWriteFeatures.
    • configure

      public final JsonGenerator configure(StreamWriteFeature f, boolean state)
      Description copied from class: JsonGenerator
      Method for enabling or disabling specified feature: check StreamWriteFeature for list of available features.

      NOTE: mostly left in 3.0 just to support disabling of StreamWriteFeature.AUTO_CLOSE_CONTENT by jackson-databind

      Specified by:
      configure in class JsonGenerator
      Parameters:
      f - Feature to enable or disable
      state - Whether to enable the feature (true) or disable (false)
      Returns:
      This generator, to allow call chaining
    • streamWriteConstraints

      public final StreamWriteConstraints streamWriteConstraints()
      Description copied from class: JsonGenerator
      Get the constraints to apply when performing streaming writes.
      Overrides:
      streamWriteConstraints in class JsonGenerator
      Returns:
      Constraints used for this generator
    • objectWriteContext

      public ObjectWriteContext objectWriteContext()
      Description copied from class: JsonGenerator
      Accessor for context object provided by higher-level databinding functionality (or, in some cases, simple placeholder of the same) that allows some level of interaction including ability to trigger serialization of Object values through generator instance.
      Specified by:
      objectWriteContext in class JsonGenerator
      Returns:
      Object write context (ObjectWriteContext) associated with this generator
    • ioContext

      public IOContext ioContext()
      Accessor for use by jackson-core itself (tests in particular).
      Returns:
      IOContext in use by this generator
    • writeStartArray

      public JsonGenerator writeStartArray(Object forValue, int size) throws JacksonException
      Description copied from class: JsonGenerator
      Method for writing start marker of an Array value, similar to JsonGenerator.writeStartArray(), but also specifying what is the Java object that the Array Object being written represents (if any) and how many elements will be written for the array before calling JsonGenerator.writeEndArray().
      Specified by:
      writeStartArray in class JsonGenerator
      Parameters:
      forValue - Java Object that Array being written represents, if any (or null if not known or not applicable)
      size - Number of elements this Array will have: actual number of values written (before matching call to JsonGenerator.writeEndArray() MUST match; generator MAY verify this is the case (and SHOULD if format itself encodes length)
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writeStartObject

      public JsonGenerator writeStartObject(Object forValue, int size) throws JacksonException
      Description copied from class: JsonGenerator
      Method for writing starting marker of an Object value to represent the given Java Object value. Argument is offered as metadata, but more importantly it should be assigned as the "current value" for the Object content that gets constructed and initialized. In addition, caller knows number of key/value pairs ("properties") that will get written for the Object value: this is relevant for some format backends (but not, as an example, for JSON).

      Object values can be written in any context where values are allowed: meaning everywhere except for when a property name is expected.

      Specified by:
      writeStartObject in class JsonGenerator
      Parameters:
      forValue - Object value to be written (assigned as "current value" for the Object context that gets created)
      size - Number of key/value pairs this Object will have: actual number of entries written (before matching call to JsonGenerator.writeEndObject() MUST match; generator MAY verify this is the case (and SHOULD if format itself encodes length)
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writeName

      public JsonGenerator writeName(SerializableString name) throws JacksonException
      Description copied from class: JsonGenerator
      Method similar to JsonGenerator.writeName(String), main difference being that it may perform better as some of processing (such as quoting of certain characters, or encoding into external encoding if supported by generator) can be done just once and reused for later calls.

      Default implementation simple uses unprocessed name container in serialized String; implementations are strongly encouraged to make use of more efficient methods argument object has.

      Specified by:
      writeName in class JsonGenerator
      Parameters:
      name - Pre-encoded name of the Object Property to write
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writeString

      public JsonGenerator writeString(Reader reader, int len) throws JacksonException
      Description copied from class: JsonGenerator
      Method for outputting a String value. Depending on context this means either array element, (object) property value or a stand alone String; but in all cases, String will be surrounded in double quotes, and contents will be properly escaped as required by JSON specification. If len is < 0, then write all contents of the reader. Otherwise, write only len characters.

      Note: actual length of content available may exceed len but cannot be less than it: if not enough content available, a StreamWriteException will be thrown.

      Specified by:
      writeString in class JsonGenerator
      Parameters:
      reader - Reader to use for reading Text value to write
      len - Maximum Length of Text value to read (in chars, non-negative) if known; -1 to indicate "read and write it all"
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream (including the case where reader does not provide enough content)
      JacksonException
    • writeString

      public JsonGenerator writeString(SerializableString text) throws JacksonException
      Description copied from class: JsonGenerator
      Method similar to JsonGenerator.writeString(String), but that takes SerializableString which can make this potentially more efficient to call as generator may be able to reuse quoted and/or encoded representation.

      Default implementation just calls JsonGenerator.writeString(String); sub-classes should override it with more efficient implementation if possible.

      Specified by:
      writeString in class JsonGenerator
      Parameters:
      text - Pre-encoded String value to write
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writeRawValue

      public JsonGenerator writeRawValue(String text) throws JacksonException
      Description copied from class: JsonGenerator
      Method that will force generator to copy input text verbatim without any modifications, but assuming it must constitute a single legal JSON value (number, string, boolean, null, Array or List). Assuming this, proper separators are added if and as needed (comma or colon), and generator state updated to reflect this.
      Specified by:
      writeRawValue in class JsonGenerator
      Parameters:
      text - Textual contents to included in output
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writeRawValue

      public JsonGenerator writeRawValue(String text, int offset, int len) throws JacksonException
      Specified by:
      writeRawValue in class JsonGenerator
      Throws:
      JacksonException
    • writeRawValue

      public JsonGenerator writeRawValue(char[] text, int offset, int len) throws JacksonException
      Specified by:
      writeRawValue in class JsonGenerator
      Throws:
      JacksonException
    • writeRawValue

      public JsonGenerator writeRawValue(SerializableString text) throws JacksonException
      Description copied from class: JsonGenerator
      Method similar to JsonGenerator.writeRawValue(String), but potentially more efficient as it may be able to use pre-encoded content (similar to JsonGenerator.writeRaw(SerializableString).
      Overrides:
      writeRawValue in class JsonGenerator
      Parameters:
      text - Pre-encoded textual contents to included in output
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writeBinary

      public int writeBinary(Base64Variant b64variant, InputStream data, int dataLength) throws JacksonException
      Description copied from class: JsonGenerator
      Method similar to JsonGenerator.writeBinary(Base64Variant,byte[],int,int), but where input is provided through a stream, allowing for incremental writes without holding the whole input in memory.
      Specified by:
      writeBinary in class JsonGenerator
      Parameters:
      b64variant - Base64 variant to use
      data - InputStream to use for reading binary data to write. Will not be closed after successful write operation
      dataLength - (optional) number of bytes that will be available; or -1 to be indicate it is not known. If a positive length is given, data MUST provide at least that many bytes: if not, an exception will be thrown. Note that implementations need not support cases where length is not known in advance; this depends on underlying data format: JSON output does NOT require length, other formats may.
      Returns:
      Number of bytes read from data and written as binary payload
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writePOJO

      public JsonGenerator writePOJO(Object value) throws JacksonException
      Description copied from class: JsonGenerator
      Method for writing given Java object (POJO) as tokens into stream this generator manages; serialization must be a valid JSON Value (Object, Array, null, Number, String or Boolean). This is done by delegating call to ObjectWriteContext.writeValue(JsonGenerator, Object).
      Specified by:
      writePOJO in class JsonGenerator
      Parameters:
      value - Java Object (POJO) value to write
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • writeTree

      public JsonGenerator writeTree(TreeNode rootNode) throws JacksonException
      Description copied from class: JsonGenerator
      Method for writing given JSON tree (expressed as a tree where given TreeNode is the root) using this generator. This is done by delegating call to ObjectWriteContext.writeTree(tools.jackson.core.JsonGenerator, tools.jackson.core.TreeNode).
      Specified by:
      writeTree in class JsonGenerator
      Parameters:
      rootNode - TreeNode to write
      Returns:
      This generator, to allow call chaining
      Throws:
      JacksonIOException - if there is an underlying I/O problem
      StreamWriteException - for problems in encoding token stream
      JacksonException
    • isClosed

      public boolean isClosed()
      Description copied from class: JsonGenerator
      Method that can be called to determine whether this generator is closed or not. If it is closed, no more output can be done.
      Specified by:
      isClosed in class JsonGenerator
      Returns:
      True if this generator has been closed; false if not
    • close

      public void close()
      Description copied from class: JsonGenerator
      Method called to close this generator, so that no more content can be written.

      Whether the underlying target (stream, writer) gets closed depends on whether this generator either manages the target (i.e. is the only one with access to the target -- case if caller passes a reference to the resource such as File, but not stream); or has feature StreamWriteFeature.AUTO_CLOSE_TARGET enabled. If either of above is true, the target is also closed. Otherwise (not managing, feature not enabled), target is not closed.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class JsonGenerator
    • _closeInput

      protected abstract void _closeInput() throws IOException
      Throws:
      IOException
    • _releaseBuffers

      protected abstract void _releaseBuffers()
      Method called to release any buffers generator may be holding, once generator is being closed.
    • _verifyValueWrite

      protected abstract void _verifyValueWrite(String typeMsg) throws JacksonException
      Method called before trying to write a value (scalar or structured), to verify that this is legal in current output state, as well as to output separators if and as necessary.
      Parameters:
      typeMsg - Additional message used for generating exception message if value output is NOT legal in current generator output state.
      Throws:
      JacksonException - if there is a problem in trying to write a value
    • _constructDefaultPrettyPrinter

      protected PrettyPrinter _constructDefaultPrettyPrinter()
      Overridable factory method called to instantiate an appropriate PrettyPrinter for case of "just use the default one", when default pretty printer handling enabled.
      Returns:
      Instance of "default" pretty printer to use
    • _asString

      protected String _asString(BigDecimal value) throws JacksonException
      Helper method used to serialize a BigDecimal as a String, for serialization, taking into account configuration settings
      Parameters:
      value - BigDecimal value to convert to String
      Returns:
      String representation of value
      Throws:
      JacksonException - if there is a problem serializing value as String
    • _decodeSurrogate

      protected final int _decodeSurrogate(int surr1, int surr2) throws JacksonException
      Throws:
      JacksonException
    • _checkRangeBoundsForByteArray

      protected void _checkRangeBoundsForByteArray(byte[] data, int offset, int len) throws JacksonException
      Throws:
      JacksonException
    • _checkRangeBoundsForCharArray

      protected void _checkRangeBoundsForCharArray(char[] data, int offset, int len) throws JacksonException
      Throws:
      JacksonException
    • _checkRangeBoundsForString

      protected void _checkRangeBoundsForString(String data, int offset, int len) throws JacksonException
      Throws:
      JacksonException
    • _throwInternal

      protected void _throwInternal()