Class JSONWriter

java.lang.Object
com.alibaba.fastjson2.JSONWriter
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
JSONWriterUTF16JDK8UF

public abstract class JSONWriter extends Object implements Closeable
JSONWriter is the core class for writing and serializing Java objects to JSON format in FASTJSON2. It provides methods to convert various Java data types into JSON representation, supporting both standard JSON and binary JSON (JSONB) formats.

JSONWriter supports multiple output destinations including strings, byte arrays, streams, and writers. It also supports different character encodings such as UTF-8, UTF-16, and others.

Example usage:

 // Writing to a string
 try (JSONWriter writer = JSONWriter.of()) {
     writer.writeAny(object);
     String json = writer.toString();
 }

 // Writing to a stream
 try (ByteArrayOutputStream out = new ByteArrayOutputStream();
      JSONWriter writer = JSONWriter.of(out, StandardCharsets.UTF_8)) {
     writer.writeAny(object);
     byte[] jsonBytes = out.toByteArray();
 }

 // Writing with specific features
 try (JSONWriter writer = JSONWriter.of(JSONWriter.Feature.PrettyFormat)) {
     writer.writeAny(object);
     String prettyJson = writer.toString();
 }
 

JSONWriter instances are not thread-safe and should not be shared between multiple threads. Each thread should create its own JSONWriter instance or use the factory methods to create new instances as needed.

Since:
2.0.0
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Context holds the configuration and state information for JSON writing operations.
    static enum 
    Feature is used to control the behavior of JSON writing and serialization in FASTJSON2.
    static final class 
    Path represents a JSON pointer path used for reference detection during serialization.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Object
     
    protected final Charset
     
     
    final boolean
     
    protected String
     
    protected int
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected static final long
     
    protected final int
     
    protected int
     
    protected JSONWriter.Path
     
    protected byte
     
    protected final char
     
     
    protected Object
     
    protected boolean
     
     
    final boolean
     
    final boolean
     
    final boolean
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    JSONWriter(JSONWriter.Context context, SymbolTable symbolTable, boolean jsonb, Charset charset)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    Adds a manager reference for the specified object.
    final void
    checkAndWriteTypeName(Object object, Class fieldClass)
    Checks if type name should be written for the given object and writes it if necessary.
    abstract void
    Closes this JSONWriter and releases any resources associated with it.
    final void
    Configures features for this JSONWriter.
    final void
    config(JSONWriter.Feature feature, boolean state)
    Configures a specific feature for this JSONWriter.
    final boolean
    Checks if the specified object is contained in the reference map.
    final void
    Deprecated.
    This method is deprecated and will be removed in a future version
    abstract void
    Ends the current JSON array.
    abstract void
    Ends writing a JSON object.
    abstract Object
    ensureCapacity(int minCapacity)
    Ensures that the internal buffer has at least the specified minimum capacity.
    abstract int
    Flushes the content of this JSONWriter to the specified OutputStream.
    abstract int
    flushTo(OutputStream out, Charset charset)
    Flushes the content of this JSONWriter to the specified OutputStream using the specified charset.
    void
    Flushes the content of this JSONWriter to the specified Writer.
    Gets the attachment object associated with this JSONWriter.
    abstract byte[]
    Gets the content of the output buffer as a byte array.
    abstract byte[]
    getBytes(Charset charset)
    Gets the content of the output buffer as a byte array using the specified charset.
    final Charset
    Gets the charset used by this JSONWriter.
    Gets the context used by this JSONWriter.
    final long
    Gets the features bitmask.
    final long
    getFeatures(long features)
    Gets the combined features bitmask including the specified additional features.
    getObjectWriter(Class objectClass)
    Gets the ObjectWriter for the specified object class.
    getObjectWriter(Class objectClass, String format)
    Gets the ObjectWriter for the specified object class with a specific format.
    getObjectWriter(Type objectType, Class objectClass)
    Gets the ObjectWriter for the specified object type and class.
    int
    Gets the current offset in the internal buffer.
    If ReferenceDetection has been set, returns the path of the current object, otherwise returns null
    final String
    getPath(Object value)
    Gets the path of the specified object in the reference map.
    Gets the symbol table used by this JSONWriter.
    final boolean
    Checks if any filter is configured for this JSONWriter.
    final boolean
    hasFilter(boolean containsNoneFieldGetter)
    Checks if any filter is configured for this JSONWriter or if the IgnoreNonFieldGetter feature should be applied based on the containsNoneFieldGetter parameter.
    final boolean
    hasFilter(long feature)
    Checks if any filter or the specified feature is configured for this JSONWriter.
    protected static IllegalArgumentException
    illegalYear(int year)
     
    final void
    Deprecated.
    This method is deprecated and will be removed in a future version
    final boolean
    Checks if the BeanToArray feature is enabled.
    final boolean
    isEnabled(long feature)
    Checks if the specified feature is enabled.
    final boolean
    Checks if the specified feature is enabled.
    final boolean
    Checks if the IgnoreErrorGetter feature is enabled.
    final boolean
    Checks if the IgnoreNoneSerializable feature is enabled.
    final boolean
    Checks if the IgnoreNoneSerializable feature is enabled for the specified object.
    final boolean
    Checks if the ReferenceDetection feature is enabled.
    final boolean
    Checks if the ReferenceDetection feature is enabled for the specified object.
    final boolean
    Checks if single quotes are being used for this JSONWriter.
    final boolean
    Checks if this JSONWriter is using UTF-16 encoding.
    final boolean
    Checks if this JSONWriter is using UTF-8 encoding.
    protected static boolean
    isWriteAsString(long value, long features)
     
    protected static boolean
    isWriteAsString(BigDecimal value, long features)
     
    protected static boolean
    isWriteAsString(BigInteger value, long features)
     
    final boolean
    isWriteMapTypeInfo(Object object, Class fieldClass, long features)
    Checks if map type information should be written for the specified object, field class, and features.
    final boolean
    Checks if the WriteNulls feature is enabled.
    final boolean
    Checks if type information should be written for the specified object.
    final boolean
    isWriteTypeInfo(Object object, long features)
    Checks if type information should be written for the specified object and features.
    final boolean
    isWriteTypeInfo(Object object, Class fieldClass)
    Checks if type information should be written for the specified object and field class.
    final boolean
    isWriteTypeInfo(Object object, Class fieldClass, long features)
    Checks if type information should be written for the specified object, field class, and features.
    final boolean
    isWriteTypeInfo(Object object, Type fieldType)
    Checks if type information should be written for the specified object and field type.
    final boolean
    isWriteTypeInfo(Object object, Type fieldType, long features)
    Checks if type information should be written for the specified object, field type, and features.
    final int
    Gets the current nesting level of this JSONWriter.
    protected final int
    newCapacity(int minCapacity, int oldCapacity)
    Calculates the new capacity for the internal buffer based on the minimum required capacity.
    static JSONWriter
    of()
    Creates a new JSONWriter with default configuration.
    static JSONWriter
    Creates a new JSONWriter with the specified context.
    static JSONWriter
    of(JSONWriter.Feature... features)
    Creates a new JSONWriter with the specified features.
    static JSONWriter
    of(ObjectWriterProvider provider, JSONWriter.Feature... features)
    Creates a new JSONWriter with the specified object writer provider and features.
    static JSONWriter
    Creates a new JSONWriter instance for JSONB (binary JSON) format with default context.
    static JSONWriter
    Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified context.
    static JSONWriter
    ofJSONB(JSONWriter.Context context, SymbolTable symbolTable)
    Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified context and symbol table.
    static JSONWriter
    Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified features.
    static JSONWriter
    ofJSONB(SymbolTable symbolTable)
    Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified symbol table.
    static JSONWriter
    Creates a new JSONWriter instance with pretty formatting enabled.
    static JSONWriter
    Enables pretty formatting on an existing JSONWriter instance.
    static JSONWriter
    Creates a new JSONWriter instance using UTF-16 encoding with the specified features.
    static JSONWriter
    Creates a new JSONWriter instance using UTF-8 encoding with default context.
    static JSONWriter
    Creates a new JSONWriter instance using UTF-8 encoding with the specified context.
    static JSONWriter
    Creates a new JSONWriter instance using UTF-8 encoding with the specified features.
    protected final void
    Throws a JSONException indicating that the nesting level has exceeded the maximum allowed level.
    final void
    popPath(Object object)
    Removes the path for the specified object.
    final void
    popPath0(Object object)
    Removes the path for the specified object without reference detection.
    void
    Deprecated.
    This method is deprecated and will be removed in a future version
    final boolean
    Removes the reference to the specified object.
    void
    setAttachment(Object attachment)
    Sets the attachment object for this JSONWriter.
    void
    setOffset(int offset)
    Sets the offset in the internal buffer.
    final String
    setPath(int index, Object object)
    Sets the path for the specified object at the given index.
    final String
    setPath(FieldWriter fieldWriter, Object object)
    Sets the path for the specified object using the provided field writer.
    final String
    setPath(String name, Object object)
    Sets the path for the specified object with the given name.
    final String
    setPath0(int index, Object object)
    Sets the path for the specified object at the given index without reference detection.
    final String
    setPath0(FieldWriter fieldWriter, Object object)
    Sets the path for the specified object using the provided field writer without reference detection.
    final void
    setRootObject(Object rootObject)
    Sets the root object for this JSONWriter.
    abstract int
    Gets the current size of the output buffer.
    abstract void
    Starts writing a JSON array.
    void
    startArray(int size)
    Starts writing a JSON array with a specified initial capacity.
    void
    startArray(Object array, int size)
    Starts writing a JSON array with the specified array and size.
    void
    Starts writing a JSON array with zero expected elements.
    void
    Starts writing a JSON array with one expected element.
    void
    Starts writing a JSON array with ten expected elements.
    void
    Starts writing a JSON array with eleven expected elements.
    void
    Starts writing a JSON array with twelve expected elements.
    void
    Starts writing a JSON array with thirteen expected elements.
    void
    Starts writing a JSON array with fourteen expected elements.
    void
    Starts writing a JSON array with fifteen expected elements.
    void
    Starts writing a JSON array with two expected elements.
    void
    Starts writing a JSON array with three expected elements.
    void
    Starts writing a JSON array with four expected elements.
    void
    Starts writing a JSON array with five expected elements.
    void
    Starts writing a JSON array with six expected elements.
    void
    Starts writing a JSON array with seven expected elements.
    void
    Starts writing a JSON array with eight expected elements.
    void
    Starts writing a JSON array with nine expected elements.
    abstract void
    Starts writing a JSON object.
    final void
    Writes a JSONObject as a JSON object.
    abstract void
    write(List array)
    Writes a list as a JSON array.
    void
    write(Map<?,?> map)
    Writes a Map as a JSON object.
    protected abstract void
    write0(char ch)
    Writes a character to the output.
    void
    Writes any object using the appropriate writer based on its runtime type.
    void
    Writes a null array value using the current context features.
    void
    writeArrayNull(long features)
    Writes a null array value using the specified features.
    final void
    writeAs(Object value, Class type)
    Writes an object as if it were of the specified type.
    abstract void
    writeBase64(byte[] bytes)
    Writes a byte array as Base64-encoded string.
    final void
    Writes a BigInteger value using default features.
    abstract void
    writeBigInt(BigInteger value, long features)
    Writes a BigInteger value using the specified features.
    void
    writeBinary(byte[] bytes)
    Writes a byte array as either Base64-encoded string or as an array of integers, depending on the WriteByteArrayAsBase64 feature.
    abstract void
    writeBool(boolean value)
    Writes a boolean value.
    void
    writeBool(boolean[] value)
    Writes a boolean array as boolean values.
    final void
    Writes a null boolean value using the current context features.
    abstract void
    writeChar(char ch)
    Writes a character without any escaping or formatting.
    abstract void
    Writes a colon separator.
    abstract void
    Writes a comma separator.
    abstract void
    writeDateTime14(int year, int month, int dayOfMonth, int hour, int minute, int second)
    Writes a date-time value in 14-character format (yyyyMMddHHmmss).
    abstract void
    writeDateTime19(int year, int month, int dayOfMonth, int hour, int minute, int second)
    Writes a date-time value in 19-character format (yyyy-MM-dd HH:mm:ss).
    abstract void
    writeDateTimeISO8601(int year, int month, int dayOfMonth, int hour, int minute, int second, int millis, int offsetSeconds, boolean timeZone)
    Writes a date-time value in ISO8601 format.
    abstract void
    writeDateYYYMMDD10(int year, int month, int dayOfMonth)
    Writes a date in 10-character format (yyyy-MM-dd).
    abstract void
    writeDateYYYMMDD8(int year, int month, int dayOfMonth)
    Writes a date in 8-character format (yyyyMMdd).
    final void
    Writes a BigDecimal value using default features and no specific format.
    final void
    writeDecimal(BigDecimal value, long features)
    Writes a BigDecimal value using the specified features and no specific format.
    abstract void
    writeDecimal(BigDecimal value, long features, DecimalFormat format)
    Writes a BigDecimal value using the specified features and decimal format.
    final void
    Writes a null decimal value using the current context features.
    final void
    writeDecimalNull(long features)
    Writes a null decimal value using the specified features.
    abstract void
    writeDouble(double value)
    Writes a double value.
    abstract void
    writeDouble(double[] value)
    Writes a double array as floating-point numbers.
    final void
    writeDouble(double[] value, DecimalFormat format)
    Writes a double array as floating-point numbers with the specified decimal format.
    final void
    writeDouble(double value, DecimalFormat format)
    Writes a double value with the specified decimal format.
    void
    writeDoubleArray(double value0, double value1)
    Writes a double array with two elements.
    void
    Writes an Enum value.
    abstract void
    writeFloat(float value)
    Writes a float value.
    abstract void
    writeFloat(float[] value)
     
    final void
    writeFloat(float[] value, DecimalFormat format)
    Writes a float array with the specified decimal format.
    final void
    writeFloat(float value, DecimalFormat format)
    Writes a float value with the specified decimal format.
    final void
    Writes a Float object.
    abstract void
    writeHex(byte[] bytes)
    Writes a byte array as hexadecimal string.
    void
    Writes an Instant value as an ISO-8601 formatted string.
    abstract void
    writeInt16(short value)
    Writes a short value as an integer.
    void
    writeInt16(short[] value)
    Writes a short array as integers.
    abstract void
    writeInt32(int value)
    Writes an int value.
    abstract void
    writeInt32(int[] value)
    Writes an int array as integers.
    final void
    writeInt32(int value, String format)
    Writes an int value with the specified string format.
    final void
    writeInt32(int value, DecimalFormat format)
    Writes an int value with the specified decimal format.
    abstract void
    Writes an Integer object.
    abstract void
    writeInt64(long i)
    Writes a long value.
    abstract void
    writeInt64(long[] value)
    Writes a long array as integers.
    abstract void
    Writes a Long object.
    final void
    Writes a null long value using the current context features.
    abstract void
    writeInt8(byte value)
    Writes a byte value as an integer.
    abstract void
    writeInt8(byte[] value)
    Writes a byte array as integers.
    abstract void
    Writes a list of Integer values as integers.
    abstract void
    Writes a list of Long values as integers.
    abstract void
    Writes a LocalDate value.
    abstract void
    Writes a LocalDateTime value.
    protected final boolean
     
    abstract void
    Writes a LocalTime value.
    void
    writeMillis(long i)
    Writes a timestamp value as a long integer.
    final void
    writeName(int name)
    Writes a field name as an integer value in a JSON object.
    final void
    writeName(long name)
    Writes a field name as a long value in a JSON object.
    void
     
    abstract void
    writeName10Raw(long name0, long name1)
    Writes a 10-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName11Raw(long name0, long name2)
    Writes an 11-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName12Raw(long name0, long name2)
    Writes a 12-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName13Raw(long name0, long name2)
    Writes a 13-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName14Raw(long name0, long name2)
    Writes a 14-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName15Raw(long name0, long name2)
    Writes a 15-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName16Raw(long name0, long name2)
    Writes a 16-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName2Raw(long name)
    Writes a 2-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName3Raw(long name)
    Writes a 3-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName4Raw(long name)
    Writes a 4-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName5Raw(long name)
    Writes a 5-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName6Raw(long name)
    Writes a 6-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName7Raw(long name)
    Writes a 7-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName8Raw(long name0)
    Writes an 8-character field name as raw bytes without any escaping or formatting.
    abstract void
    writeName9Raw(long name0, int name1)
    Writes a 9-character field name as raw bytes without any escaping or formatting.
    void
    Writes a field name of any type in a JSON object.
    abstract void
    writeNameRaw(byte[] bytes)
    Writes raw bytes representing a field name without any escaping or formatting.
    void
    writeNameRaw(byte[] bytes, int offset, int len)
    Writes raw bytes representing a field name without any escaping or formatting.
    void
    writeNameRaw(byte[] name, long nameHash)
    Writes raw bytes representing a field name without any escaping or formatting.
    abstract void
    writeNameRaw(char[] chars)
    Writes raw characters representing a field name without any escaping or formatting.
    abstract void
    writeNameRaw(char[] bytes, int offset, int len)
    Writes raw characters representing a field name without any escaping or formatting.
    final void
    Writes a field name and its value as a key-value pair in a JSON object.
    abstract void
    Writes a null value.
    final void
    Writes a null number value using the current context features.
    final void
    writeNumberNull(long features)
    Writes a null number value using the specified features.
    void
    writeObjectNull(Class<?> fieldClass)
    Writes a null object value.
    abstract void
    Writes an OffsetDateTime value.
    abstract void
    Writes an OffsetTime value.
    void
    writeRaw(byte b)
    Writes a raw byte without any escaping or formatting.
    abstract void
    writeRaw(byte[] bytes)
    Writes raw bytes without any escaping or formatting.
    abstract void
    writeRaw(char ch)
    Writes a raw character without any escaping or formatting.
    final void
    writeRaw(char[] chars)
    Writes raw characters without any escaping or formatting.
    void
    writeRaw(char[] chars, int off, int charslen)
    Writes raw characters without any escaping or formatting.
    void
    writeRaw(char c0, char c1)
    Writes two raw characters without any escaping or formatting.
    abstract void
    Writes a raw string without any escaping or formatting.
    final boolean
    writeReference(int index, Object object)
    Writes a reference to the specified object at the given index.
    final void
    Deprecated.
    This method is deprecated and will be removed in a future version
    abstract void
    Writes a reference to a previously serialized object.
    abstract void
    writeString(boolean value)
    Writes a boolean value as a string.
    void
    writeString(boolean[] value)
    Writes a boolean array as strings.
    abstract void
    writeString(byte value)
    Writes a byte value as a string.
    void
    writeString(byte[] value)
    Writes a byte array as strings.
    abstract void
    writeString(char[] chars)
    Writes a string from character array.
    abstract void
    writeString(char[] chars, int off, int len)
    Writes a string from character array with specified offset and length.
    abstract void
    writeString(char[] chars, int off, int len, boolean quote)
    Writes a string from character array with specified offset and length.
    void
    writeString(double value)
    Writes a double value as a string.
    void
    writeString(double[] value)
    Writes a double array as strings.
    void
    writeString(float value)
    Writes a float value as a string.
    void
    writeString(float[] value)
    Writes a float array as strings.
    abstract void
    writeString(int value)
    Writes an int value as a string.
    void
    writeString(int[] value)
    Writes an int array as strings.
    abstract void
    writeString(long value)
    Writes a long value as a string.
    void
    writeString(long[] value)
    Writes a long array as strings.
    abstract void
    writeString(short value)
    Writes a short value as a string.
    void
    writeString(short[] value)
    Writes a short array as strings.
    final void
    Writes a string from a Reader.
    abstract void
    Writes a string value.
    void
    writeString(String[] strings)
    Writes an array of strings as a JSON array.
    void
    Writes a list of strings as a JSON array.
    abstract void
    writeStringLatin1(byte[] value)
    Writes a string from Latin-1 encoded bytes.
    void
    Writes a null string value.
    abstract void
    writeStringUTF16(byte[] value)
    Writes a string from UTF-16 encoded bytes.
    void
    writeSymbol(int symbol)
    Writes a symbol as a JSON value.
    void
    Writes a symbol string.
    abstract void
    writeTimeHHMMSS8(int hour, int minute, int second)
    Writes a time in 8-character format (HH:mm:ss).
    boolean
    writeTypeName(byte[] typeName, long typeNameHash)
    Writes a type name for the current object using byte array and hash.
    void
    Writes a type name for the current object.
    abstract void
    writeUUID(UUID value)
    Writes a UUID value.
    abstract void
    Writes a ZonedDateTime value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • context

      public final JSONWriter.Context context
    • utf8

      public final boolean utf8
    • utf16

      public final boolean utf16
    • jsonb

      public final boolean jsonb
    • useSingleQuote

      public final boolean useSingleQuote
    • symbolTable

      public final SymbolTable symbolTable
    • charset

      protected final Charset charset
    • quote

      protected final char quote
    • maxArraySize

      protected final int maxArraySize
    • startObject

      protected boolean startObject
    • level

      protected int level
    • off

      protected int off
    • rootObject

      protected Object rootObject
    • refs

    • path

      protected JSONWriter.Path path
    • lastReference

      protected String lastReference
    • pretty

      protected byte pretty
    • attachment

      protected Object attachment
    • MASK_WRITE_MAP_NULL_VALUE

      protected static final long MASK_WRITE_MAP_NULL_VALUE
      See Also:
    • MASK_BROWSER_COMPATIBLE

      protected static final long MASK_BROWSER_COMPATIBLE
      See Also:
    • MASK_NULL_AS_DEFAULT_VALUE

      protected static final long MASK_NULL_AS_DEFAULT_VALUE
      See Also:
    • MASK_WRITE_BOOLEAN_AS_NUMBER

      protected static final long MASK_WRITE_BOOLEAN_AS_NUMBER
      See Also:
    • MASK_WRITE_NON_STRING_VALUE_AS_STRING

      protected static final long MASK_WRITE_NON_STRING_VALUE_AS_STRING
      See Also:
    • MASK_WRITE_CLASS_NAME

      protected static final long MASK_WRITE_CLASS_NAME
      See Also:
    • MASK_NOT_WRITE_DEFAULT_VALUE

      protected static final long MASK_NOT_WRITE_DEFAULT_VALUE
      See Also:
    • MASK_WRITE_ENUMS_USING_NAME

      protected static final long MASK_WRITE_ENUMS_USING_NAME
      See Also:
    • MASK_WRITE_ENUM_USING_TO_STRING

      protected static final long MASK_WRITE_ENUM_USING_TO_STRING
      See Also:
    • MASK_PRETTY_FORMAT

      protected static final long MASK_PRETTY_FORMAT
      See Also:
    • MASK_REFERENCE_DETECTION

      protected static final long MASK_REFERENCE_DETECTION
      See Also:
    • MASK_USE_SINGLE_QUOTES

      protected static final long MASK_USE_SINGLE_QUOTES
      See Also:
    • MASK_WRITE_NULL_LIST_AS_EMPTY

      protected static final long MASK_WRITE_NULL_LIST_AS_EMPTY
      See Also:
    • MASK_WRITE_NULL_STRING_AS_EMPTY

      protected static final long MASK_WRITE_NULL_STRING_AS_EMPTY
      See Also:
    • MASK_WRITE_NULL_NUMBER_AS_ZERO

      protected static final long MASK_WRITE_NULL_NUMBER_AS_ZERO
      See Also:
    • MASK_WRITE_NULL_BOOLEAN_AS_FALSE

      protected static final long MASK_WRITE_NULL_BOOLEAN_AS_FALSE
      See Also:
    • MASK_NOT_WRITE_EMPTY_ARRAY

      protected static final long MASK_NOT_WRITE_EMPTY_ARRAY
      See Also:
    • MASK_ESCAPE_NONE_ASCII

      protected static final long MASK_ESCAPE_NONE_ASCII
      See Also:
    • MASK_IGNORE_NON_FIELD_GETTER

      protected static final long MASK_IGNORE_NON_FIELD_GETTER
      See Also:
    • MASK_WRITE_LONG_AS_STRING

      protected static final long MASK_WRITE_LONG_AS_STRING
      See Also:
    • MASK_BROWSER_SECURE

      protected static final long MASK_BROWSER_SECURE
      See Also:
    • MASK_NOT_WRITE_NUMBER_CLASS_NAME

      protected static final long MASK_NOT_WRITE_NUMBER_CLASS_NAME
      See Also:
  • Constructor Details

  • Method Details

    • getCharset

      public final Charset getCharset()
      Gets the charset used by this JSONWriter.
      Returns:
      the charset
    • isUTF8

      public final boolean isUTF8()
      Checks if this JSONWriter is using UTF-8 encoding.
      Returns:
      true if using UTF-8 encoding, false otherwise
    • isUTF16

      public final boolean isUTF16()
      Checks if this JSONWriter is using UTF-16 encoding.
      Returns:
      true if using UTF-16 encoding, false otherwise
    • isIgnoreNoneSerializable

      public final boolean isIgnoreNoneSerializable()
      Checks if the IgnoreNoneSerializable feature is enabled.
      Returns:
      true if the feature is enabled, false otherwise
    • isIgnoreNoneSerializable

      public final boolean isIgnoreNoneSerializable(Object object)
      Checks if the IgnoreNoneSerializable feature is enabled for the specified object.
      Parameters:
      object - the object to check
      Returns:
      true if the feature is enabled and the object is not serializable, false otherwise
    • getSymbolTable

      public final SymbolTable getSymbolTable()
      Gets the symbol table used by this JSONWriter.
      Returns:
      the symbol table, or null if not set
    • config

      public final void config(JSONWriter.Feature... features)
      Configures features for this JSONWriter.
      Parameters:
      features - the features to enable
    • config

      public final void config(JSONWriter.Feature feature, boolean state)
      Configures a specific feature for this JSONWriter.
      Parameters:
      feature - the feature to configure
      state - true to enable the feature, false to disable it
    • getContext

      public final JSONWriter.Context getContext()
      Gets the context used by this JSONWriter.
      Returns:
      the context
    • level

      public final int level()
      Gets the current nesting level of this JSONWriter.
      Returns:
      the current nesting level
    • setRootObject

      public final void setRootObject(Object rootObject)
      Sets the root object for this JSONWriter. This method initializes the root object and sets the path to the root path.
      Parameters:
      rootObject - the root object to set
    • setPath

      public final String setPath(String name, Object object)
      Sets the path for the specified object with the given name. This method is used for reference detection during serialization.
      Parameters:
      name - the name of the path segment
      object - the object to set the path for
      Returns:
      the previous path as a string, or null if no previous path exists
    • setPath

      public final String setPath(FieldWriter fieldWriter, Object object)
      Sets the path for the specified object using the provided field writer. This method is used for reference detection during serialization.
      Parameters:
      fieldWriter - the field writer to use for path generation
      object - the object to set the path for
      Returns:
      the previous path as a string, or null if no previous path exists
    • setPath0

      public final String setPath0(FieldWriter fieldWriter, Object object)
      Sets the path for the specified object using the provided field writer without reference detection. This method is used for reference detection during serialization.
      Parameters:
      fieldWriter - the field writer to use for path generation
      object - the object to set the path for
      Returns:
      the previous path as a string, or null if no previous path exists
    • addManagerReference

      public final void addManagerReference(Object object)
      Adds a manager reference for the specified object. This method adds a special reference to the object in the reference map, marking it as a manager reference.
      Parameters:
      object - the object to add a manager reference for
    • writeReference

      public final boolean writeReference(int index, Object object)
      Writes a reference to the specified object at the given index. This method sets the path for the object and writes a reference if one already exists.
      Parameters:
      index - the index to set the path for
      object - the object to write a reference for
      Returns:
      true if a reference was written, false otherwise
    • setPath

      public final String setPath(int index, Object object)
      Sets the path for the specified object at the given index. This method is used for reference detection during serialization of array elements.
      Parameters:
      index - the index to set the path for
      object - the object to set the path for
      Returns:
      the previous path as a string, or null if no previous path exists
    • setPath0

      public final String setPath0(int index, Object object)
      Sets the path for the specified object at the given index without reference detection. This method is used for reference detection during serialization of array elements.
      Parameters:
      index - the index to set the path for
      object - the object to set the path for
      Returns:
      the previous path as a string, or null if no previous path exists
    • popPath

      public final void popPath(Object object)
      Removes the path for the specified object. This method is used to clean up path information during serialization.
      Parameters:
      object - the object to remove the path for
    • popPath0

      public final void popPath0(Object object)
      Removes the path for the specified object without reference detection. This method is used to clean up path information during serialization.
      Parameters:
      object - the object to remove the path for
    • hasFilter

      public final boolean hasFilter()
      Checks if any filter is configured for this JSONWriter.
      Returns:
      true if any filter is configured, false otherwise
    • hasFilter

      public final boolean hasFilter(long feature)
      Checks if any filter or the specified feature is configured for this JSONWriter.
      Parameters:
      feature - the feature to check
      Returns:
      true if any filter is configured or the specified feature is enabled, false otherwise
    • hasFilter

      public final boolean hasFilter(boolean containsNoneFieldGetter)
      Checks if any filter is configured for this JSONWriter or if the IgnoreNonFieldGetter feature should be applied based on the containsNoneFieldGetter parameter.
      Parameters:
      containsNoneFieldGetter - whether to check for the IgnoreNonFieldGetter feature
      Returns:
      true if any filter is configured or the IgnoreNonFieldGetter feature should be applied, false otherwise
    • isWriteNulls

      public final boolean isWriteNulls()
      Checks if the WriteNulls feature is enabled.
      Returns:
      true if the WriteNulls feature is enabled, false otherwise
    • isRefDetect

      public final boolean isRefDetect()
      Checks if the ReferenceDetection feature is enabled.
      Returns:
      true if the ReferenceDetection feature is enabled, false otherwise
    • isUseSingleQuotes

      public final boolean isUseSingleQuotes()
      Checks if single quotes are being used for this JSONWriter.
      Returns:
      true if single quotes are being used, false otherwise
    • isRefDetect

      public final boolean isRefDetect(Object object)
      Checks if the ReferenceDetection feature is enabled for the specified object.
      Parameters:
      object - the object to check
      Returns:
      true if the ReferenceDetection feature is enabled and the object is not null and not a non-reference detect type, false otherwise
    • containsReference

      public final boolean containsReference(Object value)
      Checks if the specified object is contained in the reference map.
      Parameters:
      value - the object to check
      Returns:
      true if the object is contained in the reference map, false otherwise
    • getPath

      public final String getPath(Object value)
      Gets the path of the specified object in the reference map.
      Parameters:
      value - the object to get the path for
      Returns:
      the path of the object, or "$" if the object is not in the reference map
    • getPath

      public String getPath()
      If ReferenceDetection has been set, returns the path of the current object, otherwise returns null
      Returns:
      the path of the current object
      Since:
      2.0.51
    • removeReference

      public final boolean removeReference(Object value)
      Removes the reference to the specified object. This method removes the mapping of the object from the reference map.
      Parameters:
      value - the object whose reference should be removed
      Returns:
      true if the reference was removed, false otherwise
    • isBeanToArray

      public final boolean isBeanToArray()
      Checks if the BeanToArray feature is enabled. When enabled, Java beans will be serialized as JSON arrays instead of JSON objects.
      Returns:
      true if the BeanToArray feature is enabled, false otherwise
    • isEnabled

      public final boolean isEnabled(JSONWriter.Feature feature)
      Checks if the specified feature is enabled.
      Parameters:
      feature - the feature to check
      Returns:
      true if the feature is enabled, false otherwise
    • isEnabled

      public final boolean isEnabled(long feature)
      Checks if the specified feature is enabled.
      Parameters:
      feature - the feature to check
      Returns:
      true if the feature is enabled, false otherwise
    • getFeatures

      public final long getFeatures()
      Gets the features bitmask.
      Returns:
      the features bitmask
    • getFeatures

      public final long getFeatures(long features)
      Gets the combined features bitmask including the specified additional features.
      Parameters:
      features - the additional features to combine with the current features
      Returns:
      the combined features bitmask
    • isIgnoreErrorGetter

      public final boolean isIgnoreErrorGetter()
      Checks if the IgnoreErrorGetter feature is enabled.
      Returns:
      true if the IgnoreErrorGetter feature is enabled, false otherwise
    • isWriteTypeInfo

      public final boolean isWriteTypeInfo(Object object, Class fieldClass)
      Checks if type information should be written for the specified object and field class.
      Parameters:
      object - the object to check
      fieldClass - the field class to check
      Returns:
      true if type information should be written, false otherwise
    • isWriteTypeInfo

      public final boolean isWriteTypeInfo(Object object, Type fieldType)
      Checks if type information should be written for the specified object and field type.
      Parameters:
      object - the object to check
      fieldType - the field type to check
      Returns:
      true if type information should be written, false otherwise
    • isWriteTypeInfo

      public final boolean isWriteTypeInfo(Object object)
      Checks if type information should be written for the specified object. This method determines whether to include class name information in the serialized JSON based on various feature settings and object characteristics.
      Parameters:
      object - the object to check for type information writing
      Returns:
      true if type information should be written, false otherwise
    • isWriteTypeInfo

      public final boolean isWriteTypeInfo(Object object, Type fieldType, long features)
      Checks if type information should be written for the specified object, field type, and features.
      Parameters:
      object - the object to check
      fieldType - the field type to check
      features - the features to consider
      Returns:
      true if type information should be written, false otherwise
    • isWriteTypeInfo

      public final boolean isWriteTypeInfo(Object object, Class fieldClass, long features)
      Checks if type information should be written for the specified object, field class, and features.
      Parameters:
      object - the object to check
      fieldClass - the field class to check
      features - the features to consider
      Returns:
      true if type information should be written, false otherwise
    • isWriteMapTypeInfo

      public final boolean isWriteMapTypeInfo(Object object, Class fieldClass, long features)
      Checks if map type information should be written for the specified object, field class, and features.
      Parameters:
      object - the object to check
      fieldClass - the field class to check
      features - the features to consider
      Returns:
      true if map type information should be written, false otherwise
    • isWriteTypeInfo

      public final boolean isWriteTypeInfo(Object object, long features)
      Checks if type information should be written for the specified object and features.
      Parameters:
      object - the object to check
      features - the features to consider
      Returns:
      true if type information should be written, false otherwise
    • getObjectWriter

      public final ObjectWriter getObjectWriter(Class objectClass)
      Gets the ObjectWriter for the specified object class. This method retrieves an ObjectWriter instance that can serialize objects of the specified class.
      Parameters:
      objectClass - the class of objects to be serialized
      Returns:
      the ObjectWriter for the specified class
    • getObjectWriter

      public final ObjectWriter getObjectWriter(Class objectClass, String format)
      Gets the ObjectWriter for the specified object class with a specific format. This method retrieves an ObjectWriter instance that can serialize objects of the specified class using the provided format string.
      Parameters:
      objectClass - the class of objects to be serialized
      format - the format string to use for serialization
      Returns:
      the ObjectWriter for the specified class and format
    • getObjectWriter

      public final ObjectWriter getObjectWriter(Type objectType, Class objectClass)
      Gets the ObjectWriter for the specified object type and class. This method retrieves an ObjectWriter instance that can serialize objects of the specified type and class.
      Parameters:
      objectType - the type of objects to be serialized
      objectClass - the class of objects to be serialized
      Returns:
      the ObjectWriter for the specified type and class
    • of

      public static JSONWriter of()
      Creates a new JSONWriter with default configuration. The writer will output to an internal buffer and can be converted to a string using toString().

      Example usage:

       try (JSONWriter writer = JSONWriter.of()) {
           writer.writeAny(object);
           String json = writer.toString();
       }
       
      Returns:
      a new JSONWriter instance
    • of

      public static JSONWriter of(ObjectWriterProvider provider, JSONWriter.Feature... features)
      Creates a new JSONWriter with the specified object writer provider and features.
      Parameters:
      provider - the object writer provider to use
      features - the features to enable
      Returns:
      a new JSONWriter instance
    • of

      public static JSONWriter of(JSONWriter.Context context)
      Creates a new JSONWriter with the specified context.
      Parameters:
      context - the context to use
      Returns:
      a new JSONWriter instance
    • of

      public static JSONWriter of(JSONWriter.Feature... features)
      Creates a new JSONWriter with the specified features.
      Parameters:
      features - the features to enable
      Returns:
      a new JSONWriter instance
    • ofUTF16

      public static JSONWriter ofUTF16(JSONWriter.Feature... features)
      Creates a new JSONWriter instance using UTF-16 encoding with the specified features.
      Parameters:
      features - the features to enable for the new JSONWriter
      Returns:
      a new JSONWriter instance using UTF-16 encoding
    • ofJSONB

      public static JSONWriter ofJSONB()
      Creates a new JSONWriter instance for JSONB (binary JSON) format with default context.
      Returns:
      a new JSONWriter instance for JSONB format
    • ofJSONB

      public static JSONWriter ofJSONB(JSONWriter.Context context)
      Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified context.
      Parameters:
      context - the context to use for the new JSONWriter
      Returns:
      a new JSONWriter instance for JSONB format
    • ofJSONB

      public static JSONWriter ofJSONB(JSONWriter.Context context, SymbolTable symbolTable)
      Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified context and symbol table.
      Parameters:
      context - the context to use for the new JSONWriter
      symbolTable - the symbol table to use for the new JSONWriter
      Returns:
      a new JSONWriter instance for JSONB format
    • ofJSONB

      public static JSONWriter ofJSONB(JSONWriter.Feature... features)
      Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified features.
      Parameters:
      features - the features to enable for the new JSONWriter
      Returns:
      a new JSONWriter instance for JSONB format
    • ofJSONB

      public static JSONWriter ofJSONB(SymbolTable symbolTable)
      Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified symbol table.
      Parameters:
      symbolTable - the symbol table to use for the new JSONWriter
      Returns:
      a new JSONWriter instance for JSONB format
    • ofPretty

      public static JSONWriter ofPretty()
      Creates a new JSONWriter instance with pretty formatting enabled.
      Returns:
      a new JSONWriter instance with pretty formatting
    • ofPretty

      public static JSONWriter ofPretty(JSONWriter writer)
      Enables pretty formatting on an existing JSONWriter instance.
      Parameters:
      writer - the JSONWriter instance to enable pretty formatting on
      Returns:
      the same JSONWriter instance with pretty formatting enabled
    • ofUTF8

      public static JSONWriter ofUTF8()
      Creates a new JSONWriter instance using UTF-8 encoding with default context.
      Returns:
      a new JSONWriter instance using UTF-8 encoding
    • ofUTF8

      public static JSONWriter ofUTF8(JSONWriter.Context context)
      Creates a new JSONWriter instance using UTF-8 encoding with the specified context.
      Parameters:
      context - the context to use for the new JSONWriter
      Returns:
      a new JSONWriter instance using UTF-8 encoding
    • ofUTF8

      public static JSONWriter ofUTF8(JSONWriter.Feature... features)
      Creates a new JSONWriter instance using UTF-8 encoding with the specified features.
      Parameters:
      features - the features to enable for the new JSONWriter
      Returns:
      a new JSONWriter instance using UTF-8 encoding
    • writeBinary

      public void writeBinary(byte[] bytes)
      Writes a byte array as either Base64-encoded string or as an array of integers, depending on the WriteByteArrayAsBase64 feature.
      Parameters:
      bytes - the byte array to write
    • writeBase64

      public abstract void writeBase64(byte[] bytes)
      Writes a byte array as Base64-encoded string.
      Parameters:
      bytes - the byte array to encode and write
    • writeHex

      public abstract void writeHex(byte[] bytes)
      Writes a byte array as hexadecimal string.
      Parameters:
      bytes - the byte array to encode and write
    • write0

      protected abstract void write0(char ch)
      Writes a character to the output.
      Parameters:
      ch - the character to write
    • writeRaw

      public abstract void writeRaw(String str)
      Writes a raw string without any escaping or formatting.
      Parameters:
      str - the string to write
    • writeRaw

      public abstract void writeRaw(byte[] bytes)
      Writes raw bytes without any escaping or formatting.
      Parameters:
      bytes - the bytes to write
    • writeRaw

      public void writeRaw(byte b)
      Writes a raw byte without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      b - the byte to write
      Throws:
      JSONException - if the operation is not supported by this implementation
    • writeNameRaw

      public void writeNameRaw(byte[] bytes, int offset, int len)
      Writes raw bytes representing a field name without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      bytes - the bytes to write
      offset - the offset in the byte array
      len - the number of bytes to write
      Throws:
      JSONException - if the operation is not supported by this implementation
    • writeRaw

      public final void writeRaw(char[] chars)
      Writes raw characters without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      chars - the character array to write
    • writeRaw

      public void writeRaw(char[] chars, int off, int charslen)
      Writes raw characters without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      chars - the character array to write
      off - the offset in the character array
      charslen - the number of characters to write
      Throws:
      JSONException - if the operation is not supported by this implementation
    • writeChar

      public abstract void writeChar(char ch)
      Writes a character without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      ch - the character to write
    • writeRaw

      public abstract void writeRaw(char ch)
      Writes a raw character without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      ch - the character to write
    • writeRaw

      public void writeRaw(char c0, char c1)
      Writes two raw characters without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      c0 - the first character to write
      c1 - the second character to write
      Throws:
      JSONException - if the operation is not supported by this implementation
    • writeNameRaw

      public abstract void writeNameRaw(byte[] bytes)
      Writes raw bytes representing a field name without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      bytes - the bytes to write
    • writeName2Raw

      public abstract void writeName2Raw(long name)
      Writes a 2-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name - the 2-character field name as a long value
    • writeName3Raw

      public abstract void writeName3Raw(long name)
      Writes a 3-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name - the 3-character field name as a long value
    • writeName4Raw

      public abstract void writeName4Raw(long name)
      Writes a 4-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name - the 4-character field name as a long value
    • writeName5Raw

      public abstract void writeName5Raw(long name)
      Writes a 5-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name - the 5-character field name as a long value
    • writeName6Raw

      public abstract void writeName6Raw(long name)
      Writes a 6-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name - the 6-character field name as a long value
    • writeName7Raw

      public abstract void writeName7Raw(long name)
      Writes a 7-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name - the 7-character field name as a long value
    • writeName8Raw

      public abstract void writeName8Raw(long name0)
      Writes an 8-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the 8-character field name as a long value
    • writeName9Raw

      public abstract void writeName9Raw(long name0, int name1)
      Writes a 9-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name1 - the 9th character of the field name as an integer value
    • writeName10Raw

      public abstract void writeName10Raw(long name0, long name1)
      Writes a 10-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name1 - the last 2 characters of the field name as a long value
    • writeName11Raw

      public abstract void writeName11Raw(long name0, long name2)
      Writes an 11-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name2 - the last 3 characters of the field name as a long value
    • writeName12Raw

      public abstract void writeName12Raw(long name0, long name2)
      Writes a 12-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name2 - the last 4 characters of the field name as a long value
    • writeName13Raw

      public abstract void writeName13Raw(long name0, long name2)
      Writes a 13-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name2 - the last 5 characters of the field name as a long value
    • writeName14Raw

      public abstract void writeName14Raw(long name0, long name2)
      Writes a 14-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name2 - the last 6 characters of the field name as a long value
    • writeName15Raw

      public abstract void writeName15Raw(long name0, long name2)
      Writes a 15-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name2 - the last 7 characters of the field name as a long value
    • writeName16Raw

      public abstract void writeName16Raw(long name0, long name2)
      Writes a 16-character field name as raw bytes without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name0 - the first 8 characters of the field name as a long value
      name2 - the last 8 characters of the field name as a long value
    • writeSymbol

      public void writeSymbol(int symbol)
      Writes a symbol as a JSON value. This method is used for writing symbol values, which are typically used for identifiers or enumerated values.
      Parameters:
      symbol - the symbol to write
      Throws:
      JSONException - if the operation is not supported by this implementation
    • writeNameRaw

      public void writeNameRaw(byte[] name, long nameHash)
      Writes raw bytes representing a field name without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      name - the bytes representing the field name
      nameHash - the hash of the field name
      Throws:
      JSONException - if the operation is not supported by this implementation
    • isWriteAsString

      protected static boolean isWriteAsString(long value, long features)
    • isWriteAsString

      protected static boolean isWriteAsString(BigInteger value, long features)
    • isWriteAsString

      protected static boolean isWriteAsString(BigDecimal value, long features)
    • writeNameRaw

      public abstract void writeNameRaw(char[] chars)
      Writes raw characters representing a field name without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      chars - the character array to write
    • writeNameRaw

      public abstract void writeNameRaw(char[] bytes, int offset, int len)
      Writes raw characters representing a field name without any escaping or formatting. This method is used for low-level output operations where no JSON formatting or escaping should be applied.
      Parameters:
      bytes - the character array to write
      offset - the offset in the character array
      len - the number of characters to write
    • writeName

      public void writeName(String name)
    • writeNameValue

      public final void writeNameValue(String name, Object value)
      Writes a field name and its value as a key-value pair in a JSON object. This method writes the field name followed by a colon and then the value.
      Parameters:
      name - the field name
      value - the field value
    • writeName

      public final void writeName(long name)
      Writes a field name as a long value in a JSON object. This method writes the field name as a numeric value.
      Parameters:
      name - the field name as a long value
    • writeName

      public final void writeName(int name)
      Writes a field name as an integer value in a JSON object. This method writes the field name as a numeric value.
      Parameters:
      name - the field name as an integer value
    • writeNameAny

      public void writeNameAny(Object name)
      Writes a field name of any type in a JSON object. This method writes the field name as a JSON value of any type.
      Parameters:
      name - the field name of any type
    • startObject

      public abstract void startObject()
      Starts writing a JSON object. This method writes the opening brace '{' and prepares the writer for writing key-value pairs.
    • endObject

      public abstract void endObject()
      Ends writing a JSON object. This method writes the closing brace '}' and completes the current object.
    • startArray

      public abstract void startArray()
      Starts writing a JSON array. This method writes the opening bracket '[' and prepares the writer for writing array elements.
    • startArray

      public void startArray(int size)
      Starts writing a JSON array with a specified initial capacity. This method writes the opening bracket '[' and prepares the writer for writing array elements. The size parameter is used for optimization in some implementations.
      Parameters:
      size - the expected number of elements in the array
      Throws:
      JSONException - if the operation is not supported by this implementation
    • startArray0

      public void startArray0()
      Starts writing a JSON array with zero expected elements. This is a convenience method equivalent to calling startArray(0).
    • startArray1

      public void startArray1()
      Starts writing a JSON array with one expected element. This is a convenience method equivalent to calling startArray(1).
    • startArray2

      public void startArray2()
      Starts writing a JSON array with two expected elements. This is a convenience method equivalent to calling startArray(2).
    • startArray3

      public void startArray3()
      Starts writing a JSON array with three expected elements. This is a convenience method equivalent to calling startArray(3).
    • startArray4

      public void startArray4()
      Starts writing a JSON array with four expected elements. This is a convenience method equivalent to calling startArray(4).
    • startArray5

      public void startArray5()
      Starts writing a JSON array with five expected elements. This is a convenience method equivalent to calling startArray(5).
    • startArray6

      public void startArray6()
      Starts writing a JSON array with six expected elements. This is a convenience method equivalent to calling startArray(6).
    • startArray7

      public void startArray7()
      Starts writing a JSON array with seven expected elements. This is a convenience method equivalent to calling startArray(7).
    • startArray8

      public void startArray8()
      Starts writing a JSON array with eight expected elements. This is a convenience method equivalent to calling startArray(8).
    • startArray9

      public void startArray9()
      Starts writing a JSON array with nine expected elements. This is a convenience method equivalent to calling startArray(9).
    • startArray10

      public void startArray10()
      Starts writing a JSON array with ten expected elements. This is a convenience method equivalent to calling startArray(10).
    • startArray11

      public void startArray11()
      Starts writing a JSON array with eleven expected elements. This is a convenience method equivalent to calling startArray(11).
    • startArray12

      public void startArray12()
      Starts writing a JSON array with twelve expected elements. This is a convenience method equivalent to calling startArray(12).
    • startArray13

      public void startArray13()
      Starts writing a JSON array with thirteen expected elements. This is a convenience method equivalent to calling startArray(13).
    • startArray14

      public void startArray14()
      Starts writing a JSON array with fourteen expected elements. This is a convenience method equivalent to calling startArray(14).
    • startArray15

      public void startArray15()
      Starts writing a JSON array with fifteen expected elements. This is a convenience method equivalent to calling startArray(15).
    • startArray

      public void startArray(Object array, int size)
      Starts writing a JSON array with the specified array and size. This method is used for optimized array serialization when the array and its expected size are known in advance.
      Parameters:
      array - the array to serialize
      size - the expected number of elements in the array
      Throws:
      JSONException - if the operation is not supported by this implementation
    • endArray

      public abstract void endArray()
      Ends the current JSON array.
    • writeComma

      public abstract void writeComma()
      Writes a comma separator.
    • writeColon

      public abstract void writeColon()
      Writes a colon separator.
    • writeInt16

      public void writeInt16(short[] value)
      Writes a short array as integers. Each element in the array is written as a separate integer value in a JSON array.
      Parameters:
      value - the short array to write, can be null
    • writeInt8

      public abstract void writeInt8(byte value)
      Writes a byte value as an integer.
      Parameters:
      value - the byte value to write
    • writeInt8

      public abstract void writeInt8(byte[] value)
      Writes a byte array as integers.
      Parameters:
      value - the byte array to write
    • writeInt16

      public abstract void writeInt16(short value)
      Writes a short value as an integer.
      Parameters:
      value - the short value to write
    • writeInt32

      public abstract void writeInt32(int[] value)
      Writes an int array as integers.
      Parameters:
      value - the int array to write
    • writeInt32

      public abstract void writeInt32(int value)
      Writes an int value.
      Parameters:
      value - the int value to write
    • writeInt32

      public abstract void writeInt32(Integer i)
      Writes an Integer object.
      Parameters:
      i - the Integer object to write
    • writeInt32

      public final void writeInt32(int value, DecimalFormat format)
      Writes an int value with the specified decimal format. If the format is null or JSONB mode is enabled, this method delegates to writeInt32(int).
      Parameters:
      value - the int value to write
      format - the decimal format to use, or null to use default formatting
    • writeInt32

      public final void writeInt32(int value, String format)
      Writes an int value with the specified string format. If the format is null or JSONB mode is enabled, this method delegates to writeInt32(int).
      Parameters:
      value - the int value to write
      format - the string format to use, or null to use default formatting
    • writeInt64

      public abstract void writeInt64(long i)
      Writes a long value.
      Parameters:
      i - the long value to write
    • writeInt64

      public abstract void writeInt64(Long i)
      Writes a Long object.
      Parameters:
      i - the Long object to write
    • writeMillis

      public void writeMillis(long i)
      Writes a timestamp value as a long integer. This is typically used for writing millisecond timestamps. This method delegates to writeInt64(long).
      Parameters:
      i - the timestamp value to write as milliseconds
    • writeInt64

      public abstract void writeInt64(long[] value)
      Writes a long array as integers.
      Parameters:
      value - the long array to write
    • writeListInt64

      public abstract void writeListInt64(List<Long> values)
      Writes a list of Long values as integers.
      Parameters:
      values - the list of Long values to write
    • writeListInt32

      public abstract void writeListInt32(List<Integer> values)
      Writes a list of Integer values as integers.
      Parameters:
      values - the list of Integer values to write
    • writeFloat

      public abstract void writeFloat(float value)
      Writes a float value.
      Parameters:
      value - the float value to write
    • writeFloat

      public final void writeFloat(float value, DecimalFormat format)
      Writes a float value with the specified decimal format. If the format is null or JSONB mode is enabled, this method delegates to writeFloat(float). NaN and infinite values are written as null.
      Parameters:
      value - the float value to write
      format - the decimal format to use, or null to use default formatting
    • writeFloat

      public abstract void writeFloat(float[] value)
    • writeFloat

      public final void writeFloat(float[] value, DecimalFormat format)
      Writes a float array with the specified decimal format. If the format is null or JSONB mode is enabled, this method delegates to writeFloat(float[]). NaN and infinite values are written as null.
      Parameters:
      value - the float array to write, can be null
      format - the decimal format to use, or null to use default formatting
    • writeFloat

      public final void writeFloat(Float value)
      Writes a Float object. If the value is null, a null value is written according to the NullAsDefaultValue feature. Otherwise, the value is written as a double.
      Parameters:
      value - the Float object to write, can be null
    • writeDouble

      public abstract void writeDouble(double value)
      Writes a double value.
      Parameters:
      value - the double value to write
    • writeDouble

      public final void writeDouble(double value, DecimalFormat format)
      Writes a double value with the specified decimal format. If the format is null or JSONB mode is enabled, this method delegates to writeDouble(double). NaN and infinite values are written as null.
      Parameters:
      value - the double value to write
      format - the decimal format to use, or null to use default formatting
    • writeDoubleArray

      public void writeDoubleArray(double value0, double value1)
      Writes a double array with two elements. This is a convenience method for writing arrays that contain exactly two double values.
      Parameters:
      value0 - the first double value to write
      value1 - the second double value to write
    • writeDouble

      public final void writeDouble(double[] value, DecimalFormat format)
      Writes a double array as floating-point numbers with the specified decimal format. If the format is null or JSONB mode is enabled, this method delegates to writeDouble(double[]).
      Parameters:
      value - the double array to write, can be null
      format - the decimal format to use, or null to use default formatting
    • writeDouble

      public abstract void writeDouble(double[] value)
      Writes a double array as floating-point numbers.
      Parameters:
      value - the double array to write
    • writeBool

      public abstract void writeBool(boolean value)
      Writes a boolean value.
      Parameters:
      value - the boolean value to write
    • writeBool

      public void writeBool(boolean[] value)
      Writes a boolean array as boolean values. Each element in the array is written as a separate boolean value in a JSON array.
      Parameters:
      value - the boolean array to write, can be null
    • writeNull

      public abstract void writeNull()
      Writes a null value.
    • writeObjectNull

      public void writeObjectNull(Class<?> fieldClass)
      Writes a null object value. The serialization format depends on the context features:
      Parameters:
      fieldClass - the class of the field being written
    • writeStringNull

      public void writeStringNull()
      Writes a null string value. The serialization format depends on the context features:
    • writeArrayNull

      public void writeArrayNull()
      Writes a null array value using the current context features. The serialization format depends on the context features:
    • writeArrayNull

      public void writeArrayNull(long features)
      Writes a null array value using the specified features. The serialization format depends on the provided features:
      Parameters:
      features - the features to use for serialization
    • writeNumberNull

      public final void writeNumberNull()
      Writes a null number value using the current context features. The serialization format depends on the context features:
    • writeNumberNull

      public final void writeNumberNull(long features)
      Writes a null number value using the specified features. The serialization format depends on the provided features:
      Parameters:
      features - the features to use for serialization
    • writeDecimalNull

      public final void writeDecimalNull()
      Writes a null decimal value using the current context features. The serialization format depends on the context features:
    • writeDecimalNull

      public final void writeDecimalNull(long features)
      Writes a null decimal value using the specified features. The serialization format depends on the provided features:
      Parameters:
      features - the features to use for serialization
    • writeInt64Null

      public final void writeInt64Null()
      Writes a null long value using the current context features. The serialization format depends on the context features:
    • writeBooleanNull

      public final void writeBooleanNull()
      Writes a null boolean value using the current context features. The serialization format depends on the context features:
    • writeDecimal

      public final void writeDecimal(BigDecimal value)
      Writes a BigDecimal value using default features and no specific format. This method delegates to writeDecimal(BigDecimal, long, DecimalFormat) with features set to 0 and format set to null.
      Parameters:
      value - the BigDecimal value to write, can be null
    • writeDecimal

      public final void writeDecimal(BigDecimal value, long features)
      Writes a BigDecimal value using the specified features and no specific format. This method delegates to writeDecimal(BigDecimal, long, DecimalFormat) with format set to null.
      Parameters:
      value - the BigDecimal value to write, can be null
      features - the features to use for serialization
    • writeDecimal

      public abstract void writeDecimal(BigDecimal value, long features, DecimalFormat format)
      Writes a BigDecimal value using the specified features and decimal format.
      Parameters:
      value - the BigDecimal value to write, can be null
      features - the features to use for serialization
      format - the decimal format to use, or null to use default formatting
    • writeEnum

      public void writeEnum(Enum e)
      Writes an Enum value. The serialization format depends on the context features:
      Parameters:
      e - the Enum value to write, can be null
    • writeBigInt

      public final void writeBigInt(BigInteger value)
      Writes a BigInteger value using default features. This method delegates to writeBigInt(BigInteger, long) with features set to 0.
      Parameters:
      value - the BigInteger value to write, can be null
    • writeBigInt

      public abstract void writeBigInt(BigInteger value, long features)
      Writes a BigInteger value using the specified features.
      Parameters:
      value - the BigInteger value to write, can be null
      features - the features to use for serialization
    • writeUUID

      public abstract void writeUUID(UUID value)
      Writes a UUID value. The UUID is typically serialized as a string in standard UUID format.
      Parameters:
      value - the UUID to write, can be null
    • checkAndWriteTypeName

      public final void checkAndWriteTypeName(Object object, Class fieldClass)
      Checks if type name should be written for the given object and writes it if necessary. This method is used when the WriteClassName feature is enabled to conditionally include type information in the serialized JSON based on various criteria such as class type, feature settings, and object relationships.
      Parameters:
      object - the object being serialized
      fieldClass - the expected field class type
    • writeTypeName

      public void writeTypeName(String typeName)
      Writes a type name for the current object. This method is used when the WriteClassName feature is enabled to include type information in the serialized JSON.
      Parameters:
      typeName - the type name to write
      Throws:
      JSONException - if the operation is not supported by this implementation
    • writeTypeName

      public boolean writeTypeName(byte[] typeName, long typeNameHash)
      Writes a type name for the current object using byte array and hash. This method is used when the WriteClassName feature is enabled to include type information in the serialized JSON in a more efficient format.
      Parameters:
      typeName - the type name as byte array
      typeNameHash - the hash of the type name
      Returns:
      true if the type name was written successfully, false otherwise
      Throws:
      JSONException - if the operation is not supported by this implementation
    • writeString

      public final void writeString(Reader reader)
      Writes a string from a Reader. This method reads characters from the provided Reader and writes them as a JSON string, properly escaping any special characters as needed.
      Parameters:
      reader - the Reader to read characters from
      Throws:
      JSONException - if an I/O error occurs while reading from the Reader
    • writeString

      public abstract void writeString(String str)
      Writes a string value.
      Parameters:
      str - the string to write, can be null
    • writeString

      public abstract void writeString(boolean value)
      Writes a boolean value as a string.
      Parameters:
      value - the boolean value to write
      Since:
      2.0.49
    • writeString

      public abstract void writeString(byte value)
      Writes a byte value as a string.
      Parameters:
      value - the byte value to write
      Since:
      2.0.49
    • writeString

      public abstract void writeString(short value)
      Writes a short value as a string.
      Parameters:
      value - the short value to write
      Since:
      2.0.49
    • writeString

      public void writeString(boolean[] value)
      Writes a boolean array as strings.
      Parameters:
      value - the boolean array to write
      Since:
      2.0.49
    • writeString

      public void writeString(byte[] value)
      Writes a byte array as strings.
      Parameters:
      value - the byte array to write
      Since:
      2.0.49
    • writeString

      public void writeString(short[] value)
      Writes a short array as strings.
      Parameters:
      value - the short array to write
      Since:
      2.0.49
    • writeString

      public void writeString(int[] value)
      Writes an int array as strings.
      Parameters:
      value - the int array to write
      Since:
      2.0.49
    • writeString

      public void writeString(long[] value)
      Writes a long array as strings.
      Parameters:
      value - the long array to write
      Since:
      2.0.49
    • writeString

      public void writeString(float[] value)
      Writes a float array as strings.
      Parameters:
      value - the float array to write
      Since:
      2.0.49
    • writeString

      public void writeString(double[] value)
      Writes a double array as strings.
      Parameters:
      value - the double array to write
      Since:
      2.0.49
    • writeString

      public abstract void writeString(int value)
      Writes an int value as a string.
      Parameters:
      value - the int value to write
      Since:
      2.0.49
    • writeString

      public void writeString(float value)
      Writes a float value as a string.
      Parameters:
      value - the float value to write
      Since:
      2.0.49
    • writeString

      public void writeString(double value)
      Writes a double value as a string.
      Parameters:
      value - the double value to write
      Since:
      2.0.49
    • writeString

      public abstract void writeString(long value)
      Writes a long value as a string.
      Parameters:
      value - the long value to write
      Since:
      2.0.49
    • writeStringLatin1

      public abstract void writeStringLatin1(byte[] value)
      Writes a string from Latin-1 encoded bytes.
      Parameters:
      value - the Latin-1 encoded bytes to write
    • writeStringUTF16

      public abstract void writeStringUTF16(byte[] value)
      Writes a string from UTF-16 encoded bytes.
      Parameters:
      value - the UTF-16 encoded bytes to write
    • writeString

      public void writeString(List<String> list)
      Writes a list of strings as a JSON array. Each string in the list is written as a separate string value in the array.
      Parameters:
      list - the list of strings to write, can be null
    • writeString

      public void writeString(String[] strings)
      Writes an array of strings as a JSON array. Each string in the array is written as a separate string value in the array.
      Parameters:
      strings - the array of strings to write, can be null
    • writeSymbol

      public void writeSymbol(String string)
      Writes a symbol string. Symbols are typically used for identifiers or enumerated values that may benefit from optimized serialization.
      Parameters:
      string - the symbol string to write
    • writeString

      public abstract void writeString(char[] chars)
      Writes a string from character array.
      Parameters:
      chars - the character array to write
    • writeString

      public abstract void writeString(char[] chars, int off, int len)
      Writes a string from character array with specified offset and length.
      Parameters:
      chars - the character array to write
      off - the offset in the array
      len - the number of characters to write
    • writeString

      public abstract void writeString(char[] chars, int off, int len, boolean quote)
      Writes a string from character array with specified offset and length.
      Parameters:
      chars - the character array to write
      off - the offset in the array
      len - the number of characters to write
      quote - whether to quote the string
    • writeLocalDate

      public abstract void writeLocalDate(LocalDate date)
      Writes a LocalDate value.
      Parameters:
      date - the LocalDate to write
    • writeLocalDateWithFormat

      protected final boolean writeLocalDateWithFormat(LocalDate date)
    • writeLocalDateTime

      public abstract void writeLocalDateTime(LocalDateTime dateTime)
      Writes a LocalDateTime value.
      Parameters:
      dateTime - the LocalDateTime to write
    • writeLocalTime

      public abstract void writeLocalTime(LocalTime time)
      Writes a LocalTime value.
      Parameters:
      time - the LocalTime to write
    • writeZonedDateTime

      public abstract void writeZonedDateTime(ZonedDateTime dateTime)
      Writes a ZonedDateTime value.
      Parameters:
      dateTime - the ZonedDateTime to write
    • writeOffsetDateTime

      public abstract void writeOffsetDateTime(OffsetDateTime dateTime)
      Writes an OffsetDateTime value.
      Parameters:
      dateTime - the OffsetDateTime to write
    • writeOffsetTime

      public abstract void writeOffsetTime(OffsetTime dateTime)
      Writes an OffsetTime value.
      Parameters:
      dateTime - the OffsetTime to write
    • writeInstant

      public void writeInstant(Instant instant)
      Writes an Instant value as an ISO-8601 formatted string. If the instant is null, a null value is written instead.
      Parameters:
      instant - the Instant to write, can be null
    • writeDateTime14

      public abstract void writeDateTime14(int year, int month, int dayOfMonth, int hour, int minute, int second)
      Writes a date-time value in 14-character format (yyyyMMddHHmmss).
      Parameters:
      year - the year
      month - the month (1-12)
      dayOfMonth - the day of month (1-31)
      hour - the hour (0-23)
      minute - the minute (0-59)
      second - the second (0-59)
    • writeDateTime19

      public abstract void writeDateTime19(int year, int month, int dayOfMonth, int hour, int minute, int second)
      Writes a date-time value in 19-character format (yyyy-MM-dd HH:mm:ss).
      Parameters:
      year - the year
      month - the month (1-12)
      dayOfMonth - the day of month (1-31)
      hour - the hour (0-23)
      minute - the minute (0-59)
      second - the second (0-59)
    • writeDateTimeISO8601

      public abstract void writeDateTimeISO8601(int year, int month, int dayOfMonth, int hour, int minute, int second, int millis, int offsetSeconds, boolean timeZone)
      Writes a date-time value in ISO8601 format.
      Parameters:
      year - the year
      month - the month (1-12)
      dayOfMonth - the day of month (1-31)
      hour - the hour (0-23)
      minute - the minute (0-59)
      second - the second (0-59)
      millis - the millisecond (0-999)
      offsetSeconds - the timezone offset in seconds
      timeZone - whether to include timezone information
    • writeDateYYYMMDD8

      public abstract void writeDateYYYMMDD8(int year, int month, int dayOfMonth)
      Writes a date in 8-character format (yyyyMMdd).
      Parameters:
      year - the year
      month - the month (1-12)
      dayOfMonth - the day of month (1-31)
    • writeDateYYYMMDD10

      public abstract void writeDateYYYMMDD10(int year, int month, int dayOfMonth)
      Writes a date in 10-character format (yyyy-MM-dd).
      Parameters:
      year - the year
      month - the month (1-12)
      dayOfMonth - the day of month (1-31)
    • writeTimeHHMMSS8

      public abstract void writeTimeHHMMSS8(int hour, int minute, int second)
      Writes a time in 8-character format (HH:mm:ss).
      Parameters:
      hour - the hour (0-23)
      minute - the minute (0-59)
      second - the second (0-59)
    • write

      public abstract void write(List array)
      Writes a list as a JSON array.
      Parameters:
      array - the list to write
    • write

      public final void write(JSONObject map)
      Writes a JSONObject as a JSON object. This method delegates to the write(Map) method since JSONObject extends Map.
      Parameters:
      map - the JSONObject to write
    • write

      public void write(Map<?,?> map)
      Writes a Map as a JSON object. Each entry in the map becomes a key-value pair in the JSON object. Null values are handled according to the WriteMapNullValue feature.
      Parameters:
      map - the Map to write, can be null
    • writeAny

      public void writeAny(Object value)
      Writes any object using the appropriate writer based on its runtime type. This method dynamically determines the correct serialization approach based on the actual type of the object provided.
      Parameters:
      value - the object to write, can be null
    • writeAs

      public final void writeAs(Object value, Class type)
      Writes an object as if it were of the specified type. This method is useful for writing objects with a specific type serialization, even if the actual object is of a different type.
      Parameters:
      value - the object to write
      type - the type to serialize the object as
      Since:
      2.0.43
    • writeReference

      public abstract void writeReference(String path)
      Writes a reference to a previously serialized object. This is used for handling circular references and avoiding infinite loops during serialization.
      Parameters:
      path - the JSON Pointer path to the referenced object
    • close

      public abstract void close()
      Closes this JSONWriter and releases any resources associated with it. This method should be called when finished with the writer to ensure proper cleanup of resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      RuntimeException - if an I/O error occurs
    • size

      public abstract int size()
      Gets the current size of the output buffer.
      Returns:
      the size of the output buffer in bytes
    • getBytes

      public abstract byte[] getBytes()
      Gets the content of the output buffer as a byte array.
      Returns:
      the content as a byte array
    • getBytes

      public abstract byte[] getBytes(Charset charset)
      Gets the content of the output buffer as a byte array using the specified charset.
      Parameters:
      charset - the charset to use for encoding
      Returns:
      the content as a byte array
    • flushTo

      public void flushTo(Writer to)
      Flushes the content of this JSONWriter to the specified Writer. This method converts the current content to a string and writes it to the provided Writer, then resets the internal buffer offset to zero.
      Parameters:
      to - the Writer to flush content to
      Throws:
      JSONException - if an I/O error occurs while writing to the Writer
    • flushTo

      public abstract int flushTo(OutputStream to) throws IOException
      Flushes the content of this JSONWriter to the specified OutputStream. This method writes the current content directly to the provided OutputStream without converting to a string first.
      Parameters:
      to - the OutputStream to flush content to
      Returns:
      the number of bytes written
      Throws:
      IOException - if an I/O error occurs while writing to the OutputStream
    • flushTo

      public abstract int flushTo(OutputStream out, Charset charset) throws IOException
      Flushes the content of this JSONWriter to the specified OutputStream using the specified charset. This method writes the current content directly to the provided OutputStream using the specified charset for encoding.
      Parameters:
      out - the OutputStream to flush content to
      charset - the charset to use for encoding
      Returns:
      the number of bytes written
      Throws:
      IOException - if an I/O error occurs while writing to the OutputStream
    • illegalYear

      protected static IllegalArgumentException illegalYear(int year)
    • incrementIndent

      public final void incrementIndent()
      Deprecated.
      This method is deprecated and will be removed in a future version
      Increments the indentation level. This method is used to track the current nesting level during JSON serialization.
      Since:
      2.0.51
    • decrementIdent

      public final void decrementIdent()
      Deprecated.
      This method is deprecated and will be removed in a future version
      Decrements the indentation level. This method is used to track the current nesting level during JSON serialization.
      Since:
      2.0.51
    • println

      public void println()
      Deprecated.
      This method is deprecated and will be removed in a future version
      Writes a line break followed by indentation whitespace. This method is used for pretty-printing JSON output with proper indentation.
      Since:
      2.0.51
    • writeReference

      public final void writeReference(Object object)
      Deprecated.
      This method is deprecated and will be removed in a future version
      Writes a reference to a previously serialized object. This method is used to handle circular references by writing a reference to an object that has already been serialized, instead of serializing it again.
      Parameters:
      object - the object for which to write a reference
      Since:
      2.0.51
    • newCapacity

      protected final int newCapacity(int minCapacity, int oldCapacity)
      Calculates the new capacity for the internal buffer based on the minimum required capacity. This method implements a growth strategy that increases the capacity by 50% of the current capacity, ensuring it meets the minimum required capacity. It also enforces a maximum array size limit to prevent excessive memory allocation.
      Parameters:
      minCapacity - the minimum required capacity
      oldCapacity - the current capacity of the buffer
      Returns:
      the new capacity, which is at least minCapacity and follows the growth strategy
      Throws:
      JSONLargeObjectException - if the required capacity exceeds the maximum allowed array size
      Since:
      2.0.51
    • getAttachment

      public Object getAttachment()
      Gets the attachment object associated with this JSONWriter. Attachments can be used to store additional context or metadata during serialization.
      Returns:
      the attachment object, or null if no attachment is set
    • setAttachment

      public void setAttachment(Object attachment)
      Sets the attachment object for this JSONWriter. Attachments can be used to store additional context or metadata during serialization.
      Parameters:
      attachment - the attachment object to set
    • overflowLevel

      protected final void overflowLevel()
      Throws a JSONException indicating that the nesting level has exceeded the maximum allowed level. This method is called when the serialization process exceeds the configured maximum nesting depth to prevent stack overflow and excessive memory consumption.
      Throws:
      JSONException - with a message indicating the level is too large
      Since:
      2.0.51
    • getOffset

      public int getOffset()
      Gets the current offset in the internal buffer. The offset represents the position where the next character will be written.
      Returns:
      the current offset
    • setOffset

      public void setOffset(int offset)
      Sets the offset in the internal buffer. This method allows direct manipulation of the buffer position.
      Parameters:
      offset - the offset to set
    • ensureCapacity

      public abstract Object ensureCapacity(int minCapacity)
      Ensures that the internal buffer has at least the specified minimum capacity. This method is used to dynamically expand the buffer when more space is needed during the serialization process.
      Parameters:
      minCapacity - the minimum capacity required
      Returns:
      the expanded buffer object
      Since:
      2.0.51