4

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

Kranthi
  • 133
  • 2
  • 10

1 Answers1

5

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?

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33
  • So where does implementing Serializable be useful ? Also to remove warnings I added transient keyword to the type, that should be fine right as Jackson ignores it – Kranthi Mar 18 '18 at 01:03
  • 1
    *"So where does implementing Serializable be useful ? "* - For example, when using the Java standard object serialization mechanism! ObjectInputStream / ObjectOutputStream. Jackson is a 3rd-party library. – Stephen C Mar 18 '18 at 01:43
  • @StephenC So JPA uses this mechanism ? – Kranthi Mar 18 '18 at 03:42
  • I didn't say that. But http://www.adam-bien.com/roller/abien/entry/do_jpa_entities_have_to – Stephen C Mar 18 '18 at 03:58