diff --git a/src/main/java/com/ankurm/jackson3/part6streaming/G02StreamingGenerator.java b/src/main/java/com/ankurm/jackson3/part6streaming/G02StreamingGenerator.java new file mode 100644 index 0000000..9582190 --- /dev/null +++ b/src/main/java/com/ankurm/jackson3/part6streaming/G02StreamingGenerator.java @@ -0,0 +1,55 @@ +package com.ankurm.jackson3.part6streaming; + +import tools.jackson.core.JsonEncoding; +import tools.jackson.core.JsonGenerator; +import tools.jackson.core.ObjectWriteContext; +import tools.jackson.core.json.JsonFactory; + +import java.io.File; + +/** + * Post: Streaming API and JsonNode — https://ankurm.com/jackson-streaming-api-jsonnode/ + * Section: "Writing JSON with JsonGenerator" + * + * Jackson 3 differences: writeNumberField/writeStringField are now + * writeNumberProperty/writeStringProperty, and the factory needs an + * ObjectWriteContext. The post's 1,000,000-record loop is kept — it is the whole + * point of streaming — and the peak heap is measured so "constant memory" is a + * number rather than a claim. + */ +public class G02StreamingGenerator { + + private static final int RECORD_COUNT = 1_000_000; + + public static void main(String[] args) throws Exception { + File output = File.createTempFile("output", ".json"); + output.deleteOnExit(); + + JsonFactory jsonFactory = new JsonFactory(); + Runtime runtime = Runtime.getRuntime(); + long before = runtime.totalMemory() - runtime.freeMemory(); + long start = System.nanoTime(); + + try (JsonGenerator generator = jsonFactory.createGenerator( + ObjectWriteContext.empty(), output, JsonEncoding.UTF8)) { + + generator.writeStartArray(); + for (int recordIndex = 0; recordIndex < RECORD_COUNT; recordIndex++) { + generator.writeStartObject(); + generator.writeNumberProperty("id", recordIndex); + generator.writeStringProperty("status", "active"); + generator.writeEndObject(); + } + generator.writeEndArray(); + } + + long elapsedMs = (System.nanoTime() - start) / 1_000_000; + long after = runtime.totalMemory() - runtime.freeMemory(); + + System.out.println("records written : " + RECORD_COUNT); + System.out.println("file size : " + (output.length() / 1024 / 1024) + " MB"); + System.out.println("elapsed : " + elapsedMs + " ms"); + System.out.println("heap delta : " + ((after - before) / 1024 / 1024) + " MB" + + " <- the document is never held in memory"); + } +}