Alexandr Alexandr - 7 months ago 49
Java Question

How to serialize java.nio.file.Path with Gson?

I get

java.lang.StackOverflowError
when trying to serialize
Object
that contains
java.nio.file.Path


Even when i wrote:

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());
}
}


and apply it:

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


I still can't serialize this class:

public class ConstructorSetup {

private Path appIconMimmapDirPathOnPc;

}


Stacktrace: (full on 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)


Any solution?

Answer

You problem is that Path is an interface and lets suppose you just used Paths.get("/") and this will create instance of WindowsPath on my windows pc, so now you have to tell gson how to derserialize this type.

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())

Another approach you can go with is to registerTypeHierarchyAdapter

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

The purpose of typeHierarchyAdapter is to cover the case when you want the same representation for all subtypes of a type. Just like in your case with Path

Comments