I got this Exception
Caused by: java.lang.StackOverflowError
at java.util.ArrayList.get(ArrayList.java:322)[:1.6.0_12]
at com.google.gson.stream.JsonWriter.peek(JsonWriter.java:354)
when trying to convert an Exception Object to a Json Text as the following code
Exception exception = new Exception("Test");
String gsonText = gson.toJson(exception);
You get a StackOverflowError because the exception’s cause instance member variable is initialized to itself. The serializer attempts to traverse the object and never finishes because it keeps following the reference to the cause.
There’s a message on the GSON google group describing this problem, the solutions suggested are:
Usually the advice to application developers is to not show stack traces to clients, so I would suggest calling toString on the exception or otherwise mapping the exception to a message on the server-side and send the message instead.