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

I get java.lang.StackOverflowError when trying to serialize an 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 cannot 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?

+8
java gson nio
source share
1 answer

Your problem is that Path is an interface . Suppose you used Paths.get("/") , which will instantiate something like WindowsPath on my Windows PC. Now you should tell GSON how to deserialize 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 registerTypeHierarchyAdapter :

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

The purpose of the typeHierarchyAdapter should cover the case when you want the same view for all subtypes of a type, which exactly matches your case with Path .

+8
source share

All Articles