Class JsonBuffer

java.lang.Object
io.jstach.rainbowgum.json.JsonBuffer
All Implemented Interfaces:
LogEncoder.Buffer, AutoCloseable

public final class JsonBuffer extends Object implements LogEncoder.Buffer
A buffer designed for encoding JSON efficiently.
  • Field Details

  • Constructor Details

    • JsonBuffer

      public JsonBuffer(boolean prettyPrint, JsonBuffer.ExtendedFieldPrefix extendedFieldPrefix)
      Create a JSON buffer.
      Parameters:
      prettyPrint - whether or not to pretty print the JSON.
      extendedFieldPrefix - prefix for extended fields.
  • Method Details

    • drain

      public void drain(LogOutput output, LogEvent event)
      Description copied from interface: LogEncoder.Buffer
      The appender will call this usually within a lock to transfer content from the buffer to the output.
      Specified by:
      drain in interface LogEncoder.Buffer
      Parameters:
      output - output to receive content.
      event - log event.
    • clear

      public void clear()
      Description copied from interface: LogEncoder.Buffer
      Prepare the buffer for reuse.

      An appender may not call clear before being passed to the encoder so the encoder should do its own clearing.

      Specified by:
      clear in interface LogEncoder.Buffer
    • getFormattedMessageBuilder

      Reusable String buffer for formatted messages.
      Returns:
      buffer.
    • write

      public final void write(JsonBuffer.JSONToken token)
      Writes a JSON token.
      Parameters:
      token - token not null.
    • writeLineFeed

      public final void writeLineFeed()
      Efficiently writes a line feed.
    • write

      public final int write(String k, @Nullable String v, int index)
      Writes a string field.
      Parameters:
      k - field name
      v - value
      index - the current index for comma determination
      Returns:
      index + 1
    • write

      public final int write(String k, @Nullable String v, int index, int flag)
      Writes a string field.
      Parameters:
      k - field name
      v - value
      index - the current index for comma determination
      flag - see EXTENDED_F
      Returns:
      index + 1
    • writeDouble

      public final int writeDouble(String k, double v, int index, int flag)
      Writes a double field.
      Parameters:
      k - field name
      v - value
      index - the current index for comma determination
      flag - see EXTENDED_F
      Returns:
      index + 1
    • writeInt

      public final int writeInt(String k, int v, int index, int flag)
      Writes a string field.
      Parameters:
      k - field name
      v - value
      index - the current index for comma determination
      flag - see EXTENDED_F
      Returns:
      index + 1
    • writeLong

      public final int writeLong(String k, long v, int index, int flag)
      Writes a long field.
      Parameters:
      k - field name
      v - value
      index - the current index for comma determination
      flag - see EXTENDED_F
      Returns:
      index + 1
    • writeObjectStart

      public final int writeObjectStart(String k, int index, int flag)
      Starts a nested JSON object as a field value, e.g. "key":{. Fields written after this and before the matching writeObjectEnd() should use the returned index (a fresh comma-counter local to the nested object) rather than the outer index.
      Parameters:
      k - field name
      index - the current index (of the enclosing object) for comma determination
      flag - see EXTENDED_F
      Returns:
      0, the starting index for fields inside the nested object
    • writeObjectEnd

      public final void writeObjectEnd()
      Ends a nested JSON object previously started with writeObjectStart(String, int, int).
    • writeArrayStart

      public final int writeArrayStart(String k, int index, int flag)
      Starts a nested JSON array as a field value, e.g. "key":[. Elements written after this and before the matching writeArrayEnd() should use writeArrayElementObjectStart(int) (with a fresh, local index) rather than the outer index.
      Parameters:
      k - field name
      index - the current index (of the enclosing object) for comma determination
      flag - see EXTENDED_F
      Returns:
      0, the starting index for elements inside the nested array
    • writeArrayEnd

      public final void writeArrayEnd()
      Ends a nested JSON array previously started with writeArrayStart(String, int, int).
    • writeArrayElementObjectStart

      public final int writeArrayElementObjectStart(int index)
      Starts an anonymous JSON object as an array element (no field name), e.g. the { in [{...},{...}].
      Parameters:
      index - the current index (within the array) for comma determination
      Returns:
      0, the starting index for fields inside the element object
    • writeArrayElementObjectEnd

      public final void writeArrayElementObjectEnd()
      Ends an array element object previously started with writeArrayElementObjectStart(int).