Skip to content

autonomouslogic/jackson-object-stream

Repository files navigation

Jackson Object Stream

GitHub release (latest by date) javadoc GitHub Workflow Status (branch) GitHub

A small library which handles reading and writing a series of JSON objects from a text file.

Dependency

Available from Maven Central.

Gradle

implementation 'com.autonomouslogic.jacksonobjectstream:jackson-object-stream:$version'

Maven

<dependency>
  <groupId>com.autonomouslogic.jacksonobjectstream</groupId>
  <artifactId>jackson-object-stream</artifactId>
  <version>$version</version>
</dependency>

Reading a JSON stream

Objects are read from the files one by one. Any whitespace or formatting will be ignored by Jackson, so there are no special formatting requirements.

JacksonObjectStreamFactory factory = new JacksonObjectStreamFactory(new ObjectMapper());
try (JacksonObjectIterator<TestObject> iterator = factory.createReader(new File("users.json"), User.class)) {
    while(iterator.hasNext()) {
        User user = iterator.next();
        // Use user for something.
    }
}

Writing a JSON stream

Objects will be written to the output one by one. After each object, a newline will be inserted. Pretty printing is not supported at this time.

JacksonObjectStreamFactory factory = new JacksonObjectStreamFactory(new ObjectMapper());
try (JacksonObjectStreamWriter writer = factory.createWriter(new File("users.json"))) {
    writer.writeObject(user1);
    writer.writeObject(user2);
    // etc.
}

Doesn't Jackson support this already?

Jackson does support writing/reading multiple JSON object to/from to files. This is a tiny library that simply provides the plumbing needed to do so effectively, and provides a few convenience methods at the same time.

Versioning

Jackson Object Stream follows semantic versioning.

License

This library is licensed under the MIT-0 license.

Status

Type Status
LGTM Language grade: Java Total alerts
CodeClimate Maintainability
SonarCloud Maintainability Rating Code Smells
Libraries.io Libraries.io dependency status for latest release
Snyk Known Vulnerabilities
Codecov codecov
Synatype Lift link