Examine the object returned by Paths.get("c:\\temp\\"); in the debugger. On my machine, it has a field called fs that contains WindowsFileSystem . This, in turn, has a provider field that contains a WindowsFileSystemProvider - and provider has a theFileSystem field that contains the same WindowsFileSystem as the original fs field. Voila, circular link.
Gson uses reflection to validate and serialize, recursively, every non-transition field in the object that you give it. A loopback like this sends it to infinite recursion ending in a StackOverflowError . To fix this, you need to either implement custom serialization or serialize a specific Path property, not the entire object. Marking any or all of the fields involved in the loop as transient will also work, but this will require write access to the library code.
source share