Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fracpete/gsonlines
Java library for reading/writing JSON lines data.
https://github.com/fracpete/gsonlines
java jsonlines maven
Last synced: about 1 month ago
JSON representation
Java library for reading/writing JSON lines data.
- Host: GitHub
- URL: https://github.com/fracpete/gsonlines
- Owner: fracpete
- License: apache-2.0
- Created: 2023-11-07T03:00:57.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-07T22:47:31.000Z (about 1 year ago)
- Last Synced: 2024-10-19T12:15:45.319Z (3 months ago)
- Topics: java, jsonlines, maven
- Language: Java
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gsonlines
Java library for reading/writing [JSON lines](https://jsonlines.org/) data using the
[gson](https://github.com/google/gson) library under the hood for automatic
deserialization/serialization of objects.Automatically compresses/decompresses when the file name ends with `.gz`.
## Maven
Add the following to your `pom.xml`:
```xml
com.github.fracpete
gsonlines
0.0.2
```## Examples
### Stream
The following example reads one line at a time and writes one line at a time as well:
```java
import com.github.fracpete.gsonlines.StreamReader;
import com.github.fracpete.gsonlines.StreamWriter;import java.io.File;
import java.util.Map;public class Stream {
public static void main(String[] args) throws Exception {
File fin = new File("/some/where/in.jsonl.gz");
File fout = new File("/some/where/out.jsonl.gz");
StreamReader r = new StreamReader(fin);
StreamWriter w = new StreamWriter(fout);
while (r.hasNext()) {
Map m = r.next(Map.class);
// do something with the data
w.write(m);
}
r.close();
w.close();
}
}
```### Batch
The following example reads all the lines in one go, processes them and then writes them again to another file:
```java
import com.github.fracpete.gsonlines.ArrayReader;
import com.github.fracpete.gsonlines.ArrayWriter;import java.io.File;
import java.util.Map;public class Batch {
public static void main(String[] args) throws Exception {
File fin = new File("/some/where/in.jsonl");
ArrayReader r = new ArrayReader(fin);
Map[] data = r.read(Map.class);
r.close();
// do something with the data
File fout = new File("/some/where/out.jsonl");
ArrayWriter w = new ArrayWriter(fout);
w.write(data);
w.close();
}
}
```