Currently, we serialize user’s session object into database but the object changed too much lately so we decide to redesign this process. We all agree that we can’t serialize the whole object and we need to save it field by field. There are 2 proposals,
- Store all fields in a map and serialize the map.
- Use
ObjectOutputStream.putFields().
We don’t see much difference between these 2 approaches. The #1 uses familiar map interface and it’s easier for everyone to use. The #2 provides the convenient methods like fields.get("confirmed", false) etc.
We tend to go with #1. Anyone know any other benefits of #2?
readFieldsandputFieldshave the advantage that you can start off using the default serialisation, and only add the boilerplate when you need it.A
Mapwill produce smaller streams if used once, but a stream that stores lots of these object will be smaller if it usesreadFields/putFields. This is because there is a one time overhead describing the format of the stream.Note you should always call
defaultReadObject/readFieldsat the start ofreadObjectanddefaultWriteObject/putFieldsat the start ofwriteObject. Unfortunately this is not checked.