data-file-reader-json

JSON File Format support

This library adds JSON file format support to the DFR framework. If included in your project, the library will be automatically detected using Java’s ServiceLocator.

Certain file format conventions are enforced: Firstly, your file MUST be a plain array of objects. These objects must be simple key/value maps of only primitive types. In short, it needs to be the closest possible JSON-like abstraction of a table.

With that in mind, the JSON format does not place any restrictions on schema consistency - maps may be written with any number of elements. Because of this, please care when using file format adapters that require a strict schema (such as CSV).

<dependency>
    <groupId>net.krotscheck.dfr</groupId>
    <artifactId>data-file-reader-json</artifactId>
    <version>1.0.10</version>
</dependency>

Reading a JSON file

InputStream inputStream = ResourceUtil.getResourceAsStream("my_file.bson");
JSONDataDecoder decoder = new JSONDataDecoder();
decoder.setInputStream(inputStream);

for(Map<String, Object> row : decoder) {
    // Do something
}
decoder.close();

Writing a JSON file

OutputStream outputStream = new FileOutputStream("my_file.bson");
IDataEncoder encoder = new JSONDataEncoder(outputStream);

Map<String, Object> myRow = new LinkedHashMap<String, Object>();
encoder.write(myRow);

encoder.close();

Back to top

Version: 1.0.10.