Interface JsonWriter

All Superinterfaces:
AutoCloseable, Closeable, Flushable
All Known Subinterfaces:
BufferedJsonWriter, BytesJsonWriter
All Known Implementing Classes:
DelegateJsonWriter

public interface JsonWriter extends Closeable, Flushable
Writes json content.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Set the property names that will be used for all json generation.
    void
    Write array begin.
    void
    Write object begin.
    void
    Write object being and use the already encoded property names.
    void
    Close the writer.
    void
    Write empty array.
    void
    Write array end.
    void
    Write object end.
    void
    Flush the writer.
    void
    Write a value that could be any value.
    void
    Mark the generated json as not completed due to an error.
    void
    name(int position)
    Set the next property name to write by position.
    void
    name(String name)
    Set the next property name to write.
    void
    Write null value.
    Return the current path.
    void
    pretty(boolean pretty)
    Set tp true to output json in pretty format.
    void
    Write raw JSON content.
    boolean
    Return true if empty collections should be serialised.
    void
    serializeEmpty(boolean serializeEmpty)
    Set to serialise empty collections or not.
    boolean
    Return true if null values should be serialised.
    void
    serializeNulls(boolean serializeNulls)
    Set to serialise null values or not.
    <T> T
    unwrap(Class<T> underlying)
    Unwrap the underlying generator being used.
    void
    value(boolean value)
    Write a boolean value.
    void
    value(byte[] value)
    Write binary content as base64.
    void
    value(double value)
    Write a double value.
    void
    value(int value)
    Write an int value.
    void
    value(long value)
    Write a long value.
    void
    value(Boolean value)
    Write a Boolean value.
    void
    value(Double value)
    Write a Double value.
    void
    value(Integer value)
    Write an Integer value.
    void
    value(Long value)
    Write a Long value.
    void
    value(String value)
    Write a string value.
    void
    Write a BigDecimal value.
    void
    Write a BigInteger value.
    void
    Write new line characters typically for x-json-stream content.
  • Method Details

    • unwrap

      <T> T unwrap(Class<T> underlying)
      Unwrap the underlying generator being used.

      We do this to get access to the underlying generator. For the case when using jackson-core we get access to Jackson JsonGenerator and can then do Jackson specific things like set custom escaping.

      
      
           try (JsonWriter writer = jsonb.writer(new StringWriter())) {
      
             // get access to the underlying Jackson JsonGenerator
             var generator = writer.unwrap(JsonGenerator.class)
      
             // do Jackson specific things like ...
             generator.setCharacterEscapes(new HTMLCharacterEscapes());
      
             jsonb.toJson(myBean, writer);
           }
      
       
    • serializeNulls

      void serializeNulls(boolean serializeNulls)
      Set to serialise null values or not.
    • serializeNulls

      boolean serializeNulls()
      Return true if null values should be serialised.
    • serializeEmpty

      void serializeEmpty(boolean serializeEmpty)
      Set to serialise empty collections or not.
    • serializeEmpty

      boolean serializeEmpty()
      Return true if empty collections should be serialised.
    • pretty

      void pretty(boolean pretty)
      Set tp true to output json in pretty format.
    • path

      String path()
      Return the current path.
    • allNames

      void allNames(PropertyNames names)
      Set the property names that will be used for all json generation.

      These names should be used for all json generation for this generator and set once rather than set per object via beginObject(PropertyNames) (PropertyNames)}.

      This is used by view json generation where all the names are known at the point when the view is created (a sort of flattened nested tree).

    • name

      void name(int position)
      Set the next property name to write by position. This uses the already encoded name values of PropertyNames.
    • name

      void name(String name)
      Set the next property name to write.

      This is generally less efficient than using beginObject(PropertyNames) and name(int).

    • beginArray

      void beginArray()
      Write array begin.
    • endArray

      void endArray()
      Write array end.
    • emptyArray

      void emptyArray()
      Write empty array.
    • beginObject

      void beginObject()
      Write object begin.
    • beginObject

      void beginObject(PropertyNames names)
      Write object being and use the already encoded property names.
    • endObject

      void endObject()
      Write object end.
    • nullValue

      void nullValue()
      Write null value.
    • value

      void value(String value)
      Write a string value.
    • value

      void value(boolean value)
      Write a boolean value.
    • value

      void value(int value)
      Write an int value.
    • value

      void value(long value)
      Write a long value.
    • value

      void value(double value)
      Write a double value.
    • value

      void value(Boolean value)
      Write a Boolean value.
    • value

      void value(Integer value)
      Write an Integer value.
    • value

      void value(Long value)
      Write a Long value.
    • value

      void value(Double value)
      Write a Double value.
    • value

      void value(BigDecimal value)
      Write a BigDecimal value.
    • value

      void value(BigInteger value)
      Write a BigInteger value.
    • value

      void value(byte[] value)
      Write binary content as base64.
    • jsonValue

      void jsonValue(Object value)
      Write a value that could be any value.
    • rawValue

      void rawValue(String value)
      Write raw JSON content.
    • writeNewLine

      void writeNewLine()
      Write new line characters typically for x-json-stream content.
    • flush

      void flush()
      Flush the writer.
      Specified by:
      flush in interface Flushable
    • close

      void close()
      Close the writer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • markIncomplete

      void markIncomplete()
      Mark the generated json as not completed due to an error.

      This typically means not to flush or close an underlying OutputStream which allows it to be reset to then write some error response content instead.