Package com.alibaba.fastjson2
Class JSONWriter
java.lang.Object
com.alibaba.fastjson2.JSONWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
JSONWriterUTF16JDK8UF
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 ClassesModifier and TypeClassDescriptionstatic final classContext holds the configuration and state information for JSON writing operations.static enumFeature is used to control the behavior of JSON writing and serialization in FASTJSON2.static final classPath represents a JSON pointer path used for reference detection during serialization. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Objectprotected final Charsetfinal JSONWriter.Contextfinal booleanprotected Stringprotected intprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected static final longprotected final intprotected intprotected JSONWriter.Pathprotected byteprotected final charprotected IdentityHashMap<Object, JSONWriter.Path> protected Objectprotected booleanfinal SymbolTablefinal booleanfinal booleanfinal boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedJSONWriter(JSONWriter.Context context, SymbolTable symbolTable, boolean jsonb, Charset charset) -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddManagerReference(Object object) Adds a manager reference for the specified object.final voidcheckAndWriteTypeName(Object object, Class fieldClass) Checks if type name should be written for the given object and writes it if necessary.abstract voidclose()Closes this JSONWriter and releases any resources associated with it.final voidconfig(JSONWriter.Feature... features) Configures features for this JSONWriter.final voidconfig(JSONWriter.Feature feature, boolean state) Configures a specific feature for this JSONWriter.final booleancontainsReference(Object value) Checks if the specified object is contained in the reference map.final voidDeprecated.This method is deprecated and will be removed in a future versionabstract voidendArray()Ends the current JSON array.abstract voidEnds writing a JSON object.abstract ObjectensureCapacity(int minCapacity) Ensures that the internal buffer has at least the specified minimum capacity.abstract intflushTo(OutputStream to) Flushes the content of this JSONWriter to the specified OutputStream.abstract intflushTo(OutputStream out, Charset charset) Flushes the content of this JSONWriter to the specified OutputStream using the specified charset.voidFlushes the content of this JSONWriter to the specified Writer.Gets the attachment object associated with this JSONWriter.abstract byte[]getBytes()Gets the content of the output buffer as a byte array.abstract byte[]Gets the content of the output buffer as a byte array using the specified charset.final CharsetGets the charset used by this JSONWriter.final JSONWriter.ContextGets the context used by this JSONWriter.final longGets the features bitmask.final longgetFeatures(long features) Gets the combined features bitmask including the specified additional features.final ObjectWritergetObjectWriter(Class objectClass) Gets the ObjectWriter for the specified object class.final ObjectWritergetObjectWriter(Class objectClass, String format) Gets the ObjectWriter for the specified object class with a specific format.final ObjectWritergetObjectWriter(Type objectType, Class objectClass) Gets the ObjectWriter for the specified object type and class.intGets the current offset in the internal buffer.getPath()If ReferenceDetection has been set, returns the path of the current object, otherwise returns nullfinal StringGets the path of the specified object in the reference map.final SymbolTableGets the symbol table used by this JSONWriter.final booleanChecks if any filter is configured for this JSONWriter.final booleanhasFilter(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 booleanhasFilter(long feature) Checks if any filter or the specified feature is configured for this JSONWriter.protected static IllegalArgumentExceptionillegalYear(int year) final voidDeprecated.This method is deprecated and will be removed in a future versionfinal booleanChecks if the BeanToArray feature is enabled.final booleanisEnabled(long feature) Checks if the specified feature is enabled.final booleanisEnabled(JSONWriter.Feature feature) Checks if the specified feature is enabled.final booleanChecks if the IgnoreErrorGetter feature is enabled.final booleanChecks if the IgnoreNoneSerializable feature is enabled.final booleanisIgnoreNoneSerializable(Object object) Checks if the IgnoreNoneSerializable feature is enabled for the specified object.final booleanChecks if the ReferenceDetection feature is enabled.final booleanisRefDetect(Object object) Checks if the ReferenceDetection feature is enabled for the specified object.final booleanChecks if single quotes are being used for this JSONWriter.final booleanisUTF16()Checks if this JSONWriter is using UTF-16 encoding.final booleanisUTF8()Checks if this JSONWriter is using UTF-8 encoding.protected static booleanisWriteAsString(long value, long features) protected static booleanisWriteAsString(BigDecimal value, long features) protected static booleanisWriteAsString(BigInteger value, long features) final booleanisWriteMapTypeInfo(Object object, Class fieldClass, long features) Checks if map type information should be written for the specified object, field class, and features.final booleanChecks if the WriteNulls feature is enabled.final booleanisWriteTypeInfo(Object object) Checks if type information should be written for the specified object.final booleanisWriteTypeInfo(Object object, long features) Checks if type information should be written for the specified object and features.final booleanisWriteTypeInfo(Object object, Class fieldClass) Checks if type information should be written for the specified object and field class.final booleanisWriteTypeInfo(Object object, Class fieldClass, long features) Checks if type information should be written for the specified object, field class, and features.final booleanisWriteTypeInfo(Object object, Type fieldType) Checks if type information should be written for the specified object and field type.final booleanisWriteTypeInfo(Object object, Type fieldType, long features) Checks if type information should be written for the specified object, field type, and features.final intlevel()Gets the current nesting level of this JSONWriter.protected final intnewCapacity(int minCapacity, int oldCapacity) Calculates the new capacity for the internal buffer based on the minimum required capacity.static JSONWriterof()Creates a new JSONWriter with default configuration.static JSONWriterof(JSONWriter.Context context) Creates a new JSONWriter with the specified context.static JSONWriterof(JSONWriter.Feature... features) Creates a new JSONWriter with the specified features.static JSONWriterof(ObjectWriterProvider provider, JSONWriter.Feature... features) Creates a new JSONWriter with the specified object writer provider and features.static JSONWriterofJSONB()Creates a new JSONWriter instance for JSONB (binary JSON) format with default context.static JSONWriterofJSONB(JSONWriter.Context context) Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified context.static JSONWriterofJSONB(JSONWriter.Context context, SymbolTable symbolTable) Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified context and symbol table.static JSONWriterofJSONB(JSONWriter.Feature... features) Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified features.static JSONWriterofJSONB(SymbolTable symbolTable) Creates a new JSONWriter instance for JSONB (binary JSON) format with the specified symbol table.static JSONWriterofPretty()Creates a new JSONWriter instance with pretty formatting enabled.static JSONWriterofPretty(JSONWriter writer) Enables pretty formatting on an existing JSONWriter instance.static JSONWriterofUTF16(JSONWriter.Feature... features) Creates a new JSONWriter instance using UTF-16 encoding with the specified features.static JSONWriterofUTF8()Creates a new JSONWriter instance using UTF-8 encoding with default context.static JSONWriterofUTF8(JSONWriter.Context context) Creates a new JSONWriter instance using UTF-8 encoding with the specified context.static JSONWriterofUTF8(JSONWriter.Feature... features) Creates a new JSONWriter instance using UTF-8 encoding with the specified features.protected final voidThrows a JSONException indicating that the nesting level has exceeded the maximum allowed level.final voidRemoves the path for the specified object.final voidRemoves the path for the specified object without reference detection.voidprintln()Deprecated.This method is deprecated and will be removed in a future versionfinal booleanremoveReference(Object value) Removes the reference to the specified object.voidsetAttachment(Object attachment) Sets the attachment object for this JSONWriter.voidsetOffset(int offset) Sets the offset in the internal buffer.final StringSets the path for the specified object at the given index.final StringsetPath(FieldWriter fieldWriter, Object object) Sets the path for the specified object using the provided field writer.final StringSets the path for the specified object with the given name.final StringSets the path for the specified object at the given index without reference detection.final StringsetPath0(FieldWriter fieldWriter, Object object) Sets the path for the specified object using the provided field writer without reference detection.final voidsetRootObject(Object rootObject) Sets the root object for this JSONWriter.abstract intsize()Gets the current size of the output buffer.abstract voidStarts writing a JSON array.voidstartArray(int size) Starts writing a JSON array with a specified initial capacity.voidstartArray(Object array, int size) Starts writing a JSON array with the specified array and size.voidStarts writing a JSON array with zero expected elements.voidStarts writing a JSON array with one expected element.voidStarts writing a JSON array with ten expected elements.voidStarts writing a JSON array with eleven expected elements.voidStarts writing a JSON array with twelve expected elements.voidStarts writing a JSON array with thirteen expected elements.voidStarts writing a JSON array with fourteen expected elements.voidStarts writing a JSON array with fifteen expected elements.voidStarts writing a JSON array with two expected elements.voidStarts writing a JSON array with three expected elements.voidStarts writing a JSON array with four expected elements.voidStarts writing a JSON array with five expected elements.voidStarts writing a JSON array with six expected elements.voidStarts writing a JSON array with seven expected elements.voidStarts writing a JSON array with eight expected elements.voidStarts writing a JSON array with nine expected elements.abstract voidStarts writing a JSON object.final voidwrite(JSONObject map) Writes a JSONObject as a JSON object.abstract voidWrites a list as a JSON array.voidWrites a Map as a JSON object.protected abstract voidwrite0(char ch) Writes a character to the output.voidWrites any object using the appropriate writer based on its runtime type.voidWrites a null array value using the current context features.voidwriteArrayNull(long features) Writes a null array value using the specified features.final voidWrites an object as if it were of the specified type.abstract voidwriteBase64(byte[] bytes) Writes a byte array as Base64-encoded string.final voidwriteBigInt(BigInteger value) Writes a BigInteger value using default features.abstract voidwriteBigInt(BigInteger value, long features) Writes a BigInteger value using the specified features.voidwriteBinary(byte[] bytes) Writes a byte array as either Base64-encoded string or as an array of integers, depending on the WriteByteArrayAsBase64 feature.abstract voidwriteBool(boolean value) Writes a boolean value.voidwriteBool(boolean[] value) Writes a boolean array as boolean values.final voidWrites a null boolean value using the current context features.abstract voidwriteChar(char ch) Writes a character without any escaping or formatting.abstract voidWrites a colon separator.abstract voidWrites a comma separator.abstract voidwriteDateTime14(int year, int month, int dayOfMonth, int hour, int minute, int second) Writes a date-time value in 14-character format (yyyyMMddHHmmss).abstract voidwriteDateTime19(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 voidwriteDateTimeISO8601(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 voidwriteDateYYYMMDD10(int year, int month, int dayOfMonth) Writes a date in 10-character format (yyyy-MM-dd).abstract voidwriteDateYYYMMDD8(int year, int month, int dayOfMonth) Writes a date in 8-character format (yyyyMMdd).final voidwriteDecimal(BigDecimal value) Writes a BigDecimal value using default features and no specific format.final voidwriteDecimal(BigDecimal value, long features) Writes a BigDecimal value using the specified features and no specific format.abstract voidwriteDecimal(BigDecimal value, long features, DecimalFormat format) Writes a BigDecimal value using the specified features and decimal format.final voidWrites a null decimal value using the current context features.final voidwriteDecimalNull(long features) Writes a null decimal value using the specified features.abstract voidwriteDouble(double value) Writes a double value.abstract voidwriteDouble(double[] value) Writes a double array as floating-point numbers.final voidwriteDouble(double[] value, DecimalFormat format) Writes a double array as floating-point numbers with the specified decimal format.final voidwriteDouble(double value, DecimalFormat format) Writes a double value with the specified decimal format.voidwriteDoubleArray(double value0, double value1) Writes a double array with two elements.voidWrites an Enum value.abstract voidwriteFloat(float value) Writes a float value.abstract voidwriteFloat(float[] value) final voidwriteFloat(float[] value, DecimalFormat format) Writes a float array with the specified decimal format.final voidwriteFloat(float value, DecimalFormat format) Writes a float value with the specified decimal format.final voidwriteFloat(Float value) Writes a Float object.abstract voidwriteHex(byte[] bytes) Writes a byte array as hexadecimal string.voidwriteInstant(Instant instant) Writes an Instant value as an ISO-8601 formatted string.abstract voidwriteInt16(short value) Writes a short value as an integer.voidwriteInt16(short[] value) Writes a short array as integers.abstract voidwriteInt32(int value) Writes an int value.abstract voidwriteInt32(int[] value) Writes an int array as integers.final voidwriteInt32(int value, String format) Writes an int value with the specified string format.final voidwriteInt32(int value, DecimalFormat format) Writes an int value with the specified decimal format.abstract voidWrites an Integer object.abstract voidwriteInt64(long i) Writes a long value.abstract voidwriteInt64(long[] value) Writes a long array as integers.abstract voidwriteInt64(Long i) Writes a Long object.final voidWrites a null long value using the current context features.abstract voidwriteInt8(byte value) Writes a byte value as an integer.abstract voidwriteInt8(byte[] value) Writes a byte array as integers.abstract voidwriteListInt32(List<Integer> values) Writes a list of Integer values as integers.abstract voidwriteListInt64(List<Long> values) Writes a list of Long values as integers.abstract voidwriteLocalDate(LocalDate date) Writes a LocalDate value.abstract voidwriteLocalDateTime(LocalDateTime dateTime) Writes a LocalDateTime value.protected final booleanabstract voidwriteLocalTime(LocalTime time) Writes a LocalTime value.voidwriteMillis(long i) Writes a timestamp value as a long integer.final voidwriteName(int name) Writes a field name as an integer value in a JSON object.final voidwriteName(long name) Writes a field name as a long value in a JSON object.voidabstract voidwriteName10Raw(long name0, long name1) Writes a 10-character field name as raw bytes without any escaping or formatting.abstract voidwriteName11Raw(long name0, long name2) Writes an 11-character field name as raw bytes without any escaping or formatting.abstract voidwriteName12Raw(long name0, long name2) Writes a 12-character field name as raw bytes without any escaping or formatting.abstract voidwriteName13Raw(long name0, long name2) Writes a 13-character field name as raw bytes without any escaping or formatting.abstract voidwriteName14Raw(long name0, long name2) Writes a 14-character field name as raw bytes without any escaping or formatting.abstract voidwriteName15Raw(long name0, long name2) Writes a 15-character field name as raw bytes without any escaping or formatting.abstract voidwriteName16Raw(long name0, long name2) Writes a 16-character field name as raw bytes without any escaping or formatting.abstract voidwriteName2Raw(long name) Writes a 2-character field name as raw bytes without any escaping or formatting.abstract voidwriteName3Raw(long name) Writes a 3-character field name as raw bytes without any escaping or formatting.abstract voidwriteName4Raw(long name) Writes a 4-character field name as raw bytes without any escaping or formatting.abstract voidwriteName5Raw(long name) Writes a 5-character field name as raw bytes without any escaping or formatting.abstract voidwriteName6Raw(long name) Writes a 6-character field name as raw bytes without any escaping or formatting.abstract voidwriteName7Raw(long name) Writes a 7-character field name as raw bytes without any escaping or formatting.abstract voidwriteName8Raw(long name0) Writes an 8-character field name as raw bytes without any escaping or formatting.abstract voidwriteName9Raw(long name0, int name1) Writes a 9-character field name as raw bytes without any escaping or formatting.voidwriteNameAny(Object name) Writes a field name of any type in a JSON object.abstract voidwriteNameRaw(byte[] bytes) Writes raw bytes representing a field name without any escaping or formatting.voidwriteNameRaw(byte[] bytes, int offset, int len) Writes raw bytes representing a field name without any escaping or formatting.voidwriteNameRaw(byte[] name, long nameHash) Writes raw bytes representing a field name without any escaping or formatting.abstract voidwriteNameRaw(char[] chars) Writes raw characters representing a field name without any escaping or formatting.abstract voidwriteNameRaw(char[] bytes, int offset, int len) Writes raw characters representing a field name without any escaping or formatting.final voidwriteNameValue(String name, Object value) Writes a field name and its value as a key-value pair in a JSON object.abstract voidWrites a null value.final voidWrites a null number value using the current context features.final voidwriteNumberNull(long features) Writes a null number value using the specified features.voidwriteObjectNull(Class<?> fieldClass) Writes a null object value.abstract voidwriteOffsetDateTime(OffsetDateTime dateTime) Writes an OffsetDateTime value.abstract voidwriteOffsetTime(OffsetTime dateTime) Writes an OffsetTime value.voidwriteRaw(byte b) Writes a raw byte without any escaping or formatting.abstract voidwriteRaw(byte[] bytes) Writes raw bytes without any escaping or formatting.abstract voidwriteRaw(char ch) Writes a raw character without any escaping or formatting.final voidwriteRaw(char[] chars) Writes raw characters without any escaping or formatting.voidwriteRaw(char[] chars, int off, int charslen) Writes raw characters without any escaping or formatting.voidwriteRaw(char c0, char c1) Writes two raw characters without any escaping or formatting.abstract voidWrites a raw string without any escaping or formatting.final booleanwriteReference(int index, Object object) Writes a reference to the specified object at the given index.final voidwriteReference(Object object) Deprecated.This method is deprecated and will be removed in a future versionabstract voidwriteReference(String path) Writes a reference to a previously serialized object.abstract voidwriteString(boolean value) Writes a boolean value as a string.voidwriteString(boolean[] value) Writes a boolean array as strings.abstract voidwriteString(byte value) Writes a byte value as a string.voidwriteString(byte[] value) Writes a byte array as strings.abstract voidwriteString(char[] chars) Writes a string from character array.abstract voidwriteString(char[] chars, int off, int len) Writes a string from character array with specified offset and length.abstract voidwriteString(char[] chars, int off, int len, boolean quote) Writes a string from character array with specified offset and length.voidwriteString(double value) Writes a double value as a string.voidwriteString(double[] value) Writes a double array as strings.voidwriteString(float value) Writes a float value as a string.voidwriteString(float[] value) Writes a float array as strings.abstract voidwriteString(int value) Writes an int value as a string.voidwriteString(int[] value) Writes an int array as strings.abstract voidwriteString(long value) Writes a long value as a string.voidwriteString(long[] value) Writes a long array as strings.abstract voidwriteString(short value) Writes a short value as a string.voidwriteString(short[] value) Writes a short array as strings.final voidwriteString(Reader reader) Writes a string from a Reader.abstract voidwriteString(String str) Writes a string value.voidwriteString(String[] strings) Writes an array of strings as a JSON array.voidwriteString(List<String> list) Writes a list of strings as a JSON array.abstract voidwriteStringLatin1(byte[] value) Writes a string from Latin-1 encoded bytes.voidWrites a null string value.abstract voidwriteStringUTF16(byte[] value) Writes a string from UTF-16 encoded bytes.voidwriteSymbol(int symbol) Writes a symbol as a JSON value.voidwriteSymbol(String string) Writes a symbol string.abstract voidwriteTimeHHMMSS8(int hour, int minute, int second) Writes a time in 8-character format (HH:mm:ss).booleanwriteTypeName(byte[] typeName, long typeNameHash) Writes a type name for the current object using byte array and hash.voidwriteTypeName(String typeName) Writes a type name for the current object.abstract voidWrites a UUID value.abstract voidwriteZonedDateTime(ZonedDateTime dateTime) Writes a ZonedDateTime value.
-
Field Details
-
context
-
utf8
public final boolean utf8 -
utf16
public final boolean utf16 -
jsonb
public final boolean jsonb -
useSingleQuote
public final boolean useSingleQuote -
symbolTable
-
charset
-
quote
protected final char quote -
maxArraySize
protected final int maxArraySize -
startObject
protected boolean startObject -
level
protected int level -
off
protected int off -
rootObject
-
refs
-
path
-
lastReference
-
pretty
protected byte pretty -
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
-
JSONWriter
protected JSONWriter(JSONWriter.Context context, SymbolTable symbolTable, boolean jsonb, Charset charset)
-
-
Method Details
-
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
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
Gets the symbol table used by this JSONWriter.- Returns:
- the symbol table, or null if not set
-
config
Configures features for this JSONWriter.- Parameters:
features- the features to enable
-
config
Configures a specific feature for this JSONWriter.- Parameters:
feature- the feature to configurestate- true to enable the feature, false to disable it
-
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
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
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 segmentobject- the object to set the path for- Returns:
- the previous path as a string, or null if no previous path exists
-
setPath
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 generationobject- the object to set the path for- Returns:
- the previous path as a string, or null if no previous path exists
-
setPath0
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 generationobject- the object to set the path for- Returns:
- the previous path as a string, or null if no previous path exists
-
addManagerReference
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
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 forobject- the object to write a reference for- Returns:
- true if a reference was written, false otherwise
-
setPath
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 forobject- the object to set the path for- Returns:
- the previous path as a string, or null if no previous path exists
-
setPath0
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 forobject- the object to set the path for- Returns:
- the previous path as a string, or null if no previous path exists
-
popPath
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
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
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
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
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
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
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
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
Checks if type information should be written for the specified object and field class.- Parameters:
object- the object to checkfieldClass- the field class to check- Returns:
- true if type information should be written, false otherwise
-
isWriteTypeInfo
Checks if type information should be written for the specified object and field type.- Parameters:
object- the object to checkfieldType- the field type to check- Returns:
- true if type information should be written, false otherwise
-
isWriteTypeInfo
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
Checks if type information should be written for the specified object, field type, and features.- Parameters:
object- the object to checkfieldType- the field type to checkfeatures- the features to consider- Returns:
- true if type information should be written, false otherwise
-
isWriteTypeInfo
Checks if type information should be written for the specified object, field class, and features.- Parameters:
object- the object to checkfieldClass- the field class to checkfeatures- the features to consider- Returns:
- true if type information should be written, false otherwise
-
isWriteMapTypeInfo
Checks if map type information should be written for the specified object, field class, and features.- Parameters:
object- the object to checkfieldClass- the field class to checkfeatures- the features to consider- Returns:
- true if map type information should be written, false otherwise
-
isWriteTypeInfo
Checks if type information should be written for the specified object and features.- Parameters:
object- the object to checkfeatures- the features to consider- Returns:
- true if type information should be written, false otherwise
-
getObjectWriter
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
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 serializedformat- the format string to use for serialization- Returns:
- the ObjectWriter for the specified class and format
-
getObjectWriter
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 serializedobjectClass- the class of objects to be serialized- Returns:
- the ObjectWriter for the specified type and class
-
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
Creates a new JSONWriter with the specified object writer provider and features.- Parameters:
provider- the object writer provider to usefeatures- the features to enable- Returns:
- a new JSONWriter instance
-
of
Creates a new JSONWriter with the specified context.- Parameters:
context- the context to use- Returns:
- a new JSONWriter instance
-
of
Creates a new JSONWriter with the specified features.- Parameters:
features- the features to enable- Returns:
- a new JSONWriter instance
-
ofUTF16
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
Creates a new JSONWriter instance for JSONB (binary JSON) format with default context.- Returns:
- a new JSONWriter instance for JSONB format
-
ofJSONB
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
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 JSONWritersymbolTable- the symbol table to use for the new JSONWriter- Returns:
- a new JSONWriter instance for JSONB format
-
ofJSONB
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
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
Creates a new JSONWriter instance with pretty formatting enabled.- Returns:
- a new JSONWriter instance with pretty formatting
-
ofPretty
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
Creates a new JSONWriter instance using UTF-8 encoding with default context.- Returns:
- a new JSONWriter instance using UTF-8 encoding
-
ofUTF8
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
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
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 writeoffset- the offset in the byte arraylen- 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 writeoff- the offset in the character arraycharslen- 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 writec1- 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 valuename1- 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 valuename1- 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 valuename2- 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 valuename2- 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 valuename2- 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 valuename2- 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 valuename2- 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 valuename2- 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 namenameHash- 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
-
isWriteAsString
-
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 writeoffset- the offset in the character arraylen- the number of characters to write
-
writeName
-
writeNameValue
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 namevalue- 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
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
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 serializesize- 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
Writes an Integer object.- Parameters:
i- the Integer object to write
-
writeInt32
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 writeformat- the decimal format to use, or null to use default formatting
-
writeInt32
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 writeformat- 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
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
Writes a list of Long values as integers.- Parameters:
values- the list of Long values to write
-
writeListInt32
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
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 writeformat- the decimal format to use, or null to use default formatting
-
writeFloat
public abstract void writeFloat(float[] value) -
writeFloat
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 nullformat- the decimal format to use, or null to use default formatting
-
writeFloat
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
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 writeformat- 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 writevalue1- the second double value to write
-
writeDouble
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 nullformat- 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
Writes a null object value. The serialization format depends on the context features:- If
JSONWriter.Feature.NullAsDefaultValueis enabled, a default empty object or null character is written - Otherwise, a null value is written
- Parameters:
fieldClass- the class of the field being written
- If
-
writeStringNull
public void writeStringNull()Writes a null string value. The serialization format depends on the context features:- If
JSONWriter.Feature.NullAsDefaultValueorJSONWriter.Feature.WriteNullStringAsEmptyis enabled, an empty string is written - Otherwise, a null value is written
- If
-
writeArrayNull
public void writeArrayNull()Writes a null array value using the current context features. The serialization format depends on the context features:- If
JSONWriter.Feature.NullAsDefaultValueorJSONWriter.Feature.WriteNullListAsEmptyis enabled, an empty array is written - Otherwise, a null value is written
- If
-
writeArrayNull
public void writeArrayNull(long features) Writes a null array value using the specified features. The serialization format depends on the provided features:- If
JSONWriter.Feature.NullAsDefaultValueorJSONWriter.Feature.WriteNullListAsEmptyis enabled, an empty array is written - Otherwise, a null value is written
- Parameters:
features- the features to use for serialization
- If
-
writeNumberNull
public final void writeNumberNull()Writes a null number value using the current context features. The serialization format depends on the context features:- If
JSONWriter.Feature.NullAsDefaultValueorJSONWriter.Feature.WriteNullNumberAsZerois enabled, zero is written - Otherwise, a null value is written
- If
-
writeNumberNull
public final void writeNumberNull(long features) Writes a null number value using the specified features. The serialization format depends on the provided features:- If
JSONWriter.Feature.NullAsDefaultValueorJSONWriter.Feature.WriteNullNumberAsZerois enabled, zero is written - Otherwise, a null value is written
- Parameters:
features- the features to use for serialization
- If
-
writeDecimalNull
public final void writeDecimalNull()Writes a null decimal value using the current context features. The serialization format depends on the context features:- If
JSONWriter.Feature.NullAsDefaultValueis enabled, 0.0 is written - If
JSONWriter.Feature.WriteNullNumberAsZerois enabled, zero is written - Otherwise, a null value is written
- If
-
writeDecimalNull
public final void writeDecimalNull(long features) Writes a null decimal value using the specified features. The serialization format depends on the provided features:- If
JSONWriter.Feature.NullAsDefaultValueis enabled, 0.0 is written - If
JSONWriter.Feature.WriteNullNumberAsZerois enabled, zero is written - Otherwise, a null value is written
- Parameters:
features- the features to use for serialization
- If
-
writeInt64Null
public final void writeInt64Null()Writes a null long value using the current context features. The serialization format depends on the context features:- If
JSONWriter.Feature.NullAsDefaultValueorJSONWriter.Feature.WriteNullNumberAsZerois enabled, zero is written - Otherwise, a null value is written
- If
-
writeBooleanNull
public final void writeBooleanNull()Writes a null boolean value using the current context features. The serialization format depends on the context features:- If
JSONWriter.Feature.NullAsDefaultValueorJSONWriter.Feature.WriteNullBooleanAsFalseis enabled, false is written - Otherwise, a null value is written
- If
-
writeDecimal
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
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 nullfeatures- the features to use for serialization
-
writeDecimal
Writes a BigDecimal value using the specified features and decimal format.- Parameters:
value- the BigDecimal value to write, can be nullfeatures- the features to use for serializationformat- the decimal format to use, or null to use default formatting
-
writeEnum
Writes an Enum value. The serialization format depends on the context features:- If
JSONWriter.Feature.WriteEnumUsingToStringis enabled, the enum is written using its toString() method - If
JSONWriter.Feature.WriteEnumsUsingNameis enabled, the enum is written using its name() - Otherwise, the enum is written as its ordinal value (integer)
- Parameters:
e- the Enum value to write, can be null
- If
-
writeBigInt
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
Writes a BigInteger value using the specified features.- Parameters:
value- the BigInteger value to write, can be nullfeatures- the features to use for serialization
-
writeUUID
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
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 serializedfieldClass- the expected field class type
-
writeTypeName
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 arraytypeNameHash- 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
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
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
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
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
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 writeoff- the offset in the arraylen- 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 writeoff- the offset in the arraylen- the number of characters to writequote- whether to quote the string
-
writeLocalDate
Writes a LocalDate value.- Parameters:
date- the LocalDate to write
-
writeLocalDateWithFormat
-
writeLocalDateTime
Writes a LocalDateTime value.- Parameters:
dateTime- the LocalDateTime to write
-
writeLocalTime
Writes a LocalTime value.- Parameters:
time- the LocalTime to write
-
writeZonedDateTime
Writes a ZonedDateTime value.- Parameters:
dateTime- the ZonedDateTime to write
-
writeOffsetDateTime
Writes an OffsetDateTime value.- Parameters:
dateTime- the OffsetDateTime to write
-
writeOffsetTime
Writes an OffsetTime value.- Parameters:
dateTime- the OffsetTime to write
-
writeInstant
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 yearmonth- 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 yearmonth- 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 yearmonth- 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 secondstimeZone- 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 yearmonth- 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 yearmonth- 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
Writes a list as a JSON array.- Parameters:
array- the list to write
-
write
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
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
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
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 writetype- the type to serialize the object as- Since:
- 2.0.43
-
writeReference
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- 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
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
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
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
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 tocharset- 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
-
incrementIndent
public final void incrementIndent()Deprecated.This method is deprecated and will be removed in a future versionIncrements 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 versionDecrements 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 versionWrites a line break followed by indentation whitespace. This method is used for pretty-printing JSON output with proper indentation.- Since:
- 2.0.51
-
writeReference
Deprecated.This method is deprecated and will be removed in a future versionWrites 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 capacityoldCapacity- 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
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
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
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
-