Kranthi
Kranthi

Reputation: 133

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) ?

Upvotes: 4

Views: 2340

Answers (1)

Laksitha Ranasingha
Laksitha Ranasingha

Reputation: 4507

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?

Upvotes: 6

Related Questions