This repository was archived by the owner on Jan 7, 2024. It is now read-only.
forked from tabulapdf/tabula-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONWriter.java
More file actions
49 lines (40 loc) · 1.72 KB
/
JSONWriter.java
File metadata and controls
49 lines (40 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package technology.tabula.writers;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.List;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import technology.tabula.Cell;
import technology.tabula.RectangularTextContainer;
import technology.tabula.Table;
import technology.tabula.TextChunk;
import technology.tabula.json.RectangularTextContainerSerializer;
import technology.tabula.json.TableSerializer;
public class JSONWriter implements Writer {
private static final ExclusionStrategy ALLCLASSES_SKIPNONPUBLIC = new ExclusionStrategy() {
@Override public boolean shouldSkipClass(Class<?> c) { return false; }
@Override public boolean shouldSkipField(FieldAttributes fa) { return !fa.hasModifier(Modifier.PUBLIC); }
};
@Override
public void write(Appendable out, Table table) throws IOException {
out.append(gson().toJson(table, Table.class));
}
@Override public void write(Appendable out, List<Table> tables) throws IOException {
Gson gson = gson();
JsonArray array = new JsonArray();
for (Table table : tables) array.add(gson.toJsonTree(table, Table.class));
out.append(gson.toJson(array));
}
private static Gson gson() {
return new GsonBuilder()
.addSerializationExclusionStrategy(ALLCLASSES_SKIPNONPUBLIC)
.registerTypeAdapter(Table.class, TableSerializer.INSTANCE)
.registerTypeAdapter(RectangularTextContainer.class, RectangularTextContainerSerializer.INSTANCE)
.registerTypeAdapter(Cell.class, RectangularTextContainerSerializer.INSTANCE)
.registerTypeAdapter(TextChunk.class, RectangularTextContainerSerializer.INSTANCE)
.create();
}
}