Core Jackson processing abstractions (aka Streaming API), implementation for JSON
—
JsonGenerator provides streaming JSON generation with support for all JSON data types, pretty printing, and configurable output features. It writes JSON content incrementally with efficient memory usage and proper escaping.
public abstract void writeStartObject() throws IOException;
public abstract void writeEndObject() throws IOException;
public abstract void writeStartArray() throws IOException;
public abstract void writeEndArray() throws IOException;
public void writeStartObject(Object forValue) throws IOException;
public void writeStartArray(Object forValue) throws IOException;
public void writeStartArray(Object forValue, int size) throws IOException;public abstract void writeFieldName(String name) throws IOException;
public abstract void writeFieldName(SerializableString name) throws IOException;
public void writeFieldId(long id) throws IOException;public abstract void writeString(String text) throws IOException;
public abstract void writeString(char[] text, int offset, int len) throws IOException;
public abstract void writeString(SerializableString text) throws IOException;
public void writeString(Reader reader, int len) throws IOException;
public abstract void writeRawUTF8String(byte[] text, int offset, int length) throws IOException;
public abstract void writeUTF8String(byte[] text, int offset, int length) throws IOException;
public abstract void writeRaw(String text) throws IOException;
public abstract void writeRaw(String text, int offset, int len) throws IOException;
public abstract void writeRaw(char[] text, int offset, int len) throws IOException;
public abstract void writeRaw(char c) throws IOException;
public void writeRawValue(String text) throws IOException;public abstract void writeNumber(short v) throws IOException;
public abstract void writeNumber(int v) throws IOException;
public abstract void writeNumber(long v) throws IOException;
public abstract void writeNumber(BigInteger v) throws IOException;
public abstract void writeNumber(double v) throws IOException;
public abstract void writeNumber(float v) throws IOException;
public abstract void writeNumber(BigDecimal v) throws IOException;
public abstract void writeNumber(String encodedValue) throws IOException;public abstract void writeBoolean(boolean state) throws IOException;
public abstract void writeNull() throws IOException;public abstract void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len) throws IOException;
public void writeBinary(byte[] data, int offset, int len) throws IOException;
public void writeBinary(byte[] data) throws IOException;
public int writeBinary(Base64Variant b64variant, InputStream data, int dataLength) throws IOException;public void writeStringField(String fieldName, String value) throws IOException;
public void writeBooleanField(String fieldName, boolean value) throws IOException;
public void writeNullField(String fieldName) throws IOException;
public void writeNumberField(String fieldName, short value) throws IOException;
public void writeNumberField(String fieldName, int value) throws IOException;
public void writeNumberField(String fieldName, long value) throws IOException;
public void writeNumberField(String fieldName, BigInteger value) throws IOException;
public void writeNumberField(String fieldName, float value) throws IOException;
public void writeNumberField(String fieldName, double value) throws IOException;
public void writeNumberField(String fieldName, BigDecimal value) throws IOException;
public void writeBinaryField(String fieldName, byte[] data) throws IOException;
public void writeArrayFieldStart(String fieldName) throws IOException;
public void writeObjectFieldStart(String fieldName) throws IOException;public void writeObjectField(String fieldName, Object pojo) throws IOException;
public void writeTree(TreeNode rootNode) throws IOException;public enum JsonWriteFeature implements FormatFeature {
QUOTE_FIELD_NAMES(true),
WRITE_NAN_AS_STRINGS(false),
WRITE_NUMBERS_AS_STRINGS(false),
ESCAPE_NON_ASCII(false),
WRITE_BIGDECIMAL_AS_PLAIN(false);
public boolean enabledByDefault();
public boolean enabledIn(int flags);
public int getMask();
}public enum StreamWriteFeature implements JacksonFeature {
AUTO_CLOSE_TARGET(true),
AUTO_CLOSE_JSON_CONTENT(true),
FLUSH_PASSED_TO_STREAM(true),
WRITE_BIGDECIMAL_AS_PLAIN(false),
STRICT_DUPLICATE_DETECTION(false),
IGNORE_UNKNOWN(false);
public boolean enabledByDefault();
public boolean enabledIn(int flags);
public int getMask();
}public interface PrettyPrinter {
void writeRootValueSeparator(JsonGenerator gen) throws IOException;
void writeStartObject(JsonGenerator gen) throws IOException;
void writeEndObject(JsonGenerator gen, int nrOfEntries) throws IOException;
void writeObjectEntryValueSeparator(JsonGenerator gen) throws IOException;
void writeObjectFieldValueSeparator(JsonGenerator gen) throws IOException;
void writeStartArray(JsonGenerator gen) throws IOException;
void writeEndArray(JsonGenerator gen, int nrOfValues) throws IOException;
void writeArrayValueSeparator(JsonGenerator gen) throws IOException;
void beforeArrayValues(JsonGenerator gen) throws IOException;
void beforeObjectEntries(JsonGenerator gen) throws IOException;
}public class DefaultPrettyPrinter implements PrettyPrinter, Instantiatable<DefaultPrettyPrinter>, Serializable {
public DefaultPrettyPrinter();
public DefaultPrettyPrinter(String rootSeparator);
public DefaultPrettyPrinter(DefaultPrettyPrinter base);
public DefaultPrettyPrinter withRootSeparator(String sep);
public DefaultPrettyPrinter withObjectIndenter(Indenter oi);
public DefaultPrettyPrinter withArrayIndenter(Indenter ai);
public DefaultPrettyPrinter withSeparators(Separators separators);
}
public interface Indenter {
void writeIndentation(JsonGenerator gen, int level) throws IOException;
boolean isInline();
}
public class DefaultIndenter implements Indenter, Serializable {
public static final DefaultIndenter SYSTEM_LINEFEED_INSTANCE;
public static final DefaultIndenter LF_INSTANCE;
public DefaultIndenter();
public DefaultIndenter(String indent, String eol);
}public JsonGenerator setPrettyPrinter(PrettyPrinter pp);
public PrettyPrinter getPrettyPrinter();
public JsonGenerator useDefaultPrettyPrinter();public abstract boolean isClosed();
public JsonGenerator enable(StreamWriteFeature f);
public JsonGenerator disable(StreamWriteFeature f);
public boolean isEnabled(StreamWriteFeature f);
public int getFeatureMask();
public JsonGenerator setFeatureMask(int mask);
public JsonGenerator setCharacterEscapes(CharacterEscapes esc);
public CharacterEscapes getCharacterEscapes();
public JsonGenerator setHighestNonEscapedChar(int charCode);
public int getHighestEscapedChar();public JsonStreamContext getOutputContext();
public int getCurrentDepth();
public boolean canWriteStartObject();
public boolean canWriteStartArray();
public boolean canWriteFieldName();
public boolean canWriteValue();StringWriter sw = new StringWriter();
JsonGenerator gen = factory.createGenerator(sw);
gen.writeStartObject();
gen.writeStringField("name", "John Doe");
gen.writeNumberField("age", 30);
gen.writeBooleanField("active", true);
gen.writeFieldName("address");
gen.writeStartObject();
gen.writeStringField("street", "123 Main St");
gen.writeStringField("city", "Anytown");
gen.writeEndObject();
gen.writeEndObject();
gen.close();
String json = sw.toString();
// Result: {"name":"John Doe","age":30,"active":true,"address":{"street":"123 Main St","city":"Anytown"}}JsonGenerator gen = factory.createGenerator(outputStream);
gen.writeStartArray();
for (String item : items) {
gen.writeString(item);
}
gen.writeEndArray();
gen.close();JsonGenerator gen = factory.createGenerator(outputStream)
.useDefaultPrettyPrinter();
gen.writeStartObject();
gen.writeStringField("message", "Hello World");
gen.writeNumberField("timestamp", System.currentTimeMillis());
gen.writeEndObject();
gen.close();
// Result (formatted):
// {
// "message" : "Hello World",
// "timestamp" : 1638360000000
// }DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter()
.withObjectIndenter(new DefaultIndenter(" ", "\n"))
.withArrayIndenter(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
JsonGenerator gen = factory.createGenerator(outputStream)
.setPrettyPrinter(prettyPrinter);byte[] imageData = loadImageBytes();
JsonGenerator gen = factory.createGenerator(outputStream);
gen.writeStartObject();
gen.writeStringField("filename", "image.jpg");
gen.writeBinaryField("data", imageData); // Base64 encoded automatically
gen.writeEndObject();
gen.close();JsonGenerator gen = factory.createGenerator(outputStream);
gen.writeStartObject();
gen.writeStringField("status", "ok");
gen.writeArrayFieldStart("results");
// Stream large number of items without loading all into memory
for (ResultItem item : resultStream) {
gen.writeStartObject();
gen.writeStringField("id", item.getId());
gen.writeStringField("value", item.getValue());
gen.writeEndObject();
// Optional: flush periodically for large datasets
if (item.getIndex() % 1000 == 0) {
gen.flush();
}
}
gen.writeEndArray();
gen.writeEndObject();
gen.close();try (JsonGenerator gen = factory.createGenerator(outputStream)) {
gen.writeStartObject();
gen.writeStringField("data", someValue);
gen.writeEndObject();
} catch (JsonGenerationException e) {
System.err.println("Generation error: " + e.getMessage());
} catch (IOException e) {
System.err.println("IO error: " + e.getMessage());
}public abstract class CharacterEscapes {
public static final int ESCAPE_NONE = 0;
public static final int ESCAPE_STANDARD = -1;
public static final int ESCAPE_CUSTOM = -2;
public abstract int[] getEscapeCodesForAscii();
public abstract SerializableString getEscapeSequence(int ch);
}Example with custom escaping:
CharacterEscapes customEscapes = new CharacterEscapes() {
@Override
public int[] getEscapeCodesForAscii() {
int[] escapes = CharacterEscapes.standardAsciiEscapesForJSON();
escapes['<'] = CharacterEscapes.ESCAPE_CUSTOM;
escapes['>'] = CharacterEscapes.ESCAPE_CUSTOM;
return escapes;
}
@Override
public SerializableString getEscapeSequence(int ch) {
switch (ch) {
case '<': return new SerializedString("<");
case '>': return new SerializedString(">");
default: return null;
}
}
};
JsonGenerator gen = factory.createGenerator(outputStream)
.setCharacterEscapes(customEscapes);Install with Tessl CLI
npx tessl i tessl/maven-com-fasterxml-jackson-core--jackson-core