如何使用Gson序列化java.nio.file.Path?

时间:2016-05-01 09:13:39

标签: java gson nio

尝试序列化包含java.lang.StackOverflowError <{p>的Object时,我得到java.nio.file.Path

即使我写道:

public class PathConverter implements JsonDeserializer<Path>, JsonSerializer<Path> {
    @Override
    public Path deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        return Paths.get(jsonElement.getAsString());
    }

    @Override
    public JsonElement serialize(Path path, Type type, JsonSerializationContext jsonSerializationContext) {
        return new JsonPrimitive(path.toString());
    }
}

并应用它:

    String json = new GsonBuilder()
            .registerTypeAdapter(Path.class, new PathConverter())
            .create()
            .toJson(constructorSetup, new TypeToken<ConstructorSetup>() {}.getType());

我仍然无法将此课程序列化:

public class ConstructorSetup {

    private Path appIconMimmapDirPathOnPc;

}

Stacktrace :(完全在pastebin上)

Exception in thread "main" java.lang.StackOverflowError
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
        ...
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:355)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
    at com.google.gson.Gson.getAdapter(Gson.java:356)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
    at com.google.gson.Gson.getAdapter(Gson.java:356)
        ...
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
    at com.google.gson.Gson.getAdapter(Gson.java:356)

任何解决方案?

1 个答案:

答案 0 :(得分:9)

您的问题是Pathinterface。假设您使用了Paths.get("/"),它将在我的Windows PC上创建类似WindowsPath的实例。现在,您必须告诉GSON如何反序列化此类型:

ConstructorSetup setup = new ConstructorSetup();
setup.setAppIconMimmapDirPathOnPc(Paths.get("/"));

// here we get actual class type of our Path object
Class classT = setup.getAppIconMimmapDirPathOnPc().getClass();

Gson gson = new GsonBuilder().registerTypeAdapter(classT, new MyPathConverter())

您可以采用的另一种方法是registerTypeHierarchyAdapter

.registerTypeHierarchyAdapter(Path.class, new MyPathConverter())

typeHierarchyAdapter的目的是覆盖您希望对类型的所有子类型使用相同表示的情况,这与Path完全相同。