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 (33 loc) · 1.24 KB
/
TableSerializer.java
File metadata and controls
46 lines (33 loc) · 1.24 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() {
// singleton
}
@Override
public JsonElement serialize(Table src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.addProperty("extraction_method", src.getExtractionMethod());
result.addProperty("top", src.getTop());
result.addProperty("left", src.getLeft());
result.addProperty("width", src.getWidth());
result.addProperty("height", src.getHeight());
JsonArray data;
result.add("data", data = new JsonArray());
for (List<RectangularTextContainer> srcRow : src.getRows()) {
JsonArray row = new JsonArray();
for (RectangularTextContainer textChunk : srcRow) row.add(context.serialize(textChunk));
data.add(row);
}
return result;
}
}