forked from tabulapdf/tabula-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableSerializer.java
More file actions
46 lines (35 loc) · 1.51 KB
/
TableSerializer.java
File metadata and controls
46 lines (35 loc) · 1.51 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
package technology.tabula.json;
import java.lang.reflect.Type;
import java.util.List;
import technology.tabula.RectangularTextContainer;
import technology.tabula.Table;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public final class TableSerializer implements JsonSerializer<Table> {
public static final TableSerializer INSTANCE = new TableSerializer();
private TableSerializer() {}
@Override
public JsonElement serialize(Table table, Type type, JsonSerializationContext context) {
JsonObject json = new JsonObject();
JsonArray data = new JsonArray();
json.addProperty("extraction_method", table.getExtractionMethod());
json.addProperty("page_number", table.getPageNumber());
json.addProperty("top", table.getTop());
json.addProperty("left", table.getLeft());
json.addProperty("width", table.getWidth());
json.addProperty("height", table.getHeight());
json.addProperty("right", table.getRight());
json.addProperty("bottom", table.getBottom());
json.add("data", data);
for (List<RectangularTextContainer> tableRow : table.getRows()) {
JsonArray jsonRow = new JsonArray();
for (RectangularTextContainer textChunk : tableRow)
jsonRow.add(context.serialize(textChunk));
data.add(jsonRow);
}
return json;
}
}