Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field getting serialized without implementing Serializable interface

I have a java class that implements Serializable interface. It has String, BigDecimal and other java predefined types which are serializable. It also includes a custom defined class that doesn’t implement Serializable. The custom type is also getting serialized into JSON.

But there are warnings showing either make it transient or make the custom type implement serializable.

How is the type getting serialized even though it doesn’t implement Serializable ? And Should I ignore the warnings ( SonarLint messages) ?

like image 889
Kranthi Avatar asked Sep 14 '25 02:09

Kranthi


1 Answers

That's because Jackson doesn't use java.lang.Serializable type hints when serializing/deserializing. Jackson can serialize/deserialise most of the Java types. Have a look at com.fasterxml.jackson.databind.ser.std and com.fasterxml.jackson.databind.deser.std. If you use ObjectMapper, you could configure required feature as in this guide.

Ignoring warnings depends on your object types. For example, there's a good discussion about value type serialization.

Why should Java's value-based classes not be serialized?

like image 128
Laksitha Ranasingha Avatar answered Sep 16 '25 16:09

Laksitha Ranasingha