我无法使用GSON序列化一个包含表格形式的Java表的列表。

huangapple go评论75阅读模式
英文:

I can't serialize a list of tabular java tables using GSON

问题

我使用Tabula(Java)来提取PDF文件中包含的表格。在获得表格列表后,我使用Gson类来实现这个列表。然而,出现了以下异常。

表格提取和序列化

List<Table> tables = new PDFTableExtractor(document).extract(page);
new Gson().toJson(tables);

抛出异常

java.lang.IllegalArgumentException: jdk.internal.ref.PhantomCleanable<?> 声明了多个名为next的JSON字段
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)

我正在使用Tabula库中的Table类。该类包含以下属性:

private final String extractionMethod;
private int rowCount = 0; 
private int colCount = 0; 
private List<List<RectangularTextContainer>> memoizedRows = null; 
/* 对于测试而可见 */ final TreeMap<CellPosition, RectangularTextContainer> cells = new TreeMap<>();
英文:

I use Tabula (java) to extract tables contained in pdf files. After obtaining a list of tables I use the Gson class to realize this list. However, the following exception is thrown.

Table extraction and serelization

List<Table> tables = new PDFTableExtractor(document).extract(page);
new Gson().toJson(tables);

Exception thrown

java.lang.IllegalArgumentException: jdk.internal.ref.PhantomCleanable<?> declares multiple JSON fields named next
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)

I'm using the Table class from the Tabula library. The class contains the following attributes:

private final String extractionMethod;
private int rowCount = 0; 
private int colCount = 0; 
private List<List<RectangularTextContainer>> memoizedRows = null; 
/* visible for testing */ final TreeMap<CellPosition, RectangularTextContainer> cells = new TreeMap<>();

答案1

得分: 0

tabula-java 的维护者在这里)

该库已经使用 Gson 实现了序列化。请查看 TableSerializer

英文:

(maintainer of tabula-java here)

The library already implements serialization with Gson. Take a look at TableSerializer

huangapple
  • 本文由 发表于 2020年8月28日 05:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63624644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定