Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON unable to serialize exception with Java 17

Tags:

java

gson

The following code used to work on Java 11:

new Gson().toJson(new Exception())

On JDK 17 I get the following error:

Unable to make field private java.lang.String java.lang.Throwable.detailMessage accessible: module java.base does not "opens java.lang" to unnamed module @147ed70f

From reading this page, I think I could resolve it with --add-opens java.base/java.lang=ALL-UNNAMED. Is there a better way however? Perhaps with a custom de/serializer?

like image 599
Malcolm Crum Avatar asked May 05 '26 04:05

Malcolm Crum


1 Answers

I had this yesterday. I was using Java 17. I went back to Java 11 and it worked fine.

I think it's because of this: https://bugs.openjdk.java.net/browse/JDK-8256358

I am lazy and using the GSON default reflection type adapter.

You have to implement your own TypeAdapter to fix it. Or perhaps use another JSON deserializer like Jackson, which I might do later.

like image 60
Matt McEwan Avatar answered May 09 '26 01:05

Matt McEwan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!