You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
javax.json.bind.JsonbException: Unable to serialize property 'exception' from org.example.testcase.jsonbtypeadapter.ValueClass
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:68)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.Marshaller.serializeRoot(Marshaller.java:148)
at org.eclipse.yasson.internal.Marshaller.marshall(Marshaller.java:76)
at org.eclipse.yasson.internal.Marshaller.marshall(Marshaller.java:102)
at org.eclipse.yasson.internal.JsonBinding.toJson(JsonBinding.java:118)
at org.example.testcase.jsonbtypeadapter.JsonbTypeAdapterTest.testTypeAdapter(JsonbTypeAdapterTest.java:31)
Caused by: javax.json.bind.JsonbException: Problem adapting object of type class java.lang.Throwable to java.util.Map<java.lang.String, java.lang.Object> in class class org.example.testcase.jsonbtypeadapter.ThrowableAdapter
at org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer.serialize(AdaptedObjectSerializer.java:72)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:100)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:66)
... 24 more
Caused by: java.lang.ClassCastException: java.util.Optional incompatible with java.lang.Throwable
at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:71)
at org.example.testcase.jsonbtypeadapter.ThrowableAdapter.adaptToJson(ThrowableAdapter.java:9)
at org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer.serialize(AdaptedObjectSerializer.java:61)
... 27 more
The text was updated successfully, but these errors were encountered:
After investigating this more I realized that it has to do with a little more than just the getter methods on the JSON model class. This occurs when there is a field of the same name as a getter, but with different types, for example:
Good finding! Actually, in my real application, I was using Immutables (https://github.com/immutables/immutables), i.e. a generated class. It indeed uses @Nullable Throwable exception internally, because Optional should of course never be used in a field.
That's omnited in the example from #309 (comment), but does it still produce the same error?
hi @bmhm, yes I was able to reproduce the issue with the generated class, and after attaching a debugger I realized that the root issue was the property field and corresponding getter method having different Java types.
I expect to have a PR soon which you could use to confirm the fix.
aguibert
changed the title
Custom adapters of type T do not also apply to Optional<T>
Adapters not applied properly for properties of differing read/write type
Aug 26, 2019
If I register a JsonbAdapter for a type T, it will not be correctly applied to properties of type Optional upon serialization. For example:
Adapter class:
Property class:
If we attempt to serialize an object of type
ValueClass
like this:Stack trace:
The text was updated successfully, but these errors were encountered: