您的问题是那
Path是一个
interface。假设您使用
Paths.get("/")它将WindowsPath在Windows
PC上创建类似的实例。现在,您必须告诉GSON如何反序列化此类型:
ConstructorSetup setup = new ConstructorSetup();setup.setAppIconMimmapDirPathOnPc(Paths.get("/"));// here we get actual class type of our Path objectClass classT = setup.getAppIconMimmapDirPathOnPc().getClass();Gson gson = new GsonBuilder().registerTypeAdapter(classT, new MyPathConverter())您可以使用的另一种方法是
registerTypeHierarchyAdapter:
.registerTypeHierarchyAdapter(Path.class, new MyPathConverter())
typeHierarchyAdapter的目的是在您希望为类型的所有子类型使用相同的表示形式时涵盖这种情况,而这正是您使用的情况
Path。



