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
40 lines (32 loc) · 1.34 KB
/
TableSerializer.java
File metadata and controls
40 lines (32 loc) · 1.34 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
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 class TableSerializer implements JsonSerializer<Table> {
@Override
public JsonElement serialize(Table table, Type type,
JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.addProperty("extraction_method", table.getExtractionAlgorithm().toString());
object.addProperty("top", table.getTop());
object.addProperty("left", table.getLeft());
object.addProperty("width", table.getWidth());
object.addProperty("height", table.getHeight());
JsonArray jsonDataArray = new JsonArray();
for (List<RectangularTextContainer> row: table.getRows()) {
JsonArray jsonRowArray = new JsonArray();
for (RectangularTextContainer textChunk: row) {
jsonRowArray.add(context.serialize(textChunk));
}
jsonDataArray.add(jsonRowArray);
}
object.add("data", jsonDataArray);
return object;
}
}